-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathls_ide_to_clang.go
91 lines (82 loc) · 3.28 KB
/
ls_ide_to_clang.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package ls
import (
"github.com/arduino/arduino-language-server/sourcemapper"
"go.bug.st/lsp"
"go.bug.st/lsp/jsonrpc"
)
func (ls *INOLanguageServer) idePathToIdeURI(logger jsonrpc.FunctionLogger, inoPath string) (lsp.DocumentURI, error) {
if inoPath == sourcemapper.NotIno.File {
return sourcemapper.NotInoURI, nil
}
doc, ok := ls.trackedIDEDocs[inoPath]
if !ok {
logger.Logf(" !!! Unresolved .ino path: %s", inoPath)
logger.Logf(" !!! Known doc paths are:")
for p := range ls.trackedIDEDocs {
logger.Logf(" !!! > %s", p)
}
uri := lsp.NewDocumentURI(inoPath)
return uri, unknownURI(uri)
}
return doc.URI, nil
}
func (ls *INOLanguageServer) ide2ClangTextDocumentIdentifier(logger jsonrpc.FunctionLogger, ideTextDocIdentifier lsp.TextDocumentIdentifier) (lsp.TextDocumentIdentifier, error) {
clangURI, err := ls.ide2ClangDocumentURI(logger, ideTextDocIdentifier.URI)
return lsp.TextDocumentIdentifier{URI: clangURI}, err
}
func (ls *INOLanguageServer) ide2ClangDocumentURI(logger jsonrpc.FunctionLogger, ideURI lsp.DocumentURI) (lsp.DocumentURI, error) {
// Sketchbook/Sketch/Sketch.ino -> build-path/sketch/Sketch.ino.cpp
// Sketchbook/Sketch/AnotherTab.ino -> build-path/sketch/Sketch.ino.cpp (different section from above)
idePath := ideURI.AsPath()
if idePath.Ext() == ".ino" {
clangURI := lsp.NewDocumentURIFromPath(ls.buildSketchCpp)
logger.Logf("URI: %s -> %s", ideURI, clangURI)
return clangURI, nil
}
// another/path/source.cpp -> another/path/source.cpp (unchanged)
inside, err := idePath.IsInsideDir(ls.sketchRoot)
if err != nil {
logger.Logf("ERROR: could not determine if '%s' is inside '%s'", idePath, ls.sketchRoot)
return lsp.NilURI, unknownURI(ideURI)
}
if !inside {
clangURI := ideURI
logger.Logf("URI: %s -> %s", ideURI, clangURI)
return clangURI, nil
}
// Sketchbook/Sketch/AnotherFile.cpp -> build-path/sketch/AnotherFile.cpp
rel, err := ls.sketchRoot.RelTo(idePath)
if err != nil {
logger.Logf("ERROR: could not determine rel-path of '%s' in '%s': %s", idePath, ls.sketchRoot, err)
return lsp.NilURI, err
}
clangPath := ls.buildSketchRoot.JoinPath(rel)
clangURI := lsp.NewDocumentURIFromPath(clangPath)
logger.Logf("URI: %s -> %s", ideURI, clangURI)
return clangURI, nil
}
func (ls *INOLanguageServer) ide2ClangTextDocumentPositionParams(logger jsonrpc.FunctionLogger, ideParams lsp.TextDocumentPositionParams) (lsp.TextDocumentPositionParams, error) {
ideTextDocument := ideParams.TextDocument
idePosition := ideParams.Position
ideURI := ideTextDocument.URI
clangTextDocument, err := ls.ide2ClangTextDocumentIdentifier(logger, ideTextDocument)
if err != nil {
logger.Logf("%s -> invalid text document: %s", ideParams, err)
return lsp.TextDocumentPositionParams{}, err
}
clangPosition := idePosition
if ls.clangURIRefersToIno(clangTextDocument.URI) {
if cppLine, ok := ls.sketchMapper.InoToCppLineOk(ideURI, idePosition.Line); ok {
clangPosition.Line = cppLine
} else {
logger.Logf("%s -> invalid line requested: %s:%d", ideParams, ideURI, idePosition.Line)
return lsp.TextDocumentPositionParams{}, unknownURI(ideURI)
}
}
clangParams := lsp.TextDocumentPositionParams{
TextDocument: clangTextDocument,
Position: clangPosition,
}
logger.Logf("%s -> %s", ideParams, clangParams)
return clangParams, nil
}