Skip to content

Commit e990796

Browse files
committed
Fix a bunch of type alias related autocompletion problems. Add tests.
#479
1 parent e81ad18 commit e990796

File tree

9 files changed

+58
-1
lines changed

9 files changed

+58
-1
lines changed

_testing/DESC

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ test.0057 - embedded interfaces
5858
test.0058 - canonical import aliases
5959
test.0059 - canonical import aliases, more complicated case (doesn't work as you might expect)
6060
test.0060 - shortcut syntax for return value types in functions
61+
test.0061 - function body vs struct literal cursor context detection
62+
test.0062 - struct type alias embedding
63+
test.0063 - fields autocompletion for a struct literal which is defined by a type alias

_testing/test.0062/cursor.141

Whitespace-only changes.

_testing/test.0062/out.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Found 4 candidates:
2+
var A int
3+
var ABCAlias ABCAlias
4+
var B int
5+
var C int

_testing/test.0062/test.go.in

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
type ABC struct {
4+
A int
5+
B int
6+
C int
7+
}
8+
9+
type ABCAlias = ABC
10+
11+
type Foo struct {
12+
ABCAlias
13+
}
14+
15+
func main() {
16+
foo := Foo{}
17+
foo.
18+
}

_testing/test.0063/cursor.109

Whitespace-only changes.

_testing/test.0063/out.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Found 3 candidates:
2+
var A int
3+
var B int
4+
var C int

_testing/test.0063/test.go.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
type ABC struct {
4+
A int
5+
B int
6+
C int
7+
}
8+
9+
type ABCAlias = ABC
10+
11+
func main() {
12+
x := ABCAlias{
13+
14+
}
15+
}

autocompletecontext.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ func (b *out_buffers) append_embedded(p string, decl *decl, pkg string, class de
110110
continue
111111
}
112112

113+
// could be type alias
114+
if typedecl.is_alias() {
115+
typedecl = typedecl.type_dealias()
116+
}
117+
113118
// prevent infinite recursion here
114119
if typedecl.is_visited() {
115120
continue

cursorcontext.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,14 @@ func (c *auto_complete_context) deduce_struct_type_decl(iter *token_iterator) *d
286286
if decl == nil {
287287
return nil
288288
}
289-
if _, ok := decl.typ.(*ast.StructType); !ok {
289+
290+
// we allow only struct types here, but also support type aliases
291+
if decl.is_alias() {
292+
dd := decl.type_dealias()
293+
if _, ok := dd.typ.(*ast.StructType); !ok {
294+
return nil
295+
}
296+
} else if _, ok := decl.typ.(*ast.StructType); !ok {
290297
return nil
291298
}
292299
return decl

0 commit comments

Comments
 (0)