Skip to content

Commit

Permalink
Add semantic highlight in assignment contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
kirides committed Jan 2, 2024
1 parent a0f61e5 commit 85de30a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions langserver/daedalus_identifier_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ func (l *DaedalusIdentifierListener) onIdentifier(ctx antlr.ParserRuleContext) {
})
}

func (l *DaedalusIdentifierListener) EnterAssignment(ctx *parser.AssignmentContext) {
refAtom := ctx.Reference()
l.onIdentifier(refAtom)
}

func (l *DaedalusIdentifierListener) EnterArrayIndex(ctx *parser.ArrayIndexContext) {
refAtom := ctx.ReferenceAtom()
if refAtom == nil {
Expand Down
60 changes: 60 additions & 0 deletions langserver/daedalus_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package langserver

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/kirides/DaedalusLanguageServer/protocol"
"golang.org/x/exp/slices"
"golang.org/x/text/encoding/charmap"
)

Expand Down Expand Up @@ -97,3 +100,60 @@ func BenchmarkGothicSrc(b *testing.B) {
}
}
}

func TestParseSemanticForGlobals(t *testing.T) {
script := `var int CurrentLevel;
const int ADDONWORLD_ZEN = 4;
func void FuncName() {
CurrentLevel = ADDONWORLD_ZEN;
};`

data := []struct {
expected string
}{
{expected: "ADDONWORLD_ZEN"},
{expected: "CurrentLevel"},
}
m := newParseResultsManager(nopLogger{})
result, _ := m.ParseSemanticsContentDataTypesRange(context.Background(), "C:\\temp", script, protocol.Range{Start: protocol.Position{Line: 0, Character: 0}, End: protocol.Position{Line: 999, Character: 999}}, DataAll)

for _, v := range data {
t.Run(fmt.Sprintf("Expect %q", v.expected), func(t *testing.T) {
if !slices.ContainsFunc(result.GlobalIdentifiers, func(i Identifier) bool {
return i.Name() == v.expected
}) {
t.Fail()
}
})
}
}

func TestParseSemanticForLocals(t *testing.T) {
script := `
func void FuncName() {
var int CurrentLevel;
const int ADDONWORLD_ZEN = 4;
CurrentLevel = ADDONWORLD_ZEN;
};`

data := []struct {
expected string
}{
{expected: "ADDONWORLD_ZEN"},
{expected: "CurrentLevel"},
}
m := newParseResultsManager(nopLogger{})
result, _ := m.ParseSemanticsContentDataTypesRange(context.Background(), "C:\\temp", script, protocol.Range{Start: protocol.Position{Line: 0, Character: 0}, End: protocol.Position{Line: 999, Character: 999}}, DataAll)

for _, v := range data {
t.Run(fmt.Sprintf("Expect %q", v.expected), func(t *testing.T) {
if !slices.ContainsFunc(result.GlobalIdentifiers, func(i Identifier) bool {
return i.Name() == v.expected
}) {
t.Fail()
}
})
}
}

0 comments on commit 85de30a

Please sign in to comment.