@@ -126,7 +126,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
126
126
}
127
127
128
128
// Handle LSP methods: transform parameters and send to clangd
129
- var uri lsp.DocumentURI
129
+ var inoURI , cppURI lsp.DocumentURI
130
130
131
131
params , err := lsp .ReadParams (req .Method , req .Params )
132
132
if err != nil {
@@ -146,7 +146,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
146
146
147
147
case * lsp.DidOpenTextDocumentParams :
148
148
// method "textDocument/didOpen"
149
- uri = p .TextDocument .URI
149
+ inoURI = p .TextDocument .URI
150
150
log .Printf ("--> didOpen(%s@%d as '%s')" , p .TextDocument .URI , p .TextDocument .Version , p .TextDocument .LanguageID )
151
151
152
152
res , err := handler .didOpen (ctx , p )
@@ -161,7 +161,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
161
161
162
162
case * lsp.DidChangeTextDocumentParams :
163
163
// notification "textDocument/didChange"
164
- uri = p .TextDocument .URI
164
+ inoURI = p .TextDocument .URI
165
165
log .Printf ("--> didChange(%s@%d)" , p .TextDocument .URI , p .TextDocument .Version )
166
166
for _ , change := range p .ContentChanges {
167
167
log .Printf (" > %s -> %s" , change .Range , strconv .Quote (change .Text ))
@@ -186,33 +186,33 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
186
186
187
187
case * lsp.CompletionParams :
188
188
// method: "textDocument/completion"
189
- uri = p .TextDocument .URI
189
+ inoURI = p .TextDocument .URI
190
190
log .Printf ("--> completion(%s:%d:%d)\n " , p .TextDocument .URI , p .Position .Line , p .Position .Character )
191
191
192
192
err = handler .ino2cppTextDocumentPositionParams (& p .TextDocumentPositionParams )
193
193
log .Printf (" --> completion(%s:%d:%d)\n " , p .TextDocument .URI , p .Position .Line , p .Position .Character )
194
194
195
195
case * lsp.CodeActionParams :
196
196
// method "textDocument/codeAction"
197
- uri = p .TextDocument .URI
197
+ inoURI = p .TextDocument .URI
198
198
log .Printf ("--> codeAction(%s:%s)" , p .TextDocument .URI , p .Range .Start )
199
199
200
200
p .TextDocument , err = handler .ino2cppTextDocumentIdentifier (p .TextDocument )
201
201
if err != nil {
202
202
break
203
203
}
204
204
if p .TextDocument .URI .AsPath ().EquivalentTo (handler .buildSketchCpp ) {
205
- p .Range = handler .sketchMapper .InoToCppLSPRange (uri , p .Range )
205
+ p .Range = handler .sketchMapper .InoToCppLSPRange (inoURI , p .Range )
206
206
for index := range p .Context .Diagnostics {
207
207
r := & p .Context .Diagnostics [index ].Range
208
- * r = handler .sketchMapper .InoToCppLSPRange (uri , * r )
208
+ * r = handler .sketchMapper .InoToCppLSPRange (inoURI , * r )
209
209
}
210
210
}
211
211
log .Printf (" --> codeAction(%s:%s)" , p .TextDocument .URI , p .Range .Start )
212
212
213
213
case * lsp.HoverParams :
214
214
// method: "textDocument/hover"
215
- uri = p .TextDocument .URI
215
+ inoURI = p .TextDocument .URI
216
216
doc := & p .TextDocumentPositionParams
217
217
log .Printf ("--> hover(%s:%d:%d)\n " , doc .TextDocument .URI , doc .Position .Line , doc .Position .Character )
218
218
@@ -221,16 +221,24 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
221
221
222
222
case * lsp.DocumentSymbolParams :
223
223
// method "textDocument/documentSymbol"
224
- uri = p .TextDocument .URI
224
+ inoURI = p .TextDocument .URI
225
225
log .Printf ("--> documentSymbol(%s)" , p .TextDocument .URI )
226
226
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
233
- uri = p .TextDocument .URI
241
+ inoURI = p .TextDocument .URI
234
242
p .TextDocument , err = handler .ino2cppTextDocumentIdentifier (p .TextDocument )
235
243
case * lsp.DidCloseTextDocumentParams : // "textDocument/didClose":
236
244
log .Printf ("--X " + req .Method )
@@ -249,32 +257,27 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
249
257
case * lsp.TextDocumentPositionParams : // "textDocument/documentHighlight":
250
258
log .Printf ("--X " + req .Method )
251
259
return nil , nil
252
- uri = p .TextDocument .URI
260
+ inoURI = p .TextDocument .URI
253
261
err = handler .ino2cppTextDocumentPositionParams (p )
254
262
case * lsp.ReferenceParams : // "textDocument/references":
255
263
log .Printf ("--X " + req .Method )
256
264
return nil , nil
257
- uri = p .TextDocument .URI
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
- uri = 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
267
- uri = p .TextDocument .URI
270
+ inoURI = p .TextDocument .URI
268
271
err = handler .ino2cppDocumentRangeFormattingParams (p )
269
272
case * lsp.DocumentOnTypeFormattingParams : // "textDocument/onTypeFormatting":
270
273
log .Printf ("--X " + req .Method )
271
274
return nil , nil
272
- uri = p .TextDocument .URI
275
+ inoURI = p .TextDocument .URI
273
276
err = handler .ino2cppDocumentOnTypeFormattingParams (p )
274
277
case * lsp.RenameParams : // "textDocument/rename":
275
278
log .Printf ("--X " + req .Method )
276
279
return nil , nil
277
- uri = p .TextDocument .URI
280
+ inoURI = p .TextDocument .URI
278
281
err = handler .ino2cppRenameParams (p )
279
282
case * lsp.DidChangeWatchedFilesParams : // "workspace/didChangeWatchedFiles":
280
283
log .Printf ("--X " + req .Method )
@@ -324,7 +327,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
324
327
325
328
// Transform and return the result
326
329
if result != nil {
327
- result = handler .transformClangdResult (req .Method , uri , result )
330
+ result = handler .transformClangdResult (req .Method , inoURI , cppURI , result )
328
331
}
329
332
return result , err
330
333
}
@@ -416,7 +419,7 @@ func (handler *InoHandler) refreshCppDocumentSymbols() error {
416
419
if err != nil {
417
420
return errors .WithMessage (err , "quering source code symbols" )
418
421
}
419
- result = handler .transformClangdResult ("textDocument/documentSymbol" , cppURI , result )
422
+ result = handler .transformClangdResult ("textDocument/documentSymbol" , cppURI , "" , result )
420
423
if symbols , ok := result .([]lsp.DocumentSymbol ); ! ok {
421
424
return errors .WithMessage (err , "quering source code symbols (2)" )
422
425
} else {
@@ -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 )
@@ -817,8 +829,8 @@ func (handler *InoHandler) ino2cppWorkspaceEdit(origEdit *lsp.WorkspaceEdit) *ls
817
829
return & newEdit
818
830
}
819
831
820
- func (handler * InoHandler ) transformClangdResult (method string , uri lsp.DocumentURI , result interface {}) interface {} {
821
- cppToIno := uri != "" && uri .AsPath ().EquivalentTo (handler .buildSketchCpp )
832
+ func (handler * InoHandler ) transformClangdResult (method string , inoURI , cppURI lsp.DocumentURI , result interface {}) interface {} {
833
+ cppToIno := inoURI != "" && inoURI .AsPath ().EquivalentTo (handler .buildSketchCpp )
822
834
823
835
switch r := result .(type ) {
824
836
case * lsp.Hover :
@@ -853,7 +865,7 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
853
865
854
866
if r .DocumentSymbolArray != nil {
855
867
// Treat the input as []DocumentSymbol
856
- return handler .cpp2inoDocumentSymbols (* r .DocumentSymbolArray , uri )
868
+ return handler .cpp2inoDocumentSymbols (* r .DocumentSymbolArray , inoURI )
857
869
} else if r .SymbolInformationArray != nil {
858
870
// Treat the input as []SymbolInformation
859
871
return handler .cpp2inoSymbolInformation (* r .SymbolInformationArray )
@@ -873,11 +885,35 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
873
885
}
874
886
(* r )[i ] = lsp.CommandOrCodeAction {
875
887
Command : handler .Cpp2InoCommand (item .Command ),
876
- CodeAction : handler .cpp2inoCodeAction (item .CodeAction , uri ),
888
+ CodeAction : handler .cpp2inoCodeAction (item .CodeAction , inoURI ),
877
889
}
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":
@@ -890,15 +926,7 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
890
926
}
891
927
case * []lsp.DocumentHighlight : // "textDocument/documentHighlight":
892
928
for index := range * r {
893
- handler .cpp2inoDocumentHighlight (& (* r )[index ], uri )
894
- }
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 ], uri )
929
+ handler .cpp2inoDocumentHighlight (& (* r )[index ], inoURI )
902
930
}
903
931
case * lsp.WorkspaceEdit : // "textDocument/rename":
904
932
return handler .cpp2inoWorkspaceEdit (r )
@@ -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