/
diagnostics.go
74 lines (59 loc) · 2.01 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
74
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lspimpl
import (
"context"
"io/ioutil"
"os"
"github.com/antlr/antlr4/runtime/Go/antlr"
parser "github.com/anz-bank/sysl/pkg/grammar"
"github.com/anz-bank/sysl/pkg/lspimpl/lspframework/jsonrpc2"
"github.com/anz-bank/sysl/pkg/lspimpl/lspframework/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 float64) error {
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(spanUri.Filename())
if err != nil {
return jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "file not found (%v)", err)
}
defer fh.Close()
content, err := ioutil.ReadAll(fh)
if err != nil {
return jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "file not found (%v)", err)
}
// 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 {
//fmt.Println("publishReports failed")
return err
}
return nil
}