Skip to content

Commit

Permalink
add diagnose
Browse files Browse the repository at this point in the history
  • Loading branch information
x committed Dec 30, 2023
1 parent 20a166a commit df4c38c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
10 changes: 10 additions & 0 deletions go-lsp/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ func (s *Server) newSession(conn ReaderWriter) *Session {
s.session[id] = session
return session
}

func (s *Server) SendMsg(resp interface{}) error {
for _, session := range s.session {
if session != nil {
session.SendMsg(resp)
break
}
}
return nil
}
20 changes: 20 additions & 0 deletions go-lsp/jsonrpc/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,26 @@ func (s *Session) handlerRequest(req RequestMessage) error {
return nil
}

func (s *Session) SendMsg(resp interface{}) error {
s.writeLock.Lock()
defer s.writeLock.Unlock()
res, err := jsoniter.Marshal(resp)
if err != nil {
return err
}
logs.Printf("SendMsg: [%v]\n", string(res))
totalLen := len(res)
err = s.mustWrite([]byte(fmt.Sprintf("Content-Length: %d\r\n\r\n", totalLen)))
if err != nil {
return err
}
err = s.mustWrite(res)
if err != nil {
return err
}
return nil
}

func (s *Session) write(resp ResponseMessage) error {
s.writeLock.Lock()
defer s.writeLock.Unlock()
Expand Down
4 changes: 4 additions & 0 deletions go-lsp/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ func isNil(i interface{}) bool {
}
return false
}

func (s *Server) SendMsg(resp interface{}) error {
return s.rpcServer.SendMsg(resp)
}
65 changes: 64 additions & 1 deletion proto/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"unicode/utf8"
Expand All @@ -33,6 +35,7 @@ type view struct {
openFileMu *sync.RWMutex

pbHeaders map[defines.DocumentUri][]string
Server *lsp.Server
}

func (v *view) GetFile(document_uri defines.DocumentUri) (ProtoFile, error) {
Expand All @@ -51,6 +54,11 @@ func (v *view) GetFile(document_uri defines.DocumentUri) (ProtoFile, error) {
return nil, fmt.Errorf("%v not found", document_uri)
}

type Diagnositcs struct {
Method string `json:"method"`
Params defines.PublishDiagnosticsParams `json:"params"`
}

// setContent sets the file contents for a file.
func (v *view) setContent(ctx context.Context, document_uri defines.DocumentUri, data []byte) {

Expand All @@ -77,6 +85,7 @@ func (v *view) setContent(ctx context.Context, document_uri defines.DocumentUri,
// Currently it parses every time of file change.
proto, err := parseProto(document_uri, data)

defer v.sendDiagnose(document_uri, err)
if err != nil {
return
}
Expand Down Expand Up @@ -134,6 +143,58 @@ func (v *view) isOpen(document_uri defines.DocumentUri) bool {
return open
}

func (v *view) sendDiagnose(document_uri defines.DocumentUri, err error) {
res := Diagnositcs{
Method: "textDocument/publishDiagnostics",
Params: defines.PublishDiagnosticsParams{
Uri: document_uri,
Diagnostics: []defines.Diagnostic{},
},
}
defer func() {
ViewManager.Server.SendMsg(res)
}()
if err == nil {
return
}
input := err.Error()
re := regexp.MustCompile(`<input>:(\d+):(\d+)`)
matches := re.FindStringSubmatch(input)

line, row := 0, 0
if len(matches) == 3 {
line, err = strconv.Atoi(matches[1])
if err != nil {
logs.Printf("Error:%v\n", err)
return
}
row, err = strconv.Atoi(matches[2])
if err != nil {
logs.Printf("Error:%v\n", err)
return
}
}
if line == 0 || row == 0 {
return
}
logs.Println(row)
severity := defines.DiagnosticSeverityError
res.Params.Diagnostics = append(res.Params.Diagnostics, defines.Diagnostic{
Message: input,
Severity: &severity,
Range: defines.Range{
Start: defines.Position{
Line: uint(line - 1),
Character: uint(row - 1),
},
End: defines.Position{
Line: uint(line - 1),
Character: uint(row),
},
},
})
}

func (v *view) openFile(document_uri defines.DocumentUri, data []byte) {
v.fileMu.Lock()
defer v.fileMu.Unlock()
Expand All @@ -147,6 +208,7 @@ func (v *view) openFile(document_uri defines.DocumentUri, data []byte) {
}

proto, err := parseProto(document_uri, data)
defer v.sendDiagnose(document_uri, err)
if err != nil {
return
}
Expand Down Expand Up @@ -249,7 +311,6 @@ func didOpen(ctx context.Context, params *defines.DidOpenTextDocumentParams) err
text := []byte(params.TextDocument.Text)

ViewManager.didOpen(document_uri, text)

return nil
}

Expand Down Expand Up @@ -309,7 +370,9 @@ func onDidChangeConfiguration(ctx context.Context, req *defines.DidChangeConfigu
}

func Init(server *lsp.Server) {

ViewManager = newView()
ViewManager.Server = server

server.OnInitialized(onInitialized)
server.OnDidChangeConfiguration(onDidChangeConfiguration)
Expand Down

0 comments on commit df4c38c

Please sign in to comment.