-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve the infer-interface code action and add an update-signature code action #1289
Merged
rgrinberg
merged 7 commits into
ocaml:master
from
awilliambauer:improved-infer-interface
May 20, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
710b600
improving infer-interface code action and adding update-signature cod…
awilliambauer b7e8c42
moving new code action file to correct directory
awilliambauer 13096ae
remove dependency on core; fix a couple test differences
awilliambauer 655f795
reverting test differences
awilliambauer bb2217d
_
rgrinberg d4b6d7d
_
rgrinberg 1ce533a
_
rgrinberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to relax
ocaml-lsp
's dependency on theTypedtree
on the long term we should really try to put such utilities inmerlin-lib
instead...Also, I am curious about the reason why classes' ids are not needed for the update-signature action ?