Skip to content

Commit

Permalink
Fix a bunch of type alias related autocompletion problems. Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nsf committed Oct 15, 2017
1 parent e81ad18 commit e990796
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions _testing/DESC
Original file line number Diff line number Diff line change
Expand Up @@ -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
Empty file added _testing/test.0062/cursor.141
Empty file.
5 changes: 5 additions & 0 deletions _testing/test.0062/out.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Found 4 candidates:
var A int
var ABCAlias ABCAlias
var B int
var C int
18 changes: 18 additions & 0 deletions _testing/test.0062/test.go.in
Original file line number Diff line number Diff line change
@@ -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.
}
Empty file added _testing/test.0063/cursor.109
Empty file.
4 changes: 4 additions & 0 deletions _testing/test.0063/out.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Found 3 candidates:
var A int
var B int
var C int
15 changes: 15 additions & 0 deletions _testing/test.0063/test.go.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

type ABC struct {
A int
B int
C int
}

type ABCAlias = ABC

func main() {
x := ABCAlias{

}
}
5 changes: 5 additions & 0 deletions autocompletecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion cursorcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e990796

Please sign in to comment.