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>
diff --git a/internal/lsp/source/identifier.go b/internal/lsp/source/identifier.go
index 918ec81..2c5447b 100644
--- a/internal/lsp/source/identifier.go
+++ b/internal/lsp/source/identifier.go
@@ -282,7 +282,7 @@
 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 41ae665..0d2a49c 100644
--- a/internal/lsp/testdata/godef/a/a.go.golden
+++ b/internal/lsp/testdata/godef/a/a.go.golden
@@ -67,8 +67,8 @@
 
 -- 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 45ba995..c7cb59a 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 @@
 
 -- 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 a881d1a..0d75611 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 @@
 	_ = 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 fb534d2..e3b0d19 100644
--- a/internal/lsp/testdata/godef/b/b.go.golden
+++ b/internal/lsp/testdata/godef/b/b.go.golden
@@ -66,7 +66,7 @@
 }
 
 -- 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 @@
 
 -- 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 01b7d11..a79c1e3 100644
--- a/internal/lsp/tests/tests.go
+++ b/internal/lsp/tests/tests.go
@@ -34,7 +34,7 @@
 	ExpectedDiagnosticsCount       = 21
 	ExpectedFormatCount            = 6
 	ExpectedImportCount            = 2
-	ExpectedDefinitionsCount       = 38
+	ExpectedDefinitionsCount       = 39
 	ExpectedTypeDefinitionsCount   = 2
 	ExpectedHighlightsCount        = 2
 	ExpectedReferencesCount        = 5