From 71f556f074d4f3c11c9e0cb2ec3317317e8393a5 Mon Sep 17 00:00:00 2001 From: Suzy Mueller Date: Wed, 21 Aug 2019 19:36:09 -0400 Subject: [PATCH] internal/lsp: use the explicit import spec name as ident When there is an explicit name for an import spec, treat it as its own identifier, separate from the import path. Example: import h "hello" The name h is defined in that import spec, not in the package hello it contains its own references. If asked about a position within the import path, continue treating that as referencing the imported package. If the position is within the name, use the identifier there that is local to that file. This change allows for go to definition of the explicit name to point to itself, find all references from the import spec, and rename the explicit name from the import spec. Change-Id: Ia1d927a26e73f2dc450d256d71909c006bdf4c37 Reviewed-on: https://go-review.googlesource.com/c/tools/+/191164 Run-TryBot: Suzy Mueller TryBot-Result: Gobot Gobot Reviewed-by: Ian Cottrell --- internal/lsp/source/identifier.go | 2 +- internal/lsp/testdata/godef/a/a.go.golden | 4 +- internal/lsp/testdata/godef/a/f.go.golden | 48 +++++++++++------------ internal/lsp/testdata/godef/b/b.go | 4 +- internal/lsp/testdata/godef/b/b.go.golden | 4 +- internal/lsp/tests/tests.go | 2 +- 6 files changed, 33 insertions(+), 31 deletions(-) diff --git a/internal/lsp/source/identifier.go b/internal/lsp/source/identifier.go index 918ec81665..2c5447bb19 100644 --- a/internal/lsp/source/identifier.go +++ b/internal/lsp/source/identifier.go @@ -282,7 +282,7 @@ func objToNode(ctx context.Context, view View, originPkg *types.Package, obj typ func importSpec(ctx context.Context, f GoFile, fAST *ast.File, pkg Package, pos token.Pos) (*IdentifierInfo, error) { var imp *ast.ImportSpec for _, spec := range fAST.Imports { - if spec.Pos() <= pos && pos < spec.End() { + if spec.Path.Pos() <= pos && pos < spec.Path.End() { imp = spec } } diff --git a/internal/lsp/testdata/godef/a/a.go.golden b/internal/lsp/testdata/godef/a/a.go.golden index 41ae66529b..0d2a49ca20 100644 --- a/internal/lsp/testdata/godef/a/a.go.golden +++ b/internal/lsp/testdata/godef/a/a.go.golden @@ -67,8 +67,8 @@ godef/a/a.go:14:6-9: defined here as var err error -- err-hover -- var err error --- string-hover -- -string -- make-hover -- The make built-in function allocates and initializes an object of type slice, map, or chan (only). func(t Type, size ...IntegerType) Type +-- string-hover -- +string diff --git a/internal/lsp/testdata/godef/a/f.go.golden b/internal/lsp/testdata/godef/a/f.go.golden index 45ba99575d..c7cb59afca 100644 --- a/internal/lsp/testdata/godef/a/f.go.golden +++ b/internal/lsp/testdata/godef/a/f.go.golden @@ -1,3 +1,27 @@ +-- switchStringY-definition -- +godef/a/f.go:8:9-10: defined here as var y string + +-- switchStringY-definition-json -- +{ + "span": { + "uri": "file://godef/a/f.go", + "start": { + "line": 8, + "column": 9, + "offset": 76 + }, + "end": { + "line": 8, + "column": 10, + "offset": 77 + } + }, + "description": "var y string" +} + + +-- switchStringY-hover -- +var y string -- switchY-definition -- godef/a/f.go:8:9-10: defined here as var y int @@ -22,27 +46,3 @@ godef/a/f.go:8:9-10: defined here as var y int -- switchY-hover -- var y int --- switchStringY-definition -- -godef/a/f.go:8:9-10: defined here as var y string - --- switchStringY-definition-json -- -{ - "span": { - "uri": "file://godef/a/f.go", - "start": { - "line": 8, - "column": 9, - "offset": 76 - }, - "end": { - "line": 8, - "column": 10, - "offset": 77 - } - }, - "description": "var y string" -} - - --- switchStringY-hover -- -var y string \ No newline at end of file diff --git a/internal/lsp/testdata/godef/b/b.go b/internal/lsp/testdata/godef/b/b.go index a881d1a6c6..0d756112a3 100644 --- a/internal/lsp/testdata/godef/b/b.go +++ b/internal/lsp/testdata/godef/b/b.go @@ -1,7 +1,7 @@ package b import ( - myFoo "golang.org/x/tools/internal/lsp/foo" //@godef("foo", PackageFoo),godef("myFoo", PackageFoo) + myFoo "golang.org/x/tools/internal/lsp/foo" //@mark(myFoo, "myFoo"),godef("foo", PackageFoo),godef("myFoo", myFoo) "golang.org/x/tools/internal/lsp/godef/a" //@mark(AImport, "\"") ) @@ -31,5 +31,5 @@ func Bar() { _ = x.F2 //@godef("F2", S2F2) _ = x.S2.F1 //@godef("F1", S2F1) - var _ *myFoo.StructFoo + var _ *myFoo.StructFoo //@godef("myFoo", myFoo) } diff --git a/internal/lsp/testdata/godef/b/b.go.golden b/internal/lsp/testdata/godef/b/b.go.golden index fb534d22c6..e3b0d1941d 100644 --- a/internal/lsp/testdata/godef/b/b.go.golden +++ b/internal/lsp/testdata/godef/b/b.go.golden @@ -66,7 +66,7 @@ foo/foo.go:1:9-12: defined here as } -- PackageFoo-hover -- -myFoo "golang.org/x/tools/internal/lsp/foo" //@godef("foo", PackageFoo),godef("myFoo", PackageFoo) +myFoo "golang.org/x/tools/internal/lsp/foo" //@mark(myFoo, "myFoo"),godef("foo", PackageFoo),godef("myFoo", myFoo) -- S1-definition -- godef/b/b.go:8:6-8: defined here as S1 struct { @@ -249,3 +249,5 @@ godef/a/a.go:9:6-11: defined here as func a.Stuff() -- Stuff-hover -- func a.Stuff() +-- myFoo-hover -- +package myFoo ("golang.org/x/tools/internal/lsp/foo") diff --git a/internal/lsp/tests/tests.go b/internal/lsp/tests/tests.go index 01b7d1169f..a79c1e3726 100644 --- a/internal/lsp/tests/tests.go +++ b/internal/lsp/tests/tests.go @@ -34,7 +34,7 @@ const ( ExpectedDiagnosticsCount = 21 ExpectedFormatCount = 6 ExpectedImportCount = 2 - ExpectedDefinitionsCount = 38 + ExpectedDefinitionsCount = 39 ExpectedTypeDefinitionsCount = 2 ExpectedHighlightsCount = 2 ExpectedReferencesCount = 5