Debian Codegraph @

gopls/internal/test/integration/misc/codeactions_test.go

raw symbols SCIP
1 // Copyright 2024 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 misc
6
7 import (
8 "fmt"
9 "slices"
10 "strings"
11 "testing"
12
13 "github.com/google/go-cmp/cmp"
14 "golang.org/x/tools/gopls/internal/protocol"
15 "golang.org/x/tools/gopls/internal/settings"
16 . "golang.org/x/tools/gopls/internal/test/integration"
17 )
18
19 // This test exercises the filtering of code actions in generated files.
20 // Most code actions, being potential edits, are discarded, but
21 // some (GoTest, GoDoc) are pure queries, and so are allowed.
22 func TestCodeActionsInGeneratedFiles(t *testing.T) {
23 const src = `
24 -- go.mod --
25 module example.com
26 go 1.19
27
28 -- src/a.go --
29 package a
30
31 func f() { g() }
32 func g() {}
33 -- gen/a.go --
34 // Code generated by hand; DO NOT EDIT.
35 package a
36
37 func f() { g() }
38 func g() {}
39
40 -- issue72742/a.go --
41 package main
42
43 func main(){
44 fmt.Println("helloworld")
45 }
46 `
47
48 Run(t, src, func(t *testing.T, env *Env) {
49 check := func(filename string, re string, want []protocol.CodeActionKind) {
50 env.OpenFile(filename)
51 loc := env.RegexpSearch(filename, re)
52 actions, err := env.Editor.CodeAction(env.Ctx, loc, nil, protocol.CodeActionUnknownTrigger)
53 if err != nil {
54 t.Fatal(err)
55 }
56
57 type kinds = []protocol.CodeActionKind
58 got := make(kinds, 0)
59 for _, act := range actions {
60 got = append(got, act.Kind)
61 }
62
63 if diff := cmp.Diff(want, got); diff != "" {
64 t.Errorf("%s: unexpected CodeActionKinds: (-want +got):\n%s",
65 filename, diff)
66 t.Log(actions)
67 }
68 }
69
70 check("src/a.go", `g\(\)`, []protocol.CodeActionKind{
71 settings.AddTest,
72 settings.GoAssembly,
73 settings.GoDoc,
74 settings.GoFreeSymbols,
75 settings.GoSplitPackage,
76 settings.GoToggleCompilerOptDetails,
77 settings.RefactorInlineCall,
78 settings.GoplsDocFeatures,
79 })
80
81 check("gen/a.go", `g\(\)`, []protocol.CodeActionKind{
82 settings.GoAssembly,
83 settings.GoDoc,
84 settings.GoFreeSymbols,
85 settings.GoSplitPackage,
86 settings.GoToggleCompilerOptDetails,
87 settings.GoplsDocFeatures,
88 })
89
90 check("issue72742/a.go", `fmt`, []protocol.CodeActionKind{
91 settings.OrganizeImports,
92 settings.AddTest,
93 settings.GoAssembly,
94 settings.GoDoc,
95 settings.GoFreeSymbols,
96 settings.GoSplitPackage,
97 settings.GoToggleCompilerOptDetails,
98 settings.GoplsDocFeatures,
99 })
100 })
101 }
102
103 // Test refactor.inline.call is not included in automatically triggered code action
104 // unless users want refactoring.
105 //
106 // (The mechanism behind this behavior has changed. It was added when
107 // we used to interpret CodeAction(Only=[]) as "all kinds", which was
108 // a distracting nuisance (too many lightbulbs); this was fixed by
109 // adding special logic to refactor.inline.call to respect the trigger
110 // kind; but now we do this for all actions (for similar reasons) and
111 // interpret Only=[] as Only=[quickfix] unless triggerKind=invoked;
112 // except that the test client always requests CodeAction(Only=[""]).
113 // So, we should remove the special logic from refactorInlineCall
114 // and vary the Only parameter used by the test client.)
115 func TestVSCodeIssue65167(t *testing.T) {
116 const vim1 = `package main
117
118 func main() {
119 Func() // range to be selected
120 }
121
122 func Func() int { return 0 }
123 `
124
125 Run(t, "", func(t *testing.T, env *Env) {
126 env.CreateBuffer("main.go", vim1)
127 for _, trigger := range []protocol.CodeActionTriggerKind{
128 protocol.CodeActionUnknownTrigger,
129 protocol.CodeActionInvoked,
130 protocol.CodeActionAutomatic,
131 } {
132 t.Run(fmt.Sprintf("trigger=%v", trigger), func(t *testing.T) {
133 for _, selectedRange := range []bool{false, true} {
134 t.Run(fmt.Sprintf("range=%t", selectedRange), func(t *testing.T) {
135 loc := env.RegexpSearch("main.go", "Func")
136 if !selectedRange {
137 // assume the cursor is placed at the beginning of `Func`, so end==start.
138 loc.Range.End = loc.Range.Start
139 }
140 actions := env.CodeAction(loc, nil, trigger)
141 want := trigger != protocol.CodeActionAutomatic || selectedRange
142 if got := slices.ContainsFunc(actions, func(act protocol.CodeAction) bool {
143 return act.Kind == settings.RefactorInlineCall
144 }); got != want {
145 t.Errorf("got refactor.inline.call = %t, want %t", got, want)
146 }
147 })
148 }
149 })
150 }
151 })
152 }
153
154 // TestDescendingRange isn't really a test of CodeAction at all: it
155 // merely tests the response of the server to any (start, end) range
156 // that is descending. See #74394.
157 func TestDescendingRange(t *testing.T) {
158 const src = `
159 -- go.mod --
160 module example.com
161 go 1.19
162
163 -- a/a.go --
164 package a
165 `
166 Run(t, src, func(t *testing.T, env *Env) {
167 env.OpenFile("a/a.go")
168 loc := env.RegexpSearch("a/a.go", "package")
169 rng := &loc.Range
170 rng.Start, rng.End = rng.End, rng.Start
171 _, err := env.Editor.CodeAction(env.Ctx, loc, nil, protocol.CodeActionUnknownTrigger)
172 got, wantSubstr := fmt.Sprint(err), "start (offset 7) > end (offset 0)"
173 if !strings.Contains(got, wantSubstr) {
174 t.Fatalf("CodeAction error: got %q, want substring %q", got, wantSubstr)
175 }
176 })
177 }
178
179 // TestIssue76235 is a regression test for a crash in CodeAction
180 // (refactor.extract.variable-all). It can't be a marker test since
181 // the fixed behavior is a no-op: no code actions, no crash.
182 func TestIssue76235(t *testing.T) {
183 const src = `
184 -- a/a.go --
185 package a; func f([3]int)
186 `
187 Run(t, src, func(t *testing.T, env *Env) {
188 env.OpenFile("a/a.go")
189 loc := env.RegexpSearch("a/a.go", "3")
190 env.Editor.CodeAction(env.Ctx, loc, nil, protocol.CodeActionUnknownTrigger) // ignore error
191 })
192 }