@@ -705,33 +705,33 @@ func (ls *INOLanguageServer) TextDocumentCodeActionReqFromIDE(ctx context.Contex
705705 return inoResp , nil
706706}
707707
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 ) {
709709 ls .readLock (logger , true )
710710 defer ls .readUnlock (logger )
711711
712- inoTextDocument := inoParams .TextDocument
713- inoURI := inoTextDocument .URI
714- logger .Logf ("--> formatting(%s)" , inoTextDocument )
712+ ideTextDocument := ideParams .TextDocument
713+ ideURI := ideTextDocument .URI
715714
716- cppTextDocument , err := ls .ide2ClangTextDocumentIdentifier (logger , inoTextDocument )
715+ clangTextDocument , err := ls .ide2ClangTextDocumentIdentifier (logger , ideTextDocument )
717716 if err != nil {
718717 logger .Logf ("Error: %s" , err )
719718 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
720719 }
721- cppURI := cppTextDocument .URI
722-
723- logger .Logf (" --> formatting(%s)" , cppTextDocument )
720+ clangURI := clangTextDocument .URI
724721
725- if cleanup , e := ls .createClangdFormatterConfig (logger , cppURI ); e != nil {
722+ if cleanup , err := ls .createClangdFormatterConfig (logger , clangURI ); err != nil {
726723 logger .Logf ("Error: %s" , err )
727724 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
728725 } else {
729726 defer cleanup ()
730727 }
731728
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 )
735735 if err != nil {
736736 logger .Logf ("clangd connectiono error: %v" , err )
737737 ls .Close ()
@@ -742,44 +742,49 @@ func (ls *INOLanguageServer) TextDocumentFormattingReqFromIDE(ctx context.Contex
742742 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : clangErr .AsError ().Error ()}
743743 }
744744
745- if cppEdits == nil {
745+ if clangEdits == nil {
746746 return nil , nil
747747 }
748748
749- sketchEdits , err := ls .cpp2inoTextEdits (logger , cppURI , cppEdits )
749+ ideEdits , err := ls .cland2IdeTextEdits (logger , clangURI , clangEdits )
750750 if err != nil {
751751 logger .Logf ("ERROR converting textEdits: %s" , err )
752752 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
753753 }
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 {
755757 return []lsp.TextEdit {}, nil
756758 } else {
757759 return inoEdits , nil
758760 }
759761}
760762
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 ) {
762764 ls .readLock (logger , true )
763765 defer ls .readUnlock (logger )
764766
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 )
769769 if err != nil {
770770 logger .Logf ("Error: %s" , err )
771771 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
772772 }
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 {
776781 logger .Logf ("cannot create formatter config file: %v" , err )
777782 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
778783 } else {
779784 defer cleanup ()
780785 }
781786
782- cppEdits , clangErr , err := ls .Clangd .conn .TextDocumentRangeFormatting (ctx , cppParams )
787+ clangEdits , clangErr , err := ls .Clangd .conn .TextDocumentRangeFormatting (ctx , clangParams )
783788 if err != nil {
784789 logger .Logf ("clangd connectiono error: %v" , err )
785790 ls .Close ()
@@ -790,17 +795,18 @@ func (ls *INOLanguageServer) TextDocumentRangeFormattingReqFromIDE(ctx context.C
790795 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : clangErr .AsError ().Error ()}
791796 }
792797
793- // Transform and return the result
794- if cppEdits != nil {
798+ if clangEdits == nil {
795799 return nil , nil
796800 }
797801
798- sketchEdits , err := ls .cpp2inoTextEdits (logger , cppURI , cppEdits )
802+ sketchEdits , err := ls .cland2IdeTextEdits (logger , clangURI , clangEdits )
799803 if err != nil {
800804 logger .Logf ("ERROR converting textEdits: %s" , err )
801805 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
802806 }
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 {
804810 return []lsp.TextEdit {}, nil
805811 } else {
806812 return inoEdits , nil
@@ -1235,21 +1241,6 @@ func (ls *INOLanguageServer) cpp2inoLocationArray(logger jsonrpc.FunctionLogger,
12351241 return inoLocations , nil
12361242}
12371243
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-
12531244func (ls * INOLanguageServer ) cpp2inoCodeAction (logger jsonrpc.FunctionLogger , codeAction lsp.CodeAction , uri lsp.DocumentURI ) lsp.CodeAction {
12541245 inoCodeAction := lsp.CodeAction {
12551246 Title : codeAction .Title ,
@@ -1353,33 +1344,6 @@ func (ls *INOLanguageServer) cpp2inoLocation(logger jsonrpc.FunctionLogger, cppL
13531344 }, inPreprocessed , err
13541345}
13551346
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-
13831347func (ls * INOLanguageServer ) cpp2inoTextEdit (logger jsonrpc.FunctionLogger , cppURI lsp.DocumentURI , cppEdit lsp.TextEdit ) (lsp.DocumentURI , lsp.TextEdit , bool , error ) {
13841348 inoURI , inoRange , inPreprocessed , err := ls .clang2IdeRangeAndDocumentURI (logger , cppURI , cppEdit .Range )
13851349 inoEdit := cppEdit
0 commit comments