@@ -14,6 +14,18 @@ import (
14
14
"github.com/santhosh-tekuri/jsonschema/v6"
15
15
)
16
16
17
+ func prefix (line string , character int ) string {
18
+ sb := strings.Builder {}
19
+ for i := 0 ; i < character ; i ++ {
20
+ if unicode .IsSpace (rune (line [i ])) {
21
+ sb .Reset ()
22
+ } else {
23
+ sb .WriteByte (line [i ])
24
+ }
25
+ }
26
+ return sb .String ()
27
+ }
28
+
17
29
func Completion (ctx context.Context , params * protocol.CompletionParams , doc document.ComposeDocument ) (* protocol.CompletionList , error ) {
18
30
if params .Position .Character == 0 {
19
31
items := []protocol.CompletionItem {}
@@ -52,12 +64,24 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, doc docu
52
64
53
65
items := []protocol.CompletionItem {}
54
66
nodeProps := nodeProperties (path , line , character )
67
+ wordPrefix := prefix (lines [lspLine ], character - 1 )
55
68
if schema , ok := nodeProps .(* jsonschema.Schema ); ok {
56
69
if schema .Enum != nil {
57
70
for _ , value := range schema .Enum .Values {
71
+ enumValue := value .(string )
58
72
item := protocol.CompletionItem {
59
73
Detail : extractDetail (schema ),
60
- Label : value .(string ),
74
+ Label : enumValue ,
75
+ TextEdit : protocol.TextEdit {
76
+ NewText : enumValue ,
77
+ Range : protocol.Range {
78
+ Start : protocol.Position {
79
+ Line : params .Position .Line ,
80
+ Character : params .Position .Character - protocol .UInteger (len (wordPrefix )),
81
+ },
82
+ End : params .Position ,
83
+ },
84
+ },
61
85
}
62
86
items = append (items , item )
63
87
}
@@ -72,9 +96,18 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, doc docu
72
96
sb .WriteString (" " )
73
97
for attributeName , schema := range properties {
74
98
item := protocol.CompletionItem {
75
- Detail : extractDetail (schema ),
76
- Label : attributeName ,
77
- InsertText : insertText (sb .String (), attributeName , schema ),
99
+ Detail : extractDetail (schema ),
100
+ Label : attributeName ,
101
+ TextEdit : protocol.TextEdit {
102
+ NewText : insertText (sb .String (), attributeName , schema ),
103
+ Range : protocol.Range {
104
+ Start : protocol.Position {
105
+ Line : params .Position .Line ,
106
+ Character : params .Position .Character - protocol .UInteger (len (wordPrefix )),
107
+ },
108
+ End : params .Position ,
109
+ },
110
+ },
78
111
InsertTextMode : types .CreateInsertTextModePointer (protocol .InsertTextModeAsIs ),
79
112
}
80
113
@@ -94,7 +127,16 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, doc docu
94
127
}
95
128
}
96
129
sb .WriteString ("|}" )
97
- item .InsertText = types .CreateStringPointer (sb .String ())
130
+ item .TextEdit = protocol.TextEdit {
131
+ NewText : sb .String (),
132
+ Range : protocol.Range {
133
+ Start : protocol.Position {
134
+ Line : params .Position .Line ,
135
+ Character : params .Position .Character - protocol .UInteger (len (wordPrefix )),
136
+ },
137
+ End : params .Position ,
138
+ },
139
+ }
98
140
item .InsertTextFormat = types .CreateInsertTextFormatPointer (protocol .InsertTextFormatSnippet )
99
141
}
100
142
items = append (items , item )
@@ -218,21 +260,21 @@ func extractDetail(schema *jsonschema.Schema) *string {
218
260
return types .CreateStringPointer (strings .Join (schemaTypes , " or " ))
219
261
}
220
262
221
- func insertText (spacing , attributeName string , schema * jsonschema.Schema ) * string {
263
+ func insertText (spacing , attributeName string , schema * jsonschema.Schema ) string {
222
264
schemaTypes := referencedTypes (schema )
223
265
if slices .Contains (schemaTypes , "array" ) {
224
266
if len (schemaTypes ) == 1 {
225
- return types . CreateStringPointer ( fmt .Sprintf ("%v:\n %v- " , attributeName , spacing ) )
267
+ return fmt .Sprintf ("%v:\n %v- " , attributeName , spacing )
226
268
} else if len (schemaTypes ) == 2 && slices .Contains (schemaTypes , "object" ) {
227
- return types . CreateStringPointer ( fmt .Sprintf ("%v:\n %v" , attributeName , spacing ) )
269
+ return fmt .Sprintf ("%v:\n %v" , attributeName , spacing )
228
270
}
229
- return nil
271
+ return fmt . Sprintf ( "%v:" , attributeName )
230
272
}
231
273
if slices .Contains (schemaTypes , "object" ) {
232
274
if len (schemaTypes ) == 1 {
233
- return types . CreateStringPointer ( fmt .Sprintf ("%v:\n %v" , attributeName , spacing ) )
275
+ return fmt .Sprintf ("%v:\n %v" , attributeName , spacing )
234
276
}
235
- return types . CreateStringPointer ( fmt .Sprintf ("%v:" , attributeName ) )
277
+ return fmt .Sprintf ("%v:" , attributeName )
236
278
}
237
- return types . CreateStringPointer ( fmt .Sprintf ("%v: " , attributeName ) )
279
+ return fmt .Sprintf ("%v: " , attributeName )
238
280
}
0 commit comments