From e990796e68a2ebfc099afed20b99e26d6b64061a Mon Sep 17 00:00:00 2001 From: nsf Date: Sun, 15 Oct 2017 15:46:04 +0500 Subject: [PATCH] Fix a bunch of type alias related autocompletion problems. Add tests. https://github.com/nsf/gocode/issues/479 --- _testing/DESC | 3 +++ _testing/test.0062/cursor.141 | 0 _testing/test.0062/out.expected | 5 +++++ _testing/test.0062/test.go.in | 18 ++++++++++++++++++ _testing/test.0063/cursor.109 | 0 _testing/test.0063/out.expected | 4 ++++ _testing/test.0063/test.go.in | 15 +++++++++++++++ autocompletecontext.go | 5 +++++ cursorcontext.go | 9 ++++++++- 9 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 _testing/test.0062/cursor.141 create mode 100644 _testing/test.0062/out.expected create mode 100644 _testing/test.0062/test.go.in create mode 100644 _testing/test.0063/cursor.109 create mode 100644 _testing/test.0063/out.expected create mode 100644 _testing/test.0063/test.go.in diff --git a/_testing/DESC b/_testing/DESC index 0d979a76..c81a38a6 100644 --- a/_testing/DESC +++ b/_testing/DESC @@ -58,3 +58,6 @@ test.0057 - embedded interfaces test.0058 - canonical import aliases test.0059 - canonical import aliases, more complicated case (doesn't work as you might expect) test.0060 - shortcut syntax for return value types in functions +test.0061 - function body vs struct literal cursor context detection +test.0062 - struct type alias embedding +test.0063 - fields autocompletion for a struct literal which is defined by a type alias diff --git a/_testing/test.0062/cursor.141 b/_testing/test.0062/cursor.141 new file mode 100644 index 00000000..e69de29b diff --git a/_testing/test.0062/out.expected b/_testing/test.0062/out.expected new file mode 100644 index 00000000..1d5fbd8f --- /dev/null +++ b/_testing/test.0062/out.expected @@ -0,0 +1,5 @@ +Found 4 candidates: + var A int + var ABCAlias ABCAlias + var B int + var C int diff --git a/_testing/test.0062/test.go.in b/_testing/test.0062/test.go.in new file mode 100644 index 00000000..9e6ad97e --- /dev/null +++ b/_testing/test.0062/test.go.in @@ -0,0 +1,18 @@ +package main + +type ABC struct { + A int + B int + C int +} + +type ABCAlias = ABC + +type Foo struct { + ABCAlias +} + +func main() { + foo := Foo{} + foo. +} diff --git a/_testing/test.0063/cursor.109 b/_testing/test.0063/cursor.109 new file mode 100644 index 00000000..e69de29b diff --git a/_testing/test.0063/out.expected b/_testing/test.0063/out.expected new file mode 100644 index 00000000..ee3d265f --- /dev/null +++ b/_testing/test.0063/out.expected @@ -0,0 +1,4 @@ +Found 3 candidates: + var A int + var B int + var C int diff --git a/_testing/test.0063/test.go.in b/_testing/test.0063/test.go.in new file mode 100644 index 00000000..d4fd29d7 --- /dev/null +++ b/_testing/test.0063/test.go.in @@ -0,0 +1,15 @@ +package main + +type ABC struct { + A int + B int + C int +} + +type ABCAlias = ABC + +func main() { + x := ABCAlias{ + + } +} diff --git a/autocompletecontext.go b/autocompletecontext.go index 41013b41..ee70cdde 100644 --- a/autocompletecontext.go +++ b/autocompletecontext.go @@ -110,6 +110,11 @@ func (b *out_buffers) append_embedded(p string, decl *decl, pkg string, class de continue } + // could be type alias + if typedecl.is_alias() { + typedecl = typedecl.type_dealias() + } + // prevent infinite recursion here if typedecl.is_visited() { continue diff --git a/cursorcontext.go b/cursorcontext.go index 760e17f5..fd8277cd 100644 --- a/cursorcontext.go +++ b/cursorcontext.go @@ -286,7 +286,14 @@ func (c *auto_complete_context) deduce_struct_type_decl(iter *token_iterator) *d if decl == nil { return nil } - if _, ok := decl.typ.(*ast.StructType); !ok { + + // we allow only struct types here, but also support type aliases + if decl.is_alias() { + dd := decl.type_dealias() + if _, ok := dd.typ.(*ast.StructType); !ok { + return nil + } + } else if _, ok := decl.typ.(*ast.StructType); !ok { return nil } return decl