|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Red Hat, Inc. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +import { |
| 6 | + ExtensionContext, |
| 7 | + window, |
| 8 | + commands, |
| 9 | + StatusBarAlignment, |
| 10 | + TextEditor, |
| 11 | + StatusBarItem, |
| 12 | + QuickPickItem, |
| 13 | + ThemeColor, |
| 14 | + workspace, |
| 15 | +} from 'vscode'; |
| 16 | +import { CommonLanguageClient, RequestType } from 'vscode-languageclient/node'; |
| 17 | + |
| 18 | +type FileUri = string; |
| 19 | +interface JSONSchema { |
| 20 | + name?: string; |
| 21 | + description?: string; |
| 22 | + uri: string; |
| 23 | +} |
| 24 | + |
| 25 | +interface MatchingJSONSchema extends JSONSchema { |
| 26 | + usedForCurrentFile: boolean; |
| 27 | + fromStore: boolean; |
| 28 | +} |
| 29 | + |
| 30 | +interface SchemaItem extends QuickPickItem { |
| 31 | + schema?: MatchingJSONSchema; |
| 32 | +} |
| 33 | + |
| 34 | +// eslint-disable-next-line @typescript-eslint/ban-types |
| 35 | +const getJSONSchemas: RequestType<FileUri, MatchingJSONSchema[], {}> = new RequestType('yaml/get/all/jsonSchemas'); |
| 36 | + |
| 37 | +// eslint-disable-next-line @typescript-eslint/ban-types |
| 38 | +const getSchema: RequestType<FileUri, JSONSchema[], {}> = new RequestType('yaml/get/jsonSchema'); |
| 39 | + |
| 40 | +export let statusBarItem: StatusBarItem; |
| 41 | + |
| 42 | +let client: CommonLanguageClient; |
| 43 | +export function createJSONSchemaStatusBarItem(context: ExtensionContext, languageclient: CommonLanguageClient): void { |
| 44 | + if (statusBarItem) { |
| 45 | + updateStatusBar(window.activeTextEditor); |
| 46 | + return; |
| 47 | + } |
| 48 | + const commandId = 'yaml.select.json.schema'; |
| 49 | + client = languageclient; |
| 50 | + commands.registerCommand(commandId, () => { |
| 51 | + return showSchemaSelection(); |
| 52 | + }); |
| 53 | + statusBarItem = window.createStatusBarItem(StatusBarAlignment.Right); |
| 54 | + statusBarItem.command = commandId; |
| 55 | + context.subscriptions.push(statusBarItem); |
| 56 | + |
| 57 | + context.subscriptions.push(window.onDidChangeActiveTextEditor(updateStatusBar)); |
| 58 | + setTimeout(() => updateStatusBar(window.activeTextEditor), 5000); |
| 59 | +} |
| 60 | + |
| 61 | +async function updateStatusBar(editor: TextEditor): Promise<void> { |
| 62 | + if (editor && editor.document.languageId === 'yaml') { |
| 63 | + // get schema info there |
| 64 | + const schema = await client.sendRequest(getSchema, editor.document.uri.toString()); |
| 65 | + if (schema.length === 0) { |
| 66 | + statusBarItem.text = 'No JSON Schema'; |
| 67 | + statusBarItem.tooltip = 'Select JSON Schema'; |
| 68 | + statusBarItem.backgroundColor = undefined; |
| 69 | + } else if (schema.length === 1) { |
| 70 | + statusBarItem.text = schema[0].name ?? schema[0].uri; |
| 71 | + statusBarItem.tooltip = 'Select JSON Schema'; |
| 72 | + statusBarItem.backgroundColor = undefined; |
| 73 | + } else { |
| 74 | + statusBarItem.text = 'Multiple JSON Schemas...'; |
| 75 | + statusBarItem.tooltip = 'Multiple JSON Schema used to validate this file, click to select one'; |
| 76 | + statusBarItem.backgroundColor = new ThemeColor('statusBarItem.warningBackground'); |
| 77 | + } |
| 78 | + |
| 79 | + statusBarItem.show(); |
| 80 | + } else { |
| 81 | + statusBarItem.hide(); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +async function showSchemaSelection(): Promise<void> { |
| 86 | + const schemas = await client.sendRequest(getJSONSchemas, window.activeTextEditor.document.uri.toString()); |
| 87 | + const schemasPick = window.createQuickPick<SchemaItem>(); |
| 88 | + const pickItems: SchemaItem[] = []; |
| 89 | + |
| 90 | + for (const val of schemas) { |
| 91 | + const item = { |
| 92 | + label: val.name ?? val.uri, |
| 93 | + description: val.description, |
| 94 | + detail: val.usedForCurrentFile ? 'Used for current file$(check)' : '', |
| 95 | + alwaysShow: val.usedForCurrentFile, |
| 96 | + schema: val, |
| 97 | + }; |
| 98 | + pickItems.push(item); |
| 99 | + } |
| 100 | + |
| 101 | + pickItems.sort((a, b) => { |
| 102 | + if (a.schema?.usedForCurrentFile) { |
| 103 | + return -1; |
| 104 | + } |
| 105 | + if (b.schema?.usedForCurrentFile) { |
| 106 | + return 1; |
| 107 | + } |
| 108 | + return a.label.localeCompare(b.label); |
| 109 | + }); |
| 110 | + |
| 111 | + schemasPick.items = pickItems; |
| 112 | + schemasPick.placeholder = 'Search JSON schema'; |
| 113 | + schemasPick.title = 'Select JSON schema'; |
| 114 | + schemasPick.onDidHide(() => schemasPick.dispose()); |
| 115 | + |
| 116 | + schemasPick.onDidChangeSelection((selection) => { |
| 117 | + try { |
| 118 | + if (selection.length > 0) { |
| 119 | + if (selection[0].schema) { |
| 120 | + const settings: Record<string, unknown> = workspace.getConfiguration('yaml').get('schemas'); |
| 121 | + const fileUri = window.activeTextEditor.document.uri.toString(); |
| 122 | + const newSettings = Object.assign({}, settings); |
| 123 | + deleteExistingFilePattern(newSettings, fileUri); |
| 124 | + const schemaURI = selection[0].schema.uri; |
| 125 | + const schemaSettings = newSettings[schemaURI]; |
| 126 | + if (schemaSettings) { |
| 127 | + if (Array.isArray(schemaSettings)) { |
| 128 | + (schemaSettings as Array<string>).push(fileUri); |
| 129 | + } else if (typeof schemaSettings === 'string') { |
| 130 | + newSettings[schemaURI] = [schemaSettings, fileUri]; |
| 131 | + } |
| 132 | + } else { |
| 133 | + newSettings[schemaURI] = fileUri; |
| 134 | + } |
| 135 | + workspace.getConfiguration('yaml').update('schemas', newSettings); |
| 136 | + } |
| 137 | + } |
| 138 | + } catch (err) { |
| 139 | + console.error(err); |
| 140 | + } |
| 141 | + schemasPick.hide(); |
| 142 | + }); |
| 143 | + schemasPick.show(); |
| 144 | +} |
| 145 | + |
| 146 | +function deleteExistingFilePattern(settings: Record<string, unknown>, fileUri: string): unknown { |
| 147 | + for (const key in settings) { |
| 148 | + if (Object.prototype.hasOwnProperty.call(settings, key)) { |
| 149 | + const element = settings[key]; |
| 150 | + |
| 151 | + if (Array.isArray(element)) { |
| 152 | + const filePatterns = element.filter((val) => val !== fileUri); |
| 153 | + settings[key] = filePatterns; |
| 154 | + } |
| 155 | + |
| 156 | + if (element === fileUri) { |
| 157 | + delete settings[key]; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return settings; |
| 163 | +} |
0 commit comments