Skip to content

Commit 8347ecf

Browse files
authored
Merge pull request #31 from vknabel/feature/syntax-highlighting-#28
Feature/syntax highlighting #28
2 parents e21e7a6 + 5b10b0f commit 8347ecf

9 files changed

+332
-149
lines changed

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"lithia.path": "/Users/vknabel/dev/lithia/lithia"
3+
}

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v0.0.13
4+
5+
- lsp: semantic syntax highlighting #28
6+
- lsp: diagnostics #29
7+
38
## v0.0.12
49

510
- cli: new CLI interface, including, help and version

langsrv/document-cache.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package langsrv
2+
3+
import (
4+
protocol "github.com/tliron/glsp/protocol_3_16"
5+
"github.com/vknabel/lithia/ast"
6+
"github.com/vknabel/lithia/parser"
7+
)
8+
9+
type documentCache struct {
10+
documents map[protocol.URI]*textDocumentEntry
11+
}
12+
13+
type textDocumentEntry struct {
14+
item protocol.TextDocumentItem
15+
parser *parser.Parser
16+
fileParser *parser.FileParser
17+
sourceFile *ast.SourceFile
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package langsrv
2+
3+
import (
4+
"github.com/tliron/glsp"
5+
protocol "github.com/tliron/glsp/protocol_3_16"
6+
)
7+
8+
func textDocumentDidChange(context *glsp.Context, params *protocol.DidChangeTextDocumentParams) error {
9+
entry := langserver.documentCache.documents[params.TextDocument.URI]
10+
text := entry.item.Text
11+
for _, event := range params.ContentChanges {
12+
switch e := event.(type) {
13+
case protocol.TextDocumentContentChangeEvent:
14+
text = text[:e.Range.Start.IndexIn(text)] + e.Text + text[e.Range.End.IndexIn(text):]
15+
case protocol.TextDocumentContentChangeEventWhole:
16+
text = e.Text
17+
}
18+
}
19+
entry.item.Text = text
20+
fileParser, errs := entry.parser.Parse("default-module", string(params.TextDocument.URI), text)
21+
if len(errs) > 0 {
22+
publishSyntaxErrorDiagnostics(context, params.TextDocument.URI, uint32(params.TextDocument.Version), errs)
23+
return nil
24+
}
25+
sourceFile, errs := fileParser.ParseSourceFile()
26+
if len(errs) > 0 {
27+
publishSyntaxErrorDiagnostics(context, params.TextDocument.URI, uint32(params.TextDocument.Version), errs)
28+
return nil
29+
}
30+
langserver.documentCache.documents[params.TextDocument.URI].fileParser = fileParser
31+
langserver.documentCache.documents[params.TextDocument.URI].sourceFile = sourceFile
32+
publishSyntaxErrorDiagnostics(context, params.TextDocument.URI, uint32(params.TextDocument.Version), nil)
33+
return nil
34+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package langsrv
2+
3+
import (
4+
"github.com/tliron/glsp"
5+
protocol "github.com/tliron/glsp/protocol_3_16"
6+
"github.com/vknabel/lithia/parser"
7+
)
8+
9+
func textDocumentDidOpen(context *glsp.Context, params *protocol.DidOpenTextDocumentParams) error {
10+
lithiaParser := parser.NewParser()
11+
fileParser, errs := lithiaParser.Parse("default-module", string(params.TextDocument.URI), params.TextDocument.Text)
12+
if len(errs) > 0 {
13+
publishSyntaxErrorDiagnostics(context, params.TextDocument.URI, uint32(params.TextDocument.Version), errs)
14+
return nil
15+
}
16+
sourceFile, errs := fileParser.ParseSourceFile()
17+
if len(errs) > 0 {
18+
publishSyntaxErrorDiagnostics(context, params.TextDocument.URI, uint32(params.TextDocument.Version), errs)
19+
return nil
20+
}
21+
langserver.documentCache.documents[params.TextDocument.URI] = &textDocumentEntry{
22+
item: params.TextDocument,
23+
parser: lithiaParser,
24+
fileParser: fileParser,
25+
sourceFile: sourceFile,
26+
}
27+
publishSyntaxErrorDiagnostics(context, params.TextDocument.URI, uint32(params.TextDocument.Version), nil)
28+
return nil
29+
}

0 commit comments

Comments
 (0)