Skip to content

Commit bfab2ad

Browse files
committed
Refactor to allow more code actions later.
1 parent 1d2e768 commit bfab2ad

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

analysis/src/Commands.ml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,16 +356,25 @@ let test ~path =
356356
SemanticTokens.command ~debug:true
357357
~emitter:(SemanticTokens.Token.createEmitter ())
358358
~path
359-
| "xfm" -> (
359+
| "xfm" ->
360360
print_endline
361361
("Xform " ^ path ^ " " ^ string_of_int line ^ ":"
362362
^ string_of_int col);
363-
match Xform.command ~currentFile:path ~pos:(line, col) with
364-
| Some (range, newText) ->
365-
Printf.printf "Hit IfThenElse %s newText:\n%s\n"
366-
(Protocol.stringifyRange range)
367-
newText
368-
| None -> ())
363+
let codeActions =
364+
Xform.extractCodeActions ~path ~pos:(line, col) ~currentFile:path
365+
in
366+
codeActions
367+
|> List.iter (fun codeAction ->
368+
match codeAction with
369+
| {
370+
Protocol.title;
371+
edit = {documentChanges = [{edits = [{range; newText}]}]};
372+
} ->
373+
Printf.printf "Hit %s %s\nnewText:\n%s\n" title
374+
(Protocol.stringifyRange range)
375+
newText
376+
| _ -> assert false
377+
(* TODO *))
369378
| _ -> ());
370379
print_newline ())
371380
in

analysis/src/Xform.ml

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,18 @@ let diff ~filename ~newContents =
168168
let newLines = List.rev newR in
169169
(firstLineDifferent, lastLineEqual, newLines)
170170

171-
let command ~currentFile ~pos =
172-
if Filename.check_suffix currentFile ".res" then
173-
let structure, printExpr = parse ~filename:currentFile in
174-
match IfThenElse.xform ~pos structure with
175-
| None -> None
176-
| Some newExpr ->
177-
let range = rangeOfLoc newExpr.pexp_loc in
178-
let newText = printExpr ~range newExpr in
179-
Some (range, newText)
180-
else None
181-
182171
let extractCodeActions ~path ~pos ~currentFile =
183-
match command ~currentFile ~pos with
184-
| Some (range, newText) ->
185-
[
186-
CodeActions.make ~title:"Replace with switch" ~kind:RefactorRewrite
187-
~uri:path ~newText ~range;
188-
]
189-
| None -> []
172+
let codeActions = ref [] in
173+
(if Filename.check_suffix currentFile ".res" then
174+
let structure, printExpr = parse ~filename:currentFile in
175+
match IfThenElse.xform ~pos structure with
176+
| None -> ()
177+
| Some newExpr ->
178+
let range = rangeOfLoc newExpr.pexp_loc in
179+
let newText = printExpr ~range newExpr in
180+
let codeAction =
181+
CodeActions.make ~title:"Replace with switch" ~kind:RefactorRewrite
182+
~uri:path ~newText ~range
183+
in
184+
codeActions := codeAction :: !codeActions);
185+
!codeActions

analysis/tests/src/expected/Xform.res.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Xform tests/src/Xform.res 6:5
2-
Hit IfThenElse {"start": {"line": 6, "character": 0}, "end": {"line": 11, "character": 1}} newText:
2+
Hit Replace with switch {"start": {"line": 6, "character": 0}, "end": {"line": 11, "character": 1}}
3+
newText:
34
switch kind {
45
| First =>
56
// ^xfm
@@ -8,7 +9,8 @@ switch kind {
89
}
910

1011
Xform tests/src/Xform.res 13:15
11-
Hit IfThenElse {"start": {"line": 13, "character": 0}, "end": {"line": 13, "character": 79}} newText:
12+
Hit Replace with switch {"start": {"line": 13, "character": 0}, "end": {"line": 13, "character": 79}}
13+
newText:
1214
switch kind {
1315
| #kind("First", {name: "abc", age: 3}) => ret("First")
1416
| _ => ret("Not First")

0 commit comments

Comments
 (0)