-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[plugin] fix #4621: apply only valid configurations as schemas
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
- Loading branch information
Showing
6 changed files
with
178 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 Ericsson and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
// tslint:disable:no-any | ||
|
||
import { PreferenceScope } from './preference-scope'; | ||
|
||
export interface PreferenceSchema { | ||
[name: string]: any, | ||
scope?: 'application' | 'window' | 'resource' | PreferenceScope, | ||
overridable?: boolean; | ||
properties: PreferenceSchemaProperties | ||
} | ||
export namespace PreferenceSchema { | ||
export function is(obj: Object | undefined): obj is PreferenceSchema { | ||
return !!obj && ('properties' in obj) && PreferenceSchemaProperties.is((<any>obj)['properties']); | ||
} | ||
export function getDefaultScope(schema: PreferenceSchema): PreferenceScope { | ||
let defaultScope: PreferenceScope = PreferenceScope.Workspace; | ||
if (!PreferenceScope.is(schema.scope)) { | ||
defaultScope = PreferenceScope.fromString(<string>schema.scope) || PreferenceScope.Workspace; | ||
} else { | ||
defaultScope = schema.scope; | ||
} | ||
return defaultScope; | ||
} | ||
} | ||
|
||
export interface PreferenceSchemaProperties { | ||
[name: string]: PreferenceSchemaProperty | ||
} | ||
export namespace PreferenceSchemaProperties { | ||
export function is(obj: Object | undefined): obj is PreferenceSchemaProperties { | ||
return !!obj && typeof obj === 'object'; | ||
} | ||
} | ||
|
||
export interface PreferenceDataSchema { | ||
[name: string]: any, | ||
scope?: PreferenceScope, | ||
properties: { | ||
[name: string]: PreferenceDataProperty | ||
} | ||
patternProperties: { | ||
[name: string]: PreferenceDataProperty | ||
}; | ||
} | ||
|
||
export interface PreferenceItem { | ||
type?: JsonType | JsonType[]; | ||
minimum?: number; | ||
default?: any; | ||
enum?: string[]; | ||
items?: PreferenceItem; | ||
properties?: { [name: string]: PreferenceItem }; | ||
additionalProperties?: object; | ||
[name: string]: any; | ||
overridable?: boolean; | ||
} | ||
|
||
export interface PreferenceSchemaProperty extends PreferenceItem { | ||
description?: string; | ||
scope?: 'application' | 'window' | 'resource' | PreferenceScope; | ||
} | ||
|
||
export interface PreferenceDataProperty extends PreferenceItem { | ||
description?: string; | ||
scope?: PreferenceScope; | ||
} | ||
export namespace PreferenceDataProperty { | ||
export function fromPreferenceSchemaProperty(schemaProps: PreferenceSchemaProperty, defaultScope: PreferenceScope = PreferenceScope.Workspace): PreferenceDataProperty { | ||
if (!schemaProps.scope) { | ||
schemaProps.scope = defaultScope; | ||
} else if (typeof schemaProps.scope === 'string') { | ||
return Object.assign(schemaProps, { scope: PreferenceScope.fromString(schemaProps.scope) || defaultScope }); | ||
} | ||
return <PreferenceDataProperty>schemaProps; | ||
} | ||
} | ||
|
||
export type JsonType = 'string' | 'array' | 'number' | 'integer' | 'object' | 'boolean' | 'null'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 Ericsson and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
// tslint:disable:no-any | ||
|
||
export enum PreferenceScope { | ||
Default, | ||
User, | ||
Workspace, | ||
Folder | ||
} | ||
|
||
export namespace PreferenceScope { | ||
export function is(scope: any): scope is PreferenceScope { | ||
return typeof scope === 'number' && getScopes().findIndex(s => s === scope) >= 0; | ||
} | ||
|
||
export function getScopes(): PreferenceScope[] { | ||
return Object.keys(PreferenceScope) | ||
.filter(k => typeof PreferenceScope[k as any] === 'string') | ||
.map(v => <PreferenceScope>Number(v)); | ||
} | ||
|
||
export function getReversedScopes(): PreferenceScope[] { | ||
return getScopes().reverse(); | ||
} | ||
|
||
export function getScopeNames(scope?: PreferenceScope): string[] { | ||
const names: string[] = []; | ||
const allNames = Object.keys(PreferenceScope) | ||
.filter(k => typeof PreferenceScope[k as any] === 'number'); | ||
if (scope) { | ||
for (const name of allNames) { | ||
if ((<any>PreferenceScope)[name] <= scope) { | ||
names.push(name); | ||
} | ||
} | ||
} | ||
return names; | ||
} | ||
|
||
export function fromString(strScope: string): PreferenceScope | undefined { | ||
switch (strScope) { | ||
case 'application': | ||
return PreferenceScope.User; | ||
case 'window': | ||
return PreferenceScope.Workspace; | ||
case 'resource': | ||
return PreferenceScope.Folder; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters