ollie/tools/lsp/cmd/lsp_completion/main.go

65 lines
1.4 KiB
Go

// lsp_completion — Completion candidates at cursor position.
package main
import (
"encoding/json"
"fmt"
"ollie/tools/lsp/internal/cmdutil"
)
func main() {
cmdutil.Run("completion", func(result json.RawMessage) {
var list struct {
Items []completionItem `json:"items"`
}
if json.Unmarshal(result, &list) == nil && len(list.Items) > 0 {
for _, item := range list.Items {
printItem(item)
}
return
}
var items []completionItem
if json.Unmarshal(result, &items) == nil {
for _, item := range items {
printItem(item)
}
return
}
fmt.Println("(no completions)")
})
}
func printItem(item completionItem) {
kind := completionKind(item.Kind)
if item.Detail != "" {
fmt.Printf("%s %s [%s]\n", item.Label, item.Detail, kind)
} else {
fmt.Printf("%s [%s]\n", item.Label, kind)
}
}
type completionItem struct {
Label string `json:"label"`
Kind int `json:"kind"`
Detail string `json:"detail"`
}
func completionKind(k int) string {
kinds := map[int]string{
1: "Text", 2: "Method", 3: "Function", 4: "Constructor",
5: "Field", 6: "Variable", 7: "Class", 8: "Interface",
9: "Module", 10: "Property", 11: "Unit", 12: "Value",
13: "Enum", 14: "Keyword", 15: "Snippet", 16: "Color",
17: "File", 18: "Reference", 19: "Folder", 20: "EnumMember",
21: "Constant", 22: "Struct", 23: "Event", 24: "Operator",
25: "TypeParameter",
}
if name, ok := kinds[k]; ok {
return name
}
return "Unknown"
}