1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:14:46 -07:00

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 <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Suzy Mueller 2019-08-21 19:36:09 -04:00
parent fc82fb2afd
commit 71f556f074
6 changed files with 33 additions and 31 deletions

View File

@ -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) { func importSpec(ctx context.Context, f GoFile, fAST *ast.File, pkg Package, pos token.Pos) (*IdentifierInfo, error) {
var imp *ast.ImportSpec var imp *ast.ImportSpec
for _, spec := range fAST.Imports { for _, spec := range fAST.Imports {
if spec.Pos() <= pos && pos < spec.End() { if spec.Path.Pos() <= pos && pos < spec.Path.End() {
imp = spec imp = spec
} }
} }

View File

@ -67,8 +67,8 @@ godef/a/a.go:14:6-9: defined here as var err error
-- err-hover -- -- err-hover --
var err error var err error
-- string-hover --
string
-- make-hover -- -- make-hover --
The make built-in function allocates and initializes an object of type slice, map, or chan (only). The make built-in function allocates and initializes an object of type slice, map, or chan (only).
func(t Type, size ...IntegerType) Type func(t Type, size ...IntegerType) Type
-- string-hover --
string

View File

@ -1,27 +1,3 @@
-- switchY-definition --
godef/a/f.go:8:9-10: defined here as var y int
-- switchY-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 int"
}
-- switchY-hover --
var y int
-- switchStringY-definition -- -- switchStringY-definition --
godef/a/f.go:8:9-10: defined here as var y string godef/a/f.go:8:9-10: defined here as var y string
@ -46,3 +22,27 @@ godef/a/f.go:8:9-10: defined here as var y string
-- switchStringY-hover -- -- switchStringY-hover --
var y string var y string
-- switchY-definition --
godef/a/f.go:8:9-10: defined here as var y int
-- switchY-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 int"
}
-- switchY-hover --
var y int

View File

@ -1,7 +1,7 @@
package b package b
import ( 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, "\"") "golang.org/x/tools/internal/lsp/godef/a" //@mark(AImport, "\"")
) )
@ -31,5 +31,5 @@ func Bar() {
_ = x.F2 //@godef("F2", S2F2) _ = x.F2 //@godef("F2", S2F2)
_ = x.S2.F1 //@godef("F1", S2F1) _ = x.S2.F1 //@godef("F1", S2F1)
var _ *myFoo.StructFoo var _ *myFoo.StructFoo //@godef("myFoo", myFoo)
} }

View File

@ -66,7 +66,7 @@ foo/foo.go:1:9-12: defined here as
} }
-- PackageFoo-hover -- -- 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 -- -- S1-definition --
godef/b/b.go:8:6-8: defined here as S1 struct { 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 -- -- Stuff-hover --
func a.Stuff() func a.Stuff()
-- myFoo-hover --
package myFoo ("golang.org/x/tools/internal/lsp/foo")

View File

@ -34,7 +34,7 @@ const (
ExpectedDiagnosticsCount = 21 ExpectedDiagnosticsCount = 21
ExpectedFormatCount = 6 ExpectedFormatCount = 6
ExpectedImportCount = 2 ExpectedImportCount = 2
ExpectedDefinitionsCount = 38 ExpectedDefinitionsCount = 39
ExpectedTypeDefinitionsCount = 2 ExpectedTypeDefinitionsCount = 2
ExpectedHighlightsCount = 2 ExpectedHighlightsCount = 2
ExpectedReferencesCount = 5 ExpectedReferencesCount = 5