diff --git a/gopls/internal/regtest/misc/definition_test.go b/gopls/internal/regtest/misc/definition_test.go index a5e220c6e3..48b76173e9 100644 --- a/gopls/internal/regtest/misc/definition_test.go +++ b/gopls/internal/regtest/misc/definition_test.go @@ -11,6 +11,7 @@ import ( . "golang.org/x/tools/gopls/internal/regtest" + "golang.org/x/tools/internal/lsp/fake" "golang.org/x/tools/internal/lsp/tests" ) @@ -134,3 +135,47 @@ func main() { } }) } + +func TestImportShortcut(t *testing.T) { + const mod = ` +-- go.mod -- +module mod.com + +go 1.12 +-- main.go -- +package main + +import "fmt" + +func main() {} +` + for _, tt := range []struct { + wantLinks int + wantDef bool + importShortcut string + }{ + {1, false, "Link"}, + {0, true, "Definition"}, + {1, true, "Both"}, + } { + t.Run(tt.importShortcut, func(t *testing.T) { + WithOptions( + EditorConfig{ + ImportShortcut: tt.importShortcut, + }, + ).Run(t, mod, func(t *testing.T, env *Env) { + env.OpenFile("main.go") + file, pos := env.GoToDefinition("main.go", env.RegexpSearch("main.go", `"fmt"`)) + if !tt.wantDef && (file != "" || pos != (fake.Pos{})) { + t.Fatalf("expected no definition, got one: %s:%v", file, pos) + } else if tt.wantDef && file == "" && pos == (fake.Pos{}) { + t.Fatalf("expected definition, got none") + } + links := env.DocumentLink("main.go") + if len(links) != tt.wantLinks { + t.Fatalf("expected %v links, got %v", tt.wantLinks, len(links)) + } + }) + }) + } +} diff --git a/internal/lsp/definition.go b/internal/lsp/definition.go index acd5ac2249..46643e1708 100644 --- a/internal/lsp/definition.go +++ b/internal/lsp/definition.go @@ -21,7 +21,9 @@ func (s *Server) definition(ctx context.Context, params *protocol.DefinitionPara if err != nil { return nil, err } - + if !snapshot.View().Options().ImportShortcut.ShowDefinition() { + return nil, nil + } var locations []protocol.Location for _, ref := range ident.Declaration.MappedRange { decRange, err := ref.Range() diff --git a/internal/lsp/fake/editor.go b/internal/lsp/fake/editor.go index 0764e6e626..96410d9086 100644 --- a/internal/lsp/fake/editor.go +++ b/internal/lsp/fake/editor.go @@ -109,6 +109,8 @@ type EditorConfig struct { DirectoryFilters []string VerboseOutput bool + + ImportShortcut string } // NewEditor Creates a new Editor. @@ -238,6 +240,10 @@ func (e *Editor) configuration() map[string]interface{} { config["verboseOutput"] = true } + if e.Config.ImportShortcut != "" { + config["importShortcut"] = e.Config.ImportShortcut + } + // TODO(rFindley): change to the new settings name once it is no longer // designated experimental. config["experimentalDiagnosticsDelay"] = "10ms" diff --git a/internal/lsp/source/identifier.go b/internal/lsp/source/identifier.go index 362604e89b..e648893758 100644 --- a/internal/lsp/source/identifier.go +++ b/internal/lsp/source/identifier.go @@ -98,10 +98,7 @@ func findIdentifier(ctx context.Context, snapshot Snapshot, pkg Package, file *a // Handle import specs separately, as there is no formal position for a // package declaration. if result, err := importSpec(snapshot, pkg, file, pos); result != nil || err != nil { - if snapshot.View().Options().ImportShortcut.ShowDefinition() { - return result, err - } - return nil, nil + return result, err } path := pathEnclosingObjNode(file, pos) if path == nil {