@@ -14,6 +14,7 @@ import (
14
14
"github.com/DDP-Projekt/DDPLS/log"
15
15
"github.com/DDP-Projekt/Kompilierer/src/ast"
16
16
"github.com/DDP-Projekt/Kompilierer/src/ddppath"
17
+ "github.com/DDP-Projekt/Kompilierer/src/ddptypes"
17
18
"github.com/tliron/glsp"
18
19
protocol "github.com/tliron/glsp/protocol_3_16"
19
20
)
@@ -35,10 +36,12 @@ func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDo
35
36
docModule = d .Module
36
37
}
37
38
39
+ isDotCompletion := params .Context .TriggerKind == protocol .CompletionTriggerKindTriggerCharacter && * params .Context .TriggerCharacter == "."
38
40
visitor := & tableVisitor {
39
- Table : docModule .Ast .Symbols ,
40
- tempTable : docModule .Ast .Symbols ,
41
- pos : params .Position ,
41
+ Table : docModule .Ast .Symbols ,
42
+ tempTable : docModule .Ast .Symbols ,
43
+ pos : params .Position ,
44
+ isDotCompletion : isDotCompletion ,
42
45
}
43
46
ast .VisitModule (docModule , visitor )
44
47
@@ -74,10 +77,32 @@ func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDo
74
77
}
75
78
}
76
79
}
77
-
78
80
table = table .Enclosing
79
81
}
80
82
83
+ ident := visitor .ident
84
+ if isDotCompletion && ident != nil && ident .Declaration != nil && ddptypes .IsStruct (ident .Declaration .Type ) {
85
+ structType := ident .Declaration .Type .(* ddptypes.StructType )
86
+ for _ , field := range structType .Fields {
87
+ items = append (items , protocol.CompletionItem {
88
+ Kind : ptr (protocol .CompletionItemKindField ),
89
+ Label : field .Name ,
90
+ SortText : ptr ("0" ),
91
+ TextEdit : protocol.TextEdit {
92
+ NewText : fmt .Sprintf ("%s von %s" , field .Name , ident .Declaration .Name ()),
93
+ Range : protocol.Range {
94
+ Start : helper .ToProtocolPosition (ident .GetRange ().Start ),
95
+ End : protocol.Position {
96
+ Line : params .Position .Line ,
97
+ Character : params .Position .Character ,
98
+ },
99
+ },
100
+ },
101
+ FilterText : ptr (fmt .Sprintf ("%s.%s" , ident .Declaration .Name (), field .Name )),
102
+ })
103
+ }
104
+ }
105
+
81
106
for _ , v := range varItems {
82
107
items = append (items , v )
83
108
}
@@ -231,15 +256,18 @@ func init() {
231
256
}
232
257
233
258
type tableVisitor struct {
234
- Table * ast.SymbolTable
235
- tempTable * ast.SymbolTable
236
- pos protocol.Position
259
+ Table * ast.SymbolTable
260
+ tempTable * ast.SymbolTable
261
+ pos protocol.Position
262
+ ident * ast.Ident
263
+ isDotCompletion bool
237
264
}
238
265
239
266
var (
240
267
_ ast.Visitor = (* tableVisitor )(nil )
241
268
_ ast.ScopeSetter = (* tableVisitor )(nil )
242
269
_ ast.ConditionalVisitor = (* tableVisitor )(nil )
270
+ _ ast.IdentVisitor = (* tableVisitor )(nil )
243
271
)
244
272
245
273
func (* tableVisitor ) Visitor () {}
@@ -253,9 +281,23 @@ func (t *tableVisitor) ShouldVisit(node ast.Node) bool {
253
281
if shouldVisit {
254
282
t .Table = t .tempTable
255
283
}
284
+
285
+ pos , end := helper .FromProtocolPosition (t .pos ), node .GetRange ().End
286
+ if t .isDotCompletion && end .Line == pos .Line && end .Column == pos .Column - 1 {
287
+ shouldVisit = true
288
+ }
289
+
256
290
return shouldVisit
257
291
}
258
292
293
+ func (t * tableVisitor ) VisitIdent (ident * ast.Ident ) ast.VisitResult {
294
+ pos , end := helper .FromProtocolPosition (t .pos ), ident .GetRange ().End
295
+ if t .isDotCompletion && end .Line == pos .Line && end .Column == pos .Column - 1 {
296
+ t .ident = ident
297
+ }
298
+ return ast .VisitRecurse
299
+ }
300
+
259
301
type importVisitor struct {
260
302
pos protocol.Position
261
303
items * []protocol.CompletionItem
0 commit comments