From af36406620cd51c97eb72d8e991223f9f31b193f Mon Sep 17 00:00:00 2001 From: Shoshin Nikita Date: Tue, 30 Mar 2021 18:31:28 +0000 Subject: [PATCH] internal/lsp/source: respond with the underlying type to Type Definition requests for composite types Go to Type Definition works for all composite types except maps because it is not clear which type to return if both key and value are named types. Fixes golang/go#45029 Change-Id: Ie14f333c51af11033e2494aaaac367d35e7dc87b GitHub-Last-Rev: 94a04812eafe8c157819f0155ed7be2779437867 GitHub-Pull-Request: golang/tools#292 Reviewed-on: https://go-review.googlesource.com/c/tools/+/304789 Reviewed-by: Rebecca Stambler Trust: Rebecca Stambler Trust: Heschi Kreinick Run-TryBot: Rebecca Stambler gopls-CI: kokoro TryBot-Result: Go Bot --- internal/lsp/source/identifier.go | 6 ++++ internal/lsp/testdata/summary.txt.golden | 2 +- internal/lsp/testdata/typdef/typdef.go | 38 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 internal/lsp/testdata/typdef/typdef.go diff --git a/internal/lsp/source/identifier.go b/internal/lsp/source/identifier.go index e648893758d..7c5e62c753e 100644 --- a/internal/lsp/source/identifier.go +++ b/internal/lsp/source/identifier.go @@ -323,6 +323,12 @@ func typeToObject(typ types.Type) types.Object { return typ.Obj() case *types.Pointer: return typeToObject(typ.Elem()) + case *types.Array: + return typeToObject(typ.Elem()) + case *types.Slice: + return typeToObject(typ.Elem()) + case *types.Chan: + return typeToObject(typ.Elem()) default: return nil } diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index dd19a93486b..7b9b6ed4627 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -16,7 +16,7 @@ SemanticTokenCount = 3 SuggestedFixCount = 40 FunctionExtractionCount = 13 DefinitionsCount = 90 -TypeDefinitionsCount = 2 +TypeDefinitionsCount = 10 HighlightsCount = 69 ReferencesCount = 25 RenamesCount = 33 diff --git a/internal/lsp/testdata/typdef/typdef.go b/internal/lsp/testdata/typdef/typdef.go new file mode 100644 index 00000000000..87d51978458 --- /dev/null +++ b/internal/lsp/testdata/typdef/typdef.go @@ -0,0 +1,38 @@ +package typdef + +type Struct struct { //@item(Struct, "Struct", "struct{...}", "struct") + Field string +} + +type Int int //@item(Int, "Int", "int", "type") + +func _() { + var ( + value Struct + point *Struct + ) + _ = value //@typdef("value", Struct) + _ = point //@typdef("point", Struct) + + var ( + array [3]Struct + slice []Struct + ch chan Struct + complex [3]chan *[5][]Int + ) + _ = array //@typdef("array", Struct) + _ = slice //@typdef("slice", Struct) + _ = ch //@typdef("ch", Struct) + _ = complex //@typdef("complex", Int) + + var s struct { + x struct { + xx struct { + field1 []Struct + field2 []Int + } + } + } + s.x.xx.field1 //@typdef("field1", Struct) + s.x.xx.field2 //@typdef("field2", Int) +}