@@ -227,6 +227,14 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
227227 p .TextDocument , err = handler .ino2cppTextDocumentIdentifier (p .TextDocument )
228228 log .Printf (" --> documentSymbol(%s)" , p .TextDocument .URI )
229229
230+ case * lsp.DocumentFormattingParams :
231+ // method "textDocument/formatting"
232+ inoURI = p .TextDocument .URI
233+ log .Printf ("--> formatting(%s)" , p .TextDocument .URI )
234+ p .TextDocument , err = handler .ino2cppTextDocumentIdentifier (p .TextDocument )
235+ cppURI = p .TextDocument .URI
236+ log .Printf (" --> formatting(%s)" , p .TextDocument .URI )
237+
230238 case * lsp.DidSaveTextDocumentParams : // "textDocument/didSave":
231239 log .Printf ("--X " + req .Method )
232240 return nil , nil
@@ -256,11 +264,6 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
256264 return nil , nil
257265 inoURI = p .TextDocument .URI
258266 err = handler .ino2cppTextDocumentPositionParams (& p .TextDocumentPositionParams )
259- case * lsp.DocumentFormattingParams : // "textDocument/formatting":
260- log .Printf ("--X " + req .Method )
261- return nil , nil
262- inoURI = p .TextDocument .URI
263- p .TextDocument , err = handler .ino2cppTextDocumentIdentifier (p .TextDocument )
264267 case * lsp.DocumentRangeFormattingParams : // "textDocument/rangeFormatting":
265268 log .Printf ("--X " + req .Method )
266269 return nil , nil
@@ -702,8 +705,17 @@ func (handler *InoHandler) cpp2inoDocumentURI(cppURI lsp.DocumentURI, cppRange l
702705 // Convert build path to sketch path
703706 cppPath := cppURI .AsPath ()
704707 if cppPath .EquivalentTo (handler .buildSketchCpp ) {
705- inoPath , inoRange := handler .sketchMapper .CppToInoRange (cppRange )
706- return lsp .NewDocumentURI (inoPath ), inoRange , nil
708+ inoPath , inoRange , err := handler .sketchMapper .CppToInoRangeOk (cppRange )
709+ if err == nil {
710+ log .Printf (" URI: converted %s to %s:%s" , cppRange , inoPath , inoRange )
711+ } else if _ , ok := err .(sourcemapper.AdjustedRangeErr ); ok {
712+ log .Printf (" URI: converted %s to %s:%s (END LINE ADJUSTED)" , cppRange , inoPath , inoRange )
713+ err = nil
714+ } else {
715+ log .Printf (" URI: ERROR: %s" , err )
716+ handler .sketchMapper .DebugLogAll ()
717+ }
718+ return lsp .NewDocumentURI (inoPath ), inoRange , err
707719 }
708720
709721 inside , err := cppPath .IsInsideDir (handler .buildSketchRoot )
@@ -878,6 +890,30 @@ func (handler *InoHandler) transformClangdResult(method string, inoURI, cppURI l
878890 }
879891 log .Printf ("<-- codeAction(%d elements)" , len (* r ))
880892
893+ case * []lsp.TextEdit :
894+ // Method: "textDocument/rangeFormatting"
895+ // Method: "textDocument/onTypeFormatting"
896+ // Method: "textDocument/formatting"
897+ log .Printf (" <-- %s %s textEdit(%d elements)" , method , cppURI , len (* r ))
898+ for _ , edit := range * r {
899+ log .Printf (" > %s -> %s" , edit .Range , strconv .Quote (edit .NewText ))
900+ }
901+ sketchEdits , err := handler .cpp2inoTextEdits (cppURI , * r )
902+ if err != nil {
903+ log .Printf ("ERROR converting textEdits: %s" , err )
904+ return nil
905+ }
906+
907+ inoEdits , ok := sketchEdits [inoURI ]
908+ if ! ok {
909+ inoEdits = []lsp.TextEdit {}
910+ }
911+ log .Printf ("<-- %s %s textEdit(%d elements)" , method , inoURI , len (inoEdits ))
912+ for _ , edit := range inoEdits {
913+ log .Printf (" > %s -> %s" , edit .Range , strconv .Quote (edit .NewText ))
914+ }
915+ return & inoEdits
916+
881917 // case "textDocument/definition":
882918 // fallthrough
883919 // case "textDocument/typeDefinition":
@@ -892,14 +928,6 @@ func (handler *InoHandler) transformClangdResult(method string, inoURI, cppURI l
892928 for index := range * r {
893929 handler .cpp2inoDocumentHighlight (& (* r )[index ], inoURI )
894930 }
895- // case "textDocument/formatting":
896- // fallthrough
897- // case "textDocument/rangeFormatting":
898- // fallthrough
899- case * []lsp.TextEdit : // "textDocument/onTypeFormatting":
900- for index := range * r {
901- handler .cpp2inoTextEdit (& (* r )[index ], inoURI )
902- }
903931 case * lsp.WorkspaceEdit : // "textDocument/rename":
904932 return handler .cpp2inoWorkspaceEdit (r )
905933 case * []lsp.SymbolInformation : // "workspace/symbol":
@@ -1013,11 +1041,28 @@ func (handler *InoHandler) cpp2inoDocumentHighlight(highlight *lsp.DocumentHighl
10131041 // }
10141042}
10151043
1016- func (handler * InoHandler ) cpp2inoTextEdit (edit * lsp.TextEdit , uri lsp.DocumentURI ) {
1017- panic ("not implemented" )
1018- // if data, ok := handler.data[uri]; ok {
1019- // _, edit.Range = data.sourceMap.CppToInoRange(edit.Range)
1020- // }
1044+ func (handler * InoHandler ) cpp2inoTextEdits (cppURI lsp.DocumentURI , cppEdits []lsp.TextEdit ) (map [lsp.DocumentURI ][]lsp.TextEdit , error ) {
1045+ res := map [lsp.DocumentURI ][]lsp.TextEdit {}
1046+ for _ , cppEdit := range cppEdits {
1047+ inoURI , inoEdit , err := handler .cpp2inoTextEdit (cppURI , cppEdit )
1048+ if err != nil {
1049+ return nil , err
1050+ }
1051+ inoEdits , ok := res [inoURI ]
1052+ if ! ok {
1053+ inoEdits = []lsp.TextEdit {}
1054+ }
1055+ inoEdits = append (inoEdits , inoEdit )
1056+ res [inoURI ] = inoEdits
1057+ }
1058+ return res , nil
1059+ }
1060+
1061+ func (handler * InoHandler ) cpp2inoTextEdit (cppURI lsp.DocumentURI , cppEdit lsp.TextEdit ) (lsp.DocumentURI , lsp.TextEdit , error ) {
1062+ inoURI , inoRange , err := handler .cpp2inoDocumentURI (cppURI , cppEdit .Range )
1063+ inoEdit := cppEdit
1064+ inoEdit .Range = inoRange
1065+ return inoURI , inoEdit , err
10211066}
10221067
10231068func (handler * InoHandler ) cpp2inoDocumentSymbols (origSymbols []lsp.DocumentSymbol , origURI lsp.DocumentURI ) []lsp.DocumentSymbol {
0 commit comments