Skip to content

Commit 5b8ca71

Browse files
committed
1 parent 0d5f610 commit 5b8ca71

15 files changed

+1281
-96
lines changed

go.mod

-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ require (
88
github.com/arduino/arduino-cli v0.0.0-20201118111649-5edef82c60fb
99
github.com/arduino/go-paths-helper v1.3.2
1010
github.com/arduino/go-properties-orderedmap v1.4.0
11-
github.com/davecgh/go-spew v1.1.1 // indirect
12-
github.com/kr/text v0.2.0 // indirect
13-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
1411
github.com/pkg/errors v0.9.1
15-
github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d
1612
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37
1713
github.com/stretchr/testify v1.6.1
18-
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
1914
)

go.sum

+29-4
Large diffs are not rendered by default.

handler/handler.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import (
1616
"github.com/arduino/go-paths-helper"
1717
"github.com/bcmi-labs/arduino-language-server/handler/sourcemapper"
1818
"github.com/bcmi-labs/arduino-language-server/handler/textutils"
19+
"github.com/bcmi-labs/arduino-language-server/lsp"
1920
"github.com/bcmi-labs/arduino-language-server/streams"
2021
"github.com/pkg/errors"
21-
lsp "github.com/sourcegraph/go-lsp"
2222
"github.com/sourcegraph/jsonrpc2"
2323
)
2424

@@ -39,11 +39,11 @@ func Setup(cliPath string, clangdPath string, _enableLogging bool, _asyncProcess
3939
type CLangdStarter func() (stdin io.WriteCloser, stdout io.ReadCloser, stderr io.ReadCloser)
4040

4141
// NewInoHandler creates and configures an InoHandler.
42-
func NewInoHandler(stdio io.ReadWriteCloser, board Board) *InoHandler {
42+
func NewInoHandler(stdio io.ReadWriteCloser, board lsp.Board) *InoHandler {
4343
handler := &InoHandler{
4444
data: map[lsp.DocumentURI]*FileData{},
4545
trackedFiles: map[lsp.DocumentURI]lsp.TextDocumentItem{},
46-
config: BoardConfig{
46+
config: lsp.BoardConfig{
4747
SelectedBoard: board,
4848
},
4949
}
@@ -77,7 +77,7 @@ type InoHandler struct {
7777
trackedFiles map[lsp.DocumentURI]lsp.TextDocumentItem
7878

7979
data map[lsp.DocumentURI]*FileData
80-
config BoardConfig
80+
config lsp.BoardConfig
8181
synchronizer Synchronizer
8282
}
8383

@@ -114,7 +114,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
114114
// Handle LSP methods: transform parameters and send to clangd
115115
var uri lsp.DocumentURI
116116

117-
params, err := readParams(req.Method, req.Params)
117+
params, err := lsp.ReadParams(req.Method, req.Params)
118118
if err != nil {
119119
return nil, err
120120
}
@@ -147,7 +147,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
147147
err = handler.ino2cppTextDocumentPositionParams(&p.TextDocumentPositionParams)
148148
log.Printf(" --> completion(%s:%d:%d)\n", p.TextDocument.URI, p.Position.Line, p.Position.Character)
149149

150-
case *HoverParams:
150+
case *lsp.HoverParams:
151151
// method: "textDocument/hover"
152152
uri = p.TextDocument.URI
153153
doc := &p.TextDocumentPositionParams
@@ -230,7 +230,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
230230
} else {
231231
ctx, cancel := context.WithTimeout(ctx, 800*time.Millisecond)
232232
defer cancel()
233-
result, err = sendRequest(ctx, handler.ClangdConn, req.Method, params)
233+
result, err = lsp.SendRequest(ctx, handler.ClangdConn, req.Method, params)
234234
if enableLogging {
235235
log.Println(" sent", req.Method, "request id", req.ID, " to clangd")
236236
}
@@ -609,7 +609,7 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
609609
switch r := result.(type) {
610610
case *lsp.CompletionList: // "textDocument/completion":
611611
handler.cpp2inoCompletionList(r, uri)
612-
case *[]*commandOrCodeAction: // "textDocument/codeAction":
612+
case *[]*lsp.CommandOrCodeAction: // "textDocument/codeAction":
613613
for index := range *r {
614614
command := (*r)[index].Command
615615
if command != nil {
@@ -620,7 +620,7 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
620620
handler.cpp2inoCodeAction(codeAction, uri)
621621
}
622622
}
623-
case *Hover: // "textDocument/hover":
623+
case *lsp.Hover: // "textDocument/hover":
624624
if len(r.Contents.Value) == 0 {
625625
return nil
626626
}
@@ -647,15 +647,15 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
647647
for index := range *r {
648648
handler.cpp2inoTextEdit(&(*r)[index], uri)
649649
}
650-
case *[]*documentSymbolOrSymbolInformation: // "textDocument/documentSymbol":
650+
case *[]*lsp.DocumentSymbolOrSymbolInformation: // "textDocument/documentSymbol":
651651
if len(*r) == 0 {
652652
return result
653653
}
654654

655655
slice := *r
656656
if slice[0].DocumentSymbol != nil {
657657
// Treat the input as []DocumentSymbol
658-
symbols := make([]DocumentSymbol, len(slice))
658+
symbols := make([]lsp.DocumentSymbol, len(slice))
659659
for index := range slice {
660660
symbols[index] = *slice[index].DocumentSymbol
661661
}
@@ -694,7 +694,7 @@ func (handler *InoHandler) cpp2inoCompletionList(list *lsp.CompletionList, uri l
694694
}
695695
}
696696

697-
func (handler *InoHandler) cpp2inoCodeAction(codeAction *CodeAction, uri lsp.DocumentURI) {
697+
func (handler *InoHandler) cpp2inoCodeAction(codeAction *lsp.CodeAction, uri lsp.DocumentURI) {
698698
codeAction.Edit = handler.cpp2inoWorkspaceEdit(codeAction.Edit)
699699
if data, ok := handler.data[uri]; ok {
700700
for index := range codeAction.Diagnostics {
@@ -732,7 +732,7 @@ func (handler *InoHandler) cpp2inoWorkspaceEdit(origEdit *lsp.WorkspaceEdit) *ls
732732
return &newEdit
733733
}
734734

735-
func (handler *InoHandler) cpp2inoHover(hover *Hover, uri lsp.DocumentURI) {
735+
func (handler *InoHandler) cpp2inoHover(hover *lsp.Hover, uri lsp.DocumentURI) {
736736
if data, ok := handler.data[uri]; ok {
737737
r := hover.Range
738738
if r != nil {
@@ -760,13 +760,13 @@ func (handler *InoHandler) cpp2inoTextEdit(edit *lsp.TextEdit, uri lsp.DocumentU
760760
}
761761
}
762762

763-
func (handler *InoHandler) cpp2inoDocumentSymbols(origSymbols []DocumentSymbol, uri lsp.DocumentURI) []DocumentSymbol {
763+
func (handler *InoHandler) cpp2inoDocumentSymbols(origSymbols []lsp.DocumentSymbol, uri lsp.DocumentURI) []lsp.DocumentSymbol {
764764
data, ok := handler.data[uri]
765765
if !ok || len(origSymbols) == 0 {
766766
return origSymbols
767767
}
768768

769-
symbolIdx := make(map[string]*DocumentSymbol)
769+
symbolIdx := make(map[string]*lsp.DocumentSymbol)
770770
for i := 0; i < len(origSymbols); i++ {
771771
symbol := &origSymbols[i]
772772
_, symbol.Range = data.sourceMap.CppToInoRange(symbol.Range)
@@ -787,7 +787,7 @@ func (handler *InoHandler) cpp2inoDocumentSymbols(origSymbols []DocumentSymbol,
787787
symbolIdx[symbol.Name] = symbol
788788
}
789789

790-
newSymbols := make([]DocumentSymbol, len(symbolIdx))
790+
newSymbols := make([]lsp.DocumentSymbol, len(symbolIdx))
791791
j := 0
792792
for _, s := range symbolIdx {
793793
newSymbols[j] = *s
@@ -825,7 +825,7 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
825825
handler.synchronizer.DataMux.RLock()
826826
defer handler.synchronizer.DataMux.RUnlock()
827827

828-
params, err := readParams(req.Method, req.Params)
828+
params, err := lsp.ReadParams(req.Method, req.Params)
829829
if err != nil {
830830
return nil, errors.WithMessage(err, "parsing JSON message from clangd")
831831
}
@@ -877,7 +877,7 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
877877
return nil, err
878878
}
879879

880-
case *ApplyWorkspaceEditParams:
880+
case *lsp.ApplyWorkspaceEditParams:
881881
// "workspace/applyEdit"
882882
p.Edit = *handler.cpp2inoWorkspaceEdit(&p.Edit)
883883
}
@@ -893,7 +893,7 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
893893
log.Println("From clangd:", req.Method)
894894
}
895895
} else {
896-
result, err = sendRequest(ctx, handler.StdioConn, req.Method, params)
896+
result, err = lsp.SendRequest(ctx, handler.StdioConn, req.Method, params)
897897
if enableLogging {
898898
log.Println("From clangd:", req.Method, "id", req.ID)
899899
}

handler/sourcemapper/ino.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strconv"
1111
"strings"
1212

13-
"github.com/sourcegraph/go-lsp"
13+
"github.com/bcmi-labs/arduino-language-server/lsp"
1414
)
1515

1616
// InoMapper is a mapping between the .ino sketch and the preprocessed .cpp file

handler/textutils/textutils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package textutils
33
import (
44
"fmt"
55

6-
"github.com/sourcegraph/go-lsp"
6+
"github.com/bcmi-labs/arduino-language-server/lsp"
77
)
88

99
// ApplyTextChange replaces startingText substring specified by replaceRange with insertText

handler/textutils/textutils_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"strings"
55
"testing"
66

7-
"github.com/sourcegraph/go-lsp"
7+
"github.com/bcmi-labs/arduino-language-server/lsp"
88
)
99

1010
func TestApplyTextChange(t *testing.T) {

handler/uri.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"strings"
99

1010
"github.com/arduino/go-paths-helper"
11+
"github.com/bcmi-labs/arduino-language-server/lsp"
1112
"github.com/pkg/errors"
12-
lsp "github.com/sourcegraph/go-lsp"
1313
)
1414

1515
var expDriveID = regexp.MustCompile("[a-zA-Z]:")

handler/uri_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"runtime"
66
"testing"
77

8-
lsp "github.com/sourcegraph/go-lsp"
8+
"github.com/bcmi-labs/arduino-language-server/lsp"
99
)
1010

1111
func TestUriToPath(t *testing.T) {

lsp/LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2018 Sourcegraph
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

lsp/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This module has been imported from: https://github.com/sourcegraph/go-lsp
2+
3+
# go-lsp
4+
5+
Package lsp contains Go types for the messages used in the Language Server
6+
Protocol.
7+
8+
See
9+
https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md
10+
for more information.

lsp/jsonrpc2.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package lsp
2+
3+
import (
4+
"encoding/json"
5+
"strconv"
6+
)
7+
8+
// ID represents a JSON-RPC 2.0 request ID, which may be either a
9+
// string or number (or null, which is unsupported).
10+
type ID struct {
11+
// At most one of Num or Str may be nonzero. If both are zero
12+
// valued, then IsNum specifies which field's value is to be used
13+
// as the ID.
14+
Num uint64
15+
Str string
16+
17+
// IsString controls whether the Num or Str field's value should be
18+
// used as the ID, when both are zero valued. It must always be
19+
// set to true if the request ID is a string.
20+
IsString bool
21+
}
22+
23+
func (id ID) String() string {
24+
if id.IsString {
25+
return strconv.Quote(id.Str)
26+
}
27+
return strconv.FormatUint(id.Num, 10)
28+
}
29+
30+
// MarshalJSON implements json.Marshaler.
31+
func (id ID) MarshalJSON() ([]byte, error) {
32+
if id.IsString {
33+
return json.Marshal(id.Str)
34+
}
35+
return json.Marshal(id.Num)
36+
}
37+
38+
// UnmarshalJSON implements json.Unmarshaler.
39+
func (id *ID) UnmarshalJSON(data []byte) error {
40+
// Support both uint64 and string IDs.
41+
var v uint64
42+
if err := json.Unmarshal(data, &v); err == nil {
43+
*id = ID{Num: v}
44+
return nil
45+
}
46+
var v2 string
47+
if err := json.Unmarshal(data, &v2); err != nil {
48+
return err
49+
}
50+
*id = ID{Str: v2, IsString: true}
51+
return nil
52+
}

0 commit comments

Comments
 (0)