Skip to content

Commit 9ab7e51

Browse files
committed
internal/lsp: don't show blank identifiers in outline
Fixes golang/go#41654 Change-Id: Ie8ac59051e19d2d312ba48b6649660f53b573731 Reviewed-on: https://go-review.googlesource.com/c/tools/+/259142 Trust: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Peter Weinberger <[email protected]>
1 parent 7ddb464 commit 9ab7e51

File tree

6 files changed

+46
-32
lines changed

6 files changed

+46
-32
lines changed

internal/lsp/cmd/test/workspace_symbol.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"golang.org/x/tools/internal/lsp/protocol"
14+
"golang.org/x/tools/internal/lsp/tests"
1415
)
1516

1617
func (r *runner) WorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) {
@@ -46,7 +47,7 @@ func (r *runner) runWorkspaceSymbols(t *testing.T, matcher, query string, dirs m
4647
}))
4748

4849
if expect != got {
49-
t.Errorf("workspace_symbol failed for %s expected:\n%s\ngot:\n%s", query, expect, got)
50+
t.Errorf("workspace_symbol failed for %s:\n%s", query, tests.Diff(expect, got))
5051
}
5152
}
5253

internal/lsp/source/symbols.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func DocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]p
3232
for _, decl := range pgf.File.Decls {
3333
switch decl := decl.(type) {
3434
case *ast.FuncDecl:
35+
if decl.Name.Name == "_" {
36+
continue
37+
}
3538
if obj := info.ObjectOf(decl.Name); obj != nil {
3639
fs, err := funcSymbol(snapshot, pkg, decl, obj, q)
3740
if err != nil {
@@ -48,6 +51,9 @@ func DocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]p
4851
for _, spec := range decl.Specs {
4952
switch spec := spec.(type) {
5053
case *ast.TypeSpec:
54+
if spec.Name.Name == "_" {
55+
continue
56+
}
5157
if obj := info.ObjectOf(spec.Name); obj != nil {
5258
ts, err := typeSymbol(snapshot, pkg, info, spec, obj, q)
5359
if err != nil {
@@ -58,6 +64,9 @@ func DocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]p
5864
}
5965
case *ast.ValueSpec:
6066
for _, name := range spec.Names {
67+
if name.Name == "_" {
68+
continue
69+
}
6170
if obj := info.ObjectOf(name); obj != nil {
6271
vs, err := varSymbol(snapshot, pkg, decl, name, obj, q)
6372
if err != nil {

internal/lsp/testdata/symbols/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"io"
55
)
66

7+
var _ = 1
8+
79
var x = 42 //@mark(symbolsx, "x"), symbol("x", "x", "Variable", "", "main.x")
810

911
const y = 43 //@symbol("y", "y", "Constant", "", "main.y")
@@ -34,6 +36,8 @@ func (f Foo) Baz() string { //@symbol("(Foo).Baz", "Baz", "Method", "", "main.Fo
3436
return f.baz
3537
}
3638

39+
func _() {}
40+
3741
func (q *Quux) Do() {} //@mark(qDo, "Do"), symbol("(*Quux).Do", "Do", "Method", "", "main.Quux.Do")
3842

3943
func main() { //@symbol("main", "main", "Function", "", "main.main")
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
-- symbols --
2-
x Variable 7:5-7:6
3-
y Constant 9:7-9:8
4-
Number Number 11:6-11:12
5-
Alias String 13:6-13:11
6-
NumberAlias Number 15:6-15:17
7-
Boolean Boolean 18:2-18:9
8-
BoolAlias Boolean 19:2-19:11
9-
Foo Struct 22:6-22:9
10-
Bar Field 25:2-25:5
11-
Quux Field 23:2-23:6
12-
W Field 24:2-24:3
13-
baz Field 26:2-26:5
14-
Quux Struct 29:6-29:10
15-
X Field 30:2-30:3
16-
Y Field 30:5-30:6
17-
(Foo).Baz Method 33:14-33:17
18-
(*Quux).Do Method 37:16-37:18
19-
main Function 39:6-39:10
20-
Stringer Interface 43:6-43:14
21-
String Method 44:2-44:8
22-
ABer Interface 47:6-47:10
23-
A Method 49:2-49:3
24-
B Method 48:2-48:3
25-
WithEmbeddeds Interface 52:6-52:19
26-
ABer Interface 54:2-54:6
27-
Do Method 53:2-53:4
28-
io.Writer Interface 55:2-55:11
29-
Dunk Function 58:6-58:10
30-
dunk Function 60:6-60:10
2+
x Variable 9:5-9:6
3+
y Constant 11:7-11:8
4+
Number Number 13:6-13:12
5+
Alias String 15:6-15:11
6+
NumberAlias Number 17:6-17:17
7+
Boolean Boolean 20:2-20:9
8+
BoolAlias Boolean 21:2-21:11
9+
Foo Struct 24:6-24:9
10+
Bar Field 27:2-27:5
11+
Quux Field 25:2-25:6
12+
W Field 26:2-26:3
13+
baz Field 28:2-28:5
14+
Quux Struct 31:6-31:10
15+
X Field 32:2-32:3
16+
Y Field 32:5-32:6
17+
(Foo).Baz Method 35:14-35:17
18+
(*Quux).Do Method 41:16-41:18
19+
main Function 43:6-43:10
20+
Stringer Interface 47:6-47:14
21+
String Method 48:2-48:8
22+
ABer Interface 51:6-51:10
23+
A Method 53:2-53:3
24+
B Method 52:2-52:3
25+
WithEmbeddeds Interface 56:6-56:19
26+
ABer Interface 58:2-58:6
27+
Do Method 57:2-57:4
28+
io.Writer Interface 59:2-59:11
29+
Dunk Function 62:6-62:10
30+
dunk Function 64:6-64:10
3131

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
-- workspace_symbol --
2-
symbols/main.go:58:6-10 main.Dunk Function
2+
symbols/main.go:62:6-10 main.Dunk Function
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
-- workspace_symbol --
2-
symbols/main.go:60:6-10 main.dunk Function
2+
symbols/main.go:64:6-10 main.dunk Function

0 commit comments

Comments
 (0)