@@ -13,6 +13,7 @@ import (
13
13
"github.com/DDP-Projekt/DDPLS/helper"
14
14
"github.com/DDP-Projekt/DDPLS/log"
15
15
"github.com/DDP-Projekt/Kompilierer/src/ast"
16
+ "github.com/DDP-Projekt/Kompilierer/src/ddperror"
16
17
"github.com/DDP-Projekt/Kompilierer/src/ddppath"
17
18
"github.com/DDP-Projekt/Kompilierer/src/ddptypes"
18
19
"github.com/tliron/glsp"
@@ -22,9 +23,16 @@ import (
22
23
func CreateTextDocumentCompletion (dm * documents.DocumentManager ) protocol.TextDocumentCompletionFunc {
23
24
return RecoverAnyErr (func (context * glsp.Context , params * protocol.CompletionParams ) (any , error ) {
24
25
var docModule * ast.Module
26
+ var latestError * ddperror.Error
25
27
// Get the current Document
26
28
if d , ok := dm .Get (params .TextDocument .URI ); ok {
27
29
docModule = d .Module
30
+ for _ , err := range d .LatestErrors {
31
+ if helper .IsInRange (err .Range , params .Position ) {
32
+ latestError = & err
33
+ break
34
+ }
35
+ }
28
36
}
29
37
30
38
// in case of import completion we need nothing else
@@ -58,6 +66,7 @@ func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDo
58
66
59
67
table := visitor .Table
60
68
varItems := make (map [string ]struct {}, 16 )
69
+ wantType := latestError != nil && latestError .Code == ddperror .SYN_EXPECTED_TYPENAME
61
70
for table != nil {
62
71
for name := range table .Declarations {
63
72
decl , _ , _ := table .LookupDecl (name )
@@ -67,20 +76,14 @@ func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDo
67
76
68
77
switch decl := decl .(type ) {
69
78
case * ast.VarDecl :
70
- if _ , ok := varItems [name ]; ! ok {
71
- varItems [name ] = struct {}{}
72
- items = append (items , protocol.CompletionItem {
73
- Kind : ptr (protocol .CompletionItemKindVariable ),
74
- Label : name ,
75
- })
76
- }
79
+ items = appendVarName (items , varItems , decl .Name (), wantType )
77
80
case * ast.FuncDecl :
78
81
for _ , a := range decl .Aliases {
79
- items = append (items , aliasToCompletionItem ( a ) ... )
82
+ items = appendAlias (items , a , wantType )
80
83
}
81
84
case * ast.StructDecl :
82
85
for _ , a := range decl .Aliases {
83
- items = append (items , aliasToCompletionItem ( a ) ... )
86
+ items = appendAlias (items , a , wantType )
84
87
}
85
88
items = appendTypeName (items , decl )
86
89
case * ast.TypeAliasDecl :
@@ -96,6 +99,24 @@ func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDo
96
99
})
97
100
}
98
101
102
+ func appendVarName (items []protocol.CompletionItem , varItems map [string ]struct {}, name string , wantType bool ) []protocol.CompletionItem {
103
+ if _ , ok := varItems [name ]; ! ok && ! wantType {
104
+ varItems [name ] = struct {}{}
105
+ return append (items , protocol.CompletionItem {
106
+ Kind : ptr (protocol .CompletionItemKindVariable ),
107
+ Label : name ,
108
+ })
109
+ }
110
+ return items
111
+ }
112
+
113
+ func appendAlias (items []protocol.CompletionItem , a ast.Alias , wantType bool ) []protocol.CompletionItem {
114
+ if wantType {
115
+ return items
116
+ }
117
+ return append (items , aliasToCompletionItem (a )... )
118
+ }
119
+
99
120
func appendTypeName (items []protocol.CompletionItem , decl ast.Declaration ) []protocol.CompletionItem {
100
121
return append (items , protocol.CompletionItem {
101
122
Kind : ptr (protocol .CompletionItemKindClass ),
@@ -271,6 +292,7 @@ type tableVisitor struct {
271
292
tempTable * ast.SymbolTable
272
293
pos protocol.Position
273
294
ident * ast.Ident
295
+ badDecl * ast.BadDecl
274
296
isDotCompletion bool
275
297
}
276
298
@@ -279,6 +301,7 @@ var (
279
301
_ ast.ScopeSetter = (* tableVisitor )(nil )
280
302
_ ast.ConditionalVisitor = (* tableVisitor )(nil )
281
303
_ ast.IdentVisitor = (* tableVisitor )(nil )
304
+ _ ast.BadDeclVisitor = (* tableVisitor )(nil )
282
305
)
283
306
284
307
func (* tableVisitor ) Visitor () {}
@@ -309,6 +332,11 @@ func (t *tableVisitor) VisitIdent(ident *ast.Ident) ast.VisitResult {
309
332
return ast .VisitRecurse
310
333
}
311
334
335
+ func (t * tableVisitor ) VisitBadDecl (d * ast.BadDecl ) ast.VisitResult {
336
+ t .badDecl = d
337
+ return ast .VisitRecurse
338
+ }
339
+
312
340
type importVisitor struct {
313
341
pos protocol.Position
314
342
items []protocol.CompletionItem
0 commit comments