-
Notifications
You must be signed in to change notification settings - Fork 45
/
diagnostics.go
73 lines (58 loc) · 1.89 KB
/
diagnostics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package impl
import (
"context"
"io"
"os"
"strings"
"github.com/pkg/errors"
"github.com/antlr/antlr4/runtime/Go/antlr"
parser "github.com/anz-bank/sysl/pkg/grammar"
"github.com/anz-bank/sysl/pkg/lsp/framework/jsonrpc2"
"github.com/anz-bank/sysl/pkg/lsp/framework/lsp/protocol"
)
func (s *Server) diagnoseRaw(input string, listener *SyslErrorListener) parser.ISysl_fileContext {
var chars = antlr.NewInputStream(input)
var lexer = parser.NewSyslLexer(chars)
var tokens = antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
var parser = parser.NewSyslParser(tokens)
parser.GetInterpreter().SetPredictionMode(antlr.PredictionModeSLL)
parser.RemoveErrorListeners()
parser.BuildParseTrees = true
parser.AddErrorListener(listener)
// parse file here
return parser.Sysl_file()
}
func (s *Server) provideSyntaxDiagnostics(ctx context.Context, uri protocol.DocumentURI, version int32) error {
fsPath := strings.TrimPrefix(string(uri), "file://")
errListener := NewSyslErrorListener(ctx, s.client)
//spanUri := uri.SpanURI()
//if !spanUri.IsFile() {
// return nil
//}
// TODO: in the future support didChange (syntax checking)
// text, err := s.changedText(ctx, uri, params.ContentChanges)
// if err != nil {
// return err
// }
fh, err := os.Open(fsPath)
if err != nil {
return errors.Wrapf(err, "file not found (%v)", jsonrpc2.ErrInternal)
}
defer fh.Close()
content, err := io.ReadAll(fh)
if err != nil {
return errors.Wrapf(err, "file not found (%v)", jsonrpc2.ErrInternal)
}
// TODO: figure out a way to run this in a goroutine (not sure why it doesn't work)
s.diagnoseRaw(string(content), errListener)
// publish the errors
err = s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
Diagnostics: errListener.Errors,
URI: uri,
Version: version,
})
if err != nil {
return errors.Wrapf(err, "PublishDiagnostics failed")
}
return nil
}