Skip to content

Commit 1026f9c

Browse files
diyaayayjdesrosiers
authored andcommitted
Refactor for if/then completion
1 parent f1b1c33 commit 1026f9c

File tree

1 file changed

+53
-50
lines changed

1 file changed

+53
-50
lines changed
Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CompletionItemKind } from "vscode-languageserver";
2+
import * as SchemaDocument from "../schema-document.js";
23
import { subscribe } from "../pubsub.js";
34

45

@@ -8,61 +9,63 @@ export default {
89
},
910

1011
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());
12+
subscribe("completions", async (_message, { schemaDocument, offset, completions }) => {
13+
const currentProperty = SchemaDocument.findNodeAtOffset(schemaDocument, offset);
14+
if (currentProperty && currentProperty.pointer.endsWith("/if")) {
15+
completions.push(...ifThenPatternCompletion);
1716
}
17+
18+
// const text = document.getText();
19+
// const startOfLine = text.lastIndexOf("\n", offset - 1) + 1;
20+
// const textBeforePosition = text.slice(startOfLine, offset);
21+
// if (textBeforePosition.trim().endsWith("\"if\":")) {
22+
// completions.push(...ifThenPatternCompletion());
23+
// }
1824
});
1925
}
2026
};
2127

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."
28+
const ifThenPatternCompletion = [
29+
{
30+
label: "if/then",
31+
kind: CompletionItemKind.Snippet,
32+
insertText: `{
33+
"type": "object",
34+
"properties": {
35+
"{varName}": { "const": "{value}" }
36+
},
37+
"required": ["{varName}"]
3838
},
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"
39+
"then": {
40+
}`,
41+
documentation: "Basic if/then pattern with a single condition and corresponding schema."
42+
},
43+
{
44+
label: "If/then/else",
45+
kind: CompletionItemKind.Snippet,
46+
insertText: `{
47+
"type": "object",
48+
"properties": {
49+
"{varName}": { "const": "{value}" }
50+
},
51+
"required": ["{varName}"]
5452
},
55-
{
56-
label: "true",
57-
kind: CompletionItemKind.Snippet,
58-
insertText: `true`,
59-
documentation: "if true"
53+
"then": {
6054
},
61-
{
62-
label: "false",
63-
kind: CompletionItemKind.Snippet,
64-
insertText: `false`,
65-
documentation: "if false"
66-
}
67-
];
68-
}
55+
"else": {
56+
}`,
57+
documentation: "Conditional object structure with if/then/else logic"
58+
},
59+
{
60+
label: "true",
61+
kind: CompletionItemKind.Snippet,
62+
insertText: `true`,
63+
documentation: "if true"
64+
},
65+
{
66+
label: "false",
67+
kind: CompletionItemKind.Snippet,
68+
insertText: `false`,
69+
documentation: "if false"
70+
}
71+
];

0 commit comments

Comments
 (0)