Skip to content

Commit 0a7e190

Browse files
committed
Refactor for if/then completion
1 parent b94d719 commit 0a7e190

File tree

2 files changed

+55
-53
lines changed

2 files changed

+55
-53
lines changed

language-server/src/features/if-then-completion.js

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,63 @@ export default {
88
},
99

1010
onInitialized() {
11-
subscribe("completions", async (_message, { document, offset, completions }) => {
12-
const text = document.getText();
13-
const startOfLine = text.lastIndexOf("\n", offset - 1) + 1;
14-
const textBeforePosition = text.slice(startOfLine, offset);
15-
if (textBeforePosition.trim().endsWith("\"if\":")) {
16-
completions.push(...ifThenPatternCompletion());
11+
subscribe("completions", async (_message, { schemaDocument, offset, completions }) => {
12+
const currentProperty = schemaDocument.findNodeAtOffset(offset);
13+
if (currentProperty && currentProperty.pointer.endsWith("/if")) {
14+
completions.push(...ifThenPatternCompletion);
1715
}
16+
17+
// const text = document.getText();
18+
// const startOfLine = text.lastIndexOf("\n", offset - 1) + 1;
19+
// const textBeforePosition = text.slice(startOfLine, offset);
20+
// if (textBeforePosition.trim().endsWith("\"if\":")) {
21+
// completions.push(...ifThenPatternCompletion());
22+
// }
1823
});
1924
}
2025
};
2126

22-
function ifThenPatternCompletion() {
23-
return [
24-
{
25-
label: "if/then",
26-
kind: CompletionItemKind.Snippet,
27-
insertText: `{
28-
"type": "object",
29-
"properties": {
30-
"{varName}": { "const": "{value}" }
31-
},
32-
"required": ["{varName}"]
33-
},
34-
35-
"then": {
36-
}`,
37-
documentation: "Basic if/then pattern with a single condition and corresponding schema."
27+
const ifThenPatternCompletion = [
28+
{
29+
label: "if/then",
30+
kind: CompletionItemKind.Snippet,
31+
insertText: `{
32+
"type": "object",
33+
"properties": {
34+
"{varName}": { "const": "{value}" }
35+
},
36+
"required": ["{varName}"]
3837
},
39-
{
40-
label: "If/then/else",
41-
kind: CompletionItemKind.Snippet,
42-
insertText: `{
43-
"type": "object",
44-
"properties": {
45-
"{varName}": { "const": "{value}" }
46-
},
47-
"required": ["{varName}"]
48-
},
49-
"then": {
50-
},
51-
"else": {
52-
}`,
53-
documentation: "Conditional object structure with if/then/else logic"
38+
"then": {
39+
}`,
40+
documentation: "Basic if/then pattern with a single condition and corresponding schema."
41+
},
42+
{
43+
label: "If/then/else",
44+
kind: CompletionItemKind.Snippet,
45+
insertText: `{
46+
"type": "object",
47+
"properties": {
48+
"{varName}": { "const": "{value}" }
49+
},
50+
"required": ["{varName}"]
5451
},
55-
{
56-
label: "true",
57-
kind: CompletionItemKind.Snippet,
58-
insertText: `true`,
59-
documentation: "if true"
52+
"then": {
6053
},
61-
{
62-
label: "false",
63-
kind: CompletionItemKind.Snippet,
64-
insertText: `false`,
65-
documentation: "if false"
66-
}
67-
];
68-
}
54+
"else": {
55+
}`,
56+
documentation: "Conditional object structure with if/then/else logic"
57+
},
58+
{
59+
label: "true",
60+
kind: CompletionItemKind.Snippet,
61+
insertText: `true`,
62+
documentation: "if true"
63+
},
64+
{
65+
label: "false",
66+
kind: CompletionItemKind.Snippet,
67+
insertText: `false`,
68+
documentation: "if false"
69+
}
70+
];

language-server/src/json-schema-document.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ export class JsonSchemaDocument {
129129
break;
130130

131131
case "property":
132-
if (node.children.length !== 2) {
133-
return;
134-
}
132+
// if (node.children.length !== 2) {
133+
// return;
134+
// }
135135

136136
jsonNode.children = node.children.map((child) => {
137137
return this.#buildSchemaResources(child, uri, dialectUri, pointer, jsonNode);

0 commit comments

Comments
 (0)