Skip to content

Commit f1b1c33

Browse files
diyaayayjdesrosiers
authored andcommitted
completion of if/then patterns for every if nested in blocks
1 parent 61ae4fb commit f1b1c33

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { CompletionItemKind } from "vscode-languageserver";
2+
import { subscribe } from "../pubsub.js";
3+
4+
5+
export default {
6+
onInitialize() {
7+
return {};
8+
},
9+
10+
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());
17+
}
18+
});
19+
}
20+
};
21+
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."
38+
},
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"
54+
},
55+
{
56+
label: "true",
57+
kind: CompletionItemKind.Snippet,
58+
insertText: `true`,
59+
documentation: "if true"
60+
},
61+
{
62+
label: "false",
63+
kind: CompletionItemKind.Snippet,
64+
insertText: `false`,
65+
documentation: "if false"
66+
}
67+
];
68+
}

language-server/src/server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import semanticTokensFeature from "./features/semantic-tokens.js";
1717
import validationErrorsFeature from "./features/validation-errors.js";
1818
import deprecatedFeature from "./features/deprecated.js";
1919
import completionFeature from "./features/completion.js";
20+
import ifThenCompletionFeature from "./features/if-then-completion.js";
2021
import schemaCompletion from "./features/schema-completion.js";
2122
import hoverFeature from "./features/hover.js";
2223

@@ -34,6 +35,7 @@ const features = [
3435
deprecatedFeature,
3536
completionFeature,
3637
schemaCompletion,
38+
ifThenCompletionFeature,
3739
hoverFeature,
3840
workspaceFeature // Workspace must be last
3941
];

0 commit comments

Comments
 (0)