-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathdefinition.go
160 lines (149 loc) · 4.23 KB
/
definition.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package completion
import (
"go/ast"
"go/types"
"strings"
"unicode"
"unicode/utf8"
"golang.org/x/tools/gopls/internal/cache/parsego"
"golang.org/x/tools/gopls/internal/golang/completion/snippet"
"golang.org/x/tools/gopls/internal/protocol"
)
// some function definitions in test files can be completed
// So far, TestFoo(t *testing.T), TestMain(m *testing.M)
// BenchmarkFoo(b *testing.B), FuzzFoo(f *testing.F)
// path[0] is known to be *ast.Ident
func definition(path []ast.Node, obj types.Object, pgf *parsego.File) ([]CompletionItem, *Selection) {
if _, ok := obj.(*types.Func); !ok {
return nil, nil // not a function at all
}
if !strings.HasSuffix(pgf.URI.Path(), "_test.go") {
return nil, nil // not a test file
}
name := path[0].(*ast.Ident).Name
if len(name) == 0 {
// can't happen
return nil, nil
}
start := path[0].Pos()
end := path[0].End()
sel := &Selection{
content: "",
cursor: start,
tokFile: pgf.Tok,
start: start,
end: end,
mapper: pgf.Mapper,
}
var ans []CompletionItem
var hasParens bool
n, ok := path[1].(*ast.FuncDecl)
if !ok {
return nil, nil // can't happen
}
if n.Recv != nil {
return nil, nil // a method, not a function
}
t := n.Type.Params
if t.Closing != t.Opening {
hasParens = true
}
// Always suggest TestMain, if possible
if strings.HasPrefix("TestMain", name) {
if hasParens {
ans = append(ans, defItem("TestMain", obj))
} else {
ans = append(ans, defItem("TestMain(m *testing.M)", obj))
}
}
// If a snippet is possible, suggest it
if strings.HasPrefix("Test", name) {
if hasParens {
ans = append(ans, defItem("Test", obj))
} else {
ans = append(ans, defSnippet("Test", "(t *testing.T)", obj))
}
return ans, sel
} else if strings.HasPrefix("Benchmark", name) {
if hasParens {
ans = append(ans, defItem("Benchmark", obj))
} else {
ans = append(ans, defSnippet("Benchmark", "(b *testing.B)", obj))
}
return ans, sel
} else if strings.HasPrefix("Fuzz", name) {
if hasParens {
ans = append(ans, defItem("Fuzz", obj))
} else {
ans = append(ans, defSnippet("Fuzz", "(f *testing.F)", obj))
}
return ans, sel
}
// Fill in the argument for what the user has already typed
if got := defMatches(name, "Test", path, "(t *testing.T)"); got != "" {
ans = append(ans, defItem(got, obj))
} else if got := defMatches(name, "Benchmark", path, "(b *testing.B)"); got != "" {
ans = append(ans, defItem(got, obj))
} else if got := defMatches(name, "Fuzz", path, "(f *testing.F)"); got != "" {
ans = append(ans, defItem(got, obj))
}
return ans, sel
}
// defMatches returns text for defItem, never for defSnippet
func defMatches(name, pat string, path []ast.Node, arg string) string {
if !strings.HasPrefix(name, pat) {
return ""
}
c, _ := utf8.DecodeRuneInString(name[len(pat):])
if unicode.IsLower(c) {
return ""
}
fd, ok := path[1].(*ast.FuncDecl)
if !ok {
// we don't know what's going on
return ""
}
fp := fd.Type.Params
if len(fp.List) > 0 {
// signature already there, nothing to suggest
return ""
}
if fp.Opening != fp.Closing {
// nothing: completion works on words, not easy to insert arg
return ""
}
// suggesting signature too
return name + arg
}
func defSnippet(prefix, suffix string, obj types.Object) CompletionItem {
var sn snippet.Builder
sn.WriteText(prefix)
sn.WritePlaceholder(func(b *snippet.Builder) { b.WriteText("Xxx") })
sn.WriteText(suffix + " {\n\t")
sn.WriteFinalTabstop()
sn.WriteText("\n}")
return CompletionItem{
Label: prefix + "Xxx" + suffix,
Detail: "tab, type the rest of the name, then tab",
Kind: protocol.FunctionCompletion,
Depth: 0,
Score: 10,
snippet: &sn,
Documentation: prefix + " test function",
isSlice: isSlice(obj),
}
}
func defItem(val string, obj types.Object) CompletionItem {
return CompletionItem{
Label: val,
InsertText: val,
Kind: protocol.FunctionCompletion,
Depth: 0,
Score: 9, // prefer the snippets when available
Documentation: "complete the function name",
isSlice: isSlice(obj),
}
}