Skip to content
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

Support relative paths for JSON schema associations (Fix #7140) #7225

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 66 additions & 18 deletions packages/json/src/browser/json-client-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,78 @@ import {
DocumentSelector
} from '@theia/languages/lib/browser';
import { JSON_LANGUAGE_ID, JSON_LANGUAGE_NAME, JSONC_LANGUAGE_ID } from '../common';
import { ResourceProvider, DisposableCollection } from '@theia/core';
import { ResourceProvider } from '@theia/core';
import { DisposableCollection } from '@theia/core/lib/common';
import URI from '@theia/core/lib/common/uri';
import { Path } from '@theia/core/lib/common/path';
import { JsonPreferences } from './json-preferences';
import { JsonSchemaStore } from '@theia/core/lib/browser/json-schema-store';
import { Endpoint } from '@theia/core/lib/browser';
import { JsonSchemaStore, JsonSchemaConfiguration } from '@theia/core/lib/browser/json-schema-store';
import { Endpoint, PreferenceScope, PreferenceService } from '@theia/core/lib/browser';
import { FileSystem } from '@theia/filesystem/lib/common';

@injectable()
export class JsonClientContribution extends BaseLanguageClientContribution {

readonly id = JSON_LANGUAGE_ID;
readonly name = JSON_LANGUAGE_NAME;

protected schemaRegistry: { [pattern: string]: string[] };

constructor(
@inject(Workspace) protected readonly workspace: Workspace,
@inject(ResourceProvider) protected readonly resourceProvider: ResourceProvider,
@inject(Languages) protected readonly languages: Languages,
@inject(LanguageClientFactory) protected readonly languageClientFactory: LanguageClientFactory,
@inject(JsonPreferences) protected readonly preferences: JsonPreferences,
@inject(JsonSchemaStore) protected readonly jsonSchemaStore: JsonSchemaStore
@inject(PreferenceService) protected readonly preferenceService: PreferenceService,
@inject(JsonSchemaStore) protected readonly jsonSchemaStore: JsonSchemaStore,
@inject(FileSystem) protected readonly fileSystem: FileSystem
) {
super(workspace, languages, languageClientFactory);
this.initializeJsonSchemaAssociations();
this.initializeJsonSchemaStoreAssociations();
}

protected updateSchemas(client: ILanguageClient): void {
const allConfigs = [...this.jsonSchemaStore.getJsonSchemaConfigurations()];
const config = this.preferences['json.schemas'];
if (config instanceof Array) {
allConfigs.push(...config);
}
const registry: { [pattern: string]: string[] } = {};
for (const s of allConfigs) {
if (s.fileMatch) {
for (let fileMatch of s.fileMatch) {
if (fileMatch.charAt(0) !== '/' && !fileMatch.match(/\w+:/)) {
this.schemaRegistry = {};
this.updateJsonSchemaStoreAssociations();
this.initializeJsonPreferencesAssociations();
client.sendNotification('json/schemaAssociations', this.schemaRegistry);
}

protected updateJsonSchemaStoreAssociations(): void {
const schemaStoreConfigs = [...this.jsonSchemaStore.getJsonSchemaConfigurations()];
for (const schemaConfig of schemaStoreConfigs) {
if (schemaConfig.fileMatch) {
for (let fileMatch of schemaConfig.fileMatch) {
if (!fileMatch.startsWith('/') && !fileMatch.match(/\w+:/)) {
fileMatch = '/' + fileMatch;
}
registry[fileMatch] = [s.url];
this.schemaRegistry[fileMatch] = [schemaConfig.url];
}
}
}
client.sendNotification('json/schemaAssociations', registry);
}

protected setJsonPreferencesSchemas(schemaConfigs: JsonSchemaConfiguration[], scope: PreferenceScope, rootPath?: string): void {
schemaConfigs.forEach((schemaConfig: JsonSchemaConfiguration) => {
schemaConfig.fileMatch.forEach((fileMatch: string) => {
if (!fileMatch.startsWith('/') && !fileMatch.match(/\w+:/)) {
fileMatch = '/' + fileMatch;
}
let url = schemaConfig.url;
if (rootPath) {
fileMatch = new Path(rootPath).join(fileMatch).normalize().toString();
if (url.match(rootPath)) {
url = new URI(new Path(url).toString()).toString();
} else if ((url.startsWith('.') || url.startsWith('/') || url.match(/^\w+\//))) {
url = new URI(rootPath).resolve(url).normalizePath().toString();
}
}
if (url) {
this.schemaRegistry[fileMatch] = [url];
}
});
});
}

protected get globPatterns(): string[] {
Expand Down Expand Up @@ -105,7 +135,7 @@ export class JsonClientContribution extends BaseLanguageClientContribution {
this.updateSchemas(languageClient);
}

protected async initializeJsonSchemaAssociations(): Promise<void> {
protected async initializeJsonSchemaStoreAssociations(): Promise<void> {
const url = `${new Endpoint().httpScheme}//schemastore.azurewebsites.net/api/json/catalog.json`;
const response = await fetch(url);
const schemas: SchemaData[] = (await response.json()).schemas!;
Expand All @@ -119,6 +149,24 @@ export class JsonClientContribution extends BaseLanguageClientContribution {
}
}

protected initializeJsonPreferencesAssociations(): void {
const userSettings = this.preferenceService.inspect<JsonSchemaConfiguration[]>('json.schemas');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit confused why we need to worry about json.schemas. We send complete json configuration to JSON language server whenever it is changed. I don't think we need to pass it additionally as part of json/schemaAssociations.

if (userSettings) {
if (userSettings.globalValue) {
this.setJsonPreferencesSchemas(userSettings.globalValue, PreferenceScope.User);
}
}

if (this.workspaceService.isMultiRootWorkspaceOpened) {
this.workspace.workspaceFolders.forEach((workspaceFolder: { name: string; uri: { path: string; }; }) => {
const workspaceFolderPath = workspaceFolder.uri.path;
const folderSettings = this.preferenceService.inspect<JsonSchemaConfiguration[]>('json.schemas', workspaceFolderPath);
if (folderSettings && folderSettings.workspaceFolderValue) {
this.setJsonPreferencesSchemas(folderSettings.workspaceFolderValue, PreferenceScope.Folder, workspaceFolderPath);
}
});
}
}
}

interface SchemaData {
Expand Down
6 changes: 4 additions & 2 deletions packages/json/src/browser/json-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
PreferenceService,
PreferenceContribution,
PreferenceSchema,
PreferenceChangeEvent
PreferenceChangeEvent,
PreferenceScope
} from '@theia/core/lib/browser/preferences';
import { JsonSchemaConfiguration } from '@theia/core/lib/browser/json-schema-store';

Expand Down Expand Up @@ -63,7 +64,8 @@ export const jsonPreferenceSchema: PreferenceSchema = {
'default': true,
'description': 'Enable/disable default JSON formatter'
},
}
},
scope: PreferenceScope.Folder
};

export interface JsonConfiguration {
Expand Down
30 changes: 29 additions & 1 deletion packages/preferences/src/browser/folder-preference-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import { inject, injectable } from 'inversify';
import URI from '@theia/core/lib/common/uri';
import { PreferenceScope } from '@theia/core/lib/browser';
import { PreferenceScope, PreferenceResolveResult } from '@theia/core/lib/browser';
import { FileStat } from '@theia/filesystem/lib/common';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { JsonSchemaConfiguration } from '@theia/core/src/browser/json-schema-store';
import { Path } from '@theia/core';
import { SectionPreferenceProvider } from './section-preference-provider';

export const FolderPreferenceProviderFactory = Symbol('FolderPreferenceProviderFactory');
Expand Down Expand Up @@ -57,4 +59,30 @@ export class FolderPreferenceProvider extends SectionPreferenceProvider {
getDomain(): string[] {
return [this.folderUri.toString()];
}

resolve<T>(preferenceName: string, resourceUri?: string): PreferenceResolveResult<T> {
// resolve relative paths for json schema settings in workspace scope
if (preferenceName === 'json.schemas' && this.getScope() === PreferenceScope.Workspace) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be done by clients? It does not look good to introduce handling of individual preferences into the preference service logic.

const workspaceSettings = this.getPreferences(resourceUri)[preferenceName];
if (workspaceSettings && resourceUri) {
const rootPath = new URI(resourceUri).path.toString();
workspaceSettings.forEach((schemaConfig: JsonSchemaConfiguration) => {
let url = schemaConfig.url;
if (url.match(rootPath)) {
url = new URI(new Path(url).toString()).toString();
} else if ((url.startsWith('.') || url.startsWith('/') || url.match(/^\w+\//))) {
url = new URI(rootPath).resolve(url).normalizePath().toString();
}
if (url) {
schemaConfig.url = url;
}
});
}
return {
value: workspaceSettings,
configUri: this.getConfigUri(resourceUri)
};
}
return super.resolve(preferenceName, resourceUri);
}
}