-
Notifications
You must be signed in to change notification settings - Fork 349
/
text_document_did_open.go
67 lines (57 loc) · 1.53 KB
/
text_document_did_open.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
package langserver
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
lsp "github.com/sourcegraph/go-lsp"
"github.com/sourcegraph/jsonrpc2"
"github.com/spf13/afero"
)
func (h *handler) textDocumentDidOpen(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
if req.Params == nil {
return nil, &jsonrpc2.Error{
Code: jsonrpc2.CodeInvalidRequest,
Message: "request params are nil",
}
}
var params lsp.DidOpenTextDocumentParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, &jsonrpc2.Error{
Code: jsonrpc2.CodeParseError,
Message: err.Error(),
Data: req.Params,
}
}
openedPath, err := uriToPath(params.TextDocument.URI)
if err != nil {
return nil, err
}
if err := h.chdir(filepath.Dir(openedPath)); err != nil {
return nil, err
}
if err := afero.WriteFile(h.fs, filepath.Base(openedPath), []byte(params.TextDocument.Text), os.ModePerm); err != nil {
return nil, fmt.Errorf("Failed to synchronize TextDocument.Text: %s", err)
}
diagnostics, err := h.inspect()
if err != nil {
return nil, err
}
log.Printf("Notify textDocument/publishDiagnostics with %#v", diagnostics)
for path, diags := range diagnostics {
err = conn.Notify(
ctx,
"textDocument/publishDiagnostics",
lsp.PublishDiagnosticsParams{
URI: pathToURI(path),
Diagnostics: diags,
},
)
if err != nil {
return nil, fmt.Errorf("Failed to notify textDocument/publishDiagnostics: %s", err)
}
}
return nil, nil
}