Skip to content

Commit

Permalink
refactor code action edit
Browse files Browse the repository at this point in the history
  • Loading branch information
aspeddro committed Apr 25, 2024
1 parent 3eb0e28 commit 8174604
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 112 deletions.
13 changes: 13 additions & 0 deletions analysis/src/CodeActions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@ let stringifyCodeActions codeActions =

let make ~title ~kind ~edit ~command =
{Protocol.title; codeActionKind = kind; edit; command}

let makeEdit edits uri =
Protocol.
{
documentChanges =
[
{
textDocument =
{version = None; uri = uri |> Uri.fromPath |> Uri.toString};
edits;
};
];
}
119 changes: 7 additions & 112 deletions analysis/src/Xform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,7 @@ module IfThenElse = struct
let codeAction =
CodeActions.make ~title:"Replace with switch" ~kind:RefactorRewrite
~command:None
~edit:
(Some
Protocol.
{
documentChanges =
[
{
textDocument =
{
version = None;
uri = path |> Uri.fromPath |> Uri.toString;
};
edits = [{newText; range}];
};
];
})
~edit:(Some (CodeActions.makeEdit [{newText; range}] path))
in
codeActions := codeAction :: !codeActions
end
Expand Down Expand Up @@ -193,22 +178,7 @@ module AddBracesToFn = struct
let codeAction =
CodeActions.make ~title:"Add braces to function" ~kind:RefactorRewrite
~command:None
~edit:
(Some
Protocol.
{
documentChanges =
[
{
textDocument =
{
version = None;
uri = path |> Uri.fromPath |> Uri.toString;
};
edits = [{newText; range}];
};
];
})
~edit:(Some (CodeActions.makeEdit [{newText; range}] path))
in
codeActions := codeAction :: !codeActions
end
Expand Down Expand Up @@ -282,22 +252,7 @@ module AddTypeAnnotation = struct
let codeAction =
CodeActions.make ~title:"Add type annotation" ~kind:RefactorRewrite
~command:None
~edit:
(Some
Protocol.
{
documentChanges =
[
{
textDocument =
{
version = None;
uri = path |> Uri.fromPath |> Uri.toString;
};
edits = [{newText; range}];
};
];
})
~edit:(Some (CodeActions.makeEdit [{newText; range}] path))
in
codeActions := codeAction :: !codeActions
| _ -> ()))
Expand Down Expand Up @@ -434,22 +389,7 @@ module ExhaustiveSwitch = struct
let codeAction =
CodeActions.make ~command:None ~title:"Exhaustive switch"
~kind:RefactorRewrite
~edit:
(Some
Protocol.
{
documentChanges =
[
{
textDocument =
{
version = None;
uri = path |> Uri.fromPath |> Uri.toString;
};
edits = [{newText; range}];
};
];
})
~edit:(Some (CodeActions.makeEdit [{newText; range}] path))
in
codeActions := codeAction :: !codeActions))
| Some (Switch {switchExpr; completionExpr; pos}) -> (
Expand All @@ -476,22 +416,7 @@ module ExhaustiveSwitch = struct
let codeAction =
CodeActions.make ~title:"Exhaustive switch" ~command:None
~kind:RefactorRewrite (* ~uri:path ~newText ~range *)
~edit:
(Some
Protocol.
{
documentChanges =
[
{
textDocument =
{
version = None;
uri = path |> Uri.fromPath |> Uri.toString;
};
edits = [{newText; range}];
};
];
})
~edit:(Some (CodeActions.makeEdit [{newText; range}] path))
in
codeActions := codeAction :: !codeActions))
end
Expand Down Expand Up @@ -589,22 +514,7 @@ module AddDocTemplate = struct
let codeAction =
CodeActions.make ~title:"Add Documentation template"
~kind:RefactorRewrite ~command:None
~edit:
(Some
Protocol.
{
documentChanges =
[
{
textDocument =
{
version = None;
uri = path |> Uri.fromPath |> Uri.toString;
};
edits = [{newText; range}];
};
];
})
~edit:(Some (CodeActions.makeEdit [{newText; range}] path))
in
codeActions := codeAction :: !codeActions
| None -> ())
Expand Down Expand Up @@ -690,22 +600,7 @@ module AddDocTemplate = struct
let codeAction =
CodeActions.make ~title:"Add Documentation template"
~kind:RefactorRewrite ~command:None
~edit:
(Some
Protocol.
{
documentChanges =
[
{
textDocument =
{
version = None;
uri = path |> Uri.fromPath |> Uri.toString;
};
edits = [{newText; range}];
};
];
})
~edit:(Some (CodeActions.makeEdit [{newText; range}] path))
in
codeActions := codeAction :: !codeActions
| None -> ())
Expand Down

0 comments on commit 8174604

Please sign in to comment.