Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bcb677e

Browse files
committed
gopls/internal/regtest: make RegexpSearch return a Location
...along with various other Editor methods. Do we need a more convenient way to say env.Sandbox.Workdir.URIToPath(loc.URI) ? Change-Id: I452028db4b99843e07861909ad8cef87cf9fb118 Reviewed-on: https://go-review.googlesource.com/c/tools/+/463655 Run-TryBot: Alan Donovan <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 60782e9 commit bcb677e

30 files changed

+361
-391
lines changed

gopls/internal/lsp/fake/editor.go

Lines changed: 89 additions & 90 deletions
Large diffs are not rendered by default.

gopls/internal/lsp/fake/workdir.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,25 +196,15 @@ func (w *Workdir) ReadFile(path string) ([]byte, error) {
196196
}
197197
}
198198

199-
func (w *Workdir) RegexpRange(path, re string) (protocol.Range, error) {
200-
content, err := w.ReadFile(path)
201-
if err != nil {
202-
return protocol.Range{}, err
203-
}
204-
mapper := protocol.NewMapper(w.URI(path).SpanURI(), content)
205-
return regexpRange(mapper, re)
206-
}
207-
208199
// RegexpSearch searches the file corresponding to path for the first position
209200
// matching re.
210-
func (w *Workdir) RegexpSearch(path string, re string) (protocol.Position, error) {
201+
func (w *Workdir) RegexpSearch(path string, re string) (protocol.Location, error) {
211202
content, err := w.ReadFile(path)
212203
if err != nil {
213-
return protocol.Position{}, err
204+
return protocol.Location{}, err
214205
}
215206
mapper := protocol.NewMapper(w.URI(path).SpanURI(), content)
216-
rng, err := regexpRange(mapper, re)
217-
return rng.Start, err
207+
return regexpLocation(mapper, re)
218208
}
219209

220210
// RemoveFile removes a workdir-relative file path and notifies watchers of the

gopls/internal/lsp/regtest/expectation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,11 +712,11 @@ func FromSource(source string) DiagnosticFilter {
712712
// TODO(rfindley): pass in the editor to expectations, so that they may depend
713713
// on editor state and AtRegexp can be a function rather than a method.
714714
func (e *Env) AtRegexp(name, pattern string) DiagnosticFilter {
715-
pos := e.RegexpSearch(name, pattern)
715+
loc := e.RegexpSearch(name, pattern)
716716
return DiagnosticFilter{
717717
desc: fmt.Sprintf("at the first position matching %#q in %q", pattern, name),
718718
check: func(diagName string, d protocol.Diagnostic) bool {
719-
return diagName == name && d.Range.Start == pos
719+
return diagName == name && d.Range.Start == loc.Range.Start
720720
},
721721
}
722722
}

gopls/internal/lsp/regtest/wrappers.go

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -113,37 +113,19 @@ func (e *Env) SetBufferContent(name string, content string) {
113113
}
114114
}
115115

116-
// RegexpRange returns the range of the first match for re in the buffer
117-
// specified by name, calling t.Fatal on any error. It first searches for the
118-
// position in open buffers, then in workspace files.
119-
func (e *Env) RegexpRange(name, re string) protocol.Range {
120-
e.T.Helper()
121-
rng, err := e.Editor.RegexpRange(name, re)
122-
if err == fake.ErrUnknownBuffer {
123-
rng, err = e.Sandbox.Workdir.RegexpRange(name, re)
124-
}
125-
if err != nil {
126-
e.T.Fatalf("RegexpRange: %v, %v", name, err)
127-
}
128-
return rng
129-
}
130-
131116
// RegexpSearch returns the starting position of the first match for re in the
132117
// buffer specified by name, calling t.Fatal on any error. It first searches
133118
// for the position in open buffers, then in workspace files.
134-
//
135-
// TODO(rfindley): RegexpSearch should return a protocol.Location (but that is
136-
// a large change).
137-
func (e *Env) RegexpSearch(name, re string) protocol.Position {
119+
func (e *Env) RegexpSearch(name, re string) protocol.Location {
138120
e.T.Helper()
139-
pos, err := e.Editor.RegexpSearch(name, re)
121+
loc, err := e.Editor.RegexpSearch(name, re)
140122
if err == fake.ErrUnknownBuffer {
141-
pos, err = e.Sandbox.Workdir.RegexpSearch(name, re)
123+
loc, err = e.Sandbox.Workdir.RegexpSearch(name, re)
142124
}
143125
if err != nil {
144126
e.T.Fatalf("RegexpSearch: %v, %v for %q", name, err, re)
145127
}
146-
return pos
128+
return loc
147129
}
148130

149131
// RegexpReplace replaces the first group in the first match of regexpStr with
@@ -172,13 +154,13 @@ func (e *Env) SaveBufferWithoutActions(name string) {
172154

173155
// GoToDefinition goes to definition in the editor, calling t.Fatal on any
174156
// error. It returns the path and position of the resulting jump.
175-
func (e *Env) GoToDefinition(name string, pos protocol.Position) (string, protocol.Position) {
157+
func (e *Env) GoToDefinition(loc protocol.Location) protocol.Location {
176158
e.T.Helper()
177-
n, p, err := e.Editor.GoToDefinition(e.Ctx, name, pos)
159+
loc, err := e.Editor.GoToDefinition(e.Ctx, loc)
178160
if err != nil {
179161
e.T.Fatal(err)
180162
}
181-
return n, p
163+
return loc
182164
}
183165

184166
// FormatBuffer formats the editor buffer, calling t.Fatal on any error.
@@ -201,7 +183,8 @@ func (e *Env) OrganizeImports(name string) {
201183
// ApplyQuickFixes processes the quickfix codeAction, calling t.Fatal on any error.
202184
func (e *Env) ApplyQuickFixes(path string, diagnostics []protocol.Diagnostic) {
203185
e.T.Helper()
204-
if err := e.Editor.ApplyQuickFixes(e.Ctx, path, nil, diagnostics); err != nil {
186+
loc := protocol.Location{URI: e.Sandbox.Workdir.URI(path)} // zero Range => whole file
187+
if err := e.Editor.ApplyQuickFixes(e.Ctx, loc, diagnostics); err != nil {
205188
e.T.Fatal(err)
206189
}
207190
}
@@ -217,21 +200,22 @@ func (e *Env) ApplyCodeAction(action protocol.CodeAction) {
217200
// GetQuickFixes returns the available quick fix code actions.
218201
func (e *Env) GetQuickFixes(path string, diagnostics []protocol.Diagnostic) []protocol.CodeAction {
219202
e.T.Helper()
220-
actions, err := e.Editor.GetQuickFixes(e.Ctx, path, nil, diagnostics)
203+
loc := protocol.Location{URI: e.Sandbox.Workdir.URI(path)} // zero Range => whole file
204+
actions, err := e.Editor.GetQuickFixes(e.Ctx, loc, diagnostics)
221205
if err != nil {
222206
e.T.Fatal(err)
223207
}
224208
return actions
225209
}
226210

227211
// Hover in the editor, calling t.Fatal on any error.
228-
func (e *Env) Hover(name string, pos protocol.Position) (*protocol.MarkupContent, protocol.Position) {
212+
func (e *Env) Hover(loc protocol.Location) (*protocol.MarkupContent, protocol.Location) {
229213
e.T.Helper()
230-
c, p, err := e.Editor.Hover(e.Ctx, name, pos)
214+
c, loc, err := e.Editor.Hover(e.Ctx, loc)
231215
if err != nil {
232216
e.T.Fatal(err)
233217
}
234-
return c, p
218+
return c, loc
235219
}
236220

237221
func (e *Env) DocumentLink(name string) []protocol.DocumentLink {
@@ -243,9 +227,9 @@ func (e *Env) DocumentLink(name string) []protocol.DocumentLink {
243227
return links
244228
}
245229

246-
func (e *Env) DocumentHighlight(name string, pos protocol.Position) []protocol.DocumentHighlight {
230+
func (e *Env) DocumentHighlight(loc protocol.Location) []protocol.DocumentHighlight {
247231
e.T.Helper()
248-
highlights, err := e.Editor.DocumentHighlight(e.Ctx, name, pos)
232+
highlights, err := e.Editor.DocumentHighlight(e.Ctx, loc)
249233
if err != nil {
250234
e.T.Fatal(err)
251235
}
@@ -398,27 +382,27 @@ func (e *Env) Symbol(query string) []protocol.SymbolInformation {
398382
}
399383

400384
// References wraps Editor.References, calling t.Fatal on any error.
401-
func (e *Env) References(path string, pos protocol.Position) []protocol.Location {
385+
func (e *Env) References(loc protocol.Location) []protocol.Location {
402386
e.T.Helper()
403-
locations, err := e.Editor.References(e.Ctx, path, pos)
387+
locations, err := e.Editor.References(e.Ctx, loc)
404388
if err != nil {
405389
e.T.Fatal(err)
406390
}
407391
return locations
408392
}
409393

410394
// Rename wraps Editor.Rename, calling t.Fatal on any error.
411-
func (e *Env) Rename(path string, pos protocol.Position, newName string) {
395+
func (e *Env) Rename(loc protocol.Location, newName string) {
412396
e.T.Helper()
413-
if err := e.Editor.Rename(e.Ctx, path, pos, newName); err != nil {
397+
if err := e.Editor.Rename(e.Ctx, loc, newName); err != nil {
414398
e.T.Fatal(err)
415399
}
416400
}
417401

418402
// Implementations wraps Editor.Implementations, calling t.Fatal on any error.
419-
func (e *Env) Implementations(path string, pos protocol.Position) []protocol.Location {
403+
func (e *Env) Implementations(loc protocol.Location) []protocol.Location {
420404
e.T.Helper()
421-
locations, err := e.Editor.Implementations(e.Ctx, path, pos)
405+
locations, err := e.Editor.Implementations(e.Ctx, loc)
422406
if err != nil {
423407
e.T.Fatal(err)
424408
}
@@ -434,9 +418,9 @@ func (e *Env) RenameFile(oldPath, newPath string) {
434418
}
435419

436420
// Completion executes a completion request on the server.
437-
func (e *Env) Completion(path string, pos protocol.Position) *protocol.CompletionList {
421+
func (e *Env) Completion(loc protocol.Location) *protocol.CompletionList {
438422
e.T.Helper()
439-
completions, err := e.Editor.Completion(e.Ctx, path, pos)
423+
completions, err := e.Editor.Completion(e.Ctx, loc)
440424
if err != nil {
441425
e.T.Fatal(err)
442426
}
@@ -445,9 +429,9 @@ func (e *Env) Completion(path string, pos protocol.Position) *protocol.Completio
445429

446430
// AcceptCompletion accepts a completion for the given item at the given
447431
// position.
448-
func (e *Env) AcceptCompletion(path string, pos protocol.Position, item protocol.CompletionItem) {
432+
func (e *Env) AcceptCompletion(loc protocol.Location, item protocol.CompletionItem) {
449433
e.T.Helper()
450-
if err := e.Editor.AcceptCompletion(e.Ctx, path, pos, item); err != nil {
434+
if err := e.Editor.AcceptCompletion(e.Ctx, loc, item); err != nil {
451435
e.T.Fatal(err)
452436
}
453437
}
@@ -456,7 +440,8 @@ func (e *Env) AcceptCompletion(path string, pos protocol.Position, item protocol
456440
// t.Fatal if there are errors.
457441
func (e *Env) CodeAction(path string, diagnostics []protocol.Diagnostic) []protocol.CodeAction {
458442
e.T.Helper()
459-
actions, err := e.Editor.CodeAction(e.Ctx, path, nil, diagnostics)
443+
loc := protocol.Location{URI: e.Sandbox.Workdir.URI(path)} // no Range => whole file
444+
actions, err := e.Editor.CodeAction(e.Ctx, loc, diagnostics)
460445
if err != nil {
461446
e.T.Fatal(err)
462447
}

gopls/internal/regtest/bench/completion_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func benchmarkCompletion(options completionBenchOptions, b *testing.B) {
5757
}
5858

5959
// Run a completion to make sure the system is warm.
60-
pos := env.RegexpSearch(options.file, options.locationRegexp)
61-
completions := env.Completion(options.file, pos)
60+
loc := env.RegexpSearch(options.file, options.locationRegexp)
61+
completions := env.Completion(loc)
6262

6363
if testing.Verbose() {
6464
fmt.Println("Results:")
@@ -77,7 +77,7 @@ func benchmarkCompletion(options completionBenchOptions, b *testing.B) {
7777
if options.beforeCompletion != nil {
7878
options.beforeCompletion(env)
7979
}
80-
env.Completion(options.file, pos)
80+
env.Completion(loc)
8181
}
8282
})
8383
}

gopls/internal/regtest/bench/editor_features_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,29 @@ func BenchmarkGoToDefinition(b *testing.B) {
1313
env := benchmarkEnv(b)
1414

1515
env.OpenFile("internal/imports/mod.go")
16-
pos := env.RegexpSearch("internal/imports/mod.go", "ModuleJSON")
17-
env.GoToDefinition("internal/imports/mod.go", pos)
16+
loc := env.RegexpSearch("internal/imports/mod.go", "ModuleJSON")
17+
env.GoToDefinition(loc)
1818
env.Await(env.DoneWithOpen())
1919

2020
b.ResetTimer()
2121

2222
for i := 0; i < b.N; i++ {
23-
env.GoToDefinition("internal/imports/mod.go", pos)
23+
env.GoToDefinition(loc)
2424
}
2525
}
2626

2727
func BenchmarkFindAllReferences(b *testing.B) {
2828
env := benchmarkEnv(b)
2929

3030
env.OpenFile("internal/imports/mod.go")
31-
pos := env.RegexpSearch("internal/imports/mod.go", "gopathwalk")
32-
env.References("internal/imports/mod.go", pos)
31+
loc := env.RegexpSearch("internal/imports/mod.go", "gopathwalk")
32+
env.References(loc)
3333
env.Await(env.DoneWithOpen())
3434

3535
b.ResetTimer()
3636

3737
for i := 0; i < b.N; i++ {
38-
env.References("internal/imports/mod.go", pos)
38+
env.References(loc)
3939
}
4040
}
4141

@@ -48,36 +48,36 @@ func BenchmarkRename(b *testing.B) {
4848
b.ResetTimer()
4949

5050
for i := 1; i < b.N; i++ {
51-
pos := env.RegexpSearch("internal/imports/mod.go", "gopathwalk")
51+
loc := env.RegexpSearch("internal/imports/mod.go", "gopathwalk")
5252
newName := fmt.Sprintf("%s%d", "gopathwalk", i)
53-
env.Rename("internal/imports/mod.go", pos, newName)
53+
env.Rename(loc, newName)
5454
}
5555
}
5656

5757
func BenchmarkFindAllImplementations(b *testing.B) {
5858
env := benchmarkEnv(b)
5959

6060
env.OpenFile("internal/imports/mod.go")
61-
pos := env.RegexpSearch("internal/imports/mod.go", "initAllMods")
61+
loc := env.RegexpSearch("internal/imports/mod.go", "initAllMods")
6262
env.Await(env.DoneWithOpen())
6363

6464
b.ResetTimer()
6565

6666
for i := 0; i < b.N; i++ {
67-
env.Implementations("internal/imports/mod.go", pos)
67+
env.Implementations(loc)
6868
}
6969
}
7070

7171
func BenchmarkHover(b *testing.B) {
7272
env := benchmarkEnv(b)
7373

7474
env.OpenFile("internal/imports/mod.go")
75-
pos := env.RegexpSearch("internal/imports/mod.go", "bytes")
75+
loc := env.RegexpSearch("internal/imports/mod.go", "bytes")
7676
env.Await(env.DoneWithOpen())
7777

7878
b.ResetTimer()
7979

8080
for i := 0; i < b.N; i++ {
81-
env.Hover("internal/imports/mod.go", pos)
81+
env.Hover(loc)
8282
}
8383
}

gopls/internal/regtest/completion/completion18_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ func (s SyncMap[XX,string]) g(v UU) {}
4242
env.OpenFile("main.go")
4343
env.Await(env.DoneWithOpen())
4444
for _, tst := range tests {
45-
pos := env.RegexpSearch("main.go", tst.pat)
46-
pos.Character += uint32(protocol.UTF16Len([]byte(tst.pat)))
47-
completions := env.Completion("main.go", pos)
45+
loc := env.RegexpSearch("main.go", tst.pat)
46+
loc.Range.Start.Character += uint32(protocol.UTF16Len([]byte(tst.pat)))
47+
completions := env.Completion(loc)
4848
result := compareCompletionLabels(tst.want, completions.Items)
4949
if result != "" {
5050
t.Errorf("%s: wanted %v", result, tst.want)
@@ -109,9 +109,9 @@ func FuzzHex(f *testing.F) {
109109
for _, test := range tests {
110110
env.OpenFile(test.file)
111111
env.Await(env.DoneWithOpen())
112-
pos := env.RegexpSearch(test.file, test.pat)
113-
pos.Character += test.offset // character user just typed? will type?
114-
completions := env.Completion(test.file, pos)
112+
loc := env.RegexpSearch(test.file, test.pat)
113+
loc.Range.Start.Character += test.offset // character user just typed? will type?
114+
completions := env.Completion(loc)
115115
result := compareCompletionLabels(test.want, completions.Items)
116116
if result != "" {
117117
t.Errorf("pat %q %q", test.pat, result)

0 commit comments

Comments
 (0)