-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable extensions.json
#9043
Merged
colin-grant-work
merged 1 commit into
eclipse-theia:master
from
colin-grant-work:feature/extensions-json
Jun 9, 2021
Merged
Enable extensions.json
#9043
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -92,14 +92,24 @@ export class UserConfigsPreferenceProvider extends PreferenceProvider { | |
|
||
async setPreference(preferenceName: string, value: any, resourceUri?: string): Promise<boolean> { | ||
const sectionName = preferenceName.split('.', 1)[0]; | ||
const configName = this.configurations.isSectionName(sectionName) ? sectionName : this.configurations.getConfigName(); | ||
|
||
const providers = this.providers.values(); | ||
|
||
for (const provider of providers) { | ||
if (this.configurations.getName(provider.getConfigUri()) === configName) { | ||
return provider.setPreference(preferenceName, value, resourceUri); | ||
const defaultConfigName = this.configurations.getConfigName(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also rewritten to allow us to fall back to |
||
const configName = this.configurations.isSectionName(sectionName) ? sectionName : defaultConfigName; | ||
|
||
const setWithConfigName = async (name: string): Promise<boolean> => { | ||
for (const provider of this.providers.values()) { | ||
if (this.configurations.getName(provider.getConfigUri()) === name) { | ||
if (await provider.setPreference(preferenceName, value, resourceUri)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
if (await setWithConfigName(configName)) { // Try in the section we believe it belongs in. | ||
return true; | ||
} else if (configName !== defaultConfigName) { // Fall back to `settings.json` if that fails. | ||
return setWithConfigName(defaultConfigName); | ||
} | ||
return false; | ||
} | ||
|
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
99 changes: 99 additions & 0 deletions
99
packages/vsx-registry/src/browser/recommended-extensions/preference-provider-overrides.ts
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,99 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 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 | ||
********************************************************************************/ | ||
|
||
import { | ||
FolderPreferenceProvider, | ||
FolderPreferenceProviderFactory, | ||
FolderPreferenceProviderFolder, | ||
UserPreferenceProvider, | ||
UserPreferenceProviderFactory | ||
} from '@theia/preferences/lib/browser'; | ||
import { Container, injectable, interfaces } from '@theia/core/shared/inversify'; | ||
import { extensionsConfigurationSchema } from './recommended-extensions-json-schema'; | ||
import { | ||
WorkspaceFilePreferenceProvider, | ||
WorkspaceFilePreferenceProviderFactory, | ||
WorkspaceFilePreferenceProviderOptions | ||
} from '@theia/preferences/lib/browser/workspace-file-preference-provider'; | ||
import { bindFactory } from '@theia/preferences/lib/browser/preference-bindings'; | ||
import { SectionPreferenceProviderSection, SectionPreferenceProviderUri } from '@theia/preferences/lib/browser/section-preference-provider'; | ||
|
||
/** | ||
* The overrides in this file are required because the base preference providers assume that a | ||
* section name (extensions) will not be used as a prefix (extensions.ignoreRecommendations). | ||
*/ | ||
|
||
@injectable() | ||
export class FolderPreferenceProviderWithExtensions extends FolderPreferenceProvider { | ||
protected getPath(preferenceName: string): string[] | undefined { | ||
const path = super.getPath(preferenceName); | ||
if (this.section !== 'extensions' || !path?.length) { | ||
return path; | ||
} | ||
const isExtensionsField = path[0] in extensionsConfigurationSchema.properties!; | ||
if (isExtensionsField) { | ||
return path; | ||
} | ||
return undefined; | ||
} | ||
} | ||
|
||
@injectable() | ||
export class UserPreferenceProviderWithExtensions extends UserPreferenceProvider { | ||
protected getPath(preferenceName: string): string[] | undefined { | ||
const path = super.getPath(preferenceName); | ||
if (this.section !== 'extensions' || !path?.length) { | ||
return path; | ||
} | ||
const isExtensionsField = path[0] in extensionsConfigurationSchema.properties!; | ||
if (isExtensionsField) { | ||
return path; | ||
} | ||
return undefined; | ||
} | ||
} | ||
|
||
@injectable() | ||
export class WorkspaceFilePreferenceProviderWithExtensions extends WorkspaceFilePreferenceProvider { | ||
protected belongsInSection(firstSegment: string, remainder: string): boolean { | ||
if (firstSegment === 'extensions') { | ||
return remainder in extensionsConfigurationSchema.properties!; | ||
} | ||
return this.configurations.isSectionName(firstSegment); | ||
} | ||
} | ||
|
||
export function bindPreferenceProviderOverrides(bind: interfaces.Bind, unbind: interfaces.Unbind): void { | ||
unbind(UserPreferenceProviderFactory); | ||
unbind(FolderPreferenceProviderFactory); | ||
unbind(WorkspaceFilePreferenceProviderFactory); | ||
bindFactory(bind, UserPreferenceProviderFactory, UserPreferenceProviderWithExtensions, SectionPreferenceProviderUri, SectionPreferenceProviderSection); | ||
bindFactory( | ||
bind, | ||
FolderPreferenceProviderFactory, | ||
FolderPreferenceProviderWithExtensions, | ||
SectionPreferenceProviderUri, | ||
SectionPreferenceProviderSection, | ||
FolderPreferenceProviderFolder, | ||
); | ||
bind(WorkspaceFilePreferenceProviderFactory).toFactory(ctx => (options: WorkspaceFilePreferenceProviderOptions) => { | ||
const child = new Container({ defaultScope: 'Singleton' }); | ||
child.parent = ctx.container; | ||
child.bind(WorkspaceFilePreferenceProvider).to(WorkspaceFilePreferenceProviderWithExtensions); | ||
child.bind(WorkspaceFilePreferenceProviderOptions).toConstantValue(options); | ||
return child.get(WorkspaceFilePreferenceProvider); | ||
}); | ||
} |
72 changes: 72 additions & 0 deletions
72
...ges/vsx-registry/src/browser/recommended-extensions/recommended-extensions-json-schema.ts
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,72 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 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 | ||
********************************************************************************/ | ||
|
||
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify'; | ||
import { InMemoryResources } from '@theia/core'; | ||
import { JsonSchemaContribution, JsonSchemaRegisterContext } from '@theia/core/lib/browser/json-schema-store'; | ||
import { IJSONSchema } from '@theia/core/lib/common/json-schema'; | ||
import URI from '@theia/core/lib/common/uri'; | ||
import { WorkspaceService } from '@theia/workspace/lib/browser'; | ||
|
||
export const extensionsSchemaID = 'vscode://schemas/extensions'; | ||
export const extensionsConfigurationSchema: IJSONSchema = { | ||
$id: extensionsSchemaID, | ||
default: { recommendations: [] }, | ||
type: 'object', | ||
|
||
properties: { | ||
recommendations: { | ||
title: 'A list of extensions recommended for users of this workspace. Should use the form "<publisher>.<extension name>"', | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
pattern: '^\\w[\\w-]+\\.\\w[\\w-]+$', | ||
patternErrorMessage: "Expected format '${publisher}.${name}'. Example: 'eclipse.theia'." | ||
}, | ||
default: [], | ||
}, | ||
unwantedRecommendations: { | ||
title: 'A list of extensions recommended by default that should not be recommended to users of this workspace. Should use the form "<publisher>.<extension name>"', | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
pattern: '^\\w[\\w-]+\\.\\w[\\w-]+$', | ||
patternErrorMessage: "Expected format '${publisher}.${name}'. Example: 'eclipse.theia'." | ||
}, | ||
default: [], | ||
} | ||
} | ||
}; | ||
|
||
@injectable() | ||
export class ExtensionSchemaContribution implements JsonSchemaContribution { | ||
protected readonly uri = new URI(extensionsSchemaID); | ||
@inject(InMemoryResources) protected readonly inmemoryResources: InMemoryResources; | ||
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService; | ||
|
||
@postConstruct() | ||
protected init(): void { | ||
this.inmemoryResources.add(this.uri, JSON.stringify(extensionsConfigurationSchema)); | ||
} | ||
|
||
registerSchemas(context: JsonSchemaRegisterContext): void { | ||
context.registerSchema({ | ||
fileMatch: ['extensions.json'], | ||
url: this.uri.toString(), | ||
}); | ||
this.workspaceService.updateSchema('extensions', { $ref: this.uri.toString() }); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of code has been rewritten here. The substantive change is that after trying the files matching the section name (e.g.
extensions.json
if the preference starts with'extensions'
), we fall back tosettings.json
if the change has been rejected. This allows the provider forextensions.json
to reject changes that contain the prefixextensions
but are not in the schema for that file (e.g.extensions.ignoreRecommendations
).There is also a stylistic change in the setup of the final iterators. The code on
master
uses an array containing functions that either return a provider or add another function to the end of the array. The conditions for returning the provider are increasingly broad with each subsequent function, so it was effectively a prioritization mechanism that avoided asort
call.The new code follows a similar principle, but instead of gradually adding to the same array, it creates a number of prioritized buckets, guaranteeing that we only consider each candidate once after it has been prioritized, and, I hope, clarifying the precedence order of the various conditions.