Skip to content
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

fix: unify doNotSuggest #213

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/services/jsonCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
55 changes: 54 additions & 1 deletion src/test/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you call this method with expected.count === 0 it won't check zero length (assert.equal(list!.items.length, expected.count)

but after this change, it seems that test for test('In comment', ... is not correct

assert.equal(list!.items.length, expected.count, value + ' ' + list!.items.map(i => i.label).join(', '));
}
if (expected.items) {
Expand Down Expand Up @@ -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',
Expand Down
Loading