Skip to content

Commit dbbc1ca

Browse files
committed
Update for review comments
1 parent 7832f03 commit dbbc1ca

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

gopls/internal/lsp/cmd/test/cmdtest.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ func (r *runner) InlayHints(t *testing.T, spn span.Span) {
118118
// TODO: inlayHints not supported on command line
119119
}
120120

121-
func (r *runner) SelectionRanges(t *testing.T, spn span.Span) {
122-
// TODO: selectionRanges not supported on command line
123-
}
121+
func (r *runner) SelectionRanges(t *testing.T, spn span.Span) {}
124122

125123
func (r *runner) runGoplsCmd(t testing.TB, args ...string) (string, string) {
126124
rStdout, wStdout, err := os.Pipe()

gopls/internal/lsp/lsp_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,19 +1307,19 @@ func (r *runner) SelectionRanges(t *testing.T, spn span.Span) {
13071307
Positions: []protocol.Position{loc.Range.Start},
13081308
})
13091309
if err != nil {
1310-
t.Error(err)
1310+
t.Fatal(err)
13111311
}
13121312

13131313
sb := &strings.Builder{}
13141314
for i, path := range ranges {
13151315
fmt.Fprintf(sb, "Ranges %d: ", i)
1316-
r := path
1316+
rng := path
13171317
for {
1318-
s, err := sm.Offset(r.Range.Start)
1318+
s, err := sm.Offset(rng.Range.Start)
13191319
if err != nil {
13201320
t.Error(err)
13211321
}
1322-
e, err := sm.Offset(r.Range.End)
1322+
e, err := sm.Offset(rng.Range.End)
13231323
if err != nil {
13241324
t.Error(err)
13251325
}
@@ -1331,25 +1331,26 @@ func (r *runner) SelectionRanges(t *testing.T, spn span.Span) {
13311331
snippet = string(sm.Content[s:s+15]) + "..." + string(sm.Content[e-15:e])
13321332
}
13331333

1334-
fmt.Fprintf(sb, "\n\t%v '%s'", r.Range, strings.ReplaceAll(snippet, "\n", "\\n"))
1334+
fmt.Fprintf(sb, "\n\t%v %q", rng.Range, strings.ReplaceAll(snippet, "\n", "\\n"))
13351335

1336-
if r.Parent == nil {
1336+
if rng.Parent == nil {
13371337
break
13381338
}
1339-
r = *r.Parent
1339+
rng = *rng.Parent
13401340
}
13411341
sb.WriteRune('\n')
13421342
}
13431343
got := sb.String()
13441344

1345-
want := r.data.Golden(t, "selectionrange_"+tests.SpanName(spn), uri.Filename(), func() ([]byte, error) {
1345+
testName := "selectionrange_" + tests.SpanName(spn)
1346+
want := r.data.Golden(t, testName, uri.Filename(), func() ([]byte, error) {
13461347
return []byte(got), nil
13471348
})
13481349
if want == nil {
13491350
t.Fatalf("golden file %q not found", uri.Filename())
13501351
}
13511352
if diff := compare.Text(got, string(want)); diff != "" {
1352-
t.Errorf("%s mismatch\n%s", command.AddImport, diff)
1353+
t.Errorf("%s mismatch\n%s", testName, diff)
13531354
}
13541355
}
13551356

gopls/internal/lsp/selection_range.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
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+
15
package lsp
26

37
import (
@@ -9,6 +13,17 @@ import (
913
"golang.org/x/tools/internal/event"
1014
)
1115

16+
// selectionRange defines the textDocument/selectionRange feature,
17+
// which, given a list of positions within a file,
18+
// reports a linked list of enclosing syntactic blocks, innermost first.
19+
//
20+
// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_selectionRange.
21+
//
22+
// This feature can be used by a client to implement "expand selection" in a
23+
// language-aware fashion. Multiple input positions are supported to allow
24+
// for multiple cursors, and the entire path up to the whole document is
25+
// returned for each cursor to avoid multiple round-trips when the user is
26+
// likely to issue this command multiple times in quick succession.
1227
func (s *Server) selectionRange(ctx context.Context, params *protocol.SelectionRangeParams) ([]protocol.SelectionRange, error) {
1328
ctx, done := event.Start(ctx, "lsp.Server.documentSymbol")
1429
defer done()
@@ -33,21 +48,20 @@ func (s *Server) selectionRange(ctx context.Context, params *protocol.SelectionR
3348

3449
path, _ := astutil.PathEnclosingInterval(pgf.File, pos, pos)
3550

36-
current := &result[i]
51+
tail := &result[i] // tail of the Parent linked list, built head first
3752

3853
for j, node := range path {
3954
rng, err := pgf.Mapper.PosRange(node.Pos(), node.End())
4055
if err != nil {
4156
return nil, err
4257
}
4358

44-
// Option 2
45-
current.Range = rng
46-
47-
if j < len(path)-1 {
48-
current.Parent = &protocol.SelectionRange{}
49-
current = current.Parent
59+
// Add node to tail.
60+
if j > 0 {
61+
tail.Parent = &protocol.SelectionRange{}
62+
tail = tail.Parent
5063
}
64+
tail.Range = rng
5165
}
5266
}
5367

0 commit comments

Comments
 (0)