@@ -126,6 +126,7 @@ var textEditModifiers = []textEditModifier{buildTargetModifier, serviceSuggestio
126
126
127
127
func prefix (line string , character int ) string {
128
128
sb := strings.Builder {}
129
+ sb .Grow (character )
129
130
for i := range character {
130
131
if unicode .IsSpace (rune (line [i ])) {
131
132
sb .Reset ()
@@ -136,13 +137,14 @@ func prefix(line string, character int) string {
136
137
return sb .String ()
137
138
}
138
139
139
- func createSpacing (line string , whitespaceLine , arrayAttributes bool ) string {
140
- if whitespaceLine && arrayAttributes {
140
+ func createSpacing (line string , character int , arrayAttributes bool ) string {
141
+ if arrayAttributes {
141
142
// 2 more for the attribute, then 2 more for the array offset = 4 total
142
- return strings .Repeat (" " , len ( line ) + 4 )
143
+ return strings .Repeat (" " , character + 4 )
143
144
}
144
145
sb := strings.Builder {}
145
- for i := range line {
146
+ sb .Grow (character + 2 )
147
+ for i := range character {
146
148
if unicode .IsSpace (rune (line [i ])) || line [i ] == '-' {
147
149
sb .WriteString (" " )
148
150
}
@@ -201,75 +203,87 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
201
203
return nil , nil
202
204
}
203
205
206
+ character := int (params .Position .Character ) + 1
207
+ if len (lines [lspLine ]) < character - 1 {
208
+ return nil , nil
209
+ }
204
210
whitespaceLine := currentLineTrimmed == ""
205
211
line := int (lspLine ) + 1
206
- character := int (params .Position .Character ) + 1
207
212
path := constructCompletionNodePath (file , line )
213
+ wordPrefix := protocol .UInteger (len (prefix (lines [lspLine ], character - 1 )))
208
214
if len (path ) == 0 {
209
215
if topLevelNodeOffset != - 1 && params .Position .Character != uint32 (topLevelNodeOffset ) {
210
216
return nil , nil
211
217
}
212
218
return & protocol.CompletionList {Items : createTopLevelItems ()}, nil
213
219
} else if len (path ) == 1 {
220
+ if path [0 ].Key .GetToken ().Value == "include" {
221
+ schema := schemaProperties ()["include" ].Items .(* jsonschema.Schema )
222
+ items := createSchemaItems (params , schema .Ref .OneOf [1 ].Properties , lines , lspLine , whitespaceLine , wordPrefix , file , manager , u , path )
223
+ return processItems (items , whitespaceLine ), nil
224
+ }
214
225
return nil , nil
215
226
} else if path [1 ].Key .GetToken ().Position .Column >= character {
216
227
return nil , nil
217
- } else if len (lines [lspLine ]) < character - 1 {
218
- return nil , nil
219
228
}
220
229
221
- wordPrefix := prefix (lines [lspLine ], character - 1 )
222
230
path , nodeProps , arrayAttributes := nodeProperties (path , line , character )
223
- dependencies := dependencyCompletionItems (file , u , path , params , protocol . UInteger ( len ( wordPrefix )) )
231
+ dependencies := dependencyCompletionItems (file , u , path , params , wordPrefix )
224
232
if len (dependencies ) > 0 {
225
233
return & protocol.CompletionList {Items : dependencies }, nil
226
234
}
227
- items , stop := buildTargetCompletionItems (params , manager , path , u , protocol . UInteger ( len ( wordPrefix )) )
235
+ items , stop := buildTargetCompletionItems (params , manager , path , u , wordPrefix )
228
236
if stop {
229
237
return & protocol.CompletionList {Items : items }, nil
230
238
}
231
239
232
- items = namedDependencyCompletionItems (file , path , "configs" , "configs" , params , protocol . UInteger ( len ( wordPrefix )) )
240
+ items = namedDependencyCompletionItems (file , path , "configs" , "configs" , params , wordPrefix )
233
241
if len (items ) == 0 {
234
- items = namedDependencyCompletionItems (file , path , "secrets" , "secrets" , params , protocol . UInteger ( len ( wordPrefix )) )
242
+ items = namedDependencyCompletionItems (file , path , "secrets" , "secrets" , params , wordPrefix )
235
243
}
236
244
if len (items ) == 0 {
237
- items = volumeDependencyCompletionItems (file , path , params , protocol . UInteger ( len ( wordPrefix )) )
245
+ items = volumeDependencyCompletionItems (file , path , params , wordPrefix )
238
246
}
247
+ schemaItems := createSchemaItems (params , nodeProps , lines , lspLine , whitespaceLine && arrayAttributes , wordPrefix , file , manager , u , path )
248
+ items = append (items , schemaItems ... )
249
+ if len (items ) == 0 {
250
+ return nil , nil
251
+ }
252
+ return processItems (items , whitespaceLine && arrayAttributes ), nil
253
+ }
254
+
255
+ func createEnumItems (schema * jsonschema.Schema , params * protocol.CompletionParams , wordPrefixLength protocol.UInteger ) []protocol.CompletionItem {
256
+ items := []protocol.CompletionItem {}
257
+ for _ , value := range schema .Enum .Values {
258
+ enumValue := value .(string )
259
+ item := protocol.CompletionItem {
260
+ Label : enumValue ,
261
+ Documentation : schema .Description ,
262
+ Detail : extractDetail (schema ),
263
+ TextEdit : protocol.TextEdit {
264
+ NewText : enumValue ,
265
+ Range : protocol.Range {
266
+ Start : protocol.Position {
267
+ Line : params .Position .Line ,
268
+ Character : params .Position .Character - wordPrefixLength ,
269
+ },
270
+ End : params .Position ,
271
+ },
272
+ },
273
+ }
274
+ items = append (items , item )
275
+ }
276
+ return items
277
+ }
278
+
279
+ func createSchemaItems (params * protocol.CompletionParams , nodeProps any , lines []string , lspLine int , whitespacePrefixedArrayAttribute bool , wordPrefixLength protocol.UInteger , file * ast.File , manager * document.Manager , u * url.URL , path []* ast.MappingValueNode ) []protocol.CompletionItem {
280
+ items := []protocol.CompletionItem {}
239
281
if schema , ok := nodeProps .(* jsonschema.Schema ); ok {
240
282
if schema .Enum != nil {
241
- for _ , value := range schema .Enum .Values {
242
- enumValue := value .(string )
243
- item := protocol.CompletionItem {
244
- Label : enumValue ,
245
- Documentation : schema .Description ,
246
- Detail : extractDetail (schema ),
247
- TextEdit : protocol.TextEdit {
248
- NewText : enumValue ,
249
- Range : protocol.Range {
250
- Start : protocol.Position {
251
- Line : params .Position .Line ,
252
- Character : params .Position .Character - protocol .UInteger (len (wordPrefix )),
253
- },
254
- End : params .Position ,
255
- },
256
- },
257
- }
258
- items = append (items , item )
259
- }
283
+ return createEnumItems (schema , params , wordPrefixLength )
260
284
}
261
285
} else if properties , ok := nodeProps .(map [string ]* jsonschema.Schema ); ok {
262
- sb := strings.Builder {}
263
- for i := range lines [lspLine ] {
264
- if unicode .IsSpace (rune (lines [lspLine ][i ])) || lines [lspLine ][i ] == '-' {
265
- sb .WriteString (" " )
266
- }
267
- }
268
- sb .WriteString (" " )
269
- if whitespaceLine && arrayAttributes {
270
- sb .WriteString (" " )
271
- }
272
- spacing := createSpacing (lines [lspLine ], whitespaceLine , arrayAttributes )
286
+ spacing := createSpacing (lines [lspLine ], int (params .Position .Character ), whitespacePrefixedArrayAttribute )
273
287
for attributeName , schema := range properties {
274
288
item := protocol.CompletionItem {
275
289
Detail : extractDetail (schema ),
@@ -279,7 +293,7 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
279
293
Range : protocol.Range {
280
294
Start : protocol.Position {
281
295
Line : params .Position .Line ,
282
- Character : params .Position .Character - protocol .UInteger (len ( wordPrefix ) ),
296
+ Character : params .Position .Character - protocol .UInteger (wordPrefixLength ),
283
297
},
284
298
End : params .Position ,
285
299
},
@@ -314,7 +328,7 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
314
328
Range : protocol.Range {
315
329
Start : protocol.Position {
316
330
Line : params .Position .Line ,
317
- Character : params .Position .Character - protocol .UInteger (len ( wordPrefix ) ),
331
+ Character : params .Position .Character - protocol .UInteger (wordPrefixLength ),
318
332
},
319
333
End : params .Position ,
320
334
},
@@ -324,13 +338,14 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
324
338
items = append (items , item )
325
339
}
326
340
}
327
- if len (items ) == 0 {
328
- return nil , nil
329
- }
341
+ return items
342
+ }
343
+
344
+ func processItems (items []protocol.CompletionItem , arrayPrefix bool ) * protocol.CompletionList {
330
345
slices .SortFunc (items , func (a , b protocol.CompletionItem ) int {
331
346
return strings .Compare (a .Label , b .Label )
332
347
})
333
- if whitespaceLine && arrayAttributes {
348
+ if arrayPrefix {
334
349
for i := range items {
335
350
edit := items [i ].TextEdit .(protocol.TextEdit )
336
351
items [i ].TextEdit = protocol.TextEdit {
@@ -339,7 +354,7 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
339
354
}
340
355
}
341
356
}
342
- return & protocol.CompletionList {Items : items }, nil
357
+ return & protocol.CompletionList {Items : items }
343
358
}
344
359
345
360
func createChoiceSnippetText (itemTexts []completionItemText ) string {
0 commit comments