Skip to content

Commit

Permalink
Merge pull request #31 from vknabel/feature/syntax-highlighting-#28
Browse files Browse the repository at this point in the history
Feature/syntax highlighting #28
  • Loading branch information
vknabel authored Feb 7, 2022
2 parents e21e7a6 + 5b10b0f commit 8347ecf
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 149 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"lithia.path": "/Users/vknabel/dev/lithia/lithia"
}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v0.0.13

- lsp: semantic syntax highlighting #28
- lsp: diagnostics #29

## v0.0.12

- cli: new CLI interface, including, help and version
Expand Down
18 changes: 18 additions & 0 deletions langsrv/document-cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package langsrv

import (
protocol "github.com/tliron/glsp/protocol_3_16"
"github.com/vknabel/lithia/ast"
"github.com/vknabel/lithia/parser"
)

type documentCache struct {
documents map[protocol.URI]*textDocumentEntry
}

type textDocumentEntry struct {
item protocol.TextDocumentItem
parser *parser.Parser
fileParser *parser.FileParser
sourceFile *ast.SourceFile
}
34 changes: 34 additions & 0 deletions langsrv/handler-text-document-did-change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package langsrv

import (
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)

func textDocumentDidChange(context *glsp.Context, params *protocol.DidChangeTextDocumentParams) error {
entry := langserver.documentCache.documents[params.TextDocument.URI]
text := entry.item.Text
for _, event := range params.ContentChanges {
switch e := event.(type) {
case protocol.TextDocumentContentChangeEvent:
text = text[:e.Range.Start.IndexIn(text)] + e.Text + text[e.Range.End.IndexIn(text):]
case protocol.TextDocumentContentChangeEventWhole:
text = e.Text
}
}
entry.item.Text = text
fileParser, errs := entry.parser.Parse("default-module", string(params.TextDocument.URI), text)
if len(errs) > 0 {
publishSyntaxErrorDiagnostics(context, params.TextDocument.URI, uint32(params.TextDocument.Version), errs)
return nil
}
sourceFile, errs := fileParser.ParseSourceFile()
if len(errs) > 0 {
publishSyntaxErrorDiagnostics(context, params.TextDocument.URI, uint32(params.TextDocument.Version), errs)
return nil
}
langserver.documentCache.documents[params.TextDocument.URI].fileParser = fileParser
langserver.documentCache.documents[params.TextDocument.URI].sourceFile = sourceFile
publishSyntaxErrorDiagnostics(context, params.TextDocument.URI, uint32(params.TextDocument.Version), nil)
return nil
}
29 changes: 29 additions & 0 deletions langsrv/handler-text-document-did-open.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package langsrv

import (
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
"github.com/vknabel/lithia/parser"
)

func textDocumentDidOpen(context *glsp.Context, params *protocol.DidOpenTextDocumentParams) error {
lithiaParser := parser.NewParser()
fileParser, errs := lithiaParser.Parse("default-module", string(params.TextDocument.URI), params.TextDocument.Text)
if len(errs) > 0 {
publishSyntaxErrorDiagnostics(context, params.TextDocument.URI, uint32(params.TextDocument.Version), errs)
return nil
}
sourceFile, errs := fileParser.ParseSourceFile()
if len(errs) > 0 {
publishSyntaxErrorDiagnostics(context, params.TextDocument.URI, uint32(params.TextDocument.Version), errs)
return nil
}
langserver.documentCache.documents[params.TextDocument.URI] = &textDocumentEntry{
item: params.TextDocument,
parser: lithiaParser,
fileParser: fileParser,
sourceFile: sourceFile,
}
publishSyntaxErrorDiagnostics(context, params.TextDocument.URI, uint32(params.TextDocument.Version), nil)
return nil
}
Loading

0 comments on commit 8347ecf

Please sign in to comment.