@@ -58,8 +58,8 @@ type InoHandler struct {
5858 sketchName string
5959 sketchMapper * sourcemapper.InoMapper
6060 sketchTrackedFilesCount int
61- docs map [lsp. DocumentURI ]* lsp.TextDocumentItem
62- inoDocsWithDiagnostics map [lsp. DocumentURI ]bool
61+ docs map [string ]* lsp.TextDocumentItem
62+ inoDocsWithDiagnostics map [string ]bool
6363
6464 config lsp.BoardConfig
6565 synchronizer Synchronizer
@@ -68,8 +68,8 @@ type InoHandler struct {
6868// NewInoHandler creates and configures an InoHandler.
6969func NewInoHandler (stdio io.ReadWriteCloser , board lsp.Board ) * InoHandler {
7070 handler := & InoHandler {
71- docs : map [lsp. DocumentURI ]* lsp.TextDocumentItem {},
72- inoDocsWithDiagnostics : map [lsp. DocumentURI ]bool {},
71+ docs : map [string ]* lsp.TextDocumentItem {},
72+ inoDocsWithDiagnostics : map [string ]bool {},
7373 config : lsp.BoardConfig {
7474 SelectedBoard : board ,
7575 },
@@ -542,7 +542,7 @@ func startClangd(compileCommandsDir, sketchCpp *paths.Path) (io.WriteCloser, io.
542542func (handler * InoHandler ) didOpen (inoDidOpen * lsp.DidOpenTextDocumentParams ) (* lsp.DidOpenTextDocumentParams , error ) {
543543 // Add the TextDocumentItem in the tracked files list
544544 inoItem := inoDidOpen .TextDocument
545- handler .docs [inoItem .URI ] = & inoItem
545+ handler .docs [inoItem .URI . Canonical () ] = & inoItem
546546
547547 // If we are tracking a .ino...
548548 if inoItem .URI .Ext () == ".ino" {
@@ -566,8 +566,8 @@ func (handler *InoHandler) didOpen(inoDidOpen *lsp.DidOpenTextDocumentParams) (*
566566
567567func (handler * InoHandler ) didClose (inoDidClose * lsp.DidCloseTextDocumentParams ) (* lsp.DidCloseTextDocumentParams , error ) {
568568 inoIdentifier := inoDidClose .TextDocument
569- if _ , exist := handler .docs [inoIdentifier .URI ]; exist {
570- delete (handler .docs , inoIdentifier .URI )
569+ if _ , exist := handler .docs [inoIdentifier .URI . Canonical () ]; exist {
570+ delete (handler .docs , inoIdentifier .URI . Canonical () )
571571 } else {
572572 log .Printf (" didClose of untracked document: %s" , inoIdentifier .URI )
573573 return nil , unknownURI (inoIdentifier .URI )
@@ -602,8 +602,8 @@ func (handler *InoHandler) ino2cppTextDocumentItem(inoItem lsp.TextDocumentItem)
602602 cppItem .Text = handler .sketchMapper .CppText .Text
603603 cppItem .Version = handler .sketchMapper .CppText .Version
604604 } else {
605- cppItem .Text = handler .docs [inoItem .URI ].Text
606- cppItem .Version = handler .docs [inoItem .URI ].Version
605+ cppItem .Text = handler .docs [inoItem .URI . Canonical () ].Text
606+ cppItem .Version = handler .docs [inoItem .URI . Canonical () ].Version
607607 }
608608
609609 return cppItem , nil
@@ -612,7 +612,7 @@ func (handler *InoHandler) ino2cppTextDocumentItem(inoItem lsp.TextDocumentItem)
612612func (handler * InoHandler ) didChange (ctx context.Context , req * lsp.DidChangeTextDocumentParams ) (* lsp.DidChangeTextDocumentParams , error ) {
613613 doc := req .TextDocument
614614
615- trackedDoc , ok := handler .docs [doc .URI ]
615+ trackedDoc , ok := handler .docs [doc .URI . Canonical () ]
616616 if ! ok {
617617 return nil , unknownURI (doc .URI )
618618 }
@@ -766,6 +766,23 @@ func (handler *InoHandler) ino2cppDocumentURI(inoURI lsp.DocumentURI) (lsp.Docum
766766 return lsp .NilURI , err
767767}
768768
769+ func (handler * InoHandler ) inoDocumentURIFromInoPath (inoPath string ) (lsp.DocumentURI , error ) {
770+ if inoPath == sourcemapper .NotIno .File {
771+ return sourcemapper .NotInoURI , nil
772+ }
773+ doc , ok := handler .docs [inoPath ]
774+ if ! ok {
775+ log .Printf (" !!! Unresolved .ino path: %s" , inoPath )
776+ log .Printf (" !!! Known doc paths are:" )
777+ for p := range handler .docs {
778+ log .Printf (" !!! > %s" , p )
779+ }
780+ uri := lsp .NewDocumentURI (inoPath )
781+ return uri , unknownURI (uri )
782+ }
783+ return doc .URI , nil
784+ }
785+
769786func (handler * InoHandler ) cpp2inoDocumentURI (cppURI lsp.DocumentURI , cppRange lsp.Range ) (lsp.DocumentURI , lsp.Range , error ) {
770787 // Sketchbook/Sketch/Sketch.ino <- build-path/sketch/Sketch.ino.cpp
771788 // Sketchbook/Sketch/AnotherTab.ino <- build-path/sketch/Sketch.ino.cpp (different section from above)
@@ -784,16 +801,19 @@ func (handler *InoHandler) cpp2inoDocumentURI(cppURI lsp.DocumentURI, cppRange l
784801 } else {
785802 log .Printf (" URI: ERROR: %s" , err )
786803 handler .sketchMapper .DebugLogAll ()
804+ return lsp .NilURI , lsp .NilRange , err
787805 }
788- return lsp .NewDocumentURI (inoPath ), inoRange , err
806+ inoURI , err := handler .inoDocumentURIFromInoPath (inoPath )
807+ return inoURI , inoRange , err
789808 }
790809
791810 inside , err := cppPath .IsInsideDir (handler .buildSketchRoot )
792811 if err != nil {
793812 log .Printf (" could not determine if '%s' is inside '%s'" , cppPath , handler .buildSketchRoot )
794- return lsp .NilURI , lsp.Range {} , err
813+ return lsp .NilURI , lsp .NilRange , err
795814 }
796815 if ! inside {
816+ log .Printf (" '%s' is not inside '%s'" , cppPath , handler .buildSketchRoot )
797817 log .Printf (" keep doc identifier to '%s' as-is" , cppPath )
798818 return cppURI , cppRange , nil
799819 }
@@ -806,7 +826,7 @@ func (handler *InoHandler) cpp2inoDocumentURI(cppURI lsp.DocumentURI, cppRange l
806826 }
807827
808828 log .Printf (" could not determine rel-path of '%s' in '%s': %s" , cppPath , handler .buildSketchRoot , err )
809- return lsp .NilURI , lsp.Range {} , err
829+ return lsp .NilURI , lsp .NilRange , err
810830}
811831
812832func (handler * InoHandler ) ino2cppTextDocumentPositionParams (inoParams * lsp.TextDocumentPositionParams ) (* lsp.TextDocumentPositionParams , error ) {
@@ -1111,36 +1131,41 @@ func (handler *InoHandler) Cpp2InoCommand(command *lsp.Command) *lsp.Command {
11111131 return inoCommand
11121132}
11131133
1114- func (handler * InoHandler ) cpp2inoWorkspaceEdit (origWorkspaceEdit * lsp.WorkspaceEdit ) * lsp.WorkspaceEdit {
1115- if origWorkspaceEdit == nil {
1134+ func (handler * InoHandler ) cpp2inoWorkspaceEdit (cppWorkspaceEdit * lsp.WorkspaceEdit ) * lsp.WorkspaceEdit {
1135+ if cppWorkspaceEdit == nil {
11161136 return nil
11171137 }
1118- resWorkspaceEdit := & lsp.WorkspaceEdit {
1138+ inoWorkspaceEdit := & lsp.WorkspaceEdit {
11191139 Changes : map [lsp.DocumentURI ][]lsp.TextEdit {},
11201140 }
1121- for editURI , edits := range origWorkspaceEdit .Changes {
1141+ for editURI , edits := range cppWorkspaceEdit .Changes {
11221142 // if the edits are not relative to sketch file...
11231143 if ! editURI .AsPath ().EquivalentTo (handler .buildSketchCpp ) {
11241144 // ...pass them through...
1125- resWorkspaceEdit .Changes [editURI ] = edits
1145+ inoWorkspaceEdit .Changes [editURI ] = edits
11261146 continue
11271147 }
11281148
11291149 // ...otherwise convert edits to the sketch.ino.cpp into multilpe .ino edits
11301150 for _ , edit := range edits {
1131- cppRange := edit .Range
1132- inoFile , inoRange := handler .sketchMapper .CppToInoRange (cppRange )
1133- inoURI := lsp .NewDocumentURI (inoFile )
1134- if _ , have := resWorkspaceEdit .Changes [inoURI ]; ! have {
1135- resWorkspaceEdit .Changes [inoURI ] = []lsp.TextEdit {}
1151+ inoURI , inoRange , err := handler .cpp2inoDocumentURI (editURI , edit .Range )
1152+ if err != nil {
1153+ log .Printf (" error converting edit %s:%s: %s" , editURI , edit .Range , err )
1154+ continue
1155+ }
1156+ //inoFile, inoRange := handler.sketchMapper.CppToInoRange(edit.Range)
1157+ //inoURI := lsp.NewDocumentURI(inoFile)
1158+ if _ , have := inoWorkspaceEdit .Changes [inoURI ]; ! have {
1159+ inoWorkspaceEdit .Changes [inoURI ] = []lsp.TextEdit {}
11361160 }
1137- resWorkspaceEdit .Changes [inoURI ] = append (resWorkspaceEdit .Changes [inoURI ], lsp.TextEdit {
1161+ inoWorkspaceEdit .Changes [inoURI ] = append (inoWorkspaceEdit .Changes [inoURI ], lsp.TextEdit {
11381162 NewText : edit .NewText ,
11391163 Range : inoRange ,
11401164 })
11411165 }
11421166 }
1143- return resWorkspaceEdit
1167+ log .Printf (" done converting workspaceEdit" )
1168+ return inoWorkspaceEdit
11441169}
11451170
11461171func (handler * InoHandler ) cpp2inoLocation (cppLocation lsp.Location ) (lsp.Location , error ) {
@@ -1260,7 +1285,7 @@ func (handler *InoHandler) cpp2inoDiagnostics(cppDiags *lsp.PublishDiagnosticsPa
12601285 return []* lsp.PublishDiagnosticsParams {}, nil
12611286 }
12621287
1263- inoURI , _ , err := handler .cpp2inoDocumentURI (cppDiags .URI , lsp.Range {} )
1288+ inoURI , _ , err := handler .cpp2inoDocumentURI (cppDiags .URI , lsp .NilRange )
12641289 return []* lsp.PublishDiagnosticsParams {
12651290 {
12661291 URI : inoURI ,
@@ -1332,9 +1357,9 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
13321357 }
13331358
13341359 // Push back to IDE the converted diagnostics
1335- inoDocsWithDiagnostics := map [lsp. DocumentURI ]bool {}
1360+ inoDocsWithDiagnostics := map [string ]bool {}
13361361 for _ , inoDiag := range inoDiagnostics {
1337- if inoDiag .URI == lsp . NewDocumentURI ( sourcemapper .NotIno . File ) {
1362+ if inoDiag .URI . String () == sourcemapper .NotInoURI . String ( ) {
13381363 cleanUpInoDiagnostics = true
13391364 continue
13401365 }
@@ -1350,7 +1375,7 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
13501375 // check for newly created symbols (that in turn may trigger a
13511376 // new arduino-preprocessing of the sketch).
13521377 if inoDiag .URI .Ext () == ".ino" {
1353- inoDocsWithDiagnostics [inoDiag .URI ] = true
1378+ inoDocsWithDiagnostics [inoDiag .URI . Canonical () ] = true
13541379 cleanUpInoDiagnostics = true
13551380 for _ , diag := range inoDiag .Diagnostics {
13561381 if diag .Code == "undeclared_var_use_suggest" || diag .Code == "undeclared_var_use" {
@@ -1366,14 +1391,14 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
13661391
13671392 if cleanUpInoDiagnostics {
13681393 // Remove diagnostics from all .ino where there are no errors coming from clang
1369- for sourceURI := range handler .inoDocsWithDiagnostics {
1370- if inoDocsWithDiagnostics [sourceURI ] {
1394+ for sourcePath := range handler .inoDocsWithDiagnostics {
1395+ if inoDocsWithDiagnostics [sourcePath ] {
13711396 // skip if we already sent updated diagnostics
13721397 continue
13731398 }
13741399 // otherwise clear previous diagnostics
13751400 msg := lsp.PublishDiagnosticsParams {
1376- URI : sourceURI ,
1401+ URI : lsp . NewDocumentURI ( sourcePath ) ,
13771402 Diagnostics : []lsp.Diagnostic {},
13781403 }
13791404 if enableLogging {
0 commit comments