Skip to content

Commit fca8992

Browse files
committed
internal/lsp: handle nil pointer with import shortcut = link
It makes more sense to handle the import shortcut behavior at a higher level anyway, so pull it out of findIdentifier and add a test for the configuration. Fixes golang/go#44189 Change-Id: I96f08c7def154f6761efa727d693fdfb2fb722ab Reviewed-on: https://go-review.googlesource.com/c/tools/+/290789 Trust: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 5848b84 commit fca8992

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

gopls/internal/regtest/misc/definition_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
. "golang.org/x/tools/gopls/internal/regtest"
1313

14+
"golang.org/x/tools/internal/lsp/fake"
1415
"golang.org/x/tools/internal/lsp/tests"
1516
)
1617

@@ -134,3 +135,47 @@ func main() {
134135
}
135136
})
136137
}
138+
139+
func TestImportShortcut(t *testing.T) {
140+
const mod = `
141+
-- go.mod --
142+
module mod.com
143+
144+
go 1.12
145+
-- main.go --
146+
package main
147+
148+
import "fmt"
149+
150+
func main() {}
151+
`
152+
for _, tt := range []struct {
153+
wantLinks int
154+
wantDef bool
155+
importShortcut string
156+
}{
157+
{1, false, "Link"},
158+
{0, true, "Definition"},
159+
{1, true, "Both"},
160+
} {
161+
t.Run(tt.importShortcut, func(t *testing.T) {
162+
WithOptions(
163+
EditorConfig{
164+
ImportShortcut: tt.importShortcut,
165+
},
166+
).Run(t, mod, func(t *testing.T, env *Env) {
167+
env.OpenFile("main.go")
168+
file, pos := env.GoToDefinition("main.go", env.RegexpSearch("main.go", `"fmt"`))
169+
if !tt.wantDef && (file != "" || pos != (fake.Pos{})) {
170+
t.Fatalf("expected no definition, got one: %s:%v", file, pos)
171+
} else if tt.wantDef && file == "" && pos == (fake.Pos{}) {
172+
t.Fatalf("expected definition, got none")
173+
}
174+
links := env.DocumentLink("main.go")
175+
if len(links) != tt.wantLinks {
176+
t.Fatalf("expected %v links, got %v", tt.wantLinks, len(links))
177+
}
178+
})
179+
})
180+
}
181+
}

internal/lsp/definition.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ func (s *Server) definition(ctx context.Context, params *protocol.DefinitionPara
2121
if err != nil {
2222
return nil, err
2323
}
24-
24+
if !snapshot.View().Options().ImportShortcut.ShowDefinition() {
25+
return nil, nil
26+
}
2527
var locations []protocol.Location
2628
for _, ref := range ident.Declaration.MappedRange {
2729
decRange, err := ref.Range()

internal/lsp/fake/editor.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ type EditorConfig struct {
109109
DirectoryFilters []string
110110

111111
VerboseOutput bool
112+
113+
ImportShortcut string
112114
}
113115

114116
// NewEditor Creates a new Editor.
@@ -238,6 +240,10 @@ func (e *Editor) configuration() map[string]interface{} {
238240
config["verboseOutput"] = true
239241
}
240242

243+
if e.Config.ImportShortcut != "" {
244+
config["importShortcut"] = e.Config.ImportShortcut
245+
}
246+
241247
// TODO(rFindley): change to the new settings name once it is no longer
242248
// designated experimental.
243249
config["experimentalDiagnosticsDelay"] = "10ms"

internal/lsp/source/identifier.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ func findIdentifier(ctx context.Context, snapshot Snapshot, pkg Package, file *a
9898
// Handle import specs separately, as there is no formal position for a
9999
// package declaration.
100100
if result, err := importSpec(snapshot, pkg, file, pos); result != nil || err != nil {
101-
if snapshot.View().Options().ImportShortcut.ShowDefinition() {
102-
return result, err
103-
}
104-
return nil, nil
101+
return result, err
105102
}
106103
path := pathEnclosingObjNode(file, pos)
107104
if path == nil {

0 commit comments

Comments
 (0)