@@ -227,6 +227,14 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
227
227
p .TextDocument , err = handler .ino2cppTextDocumentIdentifier (p .TextDocument )
228
228
log .Printf (" --> documentSymbol(%s)" , p .TextDocument .URI )
229
229
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
+
230
238
case * lsp.DidSaveTextDocumentParams : // "textDocument/didSave":
231
239
log .Printf ("--X " + req .Method )
232
240
return nil , nil
@@ -256,11 +264,6 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
256
264
return nil , nil
257
265
inoURI = p .TextDocument .URI
258
266
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 )
264
267
case * lsp.DocumentRangeFormattingParams : // "textDocument/rangeFormatting":
265
268
log .Printf ("--X " + req .Method )
266
269
return nil , nil
@@ -702,8 +705,17 @@ func (handler *InoHandler) cpp2inoDocumentURI(cppURI lsp.DocumentURI, cppRange l
702
705
// Convert build path to sketch path
703
706
cppPath := cppURI .AsPath ()
704
707
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
707
719
}
708
720
709
721
inside , err := cppPath .IsInsideDir (handler .buildSketchRoot )
@@ -878,6 +890,30 @@ func (handler *InoHandler) transformClangdResult(method string, inoURI, cppURI l
878
890
}
879
891
log .Printf ("<-- codeAction(%d elements)" , len (* r ))
880
892
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
+
881
917
// case "textDocument/definition":
882
918
// fallthrough
883
919
// case "textDocument/typeDefinition":
@@ -892,14 +928,6 @@ func (handler *InoHandler) transformClangdResult(method string, inoURI, cppURI l
892
928
for index := range * r {
893
929
handler .cpp2inoDocumentHighlight (& (* r )[index ], inoURI )
894
930
}
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
- }
903
931
case * lsp.WorkspaceEdit : // "textDocument/rename":
904
932
return handler .cpp2inoWorkspaceEdit (r )
905
933
case * []lsp.SymbolInformation : // "workspace/symbol":
@@ -1013,11 +1041,28 @@ func (handler *InoHandler) cpp2inoDocumentHighlight(highlight *lsp.DocumentHighl
1013
1041
// }
1014
1042
}
1015
1043
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
1021
1066
}
1022
1067
1023
1068
func (handler * InoHandler ) cpp2inoDocumentSymbols (origSymbols []lsp.DocumentSymbol , origURI lsp.DocumentURI ) []lsp.DocumentSymbol {
0 commit comments