Skip to content

Commit 992916b

Browse files
committed
a little less noise in completion
1 parent cdc13c7 commit 992916b

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/DDP-Projekt/DDPLS
33
go 1.22.2
44

55
require (
6-
github.com/DDP-Projekt/Kompilierer v0.4.0-alpha.0.20240727064752-82b1157e21d4
6+
github.com/DDP-Projekt/Kompilierer v0.4.0-alpha.0.20240727075930-9369ebd7971e
77
github.com/tliron/commonlog v0.2.8
88
github.com/tliron/glsp v0.2.2-0.20240309182338-ab78d718ad7d
99
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/DDP-Projekt/Kompilierer v0.4.0-alpha.0.20240727064752-82b1157e21d4 h1:IFRza81KvPQZ8+7JJ/X7M1v+ppX1uYrsg3NsOwOaDYw=
2-
github.com/DDP-Projekt/Kompilierer v0.4.0-alpha.0.20240727064752-82b1157e21d4/go.mod h1:vp0zj11n6rP/B+o7BIXmUhhjrpZNO1c7/pf+VPSHIzM=
1+
github.com/DDP-Projekt/Kompilierer v0.4.0-alpha.0.20240727075930-9369ebd7971e h1:7ljq6q/rKTPJZ4um9KNfGeULStZpiXw88zVf+IXVT20=
2+
github.com/DDP-Projekt/Kompilierer v0.4.0-alpha.0.20240727075930-9369ebd7971e/go.mod h1:vp0zj11n6rP/B+o7BIXmUhhjrpZNO1c7/pf+VPSHIzM=
33
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
44
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
55
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

handlers/completion.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/DDP-Projekt/DDPLS/helper"
1414
"github.com/DDP-Projekt/DDPLS/log"
1515
"github.com/DDP-Projekt/Kompilierer/src/ast"
16+
"github.com/DDP-Projekt/Kompilierer/src/ddperror"
1617
"github.com/DDP-Projekt/Kompilierer/src/ddppath"
1718
"github.com/DDP-Projekt/Kompilierer/src/ddptypes"
1819
"github.com/tliron/glsp"
@@ -22,9 +23,16 @@ import (
2223
func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDocumentCompletionFunc {
2324
return RecoverAnyErr(func(context *glsp.Context, params *protocol.CompletionParams) (any, error) {
2425
var docModule *ast.Module
26+
var latestError *ddperror.Error
2527
// Get the current Document
2628
if d, ok := dm.Get(params.TextDocument.URI); ok {
2729
docModule = d.Module
30+
for _, err := range d.LatestErrors {
31+
if helper.IsInRange(err.Range, params.Position) {
32+
latestError = &err
33+
break
34+
}
35+
}
2836
}
2937

3038
// in case of import completion we need nothing else
@@ -58,6 +66,7 @@ func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDo
5866

5967
table := visitor.Table
6068
varItems := make(map[string]struct{}, 16)
69+
wantType := latestError != nil && latestError.Code == ddperror.SYN_EXPECTED_TYPENAME
6170
for table != nil {
6271
for name := range table.Declarations {
6372
decl, _, _ := table.LookupDecl(name)
@@ -67,20 +76,14 @@ func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDo
6776

6877
switch decl := decl.(type) {
6978
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)
7780
case *ast.FuncDecl:
7881
for _, a := range decl.Aliases {
79-
items = append(items, aliasToCompletionItem(a)...)
82+
items = appendAlias(items, a, wantType)
8083
}
8184
case *ast.StructDecl:
8285
for _, a := range decl.Aliases {
83-
items = append(items, aliasToCompletionItem(a)...)
86+
items = appendAlias(items, a, wantType)
8487
}
8588
items = appendTypeName(items, decl)
8689
case *ast.TypeAliasDecl:
@@ -96,6 +99,24 @@ func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDo
9699
})
97100
}
98101

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+
99120
func appendTypeName(items []protocol.CompletionItem, decl ast.Declaration) []protocol.CompletionItem {
100121
return append(items, protocol.CompletionItem{
101122
Kind: ptr(protocol.CompletionItemKindClass),
@@ -271,6 +292,7 @@ type tableVisitor struct {
271292
tempTable *ast.SymbolTable
272293
pos protocol.Position
273294
ident *ast.Ident
295+
badDecl *ast.BadDecl
274296
isDotCompletion bool
275297
}
276298

@@ -279,6 +301,7 @@ var (
279301
_ ast.ScopeSetter = (*tableVisitor)(nil)
280302
_ ast.ConditionalVisitor = (*tableVisitor)(nil)
281303
_ ast.IdentVisitor = (*tableVisitor)(nil)
304+
_ ast.BadDeclVisitor = (*tableVisitor)(nil)
282305
)
283306

284307
func (*tableVisitor) Visitor() {}
@@ -309,6 +332,11 @@ func (t *tableVisitor) VisitIdent(ident *ast.Ident) ast.VisitResult {
309332
return ast.VisitRecurse
310333
}
311334

335+
func (t *tableVisitor) VisitBadDecl(d *ast.BadDecl) ast.VisitResult {
336+
t.badDecl = d
337+
return ast.VisitRecurse
338+
}
339+
312340
type importVisitor struct {
313341
pos protocol.Position
314342
items []protocol.CompletionItem

0 commit comments

Comments
 (0)