Skip to content

Commit 1b4e560

Browse files
committedSep 17, 2019
Fixed URI conversion for Windows
1 parent 1aeecf8 commit 1b4e560

File tree

4 files changed

+127
-20
lines changed

4 files changed

+127
-20
lines changed
 

‎handler/handler.go

+21-20
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import (
66
"encoding/json"
77
"io"
88
"log"
9-
"net/url"
109
"path/filepath"
1110
"regexp"
1211
"strings"
1312

14-
"github.com/pkg/errors"
1513
lsp "github.com/sourcegraph/go-lsp"
1614
"github.com/sourcegraph/jsonrpc2"
1715
)
@@ -154,6 +152,9 @@ func (handler *InoHandler) transformClangdParams(ctx context.Context, method str
154152
p := params.(*lsp.RenameParams)
155153
uri = p.TextDocument.URI
156154
err = handler.ino2cppRenameParams(p)
155+
case "workspace/didChangeWatchedFiles":
156+
p := params.(*lsp.DidChangeWatchedFilesParams)
157+
err = handler.ino2cppDidChangeWatchedFilesParams(p)
157158
}
158159
return
159160
}
@@ -233,7 +234,10 @@ func (handler *InoHandler) handleError(ctx context.Context, err error, fqbn stri
233234
message = "Editor support is disabled because the platform `" + fqbn + "` is not installed."
234235
message += " Use the Boards Manager to install it."
235236
} else if strings.Contains(errorStr, "No such file or directory") {
236-
exp, _ := regexp.Compile("([\\w\\.\\-]+)\\.h: No such file or directory")
237+
exp, regexpErr := regexp.Compile("([\\w\\.\\-]+)\\.h: No such file or directory")
238+
if regexpErr != nil {
239+
panic(regexpErr)
240+
}
237241
submatch := exp.FindStringSubmatch(errorStr)
238242
message = "Editor support is disabled because the library `" + submatch[1] + "` is not installed."
239243
message += " Use the Library Manager to install it"
@@ -246,8 +250,9 @@ func (handler *InoHandler) handleError(ctx context.Context, err error, fqbn stri
246250
func (handler *InoHandler) ino2cppTextDocumentIdentifier(doc *lsp.TextDocumentIdentifier) error {
247251
if data, ok := handler.data[doc.URI]; ok {
248252
doc.URI = data.targetURI
253+
return nil
249254
}
250-
return nil
255+
return unknownURI(doc.URI)
251256
}
252257

253258
func (handler *InoHandler) ino2cppTextDocumentItem(ctx context.Context, doc *lsp.TextDocumentItem) error {
@@ -330,6 +335,16 @@ func (handler *InoHandler) ino2cppRenameParams(params *lsp.RenameParams) error {
330335
return unknownURI(params.TextDocument.URI)
331336
}
332337

338+
func (handler *InoHandler) ino2cppDidChangeWatchedFilesParams(params *lsp.DidChangeWatchedFilesParams) error {
339+
for index := range params.Changes {
340+
fileEvent := &params.Changes[index]
341+
if data, ok := handler.data[fileEvent.URI]; ok {
342+
fileEvent.URI = data.targetURI
343+
}
344+
}
345+
return nil
346+
}
347+
333348
func (handler *InoHandler) transformClangdResult(method string, uri lsp.DocumentURI, result interface{}) interface{} {
334349
switch method {
335350
case "textDocument/completion":
@@ -532,6 +547,8 @@ func (handler *InoHandler) transformStdioParams(method string, raw *json.RawMess
532547
}
533548

534549
func (handler *InoHandler) cpp2inoPublishDiagnosticsParams(params *lsp.PublishDiagnosticsParams) error {
550+
log.Println("diagnostics", *params)
551+
log.Println("data", handler.data)
535552
if data, ok := handler.data[params.URI]; ok {
536553
params.URI = data.sourceURI
537554
for index := range params.Diagnostics {
@@ -550,19 +567,3 @@ func (handler *InoHandler) showMessage(ctx context.Context, msgType lsp.MessageT
550567
}
551568
handler.StdioConn.Notify(ctx, "window/showMessage", &params)
552569
}
553-
554-
func uriToPath(uri lsp.DocumentURI) string {
555-
url, err := url.Parse(string(uri))
556-
if err != nil {
557-
return string(uri)
558-
}
559-
return filepath.FromSlash(url.Path)
560-
}
561-
562-
func pathToURI(path string) lsp.DocumentURI {
563-
return "file://" + lsp.DocumentURI(filepath.ToSlash(path))
564-
}
565-
566-
func unknownURI(uri lsp.DocumentURI) error {
567-
return errors.New("Document is not available: " + string(uri))
568-
}

‎handler/protocol.go

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func readParams(method string, raw *json.RawMessage) (interface{}, error) {
7676
params := new(lsp.WorkspaceSymbolParams)
7777
err := json.Unmarshal(*raw, params)
7878
return params, err
79+
case "workspace/didChangeWatchedFiles":
80+
params := new(lsp.DidChangeWatchedFilesParams)
81+
err := json.Unmarshal(*raw, params)
82+
return params, err
7983
case "textDocument/publishDiagnostics":
8084
params := new(lsp.PublishDiagnosticsParams)
8185
err := json.Unmarshal(*raw, params)

‎handler/uri.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package handler
2+
3+
import (
4+
"net/url"
5+
"path/filepath"
6+
"regexp"
7+
"runtime"
8+
"strings"
9+
10+
"github.com/pkg/errors"
11+
lsp "github.com/sourcegraph/go-lsp"
12+
)
13+
14+
var expDriveId = regexp.MustCompile("[a-zA-Z]:")
15+
16+
func uriToPath(uri lsp.DocumentURI) string {
17+
urlObj, err := url.Parse(string(uri))
18+
if err != nil {
19+
return string(uri)
20+
}
21+
path := ""
22+
segments := strings.Split(urlObj.Path, "/")
23+
for _, segment := range segments {
24+
decoded, err := url.PathUnescape(segment)
25+
if err != nil {
26+
decoded = segment
27+
}
28+
if runtime.GOOS == "windows" && expDriveId.MatchString(decoded) {
29+
path += strings.ToUpper(decoded)
30+
} else if len(decoded) > 0 {
31+
path += string(filepath.Separator) + decoded
32+
}
33+
}
34+
return path
35+
}
36+
37+
func pathToURI(path string) lsp.DocumentURI {
38+
urlObj, err := url.Parse("file://")
39+
if err != nil {
40+
panic(err)
41+
}
42+
segments := strings.Split(path, string(filepath.Separator))
43+
for _, segment := range segments {
44+
urlObj.Path += "/" + url.PathEscape(segment)
45+
}
46+
return lsp.DocumentURI(urlObj.String())
47+
}
48+
49+
func unknownURI(uri lsp.DocumentURI) error {
50+
return errors.New("Document is not available: " + string(uri))
51+
}

‎handler/uri_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package handler
2+
3+
import (
4+
"path/filepath"
5+
"runtime"
6+
"testing"
7+
8+
lsp "github.com/sourcegraph/go-lsp"
9+
)
10+
11+
func TestUriToPath(t *testing.T) {
12+
var path string
13+
if runtime.GOOS == "windows" {
14+
path = uriToPath(lsp.DocumentURI("file:///C:/Users/test/Sketch.ino"))
15+
if path != "C:\\Users\\test\\Sketch.ino" {
16+
t.Error(path)
17+
}
18+
path = uriToPath(lsp.DocumentURI("file:///c%3A/Users/test/Sketch.ino"))
19+
if path != "C:\\Users\\test\\Sketch.ino" {
20+
t.Error(path)
21+
}
22+
} else {
23+
path = uriToPath(lsp.DocumentURI("file:///Users/test/Sketch.ino"))
24+
if path != "/Users/test/Sketch.ino" {
25+
t.Error(path)
26+
}
27+
}
28+
path = uriToPath(lsp.DocumentURI("file:///%25F0%259F%2598%259B"))
29+
if path != string(filepath.Separator)+"\U0001F61B" {
30+
t.Error(path)
31+
}
32+
}
33+
34+
func TestPathToUri(t *testing.T) {
35+
var uri lsp.DocumentURI
36+
if runtime.GOOS == "windows" {
37+
uri = pathToURI("C:\\Users\\test\\Sketch.ino")
38+
if uri != "file:///C:/Users/test/Sketch.ino" {
39+
t.Error(uri)
40+
}
41+
} else {
42+
uri = pathToURI("/Users/test/Sketch.ino")
43+
if uri != "file:///Users/test/Sketch.ino" {
44+
t.Error(uri)
45+
}
46+
}
47+
uri = pathToURI("\U0001F61B")
48+
if uri != "file:///%25F0%259F%2598%259B" {
49+
t.Error(uri)
50+
}
51+
}

0 commit comments

Comments
 (0)
Please sign in to comment.