Skip to content

Commit 2080554

Browse files
rentziassstamblerre
authored andcommitted
tools/gopls: add cmd support for highlight
This change adds command line support for highlight. Provided with an identifier position, it will display the list of highlights for that within the same file. Example: $ gopls highlight ~/tmp/foo/main.go:3:9 $ $ 3:9-6:0 $ 10:22-11:32 $ 12:10-12:9 $ 12:20-30:0 Updates golang/go#32875 Change-Id: I5de73d9fbd9bcc59a3f62e7e9a1331bc3866bc75 Reviewed-on: https://go-review.googlesource.com/c/tools/+/207291 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent b832835 commit 2080554

File tree

4 files changed

+108
-5
lines changed

4 files changed

+108
-5
lines changed

internal/lsp/cmd/cmd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,10 @@ func (app *Application) commands() []tool.Application {
144144
&check{app: app},
145145
&foldingRanges{app: app},
146146
&format{app: app},
147-
&links{app: app},
147+
&highlight{app: app},
148148
&implementation{app: app},
149149
&imports{app: app},
150+
&links{app: app},
150151
&query{app: app},
151152
&references{app: app},
152153
&rename{app: app},

internal/lsp/cmd/highlight.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2019 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 cmd
6+
7+
import (
8+
"context"
9+
"flag"
10+
"fmt"
11+
12+
"golang.org/x/tools/internal/lsp/protocol"
13+
"golang.org/x/tools/internal/span"
14+
"golang.org/x/tools/internal/tool"
15+
)
16+
17+
// highlight implements the highlight verb for gopls
18+
type highlight struct {
19+
app *Application
20+
}
21+
22+
func (r *highlight) Name() string { return "highlight" }
23+
func (r *highlight) Usage() string { return "<position>" }
24+
func (r *highlight) ShortHelp() string { return "display selected identifier's highlights" }
25+
func (r *highlight) DetailedHelp(f *flag.FlagSet) {
26+
fmt.Fprint(f.Output(), `
27+
Example:
28+
29+
$ # 1-indexed location (:line:column or :#offset) of the target identifier
30+
$ gopls highlight helper/helper.go:8:6
31+
$ gopls highlight helper/helper.go:#53
32+
33+
gopls highlight flags are:
34+
`)
35+
f.PrintDefaults()
36+
}
37+
38+
func (r *highlight) Run(ctx context.Context, args ...string) error {
39+
if len(args) != 1 {
40+
return tool.CommandLineErrorf("highlight expects 1 argument (position)")
41+
}
42+
43+
conn, err := r.app.connect(ctx)
44+
if err != nil {
45+
return err
46+
}
47+
defer conn.terminate(ctx)
48+
49+
from := span.Parse(args[0])
50+
file := conn.AddFile(ctx, from.URI())
51+
if file.err != nil {
52+
return file.err
53+
}
54+
55+
loc, err := file.mapper.Location(from)
56+
if err != nil {
57+
return err
58+
}
59+
60+
p := protocol.DocumentHighlightParams{
61+
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
62+
TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
63+
Position: loc.Range.Start,
64+
},
65+
}
66+
highlights, err := conn.DocumentHighlight(ctx, &p)
67+
if err != nil {
68+
return err
69+
}
70+
71+
for _, h := range highlights {
72+
l := protocol.Location{Range: h.Range}
73+
s, err := file.mapper.Span(l)
74+
if err != nil {
75+
return err
76+
}
77+
fmt.Println(s)
78+
}
79+
80+
return nil
81+
}

internal/lsp/cmd/test/cmdtest.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ func (r *runner) RankCompletion(t *testing.T, src span.Span, test tests.Completi
9595
//TODO: add command line completions tests when it works
9696
}
9797

98-
func (r *runner) Highlight(t *testing.T, src span.Span, locations []span.Span) {
99-
//TODO: add command line highlight tests when it works
100-
}
101-
10298
func (r *runner) PrepareRename(t *testing.T, src span.Span, want *source.PrepareItem) {
10399
//TODO: add command line prepare rename tests when it works
104100
}

internal/lsp/cmd/test/highlight.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cmdtest
2+
3+
import (
4+
"testing"
5+
6+
"fmt"
7+
8+
"golang.org/x/tools/internal/span"
9+
)
10+
11+
func (r *runner) Highlight(t *testing.T, spn span.Span, spans []span.Span) {
12+
var expect string
13+
for _, l := range spans {
14+
expect += fmt.Sprintln(l)
15+
}
16+
expect = r.Normalize(expect)
17+
18+
uri := spn.URI()
19+
filename := uri.Filename()
20+
target := filename + ":" + fmt.Sprint(spn.Start().Line()) + ":" + fmt.Sprint(spn.Start().Column())
21+
got, _ := r.NormalizeGoplsCmd(t, "highlight", target)
22+
if expect != got {
23+
t.Errorf("highlight failed for %s expected:\n%s\ngot:\n%s", target, expect, got)
24+
}
25+
}

0 commit comments

Comments
 (0)