Skip to content

Commit 049d19f

Browse files
committed
Moved URI helper in lsp module
1 parent 5b8ca71 commit 049d19f

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

Diff for: handler/handler.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,11 @@ func (handler *InoHandler) exit() {
260260
os.Exit(1)
261261
}
262262

263-
func newPathFromURI(uri lsp.DocumentURI) *paths.Path {
264-
return paths.New(uriToPath(uri))
265-
}
266-
267263
func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.InitializeParams) error {
268264
rootURI := params.RootURI
269265
log.Printf("--> initializeWorkbench(%s)\n", rootURI)
270266

271-
handler.sketchRoot = newPathFromURI(rootURI)
267+
handler.sketchRoot = rootURI.AsPath()
272268
handler.sketchName = handler.sketchRoot.Base()
273269
if buildPath, err := generateBuildEnvironment(handler.sketchRoot, handler.config.SelectedBoard.Fqbn); err == nil {
274270
handler.buildPath = buildPath
@@ -298,7 +294,7 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
298294
handler.ClangdConn = jsonrpc2.NewConn(context.Background(), clangdStream, clangdHandler)
299295

300296
params.RootPath = handler.buildSketchRoot.String()
301-
params.RootURI = pathToURI(handler.buildSketchRoot.String())
297+
params.RootURI = lsp.NewDocumenteURIFromPath(handler.buildSketchRoot)
302298
return nil
303299
}
304300

@@ -353,7 +349,7 @@ func (handler *InoHandler) didOpen(ctx context.Context, params *lsp.DidOpenTextD
353349
log.Printf("--> didOpen(%s)", doc.URI)
354350

355351
// If we are tracking a .ino...
356-
if newPathFromURI(doc.URI).Ext() == ".ino" {
352+
if doc.URI.AsPath().Ext() == ".ino" {
357353
handler.sketchTrackedFilesCount++
358354
log.Printf(" increasing .ino tracked files count: %d", handler.sketchTrackedFilesCount)
359355

@@ -362,7 +358,7 @@ func (handler *InoHandler) didOpen(ctx context.Context, params *lsp.DidOpenTextD
362358
sketchCpp, err := handler.buildSketchCpp.ReadFile()
363359
newParam := &lsp.DidOpenTextDocumentParams{
364360
TextDocument: lsp.TextDocumentItem{
365-
URI: pathToURI(handler.buildSketchCpp.String()),
361+
URI: lsp.NewDocumenteURIFromPath(handler.buildSketchCpp),
366362
Text: string(sketchCpp),
367363
LanguageID: "cpp",
368364
Version: 1,
@@ -388,11 +384,11 @@ func (handler *InoHandler) updateFileData(ctx context.Context, data *FileData, c
388384
return err
389385
}
390386
}
391-
targetBytes, err := updateCpp([]byte(newSourceText), uriToPath(data.sourceURI), handler.config.SelectedBoard.Fqbn, false, uriToPath(data.targetURI))
387+
targetBytes, err := updateCpp([]byte(newSourceText), data.sourceURI.Unbox(), handler.config.SelectedBoard.Fqbn, false, data.targetURI.Unbox())
392388
if err != nil {
393389
if rang == nil {
394390
// Fallback: use the source text unchanged
395-
targetBytes, err = copyIno2Cpp(newSourceText, uriToPath(data.targetURI))
391+
targetBytes, err = copyIno2Cpp(newSourceText, data.targetURI.Unbox())
396392
if err != nil {
397393
return err
398394
}
@@ -473,7 +469,7 @@ func (handler *InoHandler) sketchToBuildPathTextDocumentIdentifier(doc *lsp.Text
473469
// another/path/source.cpp -> unchanged
474470

475471
// Convert sketch path to build path
476-
docFile := newPathFromURI(doc.URI)
472+
docFile := doc.URI.AsPath()
477473
newDocFile := docFile
478474

479475
if docFile.Ext() == ".ino" {
@@ -490,7 +486,7 @@ func (handler *InoHandler) sketchToBuildPathTextDocumentIdentifier(doc *lsp.Text
490486
newDocFile = handler.buildSketchRoot.JoinPath(rel)
491487
}
492488
log.Printf(" URI: '%s' -> '%s'", docFile, newDocFile)
493-
doc.URI = pathToURI(newDocFile.String())
489+
doc.URI = lsp.NewDocumenteURIFromPath(newDocFile)
494490
return nil
495491
}
496492

@@ -841,7 +837,7 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
841837
log.Printf(" > %d:%d - %v: %s", diag.Range.Start.Line, diag.Range.Start.Character, diag.Severity, diag.Code)
842838
}
843839

844-
if newPathFromURI(p.URI).EquivalentTo(handler.buildSketchCpp) {
840+
if p.URI.AsPath().EquivalentTo(handler.buildSketchCpp) {
845841
// we should transform back N diagnostics of sketch.cpp.ino into
846842
// their .ino counter parts (that may span over multiple files...)
847843

@@ -860,7 +856,7 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
860856
// Push back to IDE the converted diagnostics
861857
for filename, inoDiags := range convertedDiagnostics {
862858
msg := lsp.PublishDiagnosticsParams{
863-
URI: pathToURI(filename),
859+
URI: lsp.NewDocumentURI(filename),
864860
Diagnostics: inoDiags,
865861
}
866862
if enableLogging {
@@ -936,3 +932,7 @@ func (handler *InoHandler) showMessage(ctx context.Context, msgType lsp.MessageT
936932
}
937933
handler.StdioConn.Notify(ctx, "window/showMessage", &params)
938934
}
935+
936+
func unknownURI(uri lsp.DocumentURI) error {
937+
return errors.New("Document is not available: " + string(uri))
938+
}

Diff for: handler/uri.go renamed to lsp/uri.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package handler
1+
package lsp
22

33
import (
44
"net/url"
@@ -8,13 +8,17 @@ import (
88
"strings"
99

1010
"github.com/arduino/go-paths-helper"
11-
"github.com/bcmi-labs/arduino-language-server/lsp"
12-
"github.com/pkg/errors"
1311
)
1412

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

17-
func uriToPath(uri lsp.DocumentURI) string {
15+
// AsPath convert the DocumentURI to a paths.Path
16+
func (uri DocumentURI) AsPath() *paths.Path {
17+
return paths.New(uri.Unbox())
18+
}
19+
20+
// Unbox convert the DocumentURI to a file path string
21+
func (uri DocumentURI) Unbox() string {
1822
urlObj, err := url.Parse(string(uri))
1923
if err != nil {
2024
return string(uri)
@@ -35,11 +39,13 @@ func uriToPath(uri lsp.DocumentURI) string {
3539
return path
3640
}
3741

38-
func ToURI(path *paths.Path) lsp.DocumentURI {
39-
return pathToURI(path.String())
42+
// NewDocumenteURIFromPath create a DocumentURI from the given Path object
43+
func NewDocumenteURIFromPath(path *paths.Path) DocumentURI {
44+
return NewDocumentURI(path.String())
4045
}
4146

42-
func pathToURI(path string) lsp.DocumentURI {
47+
// NewDocumentURI create a DocumentURI from the given string path
48+
func NewDocumentURI(path string) DocumentURI {
4349
urlObj, err := url.Parse("file://")
4450
if err != nil {
4551
panic(err)
@@ -50,9 +56,5 @@ func pathToURI(path string) lsp.DocumentURI {
5056
urlObj.Path += "/" + url.PathEscape(segment)
5157
}
5258
}
53-
return lsp.DocumentURI(urlObj.String())
54-
}
55-
56-
func unknownURI(uri lsp.DocumentURI) error {
57-
return errors.New("Document is not available: " + string(uri))
59+
return DocumentURI(urlObj.String())
5860
}

Diff for: handler/uri_test.go renamed to lsp/uri_test.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,48 @@
1-
package handler
1+
package lsp
22

33
import (
44
"path/filepath"
55
"runtime"
66
"testing"
7-
8-
"github.com/bcmi-labs/arduino-language-server/lsp"
97
)
108

119
func TestUriToPath(t *testing.T) {
1210
var path string
1311
if runtime.GOOS == "windows" {
14-
path = uriToPath(lsp.DocumentURI("file:///C:/Users/test/Sketch.ino"))
12+
path = DocumentURI("file:///C:/Users/test/Sketch.ino").Unbox()
1513
if path != "C:\\Users\\test\\Sketch.ino" {
1614
t.Error(path)
1715
}
18-
path = uriToPath(lsp.DocumentURI("file:///c%3A/Users/test/Sketch.ino"))
16+
path = DocumentURI("file:///c%3A/Users/test/Sketch.ino").Unbox()
1917
if path != "C:\\Users\\test\\Sketch.ino" {
2018
t.Error(path)
2119
}
2220
} else {
23-
path = uriToPath(lsp.DocumentURI("file:///Users/test/Sketch.ino"))
21+
path = DocumentURI("file:///Users/test/Sketch.ino").Unbox()
2422
if path != "/Users/test/Sketch.ino" {
2523
t.Error(path)
2624
}
2725
}
28-
path = uriToPath(lsp.DocumentURI("file:///%25F0%259F%2598%259B"))
26+
path = DocumentURI("file:///%25F0%259F%2598%259B").Unbox()
2927
if path != string(filepath.Separator)+"\U0001F61B" {
3028
t.Error(path)
3129
}
3230
}
3331

3432
func TestPathToUri(t *testing.T) {
35-
var uri lsp.DocumentURI
33+
var uri DocumentURI
3634
if runtime.GOOS == "windows" {
37-
uri = pathToURI("C:\\Users\\test\\Sketch.ino")
35+
uri = NewDocumentURI("C:\\Users\\test\\Sketch.ino")
3836
if uri != "file:///C:/Users/test/Sketch.ino" {
3937
t.Error(uri)
4038
}
4139
} else {
42-
uri = pathToURI("/Users/test/Sketch.ino")
40+
uri = NewDocumentURI("/Users/test/Sketch.ino")
4341
if uri != "file:///Users/test/Sketch.ino" {
4442
t.Error(uri)
4543
}
4644
}
47-
uri = pathToURI("\U0001F61B")
45+
uri = NewDocumentURI("\U0001F61B")
4846
if uri != "file:///%25F0%259F%2598%259B" {
4947
t.Error(uri)
5048
}

0 commit comments

Comments
 (0)