| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
| 27 |
} |
| 28 |
if !strings.HasSuffix(pgf.URI.Path(), "_test.go") { |
| 29 |
return nil, nil |
| 30 |
} |
| 31 |
|
| 32 |
name := path[0].(*ast.Ident).Name |
| 33 |
if len(name) == 0 { |
| 34 |
|
| 35 |
return nil, nil |
| 36 |
} |
| 37 |
start := path[0].Pos() |
| 38 |
end := path[0].End() |
| 39 |
sel := &Selection{ |
| 40 |
content: "", |
| 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 |
| 53 |
} |
| 54 |
if n.Recv != nil { |
| 55 |
return nil, nil |
| 56 |
} |
| 57 |
t := n.Type.Params |
| 58 |
if t.Closing != t.Opening { |
| 59 |
hasParens = true |
| 60 |
} |
| 61 |
|
| 62 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 118 |
return "" |
| 119 |
} |
| 120 |
fp := fd.Type.Params |
| 121 |
if len(fp.List) > 0 { |
| 122 |
|
| 123 |
return "" |
| 124 |
} |
| 125 |
if fp.Opening != fp.Closing { |
| 126 |
|
| 127 |
return "" |
| 128 |
} |
| 129 |
|
| 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, |
| 158 |
Documentation: "complete the function name", |
| 159 |
isSlice: isSlice(obj), |
| 160 |
} |
| 161 |
} |