Skip to content

Commit

Permalink
[json] schemas in workspace settings also apply to external files (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli authored Mar 22, 2023
1 parent 2f1f07e commit 2ea2c80
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions extensions/json-language-features/client/src/jsonClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,35 +545,45 @@ function getSettings(): Settings {
}
};

const collectSchemaSettings = (schemaSettings: JSONSchemaSettings[] | undefined, folderUri: Uri | undefined = undefined, settingsLocation = folderUri) => {
/*
* Add schemas from the settings
* folderUri to which folder the setting is scoped to. `undefined` means global (also external files)
* settingsLocation against which path relative schema URLs are resolved
*/
const collectSchemaSettings = (schemaSettings: JSONSchemaSettings[] | undefined, folderUri: string | undefined, settingsLocation: Uri | undefined) => {
if (schemaSettings) {
for (const setting of schemaSettings) {
const url = getSchemaId(setting, settingsLocation);
if (url) {
const schemaSetting: JSONSchemaSettings = { url, fileMatch: setting.fileMatch, folderUri: folderUri?.toString(false), schema: setting.schema };
const schemaSetting: JSONSchemaSettings = { url, fileMatch: setting.fileMatch, folderUri, schema: setting.schema };
schemas.push(schemaSetting);
}
}
}
};

const folders = workspace.workspaceFolders;
const folders = workspace.workspaceFolders ?? [];

const schemaConfigInfo = workspace.getConfiguration('json', null).inspect<JSONSchemaSettings[]>('schemas');
if (schemaConfigInfo) {
if (schemaConfigInfo.workspaceValue && workspace.workspaceFile && folders && folders.length) {
const settingsLocation = Uri.joinPath(workspace.workspaceFile, '..');
// settings in user config
collectSchemaSettings(schemaConfigInfo.globalValue, undefined, undefined);
if (workspace.workspaceFile) {
if (schemaConfigInfo.workspaceValue) {
const settingsLocation = Uri.joinPath(workspace.workspaceFile, '..');
// settings in the workspace configuration file apply to all files (also external files)
collectSchemaSettings(schemaConfigInfo.workspaceValue, undefined, settingsLocation);
}
for (const folder of folders) {
collectSchemaSettings(schemaConfigInfo.workspaceValue, folder.uri, settingsLocation);
const folderUri = folder.uri;
const folderSchemaConfigInfo = workspace.getConfiguration('json', folderUri).inspect<JSONSchemaSettings[]>('schemas');
collectSchemaSettings(folderSchemaConfigInfo?.workspaceFolderValue, folderUri.toString(false), folderUri);
}
} else {
if (schemaConfigInfo.workspaceValue && folders.length === 1) {
// single folder workspace: settings apply to all files (also external files)
collectSchemaSettings(schemaConfigInfo.workspaceValue, undefined, folders[0].uri);
}
}
collectSchemaSettings(schemaConfigInfo.globalValue);
}

if (folders) {
for (const folder of folders) {
const schemaConfigInfo = workspace.getConfiguration('json', folder.uri).inspect<JSONSchemaSettings[]>('schemas');
collectSchemaSettings(schemaConfigInfo?.workspaceFolderValue, folder.uri);
}
}
return settings;
Expand Down

0 comments on commit 2ea2c80

Please sign in to comment.