From aa740d4807891cb493e9f727901baf334b9fabce Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Fri, 19 Apr 2019 15:10:38 +0000 Subject: [PATCH] internal/lsp: handle embedded struct pointer definitions When jumping to definition of an embedded struct pointer, be sure to unwrap the pointer type so you properly jump to the pointee type. Also, fix jumping to definition of an embedded struct inside an anonymous struct inside a struct. The embedded struct detection was continuing too far and thinking it wasn't an embedded struct when it saw the anonymous struct. Fixes golang/go#31451 Change-Id: I96017764270712a2ae02a85306605495075d12e7 GitHub-Last-Rev: 9997f60855ebe37bcca2fecc1ba2a7b871f393d4 GitHub-Pull-Request: golang/tools#83 Reviewed-on: https://go-review.googlesource.com/c/tools/+/172583 Run-TryBot: Paul Jolly TryBot-Result: Gobot Gobot Reviewed-by: Ian Cottrell --- internal/lsp/cmd/definition_test.go | 2 +- internal/lsp/lsp_test.go | 2 +- internal/lsp/source/identifier.go | 5 +++-- internal/lsp/testdata/godef/b/b.go | 11 +++++++++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/internal/lsp/cmd/definition_test.go b/internal/lsp/cmd/definition_test.go index f16948d978..d1388a96c6 100644 --- a/internal/lsp/cmd/definition_test.go +++ b/internal/lsp/cmd/definition_test.go @@ -24,7 +24,7 @@ import ( ) const ( - expectedDefinitionsCount = 26 + expectedDefinitionsCount = 28 expectedTypeDefinitionsCount = 2 ) diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go index 55ed962d86..44b39945ec 100644 --- a/internal/lsp/lsp_test.go +++ b/internal/lsp/lsp_test.go @@ -41,7 +41,7 @@ func testLSP(t *testing.T, exporter packagestest.Exporter) { const expectedCompletionsCount = 65 const expectedDiagnosticsCount = 16 const expectedFormatCount = 4 - const expectedDefinitionsCount = 17 + const expectedDefinitionsCount = 19 const expectedTypeDefinitionsCount = 2 const expectedHighlightsCount = 2 const expectedSymbolsCount = 1 diff --git a/internal/lsp/source/identifier.go b/internal/lsp/source/identifier.go index 5671f62f43..da25c29639 100644 --- a/internal/lsp/source/identifier.go +++ b/internal/lsp/source/identifier.go @@ -76,6 +76,7 @@ func identifier(ctx context.Context, v View, f File, pos token.Pos) (*Identifier for _, n := range path[1:] { if field, ok := n.(*ast.Field); ok { result.wasEmbeddedField = len(field.Names) == 0 + break } } result.Name = result.ident.Name @@ -88,8 +89,8 @@ func identifier(ctx context.Context, v View, f File, pos token.Pos) (*Identifier // The original position was on the embedded field declaration, so we // try to dig out the type and jump to that instead. if v, ok := result.Declaration.Object.(*types.Var); ok { - if n, ok := v.Type().(*types.Named); ok { - result.Declaration.Object = n.Obj() + if typObj := typeToObject(v.Type()); typObj != nil { + result.Declaration.Object = typObj } } } diff --git a/internal/lsp/testdata/godef/b/b.go b/internal/lsp/testdata/godef/b/b.go index ae6081563a..51d5aaf01d 100644 --- a/internal/lsp/testdata/godef/b/b.go +++ b/internal/lsp/testdata/godef/b/b.go @@ -9,8 +9,15 @@ type S1 struct { //@S1 } type S2 struct { //@S2 - F1 string //@mark(S2F1, "F1") - F2 int //@mark(S2F2, "F2") + F1 string //@mark(S2F1, "F1") + F2 int //@mark(S2F2, "F2") + *a.A //@godef("A", A) +} + +type S3 struct { + F1 struct { + a.A //@godef("A", A) + } } func Bar() {