Skip to content

Commit 21d9a7c

Browse files
committed
Use canonical paths everywhere
1 parent 0489a1c commit 21d9a7c

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

Diff for: handler/handler.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ func startClangd(compileCommandsDir, sketchCpp *paths.Path, compilers map[string
731731
func (handler *InoHandler) didOpen(inoDidOpen *lsp.DidOpenTextDocumentParams) (*lsp.DidOpenTextDocumentParams, error) {
732732
// Add the TextDocumentItem in the tracked files list
733733
inoItem := inoDidOpen.TextDocument
734-
handler.docs[inoItem.URI.Canonical()] = &inoItem
734+
handler.docs[inoItem.URI.AsPath().String()] = &inoItem
735735

736736
// If we are tracking a .ino...
737737
if inoItem.URI.Ext() == ".ino" {
@@ -752,8 +752,8 @@ func (handler *InoHandler) didOpen(inoDidOpen *lsp.DidOpenTextDocumentParams) (*
752752

753753
func (handler *InoHandler) didClose(inoDidClose *lsp.DidCloseTextDocumentParams) (*lsp.DidCloseTextDocumentParams, error) {
754754
inoIdentifier := inoDidClose.TextDocument
755-
if _, exist := handler.docs[inoIdentifier.URI.Canonical()]; exist {
756-
delete(handler.docs, inoIdentifier.URI.Canonical())
755+
if _, exist := handler.docs[inoIdentifier.URI.AsPath().String()]; exist {
756+
delete(handler.docs, inoIdentifier.URI.AsPath().String())
757757
} else {
758758
log.Printf(" didClose of untracked document: %s", inoIdentifier.URI)
759759
return nil, unknownURI(inoIdentifier.URI)
@@ -789,8 +789,9 @@ func (handler *InoHandler) ino2cppTextDocumentItem(inoItem lsp.TextDocumentItem)
789789
cppItem.Version = handler.sketchMapper.CppText.Version
790790
} else {
791791
cppItem.LanguageID = inoItem.LanguageID
792-
cppItem.Text = handler.docs[inoItem.URI.Canonical()].Text
793-
cppItem.Version = handler.docs[inoItem.URI.Canonical()].Version
792+
inoPath := inoItem.URI.AsPath().String()
793+
cppItem.Text = handler.docs[inoPath].Text
794+
cppItem.Version = handler.docs[inoPath].Version
794795
}
795796

796797
return cppItem, nil
@@ -799,7 +800,7 @@ func (handler *InoHandler) ino2cppTextDocumentItem(inoItem lsp.TextDocumentItem)
799800
func (handler *InoHandler) didChange(ctx context.Context, req *lsp.DidChangeTextDocumentParams) (*lsp.DidChangeTextDocumentParams, error) {
800801
doc := req.TextDocument
801802

802-
trackedDoc, ok := handler.docs[doc.URI.Canonical()]
803+
trackedDoc, ok := handler.docs[doc.URI.AsPath().String()]
803804
if !ok {
804805
return nil, unknownURI(doc.URI)
805806
}
@@ -1405,7 +1406,7 @@ func (handler *InoHandler) cpp2inoTextEdit(cppURI lsp.DocumentURI, cppEdit lsp.T
14051406
}
14061407

14071408
func (handler *InoHandler) cpp2inoDocumentSymbols(cppSymbols []lsp.DocumentSymbol, inoRequestedURI lsp.DocumentURI) []lsp.DocumentSymbol {
1408-
inoRequested := inoRequestedURI.Canonical()
1409+
inoRequested := inoRequestedURI.AsPath().String()
14091410
log.Printf(" filtering for requested ino file: %s", inoRequested)
14101411
if inoRequestedURI.Ext() != ".ino" || len(cppSymbols) == 0 {
14111412
return cppSymbols

Diff for: handler/sourcemapper/ino.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ type InoLine struct {
4343

4444
// InoToCppLine converts a source (.ino) line into a target (.cpp) line
4545
func (s *InoMapper) InoToCppLine(sourceURI lsp.DocumentURI, line int) int {
46-
return s.toCpp[InoLine{sourceURI.Canonical(), line}]
46+
return s.toCpp[InoLine{sourceURI.AsPath().String(), line}]
4747
}
4848

4949
// InoToCppLineOk converts a source (.ino) line into a target (.cpp) line
5050
func (s *InoMapper) InoToCppLineOk(sourceURI lsp.DocumentURI, line int) (int, bool) {
51-
res, ok := s.toCpp[InoLine{sourceURI.Canonical(), line}]
51+
res, ok := s.toCpp[InoLine{sourceURI.AsPath().String(), line}]
5252
return res, ok
5353
}
5454

Diff for: lsp/uri.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,25 @@ var expDriveID = regexp.MustCompile("^/[a-zA-Z]:")
2121

2222
// AsPath convert the DocumentURI to a paths.Path
2323
func (uri DocumentURI) AsPath() *paths.Path {
24-
return paths.New(uri.Unbox())
24+
return paths.New(uri.unbox()).Canonical()
2525
}
2626

27-
// Unbox convert the DocumentURI to a file path string
28-
func (uri DocumentURI) Unbox() string {
27+
// unbox convert the DocumentURI to a file path string
28+
func (uri DocumentURI) unbox() string {
2929
path := uri.url.Path
3030
if expDriveID.MatchString(path) {
3131
return path[1:]
3232
}
3333
return path
3434
}
3535

36-
// Canonical returns the canonical path to the file pointed by the URI
37-
func (uri DocumentURI) Canonical() string {
38-
return uri.AsPath().Canonical().String()
39-
}
40-
4136
func (uri DocumentURI) String() string {
4237
return uri.url.String()
4338
}
4439

4540
// Ext returns the extension of the file pointed by the URI
4641
func (uri DocumentURI) Ext() string {
47-
return filepath.Ext(uri.Unbox())
42+
return filepath.Ext(uri.unbox())
4843
}
4944

5045
// NewDocumentURIFromPath create a DocumentURI from the given Path object

Diff for: lsp/uri_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ import (
1212
func TestUriToPath(t *testing.T) {
1313
d, err := NewDocumentURIFromURL("file:///C:/Users/test/Sketch.ino")
1414
require.NoError(t, err)
15-
require.Equal(t, "C:/Users/test/Sketch.ino", d.Unbox())
15+
require.Equal(t, "C:/Users/test/Sketch.ino", d.unbox())
1616

1717
d, err = NewDocumentURIFromURL("file:///c%3A/Users/test/Sketch.ino")
1818
require.NoError(t, err)
19-
require.Equal(t, "c:/Users/test/Sketch.ino", d.Unbox())
19+
require.Equal(t, "c:/Users/test/Sketch.ino", d.unbox())
2020

2121
d, err = NewDocumentURIFromURL("file:///Users/test/Sketch.ino")
2222
require.NoError(t, err)
23-
require.Equal(t, "/Users/test/Sketch.ino", d.Unbox())
23+
require.Equal(t, "/Users/test/Sketch.ino", d.unbox())
2424

2525
d, err = NewDocumentURIFromURL("file:///c%3A/Users/USERNA~1/AppData/Local/Temp/.arduinoProIDE-unsaved202108-10416-j28c17.lru6k/sketch_jan8a/sketch_jan8a.ino")
2626
require.NoError(t, err)
27-
require.Equal(t, "c:/Users/USERNA~1/AppData/Local/Temp/.arduinoProIDE-unsaved202108-10416-j28c17.lru6k/sketch_jan8a/sketch_jan8a.ino", d.Unbox())
27+
require.Equal(t, "c:/Users/USERNA~1/AppData/Local/Temp/.arduinoProIDE-unsaved202108-10416-j28c17.lru6k/sketch_jan8a/sketch_jan8a.ino", d.unbox())
2828

2929
d, err = NewDocumentURIFromURL("file:///%F0%9F%98%9B")
3030
require.NoError(t, err)
31-
require.Equal(t, "/\U0001F61B", d.Unbox())
31+
require.Equal(t, "/\U0001F61B", d.unbox())
3232
}
3333

3434
func TestPathToUri(t *testing.T) {
@@ -48,11 +48,11 @@ func TestJSONMarshalUnmarshal(t *testing.T) {
4848
var d DocumentURI
4949
err := json.Unmarshal([]byte(`"file:///Users/test/Sketch.ino"`), &d)
5050
require.NoError(t, err)
51-
require.Equal(t, "/Users/test/Sketch.ino", d.Unbox())
51+
require.Equal(t, "/Users/test/Sketch.ino", d.unbox())
5252

5353
err = json.Unmarshal([]byte(`"file:///%F0%9F%98%9B"`), &d)
5454
require.NoError(t, err)
55-
require.Equal(t, "/\U0001F61B", d.Unbox())
55+
require.Equal(t, "/\U0001F61B", d.unbox())
5656

5757
d = NewDocumentURI("C:\\Users\\test\\Sketch.ino")
5858
data, err := json.Marshal(d)
@@ -79,7 +79,7 @@ func TestNotInoFromSourceMapper(t *testing.T) {
7979
d, err := NewDocumentURIFromURL("file:///not-ino")
8080
require.NoError(t, err)
8181
fmt.Println(d.String())
82-
fmt.Println(d.Unbox())
82+
fmt.Println(d.unbox())
8383
}
8484

8585
func windowsToSlash(path string) string {

0 commit comments

Comments
 (0)