Skip to content

Commit d654e71

Browse files
committed
added simple dot-completion for structs
1 parent 6329cc5 commit d654e71

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

ddpls/ddpls.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func (ls *DDPLS) createInitialize() protocol.InitializeFunc {
7272
TriggerCharacters: []string{
7373
"\"",
7474
"/",
75+
".",
7576
},
7677
}
7778
temp := true

handlers/completion.go

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/DDP-Projekt/DDPLS/log"
1515
"github.com/DDP-Projekt/Kompilierer/src/ast"
1616
"github.com/DDP-Projekt/Kompilierer/src/ddppath"
17+
"github.com/DDP-Projekt/Kompilierer/src/ddptypes"
1718
"github.com/tliron/glsp"
1819
protocol "github.com/tliron/glsp/protocol_3_16"
1920
)
@@ -35,10 +36,12 @@ func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDo
3536
docModule = d.Module
3637
}
3738

39+
isDotCompletion := params.Context.TriggerKind == protocol.CompletionTriggerKindTriggerCharacter && *params.Context.TriggerCharacter == "."
3840
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,
4245
}
4346
ast.VisitModule(docModule, visitor)
4447

@@ -74,10 +77,32 @@ func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDo
7477
}
7578
}
7679
}
77-
7880
table = table.Enclosing
7981
}
8082

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+
81106
for _, v := range varItems {
82107
items = append(items, v)
83108
}
@@ -231,15 +256,18 @@ func init() {
231256
}
232257

233258
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
237264
}
238265

239266
var (
240267
_ ast.Visitor = (*tableVisitor)(nil)
241268
_ ast.ScopeSetter = (*tableVisitor)(nil)
242269
_ ast.ConditionalVisitor = (*tableVisitor)(nil)
270+
_ ast.IdentVisitor = (*tableVisitor)(nil)
243271
)
244272

245273
func (*tableVisitor) Visitor() {}
@@ -253,9 +281,23 @@ func (t *tableVisitor) ShouldVisit(node ast.Node) bool {
253281
if shouldVisit {
254282
t.Table = t.tempTable
255283
}
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+
256290
return shouldVisit
257291
}
258292

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+
259301
type importVisitor struct {
260302
pos protocol.Position
261303
items *[]protocol.CompletionItem

0 commit comments

Comments
 (0)