Skip to content

Commit 2c1fb95

Browse files
committed
sort completion items by kind
Add basic sorting of completion items, with `lsp.FieldCompletion` marked as more relevant.
1 parent a2584ef commit 2c1fb95

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

internal/completer/completer.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,28 @@ func (c *Completer) Complete(text string, params lsp.CompletionParams, lowercase
165165
}
166166

167167
items = filterCandidates(items, lastWord)
168+
populateSortText(items)
168169

169170
return items, nil
170171
}
171172

173+
// Override the sort text for each completion item.
174+
func populateSortText(items []lsp.CompletionItem) {
175+
for i := range items {
176+
items[i].SortText = getSortTextPrefix(items[i].Kind) + items[i].Label
177+
}
178+
}
179+
180+
// Some completion kinds are more relevant than others.
181+
// This prefix defines the alphabetic priority of each kind.
182+
func getSortTextPrefix(kind lsp.CompletionItemKind) string {
183+
switch kind {
184+
case lsp.FieldCompletion:
185+
return "0"
186+
}
187+
return "9"
188+
}
189+
172190
type ParentType int
173191

174192
const (

0 commit comments

Comments
 (0)