@@ -61,6 +61,45 @@ func (ls *INOLanguageServer) clang2IdeRangeAndDocumentURI(logger jsonrpc.Functio
6161 return ideURI , clangRange , false , err
6262}
6363
64+ func (ls * INOLanguageServer ) clang2IdeDocumentURI (logger jsonrpc.FunctionLogger , clangURI lsp.DocumentURI ) (lsp.DocumentURI , error ) {
65+ // Sketchbook/Sketch/Sketch.ino <-> build-path/sketch/Sketch.ino.cpp
66+ // Sketchbook/Sketch/AnotherTab.ino <-> build-path/sketch/Sketch.ino.cpp (different section from above)
67+ if ls .clangURIRefersToIno (clangURI ) {
68+ // the URI may refer to any .ino, without a range reference pick the first tracked .ino
69+ for _ , ideDoc := range ls .trackedIdeDocs {
70+ if ideDoc .URI .Ext () == ".ino" {
71+ logger .Logf ("%s -> %s" , clangURI , ideDoc .URI )
72+ return ideDoc .URI , nil
73+ }
74+ }
75+ return lsp.DocumentURI {}, & UnknownURI {URI : clangURI }
76+ }
77+
78+ // /another/global/path/to/source.cpp <-> /another/global/path/to/source.cpp
79+ clangPath := clangURI .AsPath ()
80+ inside , err := clangPath .IsInsideDir (ls .buildSketchRoot )
81+ if err != nil {
82+ logger .Logf ("ERROR: could not determine if '%s' is inside '%s'" , clangURI , ls .buildSketchRoot )
83+ return lsp.DocumentURI {}, err
84+ }
85+ if ! inside {
86+ ideURI := clangURI
87+ logger .Logf ("%s -> %s" , clangURI , ideURI )
88+ return ideURI , nil
89+ }
90+
91+ // Sketchbook/Sketch/AnotherFile.cpp <-> build-path/sketch/AnotherFile.cpp
92+ rel , err := ls .buildSketchRoot .RelTo (clangPath )
93+ if err != nil {
94+ logger .Logf ("ERROR: could not transform '%s' into a relative path on '%s': %s" , clangURI , ls .buildSketchRoot , err )
95+ return lsp.DocumentURI {}, err
96+ }
97+ idePath := ls .sketchRoot .JoinPath (rel ).String ()
98+ ideURI , err := ls .idePathToIdeURI (logger , idePath )
99+ logger .Logf ("%s -> %s" , clangURI , ideURI )
100+ return ideURI , nil
101+ }
102+
64103func (ls * INOLanguageServer ) clang2IdeDocumentHighlight (logger jsonrpc.FunctionLogger , clangHighlight lsp.DocumentHighlight , cppURI lsp.DocumentURI ) (lsp.DocumentHighlight , bool , error ) {
65104 _ , ideRange , inPreprocessed , err := ls .clang2IdeRangeAndDocumentURI (logger , cppURI , clangHighlight .Range )
66105 if err != nil || inPreprocessed {
@@ -77,6 +116,21 @@ func (ls *INOLanguageServer) clang2IdeDiagnostics(logger jsonrpc.FunctionLogger,
77116 // so we collect all of the into a map.
78117 allIdeDiagsParams := map [lsp.DocumentURI ]* lsp.PublishDiagnosticsParams {}
79118
119+ // Convert empty diagnostic directly (otherwise they will be missed from the next loop)
120+ if len (clangDiagsParams .Diagnostics ) == 0 {
121+ ideURI , err := ls .clang2IdeDocumentURI (logger , clangDiagsParams .URI )
122+ if err != nil {
123+ return nil , err
124+ }
125+ allIdeDiagsParams [ideURI ] = & lsp.PublishDiagnosticsParams {
126+ URI : ideURI ,
127+ Version : clangDiagsParams .Version ,
128+ Diagnostics : []lsp.Diagnostic {},
129+ }
130+ return allIdeDiagsParams , nil
131+ }
132+
133+ // Collect all diagnostics into different sets
80134 for _ , clangDiagnostic := range clangDiagsParams .Diagnostics {
81135 ideURI , ideDiagnostic , inPreprocessed , err := ls .clang2IdeDiagnostic (logger , clangDiagsParams .URI , clangDiagnostic )
82136 if err != nil {
0 commit comments