Skip to content

Commit 5ff8f21

Browse files
committed
Use TextEdit for all Compose completion items
Signed-off-by: Remy Suen <[email protected]>
1 parent 2554c8c commit 5ff8f21

File tree

2 files changed

+831
-675
lines changed

2 files changed

+831
-675
lines changed

internal/compose/completion.go

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ import (
1414
"github.com/santhosh-tekuri/jsonschema/v6"
1515
)
1616

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+
1729
func Completion(ctx context.Context, params *protocol.CompletionParams, doc document.ComposeDocument) (*protocol.CompletionList, error) {
1830
if params.Position.Character == 0 {
1931
items := []protocol.CompletionItem{}
@@ -52,12 +64,24 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, doc docu
5264

5365
items := []protocol.CompletionItem{}
5466
nodeProps := nodeProperties(path, line, character)
67+
wordPrefix := prefix(lines[lspLine], character-1)
5568
if schema, ok := nodeProps.(*jsonschema.Schema); ok {
5669
if schema.Enum != nil {
5770
for _, value := range schema.Enum.Values {
71+
enumValue := value.(string)
5872
item := protocol.CompletionItem{
5973
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+
},
6185
}
6286
items = append(items, item)
6387
}
@@ -72,9 +96,18 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, doc docu
7296
sb.WriteString(" ")
7397
for attributeName, schema := range properties {
7498
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+
},
78111
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
79112
}
80113

@@ -94,7 +127,16 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, doc docu
94127
}
95128
}
96129
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+
}
98140
item.InsertTextFormat = types.CreateInsertTextFormatPointer(protocol.InsertTextFormatSnippet)
99141
}
100142
items = append(items, item)
@@ -218,21 +260,21 @@ func extractDetail(schema *jsonschema.Schema) *string {
218260
return types.CreateStringPointer(strings.Join(schemaTypes, " or "))
219261
}
220262

221-
func insertText(spacing, attributeName string, schema *jsonschema.Schema) *string {
263+
func insertText(spacing, attributeName string, schema *jsonschema.Schema) string {
222264
schemaTypes := referencedTypes(schema)
223265
if slices.Contains(schemaTypes, "array") {
224266
if len(schemaTypes) == 1 {
225-
return types.CreateStringPointer(fmt.Sprintf("%v:\n%v- ", attributeName, spacing))
267+
return fmt.Sprintf("%v:\n%v- ", attributeName, spacing)
226268
} 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)
228270
}
229-
return nil
271+
return fmt.Sprintf("%v:", attributeName)
230272
}
231273
if slices.Contains(schemaTypes, "object") {
232274
if len(schemaTypes) == 1 {
233-
return types.CreateStringPointer(fmt.Sprintf("%v:\n%v", attributeName, spacing))
275+
return fmt.Sprintf("%v:\n%v", attributeName, spacing)
234276
}
235-
return types.CreateStringPointer(fmt.Sprintf("%v:", attributeName))
277+
return fmt.Sprintf("%v:", attributeName)
236278
}
237-
return types.CreateStringPointer(fmt.Sprintf("%v: ", attributeName))
279+
return fmt.Sprintf("%v: ", attributeName)
238280
}

0 commit comments

Comments
 (0)