From d8da878433ba2a1326cc511ca4f0caf1290a0a45 Mon Sep 17 00:00:00 2001 From: Petr Spacek <p-spacek@email.cz> Date: Mon, 6 Nov 2023 13:32:53 +0100 Subject: [PATCH] fix: unify doNotSuggest --- src/services/jsonCompletion.ts | 6 ++++ src/test/completion.test.ts | 55 +++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/services/jsonCompletion.ts b/src/services/jsonCompletion.ts index 90d4c9f..0875717 100644 --- a/src/services/jsonCompletion.ts +++ b/src/services/jsonCompletion.ts @@ -225,6 +225,9 @@ export class JSONCompletion { private getPropertyCompletions(schema: SchemaService.ResolvedSchema, doc: Parser.JSONDocument, node: ASTNode, addValue: boolean, separatorAfter: string, collector: CompletionsCollector): void { const matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset); matchingSchemas.forEach((s) => { + if (s.schema.doNotSuggest) { + return; + } if (s.node === node && !s.inverted) { const schemaProperties = s.schema.properties; if (schemaProperties) { @@ -543,6 +546,9 @@ export class JSONCompletion { private addSchemaValueCompletions(schema: JSONSchemaRef, separatorAfter: string, collector: CompletionsCollector, types: { [type: string]: boolean }): void { if (typeof schema === 'object') { + if (schema.doNotSuggest) { + return; + } this.addEnumValueCompletions(schema, separatorAfter, collector); this.addDefaultValueCompletions(schema, separatorAfter, collector); this.collectTypes(schema, types); diff --git a/src/test/completion.test.ts b/src/test/completion.test.ts index dc9a825..ed86cbd 100644 --- a/src/test/completion.test.ts +++ b/src/test/completion.test.ts @@ -74,7 +74,7 @@ suite('JSON Completion', () => { const position = Position.create(0, offset); const jsonDoc = ls.parseJSONDocument(document); return ls.doComplete(document, position, jsonDoc).then(list => { - if (expected.count) { + if (typeof expected.count === 'number') { assert.equal(list!.items.length, expected.count, value + ' ' + list!.items.map(i => i.label).join(', ')); } if (expected.items) { @@ -1359,6 +1359,59 @@ suite('JSON Completion', () => { }); }); + test("DoNotSuggest: should not autocomplete schema with doNotSuggest - property completion", async () => { + const schema: JSONSchema = { + properties: { + prop1: { type: "string" }, + }, + doNotSuggest: true, + }; + await testCompletionsFor("{|", schema, { count: 0 }); + }); + + test("DoNotSuggest: should not autocomplete schema with doNotSuggest - value completion", async () => { + const schema: JSONSchema = { + properties: { + prop1: { + anyOf: [ + { + type: "string", + default: "value_default", + doNotSuggest: true, + }, + { + type: "object", + defaultSnippets: [ + { + label: "snippet", + body: { + value1: "value_snippet", + }, + }, + ], + doNotSuggest: true, + }, + ], + }, + }, + }; + await testCompletionsFor('{"prop1": |', schema, { count: 0 }); + }); + + test("DoNotSuggest: should autocomplete inside schema in doNotSuggest", async () => { + const schema: JSONSchema = { + properties: { + obj1: { + properties: { + item1: { type: "string" }, + }, + }, + }, + doNotSuggest: true, + }; + await testCompletionsFor('{ "obj1": {|', schema, { count: 1, items: [{ label: "item1" }] }); + }); + test('suggestSortText', async function () { const schema: JSONSchema = { type: 'object',