@@ -705,33 +705,33 @@ func (ls *INOLanguageServer) TextDocumentCodeActionReqFromIDE(ctx context.Contex
705
705
return inoResp , nil
706
706
}
707
707
708
- func (ls * INOLanguageServer ) TextDocumentFormattingReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , inoParams * lsp.DocumentFormattingParams ) ([]lsp.TextEdit , * jsonrpc.ResponseError ) {
708
+ func (ls * INOLanguageServer ) TextDocumentFormattingReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , ideParams * lsp.DocumentFormattingParams ) ([]lsp.TextEdit , * jsonrpc.ResponseError ) {
709
709
ls .readLock (logger , true )
710
710
defer ls .readUnlock (logger )
711
711
712
- inoTextDocument := inoParams .TextDocument
713
- inoURI := inoTextDocument .URI
714
- logger .Logf ("--> formatting(%s)" , inoTextDocument )
712
+ ideTextDocument := ideParams .TextDocument
713
+ ideURI := ideTextDocument .URI
715
714
716
- cppTextDocument , err := ls .ide2ClangTextDocumentIdentifier (logger , inoTextDocument )
715
+ clangTextDocument , err := ls .ide2ClangTextDocumentIdentifier (logger , ideTextDocument )
717
716
if err != nil {
718
717
logger .Logf ("Error: %s" , err )
719
718
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
720
719
}
721
- cppURI := cppTextDocument .URI
722
-
723
- logger .Logf (" --> formatting(%s)" , cppTextDocument )
720
+ clangURI := clangTextDocument .URI
724
721
725
- if cleanup , e := ls .createClangdFormatterConfig (logger , cppURI ); e != nil {
722
+ if cleanup , err := ls .createClangdFormatterConfig (logger , clangURI ); err != nil {
726
723
logger .Logf ("Error: %s" , err )
727
724
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
728
725
} else {
729
726
defer cleanup ()
730
727
}
731
728
732
- cppParams := * inoParams
733
- cppParams .TextDocument = cppTextDocument
734
- cppEdits , clangErr , err := ls .Clangd .conn .TextDocumentFormatting (ctx , & cppParams )
729
+ clangParams := & lsp.DocumentFormattingParams {
730
+ WorkDoneProgressParams : ideParams .WorkDoneProgressParams ,
731
+ Options : ideParams .Options ,
732
+ TextDocument : clangTextDocument ,
733
+ }
734
+ clangEdits , clangErr , err := ls .Clangd .conn .TextDocumentFormatting (ctx , clangParams )
735
735
if err != nil {
736
736
logger .Logf ("clangd connectiono error: %v" , err )
737
737
ls .Close ()
@@ -742,44 +742,49 @@ func (ls *INOLanguageServer) TextDocumentFormattingReqFromIDE(ctx context.Contex
742
742
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : clangErr .AsError ().Error ()}
743
743
}
744
744
745
- if cppEdits == nil {
745
+ if clangEdits == nil {
746
746
return nil , nil
747
747
}
748
748
749
- sketchEdits , err := ls .cpp2inoTextEdits (logger , cppURI , cppEdits )
749
+ ideEdits , err := ls .cland2IdeTextEdits (logger , clangURI , clangEdits )
750
750
if err != nil {
751
751
logger .Logf ("ERROR converting textEdits: %s" , err )
752
752
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
753
753
}
754
- if inoEdits , ok := sketchEdits [inoURI ]; ! ok {
754
+
755
+ // Edits may span over multiple .ino files, filter only the edits relative to the currently displayed file
756
+ if inoEdits , ok := ideEdits [ideURI ]; ! ok {
755
757
return []lsp.TextEdit {}, nil
756
758
} else {
757
759
return inoEdits , nil
758
760
}
759
761
}
760
762
761
- func (ls * INOLanguageServer ) TextDocumentRangeFormattingReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , inoParams * lsp.DocumentRangeFormattingParams ) ([]lsp.TextEdit , * jsonrpc.ResponseError ) {
763
+ func (ls * INOLanguageServer ) TextDocumentRangeFormattingReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , ideParams * lsp.DocumentRangeFormattingParams ) ([]lsp.TextEdit , * jsonrpc.ResponseError ) {
762
764
ls .readLock (logger , true )
763
765
defer ls .readUnlock (logger )
764
766
765
- // Method: "textDocument/rangeFormatting"
766
- logger .Logf ("%s" , inoParams .TextDocument )
767
- inoURI := inoParams .TextDocument .URI
768
- cppParams , err := ls .ide2ClangDocumentRangeFormattingParams (logger , inoParams )
767
+ ideURI := ideParams .TextDocument .URI
768
+ clangURI , clangRange , err := ls .ide2ClangRange (logger , ideURI , ideParams .Range )
769
769
if err != nil {
770
770
logger .Logf ("Error: %s" , err )
771
771
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
772
772
}
773
- cppURI := cppParams .TextDocument .URI
774
- logger .Logf ("-> %s" , cppParams .TextDocument )
775
- if cleanup , e := ls .createClangdFormatterConfig (logger , cppURI ); e != nil {
773
+ clangParams := & lsp.DocumentRangeFormattingParams {
774
+ WorkDoneProgressParams : ideParams .WorkDoneProgressParams ,
775
+ Options : ideParams .Options ,
776
+ TextDocument : lsp.TextDocumentIdentifier {URI : clangURI },
777
+ Range : clangRange ,
778
+ }
779
+
780
+ if cleanup , e := ls .createClangdFormatterConfig (logger , clangURI ); e != nil {
776
781
logger .Logf ("cannot create formatter config file: %v" , err )
777
782
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
778
783
} else {
779
784
defer cleanup ()
780
785
}
781
786
782
- cppEdits , clangErr , err := ls .Clangd .conn .TextDocumentRangeFormatting (ctx , cppParams )
787
+ clangEdits , clangErr , err := ls .Clangd .conn .TextDocumentRangeFormatting (ctx , clangParams )
783
788
if err != nil {
784
789
logger .Logf ("clangd connectiono error: %v" , err )
785
790
ls .Close ()
@@ -790,17 +795,18 @@ func (ls *INOLanguageServer) TextDocumentRangeFormattingReqFromIDE(ctx context.C
790
795
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : clangErr .AsError ().Error ()}
791
796
}
792
797
793
- // Transform and return the result
794
- if cppEdits != nil {
798
+ if clangEdits == nil {
795
799
return nil , nil
796
800
}
797
801
798
- sketchEdits , err := ls .cpp2inoTextEdits (logger , cppURI , cppEdits )
802
+ sketchEdits , err := ls .cland2IdeTextEdits (logger , clangURI , clangEdits )
799
803
if err != nil {
800
804
logger .Logf ("ERROR converting textEdits: %s" , err )
801
805
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
802
806
}
803
- if inoEdits , ok := sketchEdits [inoURI ]; ! ok {
807
+
808
+ // Edits may span over multiple .ino files, filter only the edits relative to the currently displayed file
809
+ if inoEdits , ok := sketchEdits [ideURI ]; ! ok {
804
810
return []lsp.TextEdit {}, nil
805
811
} else {
806
812
return inoEdits , nil
@@ -1235,21 +1241,6 @@ func (ls *INOLanguageServer) cpp2inoLocationArray(logger jsonrpc.FunctionLogger,
1235
1241
return inoLocations , nil
1236
1242
}
1237
1243
1238
- func (ls * INOLanguageServer ) ide2ClangDocumentRangeFormattingParams (logger jsonrpc.FunctionLogger , ideParams * lsp.DocumentRangeFormattingParams ) (* lsp.DocumentRangeFormattingParams , error ) {
1239
- clangTextDocumentIdentifier , err := ls .ide2ClangTextDocumentIdentifier (logger , ideParams .TextDocument )
1240
- if err != nil {
1241
- return nil , err
1242
- }
1243
-
1244
- _ , clangRange , err := ls .ide2ClangRange (logger , ideParams .TextDocument .URI , ideParams .Range )
1245
- return & lsp.DocumentRangeFormattingParams {
1246
- WorkDoneProgressParams : ideParams .WorkDoneProgressParams ,
1247
- Options : ideParams .Options ,
1248
- TextDocument : clangTextDocumentIdentifier ,
1249
- Range : clangRange ,
1250
- }, err
1251
- }
1252
-
1253
1244
func (ls * INOLanguageServer ) cpp2inoCodeAction (logger jsonrpc.FunctionLogger , codeAction lsp.CodeAction , uri lsp.DocumentURI ) lsp.CodeAction {
1254
1245
inoCodeAction := lsp.CodeAction {
1255
1246
Title : codeAction .Title ,
@@ -1353,33 +1344,6 @@ func (ls *INOLanguageServer) cpp2inoLocation(logger jsonrpc.FunctionLogger, cppL
1353
1344
}, inPreprocessed , err
1354
1345
}
1355
1346
1356
- func (ls * INOLanguageServer ) cpp2inoTextEdits (logger jsonrpc.FunctionLogger , cppURI lsp.DocumentURI , cppEdits []lsp.TextEdit ) (map [lsp.DocumentURI ][]lsp.TextEdit , error ) {
1357
- logger .Logf ("%s cpp/textEdit (%d elements)" , cppURI , len (cppEdits ))
1358
- allInoEdits := map [lsp.DocumentURI ][]lsp.TextEdit {}
1359
- for _ , cppEdit := range cppEdits {
1360
- logger .Logf (" > %s -> %s" , cppEdit .Range , strconv .Quote (cppEdit .NewText ))
1361
- inoURI , inoEdit , inPreprocessed , err := ls .cpp2inoTextEdit (logger , cppURI , cppEdit )
1362
- if err != nil {
1363
- return nil , err
1364
- }
1365
- if inPreprocessed {
1366
- logger .Logf (("ignoring in-preprocessed-section edit" ))
1367
- continue
1368
- }
1369
- allInoEdits [inoURI ] = append (allInoEdits [inoURI ], inoEdit )
1370
- }
1371
-
1372
- logger .Logf ("converted to:" )
1373
-
1374
- for inoURI , inoEdits := range allInoEdits {
1375
- logger .Logf ("-> %s ino/textEdit (%d elements)" , inoURI , len (inoEdits ))
1376
- for _ , inoEdit := range inoEdits {
1377
- logger .Logf (" > %s -> %s" , inoEdit .Range , strconv .Quote (inoEdit .NewText ))
1378
- }
1379
- }
1380
- return allInoEdits , nil
1381
- }
1382
-
1383
1347
func (ls * INOLanguageServer ) cpp2inoTextEdit (logger jsonrpc.FunctionLogger , cppURI lsp.DocumentURI , cppEdit lsp.TextEdit ) (lsp.DocumentURI , lsp.TextEdit , bool , error ) {
1384
1348
inoURI , inoRange , inPreprocessed , err := ls .clang2IdeRangeAndDocumentURI (logger , cppURI , cppEdit .Range )
1385
1349
inoEdit := cppEdit
0 commit comments