Skip to content

Commit c8e6975

Browse files
committed
fixed completion scopes
1 parent 4d4b4f5 commit c8e6975

File tree

3 files changed

+145
-15
lines changed

3 files changed

+145
-15
lines changed

handlers/completion.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDo
3737

3838
visitor := &tableVisitor{
3939
Table: docModule.Ast.Symbols,
40+
tempTable: docModule.Ast.Symbols,
4041
pos: params.Position,
4142
}
4243
ast.VisitModule(docModule, visitor)
@@ -47,21 +48,24 @@ func CreateTextDocumentCompletion(dm *documents.DocumentManager) protocol.TextDo
4748
for table != nil {
4849
for name := range table.Declarations {
4950
if _, ok := varItems[name]; !ok {
50-
if decl, ok, isVar := table.LookupDecl(name); ok && isVar {
51+
decl, _, _ := table.LookupDecl(name)
52+
if decl.GetRange().Start.IsBehind(helper.FromProtocolPosition(params.Position)) {
53+
continue
54+
}
55+
56+
switch decl := decl.(type) {
57+
case *ast.VarDecl:
5158
varItems[name] = protocol.CompletionItem{
5259
Kind: ptr(protocol.CompletionItemKindVariable),
5360
Label: name,
5461
}
55-
} else {
56-
switch decl := decl.(type) {
57-
case *ast.FuncDecl:
58-
for _, a := range decl.Aliases {
59-
aliases = append(aliases, a)
60-
}
61-
case *ast.StructDecl:
62-
for _, a := range decl.Aliases {
63-
aliases = append(aliases, a)
64-
}
62+
case *ast.FuncDecl:
63+
for _, a := range decl.Aliases {
64+
aliases = append(aliases, a)
65+
}
66+
case *ast.StructDecl:
67+
for _, a := range decl.Aliases {
68+
aliases = append(aliases, a)
6569
}
6670
}
6771
}
@@ -191,6 +195,7 @@ var (
191195
"Boolean Liste",
192196
"Text Liste",
193197
"Buchstaben Liste",
198+
"nichts",
194199
}
195200
dudenPaths = make([]string, 0)
196201
)
@@ -222,8 +227,9 @@ func init() {
222227
}
223228

224229
type tableVisitor struct {
225-
Table *ast.SymbolTable
226-
pos protocol.Position
230+
Table *ast.SymbolTable
231+
tempTable *ast.SymbolTable
232+
pos protocol.Position
227233
}
228234

229235
var (
@@ -235,11 +241,15 @@ var (
235241
func (*tableVisitor) Visitor() {}
236242

237243
func (t *tableVisitor) SetScope(symbols *ast.SymbolTable) {
238-
t.Table = symbols
244+
t.tempTable = symbols
239245
}
240246

241247
func (t *tableVisitor) ShouldVisit(node ast.Node) bool {
242-
return helper.IsInRange(node.GetRange(), t.pos)
248+
shouldVisit := helper.IsInRange(node.GetRange(), t.pos)
249+
if shouldVisit {
250+
t.Table = t.tempTable
251+
}
252+
return shouldVisit
243253
}
244254

245255
type importVisitor struct {

helper/protocolHelper.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,33 @@ func ToProtocolRange(rang token.Range) protocol.Range {
2323
}
2424
}
2525

26+
func FromProtocolRange(rang protocol.Range) token.Range {
27+
return token.Range{
28+
Start: token.Position{
29+
Line: uint(rang.Start.Line + 1),
30+
Column: uint(rang.Start.Character + 1),
31+
},
32+
End: token.Position{
33+
Line: uint(rang.End.Line + 1),
34+
Column: uint(rang.End.Character + 1),
35+
},
36+
}
37+
}
38+
39+
func ToProtocolPosition(pos token.Position) protocol.Position {
40+
return protocol.Position{
41+
Line: uint32(pos.Line - 1),
42+
Character: uint32(pos.Column - 1),
43+
}
44+
}
45+
46+
func FromProtocolPosition(pos protocol.Position) token.Position {
47+
return token.Position{
48+
Line: uint(pos.Line + 1),
49+
Column: uint(pos.Character + 1),
50+
}
51+
}
52+
2653
// returns the length of a token.Range
2754
func GetRangeLength(rang token.Range, doc *documents.DocumentState) int {
2855
if rang.Start.Line == rang.End.Line {

helper/protocolHelper.go~

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package helper
2+
3+
import (
4+
"strings"
5+
"unicode/utf8"
6+
7+
"github.com/DDP-Projekt/DDPLS/documents"
8+
"github.com/DDP-Projekt/Kompilierer/src/token"
9+
protocol "github.com/tliron/glsp/protocol_3_16"
10+
)
11+
12+
// converts a token.Range to a protocol.Range
13+
func ToProtocolRange(rang token.Range) protocol.Range {
14+
return protocol.Range{
15+
Start: protocol.Position{
16+
Line: uint32(rang.Start.Line - 1),
17+
Character: uint32(rang.Start.Column - 1),
18+
},
19+
End: protocol.Position{
20+
Line: uint32(rang.End.Line - 1),
21+
Character: uint32(rang.End.Column - 1),
22+
},
23+
}
24+
}
25+
26+
func FromProtocolRange(rang protocol.Range) token.Range {
27+
return token.Range{
28+
Start: token.Position{
29+
Line: uint(rang.Start.Line + 1),
30+
Character: uint(rang.Start.Character + 1),
31+
},
32+
End: token.Position{
33+
Line: uint(rang.End.Line + 1),
34+
Character: uint(rang.End.Character + 1),
35+
},
36+
}
37+
}
38+
39+
// returns the length of a token.Range
40+
func GetRangeLength(rang token.Range, doc *documents.DocumentState) int {
41+
if rang.Start.Line == rang.End.Line {
42+
return int(rang.End.Column - rang.Start.Column)
43+
}
44+
lines := strings.Split(doc.Content, "\n")
45+
length := utf8.RuneCountInString(lines[rang.Start.Line-1][rang.Start.Column-1:])
46+
for i := rang.Start.Line; i < rang.End.Line-1; i++ {
47+
length += utf8.RuneCountInString(lines[i])
48+
}
49+
length += utf8.RuneCountInString(lines[rang.End.Line-1][:rang.End.Column-1])
50+
return length
51+
}
52+
53+
// returns two new ranges, constructed by cutting innerRange out of wholeRange
54+
// innerRange must be completely contained in wholeRange
55+
// the resulting ranges are wholeRange.Start - innerRange.Start and innerRange.End - wholeRange.End
56+
func CutRangeOut(wholeRange, innerRange token.Range) []token.Range {
57+
return []token.Range{
58+
{
59+
Start: wholeRange.Start,
60+
End: innerRange.Start,
61+
},
62+
{
63+
Start: innerRange.End,
64+
End: wholeRange.End,
65+
},
66+
}
67+
}
68+
69+
// returns wether the given protocol.Position is inside rang
70+
func IsInRange(rang token.Range, pos protocol.Position) bool {
71+
if pos.Line < uint32(rang.Start.Line-1) || pos.Line > uint32(rang.End.Line-1) {
72+
return false
73+
}
74+
if pos.Line == uint32(rang.Start.Line-1) && pos.Line == uint32(rang.End.Line-1) {
75+
return pos.Character >= uint32(rang.Start.Column-1) && pos.Character <= uint32(rang.End.Column-1)
76+
}
77+
if pos.Line == uint32(rang.Start.Line-1) {
78+
return pos.Character >= uint32(rang.Start.Column-1)
79+
}
80+
if pos.Line == uint32(rang.End.Line-1) {
81+
return pos.Character <= uint32(rang.End.Column-1)
82+
}
83+
return true
84+
}
85+
86+
func Contains[T comparable](s []T, e T) bool {
87+
for _, a := range s {
88+
if a == e {
89+
return true
90+
}
91+
}
92+
return false
93+
}

0 commit comments

Comments
 (0)