-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompletion.go
85 lines (71 loc) · 2.12 KB
/
completion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"strings"
"go.lsp.dev/protocol"
"go.lsp.dev/uri"
)
func completion(file *File, textBeforeCursor string) (res []protocol.CompletionItem) {
packageName, partFieldName := splitTypeIdentifier(textBeforeCursor)
logger.Sugar().Debug("packageName: ", packageName, " partFieldName: ", partFieldName)
if packageName == "" {
// include
for _, include := range file.Includes {
includePackage := includeToPackageName(include)
if strings.HasPrefix(includePackage, partFieldName) {
res = append(res, includeToCompletionItem(includePackage, partFieldName))
}
}
// struct
for _, s := range file.Structs {
if strings.HasPrefix(s.Name.Name, partFieldName) {
res = append(res, structToCompletionItem(s, partFieldName))
}
}
// enum
for _, e := range file.Enums {
if strings.HasPrefix(e.Name.Name, partFieldName) {
res = append(res, enumToCompletionItem(e, partFieldName))
}
}
return
}
for _, include := range file.Includes {
includePackage := includeToPackageName(include)
if includePackage != packageName {
if strings.HasPrefix(includePackage, packageName) {
res = append(res, includeToCompletionItem(includePackage, partFieldName))
}
continue
}
logger.Sugar().Debug("includePackage: ", includePackage)
includedFileName := IncludeToFullPath(file.URI, include)
includedFileURI := uri.File(includedFileName)
includedFile, ok := WorkspaceInstance.Files[includedFileURI]
if !ok {
return
}
return completion(includedFile, partFieldName)
}
return
}
func includeToCompletionItem(include string, prefix string) protocol.CompletionItem {
return protocol.CompletionItem{
Label: include,
Detail: "include",
Kind: protocol.CompletionItemKindModule,
}
}
func structToCompletionItem(s *Struct, prefix string) protocol.CompletionItem {
return protocol.CompletionItem{
Label: s.Name.Name,
Detail: "struct",
Kind: protocol.CompletionItemKindClass,
}
}
func enumToCompletionItem(e *Enum, prefix string) protocol.CompletionItem {
return protocol.CompletionItem{
Label: e.Name.Name,
Detail: "enum",
Kind: protocol.CompletionItemKindEnum,
}
}