Debian Codegraph @

gopls/internal/golang/completion/definition.go

raw symbols SCIP
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package completion
6
7 import (
8 "go/ast"
9 "go/types"
10 "strings"
11 "unicode"
12 "unicode/utf8"
13
14 "golang.org/x/tools/gopls/internal/cache/parsego"
15 "golang.org/x/tools/gopls/internal/golang/completion/snippet"
16 "golang.org/x/tools/gopls/internal/protocol"
17 )
18
19 // some function definitions in test files can be completed
20 // So far, TestFoo(t *testing.T), TestMain(m *testing.M)
21 // BenchmarkFoo(b *testing.B), FuzzFoo(f *testing.F)
22
23 // path[0] is known to be *ast.Ident
24 func definition(path []ast.Node, obj types.Object, pgf *parsego.File) ([]CompletionItem, *Selection) {
25 if _, ok := obj.(*types.Func); !ok {
26 return nil, nil // not a function at all
27 }
28 if !strings.HasSuffix(pgf.URI.Path(), "_test.go") {
29 return nil, nil // not a test file
30 }
31
32 name := path[0].(*ast.Ident).Name
33 if len(name) == 0 {
34 // can't happen
35 return nil, nil
36 }
37 start := path[0].Pos()
38 end := path[0].End()
39 sel := &Selection{
40 content: "", // why isn't this name?
41 cursor: start,
42 tokFile: pgf.Tok,
43 start: start,
44 end: end,
45 mapper: pgf.Mapper,
46 }
47 sel.check()
48 var ans []CompletionItem
49 var hasParens bool
50 n, ok := path[1].(*ast.FuncDecl)
51 if !ok {
52 return nil, nil // can't happen
53 }
54 if n.Recv != nil {
55 return nil, nil // a method, not a function
56 }
57 t := n.Type.Params
58 if t.Closing != t.Opening {
59 hasParens = true
60 }
61
62 // Always suggest TestMain, if possible
63 if strings.HasPrefix("TestMain", name) {
64 if hasParens {
65 ans = append(ans, defItem("TestMain", obj))
66 } else {
67 ans = append(ans, defItem("TestMain(m *testing.M)", obj))
68 }
69 }
70
71 // If a snippet is possible, suggest it
72 if strings.HasPrefix("Test", name) {
73 if hasParens {
74 ans = append(ans, defItem("Test", obj))
75 } else {
76 ans = append(ans, defSnippet("Test", "(t *testing.T)", obj))
77 }
78 return ans, sel
79 } else if strings.HasPrefix("Benchmark", name) {
80 if hasParens {
81 ans = append(ans, defItem("Benchmark", obj))
82 } else {
83 ans = append(ans, defSnippet("Benchmark", "(b *testing.B)", obj))
84 }
85 return ans, sel
86 } else if strings.HasPrefix("Fuzz", name) {
87 if hasParens {
88 ans = append(ans, defItem("Fuzz", obj))
89 } else {
90 ans = append(ans, defSnippet("Fuzz", "(f *testing.F)", obj))
91 }
92 return ans, sel
93 }
94
95 // Fill in the argument for what the user has already typed
96 if got := defMatches(name, "Test", path, "(t *testing.T)"); got != "" {
97 ans = append(ans, defItem(got, obj))
98 } else if got := defMatches(name, "Benchmark", path, "(b *testing.B)"); got != "" {
99 ans = append(ans, defItem(got, obj))
100 } else if got := defMatches(name, "Fuzz", path, "(f *testing.F)"); got != "" {
101 ans = append(ans, defItem(got, obj))
102 }
103 return ans, sel
104 }
105
106 // defMatches returns text for defItem, never for defSnippet
107 func defMatches(name, pat string, path []ast.Node, arg string) string {
108 if !strings.HasPrefix(name, pat) {
109 return ""
110 }
111 c, _ := utf8.DecodeRuneInString(name[len(pat):])
112 if unicode.IsLower(c) {
113 return ""
114 }
115 fd, ok := path[1].(*ast.FuncDecl)
116 if !ok {
117 // we don't know what's going on
118 return ""
119 }
120 fp := fd.Type.Params
121 if len(fp.List) > 0 {
122 // signature already there, nothing to suggest
123 return ""
124 }
125 if fp.Opening != fp.Closing {
126 // nothing: completion works on words, not easy to insert arg
127 return ""
128 }
129 // suggesting signature too
130 return name + arg
131 }
132
133 func defSnippet(prefix, suffix string, obj types.Object) CompletionItem {
134 var sn snippet.Builder
135 sn.WriteText(prefix)
136 sn.WritePlaceholder(func(b *snippet.Builder) { b.WriteText("Xxx") })
137 sn.WriteText(suffix + " {\n\t")
138 sn.WriteFinalTabstop()
139 sn.WriteText("\n}")
140 return CompletionItem{
141 Label: prefix + "Xxx" + suffix,
142 Detail: "tab, type the rest of the name, then tab",
143 Kind: protocol.FunctionCompletion,
144 Depth: 0,
145 Score: 10,
146 snippet: &sn,
147 Documentation: prefix + " test function",
148 isSlice: isSlice(obj),
149 }
150 }
151 func defItem(val string, obj types.Object) CompletionItem {
152 return CompletionItem{
153 Label: val,
154 InsertText: val,
155 Kind: protocol.FunctionCompletion,
156 Depth: 0,
157 Score: 9, // prefer the snippets when available
158 Documentation: "complete the function name",
159 isSlice: isSlice(obj),
160 }
161 }