forked from sqls-server/sqls
/
format.go
58 lines (48 loc) · 1.43 KB
/
format.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
package handler
import (
"context"
"encoding/json"
"fmt"
"github.com/mvelzel/sqls/internal/formatter"
"github.com/mvelzel/sqls/internal/lsp"
"github.com/sourcegraph/jsonrpc2"
)
func (s *Server) handleTextDocumentFormatting(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
if req.Params == nil {
return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams}
}
var params lsp.DocumentFormattingParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
f, ok := s.files[params.TextDocument.URI]
if !ok {
return nil, fmt.Errorf("document not found: %s", params.TextDocument.URI)
}
textEdits, err := formatter.Format(f.Text, params, s.getConfig())
if err != nil {
return nil, err
}
if len(textEdits) > 0 {
return textEdits, nil
}
return nil, nil
}
func (s *Server) handleTextDocumentRangeFormatting(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
if req.Params == nil {
return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams}
}
var params lsp.DocumentRangeFormattingParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
_, ok := s.files[params.TextDocument.URI]
if !ok {
return nil, fmt.Errorf("document not found: %s", params.TextDocument.URI)
}
textEdits := []lsp.TextEdit{}
if len(textEdits) > 0 {
return textEdits, nil
}
return nil, nil
}