Skip to content

Commit be19e0e

Browse files
committed
implement behavior to deselect all other schemas when "No JSON Schema" is selected
Signed-off-by: Morgan Chang <shin19991207@gmail.com>
1 parent 495bd1a commit be19e0e

2 files changed

Lines changed: 61 additions & 7 deletions

File tree

src/schema-status-bar-item.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,21 @@ async function showSchemaSelection(): Promise<void> {
168168
const selectedSchemaItems = pickItems.filter((item) => item.schema?.usedForCurrentFile);
169169
schemasPick.selectedItems =
170170
selectedSchemaItems.length > 0 && !isSchemaDetectionDisabled(fileUri) ? selectedSchemaItems : [noSchemaItem];
171+
let previousSelectedItems = schemasPick.selectedItems;
171172
schemasPick.placeholder = 'Search JSON schema';
172173
schemasPick.title = 'Select JSON schemas';
173174
schemasPick.onDidHide(() => schemasPick.dispose());
175+
schemasPick.onDidChangeSelection((items) => {
176+
const selectedSchemaItems = items.filter((item) => item.schema);
177+
const hasNoSchemaItem = items.some((item) => item.disableSchemaDetection);
178+
if (items.length === 0) {
179+
schemasPick.selectedItems = [noSchemaItem];
180+
} else if (hasNoSchemaItem && selectedSchemaItems.length > 0) {
181+
const previousHadNoSchemaItem = previousSelectedItems.some((item) => item.disableSchemaDetection);
182+
schemasPick.selectedItems = previousHadNoSchemaItem ? selectedSchemaItems : [noSchemaItem];
183+
}
184+
previousSelectedItems = schemasPick.selectedItems;
185+
});
174186
schemasPick.onDidTriggerItemButton((event) => {
175187
if (event.button === selectSchemaVersionButton && event.item.schema?.versions) {
176188
const selectedSchemaUris = schemasPick.selectedItems.flatMap((item) => (item.schema ? [item.schema.uri] : []));

test/json-schema-selection.test.ts

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('Status bar should work in multiple different scenarios', () => {
7474
const statusBar = ({ show: sandbox.stub(), hide: sandbox.stub() } as unknown) as vscode.StatusBarItem;
7575
createStatusBarItemStub.returns(statusBar);
7676
onDidChangeActiveTextEditorStub.returns({});
77-
clcStub.sendRequest.resolves([{ uri: 'https://foo.com/bar.json', name: 'bar schema' }]);
77+
clcStub.sendRequest.resolves([{ uri: 'https://foo.com/bar.json', name: 'bar schema', usedForCurrentFile: true }]);
7878

7979
createJSONSchemaStatusBarItem(context, (clcStub as unknown) as CommonLanguageClient);
8080
const callBackFn = onDidChangeActiveTextEditorStub.firstCall.firstArg;
@@ -198,15 +198,15 @@ describe('Status bar should work in multiple different scenarios', () => {
198198
expect(statusBar.show).calledOnce;
199199
});
200200

201-
it('Should include No JSON Schema in schema selection', async () => {
201+
it('Should include "No JSON Schema" in schema selection', async () => {
202202
const context: vscode.ExtensionContext = {
203203
subscriptions: [],
204204
} as vscode.ExtensionContext;
205205
const statusBar = ({ show: sandbox.stub(), hide: sandbox.stub() } as unknown) as vscode.StatusBarItem;
206206
const quickPick = createQuickPickStubValue<TestSchemaItem>();
207207
createStatusBarItemStub.returns(statusBar);
208208
createQuickPickStub.returns(quickPick);
209-
clcStub.sendRequest.resolves([{ uri: 'https://foo.com/bar.json', name: 'bar schema' }]);
209+
clcStub.sendRequest.resolves([{ uri: 'https://foo.com/bar.json', name: 'bar schema', usedForCurrentFile: true }]);
210210
activeTextEditor = ({
211211
document: { languageId: 'yaml', uri: vscode.Uri.parse('/foo.yaml') },
212212
} as unknown) as vscode.TextEditor;
@@ -472,7 +472,7 @@ describe('Status bar should work in multiple different scenarios', () => {
472472
expect(quickPick.hide).calledOnce;
473473
});
474474

475-
it('Should use No JSON Schema when all schemas are deselected', async () => {
475+
it('Should use and auto-select "No JSON Schema" when all schemas are deselected', async () => {
476476
const context: vscode.ExtensionContext = {
477477
subscriptions: [],
478478
} as vscode.ExtensionContext;
@@ -498,14 +498,15 @@ describe('Status bar should work in multiple different scenarios', () => {
498498
await command();
499499
expect(quickPick.selectedItems).has.length(1);
500500
quickPick.select([]);
501+
expect(quickPick.selectedItems).to.deep.equal([quickPick.items[0]]);
501502
await quickPick.accept();
502503

503504
expect(update).calledWith('disableSchemaDetection', ['file:///foo.yaml']);
504505
expect(update).not.calledWith('schemas');
505506
expect(quickPick.hide).calledOnce;
506507
});
507508

508-
it('Should let No JSON Schema override other selected schemas', async () => {
509+
it('Should auto-deselect all other selected schemas when "No JSON Schema" is selected', async () => {
509510
const context: vscode.ExtensionContext = {
510511
subscriptions: [],
511512
} as vscode.ExtensionContext;
@@ -514,7 +515,7 @@ describe('Status bar should work in multiple different scenarios', () => {
514515
const update = sandbox.stub();
515516
createStatusBarItemStub.returns(statusBar);
516517
createQuickPickStub.returns(quickPick);
517-
clcStub.sendRequest.resolves([{ uri: 'https://foo.com/bar.json', name: 'bar schema' }]);
518+
clcStub.sendRequest.resolves([{ uri: 'https://foo.com/bar.json', name: 'bar schema', usedForCurrentFile: true }]);
518519
activeTextEditor = ({
519520
document: { languageId: 'yaml', uri: vscode.Uri.parse('/foo.yaml') },
520521
} as unknown) as vscode.TextEditor;
@@ -532,14 +533,55 @@ describe('Status bar should work in multiple different scenarios', () => {
532533
const noSchemaItem = quickPick.items[0];
533534
const schemaItem = quickPick.items.find((item) => item.schema);
534535
expect(schemaItem).to.exist;
535-
quickPick.select([noSchemaItem, schemaItem as TestSchemaItem]);
536+
expect(quickPick.selectedItems).to.deep.equal([schemaItem]);
537+
quickPick.select([schemaItem as TestSchemaItem, noSchemaItem]);
538+
expect(quickPick.selectedItems).to.deep.equal([noSchemaItem]);
536539
await quickPick.accept();
537540

538541
expect(update).calledWith('disableSchemaDetection', ['file:///foo.yaml']);
539542
expect(update).not.calledWith('schemas');
540543
expect(quickPick.hide).calledOnce;
541544
});
542545

546+
it('Should deselect "No JSON Schema" when a schema is selected', async () => {
547+
const context: vscode.ExtensionContext = {
548+
subscriptions: [],
549+
} as vscode.ExtensionContext;
550+
const statusBar = ({ show: sandbox.stub(), hide: sandbox.stub() } as unknown) as vscode.StatusBarItem;
551+
const quickPick = createQuickPickStubValue<TestSchemaItem>();
552+
const update = sandbox.stub();
553+
const get = sandbox.stub();
554+
get.withArgs('disableSchemaDetection').returns([]);
555+
get.withArgs('schemas').returns({});
556+
createStatusBarItemStub.returns(statusBar);
557+
createQuickPickStub.returns(quickPick);
558+
clcStub.sendRequest.resolves([{ uri: 'https://foo.com/bar.json', name: 'bar schema' }]);
559+
activeTextEditor = ({
560+
document: { languageId: 'yaml', uri: vscode.Uri.parse('/foo.yaml') },
561+
} as unknown) as vscode.TextEditor;
562+
sandbox
563+
.stub(vscode.workspace, 'getConfiguration')
564+
.withArgs('yaml')
565+
.returns(({
566+
get,
567+
update,
568+
} as unknown) as vscode.WorkspaceConfiguration);
569+
570+
createJSONSchemaStatusBarItem(context, (clcStub as unknown) as CommonLanguageClient);
571+
const command = registerCommandStub.firstCall.args[1];
572+
await command();
573+
const noSchemaItem = quickPick.items[0];
574+
const schemaItem = quickPick.items.find((item) => item.schema);
575+
expect(schemaItem).to.exist;
576+
quickPick.select([noSchemaItem, schemaItem as TestSchemaItem]);
577+
expect(quickPick.selectedItems).to.deep.equal([schemaItem]);
578+
await quickPick.accept();
579+
580+
expect(update).calledWith('disableSchemaDetection', []);
581+
expect(update).calledWith('schemas', { 'https://foo.com/bar.json': 'file:///foo.yaml' });
582+
expect(quickPick.hide).calledOnce;
583+
});
584+
543585
it('Should select a schema version using the schema item button and preserve other selected schemas', async () => {
544586
const context: vscode.ExtensionContext = {
545587
subscriptions: [],

0 commit comments

Comments
 (0)