1
1
import { CompletionItemKind } from "vscode-languageserver" ;
2
+ import * as SchemaDocument from "../schema-document.js" ;
2
3
import { subscribe } from "../pubsub.js" ;
3
4
4
5
@@ -8,61 +9,63 @@ export default {
8
9
} ,
9
10
10
11
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 ) ;
17
16
}
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
+ // }
18
24
} ) ;
19
25
}
20
26
} ;
21
27
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}"]
38
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"
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}"]
54
52
},
55
- {
56
- label : "true" ,
57
- kind : CompletionItemKind . Snippet ,
58
- insertText : `true` ,
59
- documentation : "if true"
53
+ "then": {
60
54
},
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