forked from ocaml/ocaml-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the infer-interface code action and add an update-signature c…
…ode action (ocaml#1289) * improving infer-interface code action and adding update-signature code action
- Loading branch information
1 parent
ec05b1a
commit 6a7dff1
Showing
8 changed files
with
646 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
ocaml-lsp-server/src/code_actions/action_update_signature.ml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
open Import | ||
open Fiber.O | ||
|
||
let action_kind = "update_intf" | ||
|
||
let code_action_of_intf doc text_edits = | ||
let edit : WorkspaceEdit.t = | ||
let doc_edit = | ||
let edits = List.map text_edits ~f:(fun e -> `TextEdit e) in | ||
let textDocument = | ||
let uri = Document.uri doc in | ||
let version = Document.version doc in | ||
OptionalVersionedTextDocumentIdentifier.create ~uri ~version () | ||
in | ||
TextDocumentEdit.create ~textDocument ~edits | ||
in | ||
WorkspaceEdit.create ~documentChanges:[ `TextDocumentEdit doc_edit ] () | ||
in | ||
let title = | ||
String.capitalize_ascii "update signature(s) to match implementation" | ||
in | ||
CodeAction.create | ||
~title | ||
~kind:(CodeActionKind.Other action_kind) | ||
~edit | ||
~isPreferred:false | ||
() | ||
|
||
let code_action (state : State.t) doc (params : CodeActionParams.t) = | ||
match Document.kind doc with | ||
| `Other -> Fiber.return None | ||
| `Merlin m when Document.Merlin.kind m = Impl -> Fiber.return None | ||
| `Merlin intf_merlin -> ( | ||
let* text_edits = | ||
Inference.update_signatures ~state ~doc ~range:params.range ~intf_merlin | ||
in | ||
match text_edits with | ||
| [] -> Fiber.return None | ||
| _ -> Fiber.return (Some (code_action_of_intf doc text_edits))) | ||
|
||
let kind = CodeActionKind.Other action_kind | ||
|
||
let t state = { Code_action.kind; run = `Non_batchable (code_action state) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
val infer_intf_for_impl : Document.t -> string Fiber.t | ||
|
||
(** Called by the code action "insert inferred interface". Gets the Merlin | ||
typer_result for both the implementation and interface documents, and uses | ||
the diff between them to produce the updated interface. Any names present in | ||
the existing interface are omitted from the inserted code (regardless of | ||
whether their signatures have changed). *) | ||
val infer_missing_intf_for_impl : | ||
Document.t (** implementation *) | ||
-> Document.t (** interface *) | ||
-> string Fiber.t | ||
(** code to be inserted in the interface *) | ||
|
||
val infer_intf : State.t -> Document.t -> string option Fiber.t | ||
|
||
(** Called by the code action "update signature(s) to match implementation". | ||
Compares signatures found in the selected range of the interface document | ||
with ones inferred from the corresponding implementation document, and | ||
produces text edits for any that can be updated. *) | ||
val update_signatures : | ||
state:State.t | ||
-> intf_merlin:Document.Merlin.t | ||
-> doc:Document.t | ||
-> range:Range.t | ||
-> Import.TextEdit.t list Fiber.t |
Oops, something went wrong.