forked from facebook/hhvm
-
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.
introduce lazy code action resolution (codeAction/resolve)
Summary: This diff adds infra for lazily-resolved code actions, and uses the infra for the "flip around comma" code action. This change is a no-op from the user's point of view. The point of these changes are to make a performant "extract into method" refactor feasible. Since "extract into method" uses libhackfmt under the hood when generating edits, it is best to generate these edits lazily. I implement lazy code actions for "flip around comma" first (in this diff) because it's easier to demonstrate lazy code action resolution with a pre-existing refactor and also the integration test is easier to read for a more straightforward refactor. A subsequent diff in this stack also uses the infra here for the "extract into method" refactor. ## LSP background A "lazily-resolved code action" is a code action with this flow (LSP protocol): - client sends [`textDocument/codeAction`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_codeAction) - server replies with a partially-resolved code action with no `edit` nor `command` field and a `data` field with the shape of its choosing - client sends a [`codeAction/resolve`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeAction_resolve) request with the partially-resolved code action including the `data` field ## Goals of the design I designed lazy code action resolution to be: 1. Easy to use: for a particular refactor, all one has to to be lazy is replace `EditOnly (compute_edit args))` with `UnresolvedEdit (lazy (compute_edit args))`. No need to have a custom protocol for each refactoring. See `CodeActionsServiceFlipAroundComma.ml`. 2. Correct: we ensure the logic for finding the title and location of the code action is identical when handling the `textDocument/codeAction` request compared to the `codeAction/Resolve` request. 3. Type-safe: we distinguish at the type level between resolved and unresolved code actions. In addition, code actions have a well-typed `data` field. These are in contrast to the types in the LSP spec, which allow more kinds of invalid states to be represented. ## Design The LSP protocol is such that the `codeAction/resolve` handler has access to the original code action returned from `textDocument/resolve`, including a custom `data` field. We set `data` to be the original parameters to the `codeAction/resolve` request, which ensures we have enough info to resolve the rest of the code action regardless of the specific refactoring we are implementing (see goals 1-3). A specific refactoring implements a single `find` function that is used regardless of whether the request is `textDocument/codeAction` or `codeAction/resolve` (see goals 1 and 2). CodeActionsService then does slightly different things depending on the request. The following is type-safe due to a sparing use of a phantom type param (see goal 3 and lsp.mli): - For `textDocument/codeAction`, CodeActionsService has to transform the code action to remove functional values so it's easy to marshal the code action across our internal-to-the-language-server IPC stuff. This is just the trivial transformation from `UnresolvedEdit lazy_edit` to `UnresolvedEdit ()`. - For `codeAction/resolve`, CodeActionsService finds the (possibly-unresolved) code action matching the code action to resolve based on the request params and title. CodeActionService then converts `UnresolvedEdit lazy_edit` to `EditOnly edit` by forcing the edit. For a `CodeAction.resolved_command_or_action`, `UnresolvedEdit` is uninhabitable, so we guarantee that `codeAction/resolve` returns a resolved code action. Reviewed By: ljw1004 Differential Revision: D45828606 fbshipit-source-id: 8bc1f253141fa00d9bcced5b65f0f7f30b0e7fa1
- Loading branch information
1 parent
720c7bb
commit df2e31d
Showing
26 changed files
with
569 additions
and
95 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
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
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
(* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the "hack" directory of this source tree. | ||
* | ||
*) | ||
val find : | ||
range:Lsp.range -> | ||
path:string -> | ||
entry:Provider_context.entry -> | ||
Provider_context.t -> | ||
Lsp.CodeAction.resolvable_command_or_action list |
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
Oops, something went wrong.