-
Notifications
You must be signed in to change notification settings - Fork 254
Add status bar item for JSON schema selection #643
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
Merged
evidolob
merged 7 commits into
redhat-developer:main
from
evidolob:schema-status-bar-item
Dec 15, 2021
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b438db9
Add status bar item for JSON schema selection
evidolob 8c4e076
fix review comments
evidolob 9b73d41
Update test/json-schema-selection.test.ts
evidolob e095f45
Fix build when yaml-ls linked with 'yarn link'
evidolob 2f02b1c
Merge branch 'main' of github.com:redhat-developer/vscode-yaml into s…
evidolob 73ecbc2
Upgrade to new yaml-ls version
evidolob b30c0b2
Update ts version
evidolob 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 hidden or 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 hidden or 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,163 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Red Hat, Inc. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
| import { | ||
| ExtensionContext, | ||
| window, | ||
| commands, | ||
| StatusBarAlignment, | ||
| TextEditor, | ||
| StatusBarItem, | ||
| QuickPickItem, | ||
| ThemeColor, | ||
| workspace, | ||
| } from 'vscode'; | ||
| import { CommonLanguageClient, RequestType } from 'vscode-languageclient/node'; | ||
|
|
||
| type FileUri = string; | ||
| interface JSONSchema { | ||
| name?: string; | ||
| description?: string; | ||
| uri: string; | ||
| } | ||
|
|
||
| interface MatchingJSONSchema extends JSONSchema { | ||
| usedForCurrentFile: boolean; | ||
| fromStore: boolean; | ||
| } | ||
|
|
||
| interface SchemaItem extends QuickPickItem { | ||
| schema?: MatchingJSONSchema; | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/ban-types | ||
| const getJSONSchemas: RequestType<FileUri, MatchingJSONSchema[], {}> = new RequestType('yaml/get/all/jsonSchemas'); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/ban-types | ||
| const getSchema: RequestType<FileUri, JSONSchema[], {}> = new RequestType('yaml/get/jsonSchema'); | ||
|
|
||
| export let statusBarItem: StatusBarItem; | ||
|
|
||
| let client: CommonLanguageClient; | ||
| export function createJSONSchemaStatusBarItem(context: ExtensionContext, languageclient: CommonLanguageClient): void { | ||
| if (statusBarItem) { | ||
| updateStatusBar(window.activeTextEditor); | ||
| return; | ||
| } | ||
| const commandId = 'yaml.select.json.schema'; | ||
| client = languageclient; | ||
| commands.registerCommand(commandId, () => { | ||
| return showSchemaSelection(); | ||
| }); | ||
| statusBarItem = window.createStatusBarItem(StatusBarAlignment.Right); | ||
| statusBarItem.command = commandId; | ||
| context.subscriptions.push(statusBarItem); | ||
|
|
||
| context.subscriptions.push(window.onDidChangeActiveTextEditor(updateStatusBar)); | ||
| setTimeout(() => updateStatusBar(window.activeTextEditor), 5000); | ||
| } | ||
|
|
||
| async function updateStatusBar(editor: TextEditor): Promise<void> { | ||
| if (editor && editor.document.languageId === 'yaml') { | ||
| // get schema info there | ||
| const schema = await client.sendRequest(getSchema, editor.document.uri.toString()); | ||
| if (schema.length === 0) { | ||
| statusBarItem.text = 'No JSON Schema'; | ||
| statusBarItem.tooltip = 'Select JSON Schema'; | ||
| statusBarItem.backgroundColor = undefined; | ||
| } else if (schema.length === 1) { | ||
| statusBarItem.text = schema[0].name ?? schema[0].uri; | ||
| statusBarItem.tooltip = 'Select JSON Schema'; | ||
| statusBarItem.backgroundColor = undefined; | ||
| } else { | ||
| statusBarItem.text = 'Multiple JSON Schemas...'; | ||
| statusBarItem.tooltip = 'Multiple JSON Schema used to validate this file, click to select one'; | ||
| statusBarItem.backgroundColor = new ThemeColor('statusBarItem.warningBackground'); | ||
| } | ||
|
|
||
| statusBarItem.show(); | ||
| } else { | ||
| statusBarItem.hide(); | ||
| } | ||
| } | ||
|
|
||
| async function showSchemaSelection(): Promise<void> { | ||
| const schemas = await client.sendRequest(getJSONSchemas, window.activeTextEditor.document.uri.toString()); | ||
| const schemasPick = window.createQuickPick<SchemaItem>(); | ||
| const pickItems: SchemaItem[] = []; | ||
|
|
||
| for (const val of schemas) { | ||
| const item = { | ||
| label: val.name ?? val.uri, | ||
| description: val.description, | ||
| detail: val.usedForCurrentFile ? 'Used for current file$(check)' : '', | ||
| alwaysShow: val.usedForCurrentFile, | ||
| schema: val, | ||
| }; | ||
| pickItems.push(item); | ||
| } | ||
|
|
||
| pickItems.sort((a, b) => { | ||
| if (a.schema?.usedForCurrentFile) { | ||
| return -1; | ||
| } | ||
| if (b.schema?.usedForCurrentFile) { | ||
| return 1; | ||
| } | ||
| return a.label.localeCompare(b.label); | ||
| }); | ||
|
|
||
| schemasPick.items = pickItems; | ||
| schemasPick.placeholder = 'Search JSON schema'; | ||
| schemasPick.title = 'Select JSON schema'; | ||
| schemasPick.onDidHide(() => schemasPick.dispose()); | ||
|
|
||
| schemasPick.onDidChangeSelection((selection) => { | ||
| try { | ||
| if (selection.length > 0) { | ||
| if (selection[0].schema) { | ||
| const settings: Record<string, unknown> = workspace.getConfiguration('yaml').get('schemas'); | ||
| const fileUri = window.activeTextEditor.document.uri.toString(); | ||
| const newSettings = Object.assign({}, settings); | ||
| deleteExistingFilePattern(newSettings, fileUri); | ||
| const schemaURI = selection[0].schema.uri; | ||
| const schemaSettings = newSettings[schemaURI]; | ||
| if (schemaSettings) { | ||
| if (Array.isArray(schemaSettings)) { | ||
| (schemaSettings as Array<string>).push(fileUri); | ||
| } else if (typeof schemaSettings === 'string') { | ||
| newSettings[schemaURI] = [schemaSettings, fileUri]; | ||
| } | ||
| } else { | ||
| newSettings[schemaURI] = fileUri; | ||
| } | ||
| workspace.getConfiguration('yaml').update('schemas', newSettings); | ||
| } | ||
| } | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
| schemasPick.hide(); | ||
| }); | ||
| schemasPick.show(); | ||
| } | ||
|
|
||
| function deleteExistingFilePattern(settings: Record<string, unknown>, fileUri: string): unknown { | ||
| for (const key in settings) { | ||
| if (Object.prototype.hasOwnProperty.call(settings, key)) { | ||
| const element = settings[key]; | ||
|
|
||
| if (Array.isArray(element)) { | ||
| const filePatterns = element.filter((val) => val !== fileUri); | ||
| settings[key] = filePatterns; | ||
| } | ||
|
|
||
| if (element === fileUri) { | ||
| delete settings[key]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return settings; | ||
| } |
This file contains hidden or 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 hidden or 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,109 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Red Hat, Inc. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as sinon from 'sinon'; | ||
| import * as sinonChai from 'sinon-chai'; | ||
| import * as chai from 'chai'; | ||
| import { createJSONSchemaStatusBarItem } from '../src/schema-status-bar-item'; | ||
| import { CommonLanguageClient } from 'vscode-languageclient'; | ||
| import * as vscode from 'vscode'; | ||
| import { TestLanguageClient } from './helper'; | ||
| import * as jsonStatusBar from '../src/schema-status-bar-item'; | ||
| const expect = chai.expect; | ||
| chai.use(sinonChai); | ||
|
|
||
| describe('Completion should work in multiple different scenarios', () => { | ||
| const sandbox = sinon.createSandbox(); | ||
| let clock: sinon.SinonFakeTimers; | ||
| let clcStub: sinon.SinonStubbedInstance<TestLanguageClient>; | ||
| let registerCommandStub: sinon.SinonStub; | ||
| let createStatusBarItemStub: sinon.SinonStub; | ||
| let onDidChangeActiveTextEditorStub: sinon.SinonStub; | ||
|
|
||
| beforeEach(() => { | ||
| clcStub = sandbox.stub(new TestLanguageClient()); | ||
| registerCommandStub = sandbox.stub(vscode.commands, 'registerCommand'); | ||
| createStatusBarItemStub = sandbox.stub(vscode.window, 'createStatusBarItem'); | ||
| onDidChangeActiveTextEditorStub = sandbox.stub(vscode.window, 'onDidChangeActiveTextEditor'); | ||
| sandbox.stub(vscode.window, 'activeTextEditor').returns(undefined); | ||
| clock = sandbox.useFakeTimers(); | ||
| sandbox.stub(jsonStatusBar, 'statusBarItem').returns(undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| clock.restore(); | ||
| sandbox.restore(); | ||
| }); | ||
|
|
||
| it('Should create status bar item for JSON Schema', () => { | ||
| const context: vscode.ExtensionContext = { | ||
| subscriptions: [], | ||
| } as vscode.ExtensionContext; | ||
| createStatusBarItemStub.returns({}); | ||
|
|
||
| createJSONSchemaStatusBarItem(context, (clcStub as unknown) as CommonLanguageClient); | ||
|
|
||
| expect(registerCommandStub).calledOnceWith('yaml.select.json.schema'); | ||
| expect(createStatusBarItemStub).calledOnceWith(vscode.StatusBarAlignment.Right); | ||
| expect(context.subscriptions).has.length(2); | ||
| }); | ||
|
|
||
| it('Should update status bar on editor change', async () => { | ||
| const context: vscode.ExtensionContext = { | ||
| subscriptions: [], | ||
| } as vscode.ExtensionContext; | ||
| const statusBar = ({ show: sandbox.stub() } as unknown) as vscode.StatusBarItem; | ||
| createStatusBarItemStub.returns(statusBar); | ||
| onDidChangeActiveTextEditorStub.returns({}); | ||
| clcStub.sendRequest.resolves([{ uri: 'https://foo.com/bar.json', name: 'bar schema' }]); | ||
|
|
||
| createJSONSchemaStatusBarItem(context, (clcStub as unknown) as CommonLanguageClient); | ||
| const callBackFn = onDidChangeActiveTextEditorStub.firstCall.firstArg; | ||
| await callBackFn({ document: { languageId: 'yaml', uri: vscode.Uri.parse('/foo.yaml') } }); | ||
|
|
||
| expect(statusBar.text).to.equal('bar schema'); | ||
| expect(statusBar.tooltip).to.equal('Select JSON Schema'); | ||
| expect(statusBar.backgroundColor).to.be.undefined; | ||
| expect(statusBar.show).calledOnce; | ||
| }); | ||
|
|
||
| it('Should inform if there are no schema', async () => { | ||
| const context: vscode.ExtensionContext = { | ||
| subscriptions: [], | ||
| } as vscode.ExtensionContext; | ||
| const statusBar = ({ show: sandbox.stub() } as unknown) as vscode.StatusBarItem; | ||
| createStatusBarItemStub.returns(statusBar); | ||
| onDidChangeActiveTextEditorStub.returns({}); | ||
| clcStub.sendRequest.resolves([]); | ||
|
|
||
| createJSONSchemaStatusBarItem(context, (clcStub as unknown) as CommonLanguageClient); | ||
| const callBackFn = onDidChangeActiveTextEditorStub.firstCall.firstArg; | ||
| await callBackFn({ document: { languageId: 'yaml', uri: vscode.Uri.parse('/foo.yaml') } }); | ||
|
|
||
| expect(statusBar.text).to.equal('No JSON Schema'); | ||
| expect(statusBar.tooltip).to.equal('Select JSON Schema'); | ||
| expect(statusBar.backgroundColor).to.be.undefined; | ||
| expect(statusBar.show).calledOnce; | ||
| }); | ||
|
|
||
| it('Should inform if there are more than one schema', async () => { | ||
| const context: vscode.ExtensionContext = { | ||
| subscriptions: [], | ||
| } as vscode.ExtensionContext; | ||
| const statusBar = ({ show: sandbox.stub() } as unknown) as vscode.StatusBarItem; | ||
| createStatusBarItemStub.returns(statusBar); | ||
| onDidChangeActiveTextEditorStub.returns({}); | ||
| clcStub.sendRequest.resolves([{}, {}]); | ||
|
|
||
| createJSONSchemaStatusBarItem(context, (clcStub as unknown) as CommonLanguageClient); | ||
| const callBackFn = onDidChangeActiveTextEditorStub.firstCall.firstArg; | ||
| await callBackFn({ document: { languageId: 'yaml', uri: vscode.Uri.parse('/foo.yaml') } }); | ||
|
|
||
| expect(statusBar.text).to.equal('Multiple JSON Schemas...'); | ||
| expect(statusBar.tooltip).to.equal('Multiple JSON Schema used to validate this file, click to select one'); | ||
| expect(statusBar.backgroundColor).to.eql({ id: 'statusBarItem.warningBackground' }); | ||
| expect(statusBar.show).calledOnce; | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.