Use textDocument/codeLens
data in the actions menu when applicable (#26811)
Similar to how tasks are fetched via LSP, also queries for document's code lens and filters the ones with the commands, supported in server capabilities. Whatever's left and applicable to the range given, is added to the actions menu:  This way, Zed can get more actions to run, albeit neither r-a nor vtsls seem to provide anything by default. Currently, there are no plans to render code lens the way as in VSCode, it's just the extra actions that are show in the menu. ------------------ As part of the attempts to use rust-analyzer LSP data about the runnables, I've explored a way to get this data via standard LSP. When particular experimental client capabilities are enabled (similar to how clangd does this now), r-a starts to send back code lens with the data needed to run a cargo command: ``` {"jsonrpc":"2.0","id":48,"result":{"range":{"start":{"line":0,"character":0},"end":{"line":98,"character":0}},"command":{"title":"▶︎ Run Tests","command":"rust-analyzer.runSingle","arguments":[{"label":"test-mod tests::ecparser","location":{"targetUri":"file:///Users/someonetoignore/work/ec4rs/src/tests/ecparser.rs","targetRange":{"start":{"line":0,"character":0},"end":{"line":98,"character":0}},"targetSelectionRange":{"start":{"line":0,"character":0},"end":{"line":98,"character":0}}},"kind":"cargo","args":{"environment":{"RUSTC_TOOLCHAIN":"/Users/someonetoignore/.rustup/toolchains/1.85-aarch64-apple-darwin"},"cwd":"/Users/someonetoignore/work/ec4rs","overrideCargo":null,"workspaceRoot":"/Users/someonetoignore/work/ec4rs","cargoArgs":["test","--package","ec4rs","--lib"],"executableArgs":["tests::ecparser","--show-output"]}}]}}} ``` This data is passed as is to VSCode task processor, registered in60cd01864a/editors/code/src/main.ts (L195)
where it gets eventually executed as a VSCode's task, all handled by the r-a's extension code. rust-analyzer does not declare server capabilities for such tasks, and has no `workspace/executeCommand` handle, and Zed needs an interactive terminal output during the test runs, so we cannot ask rust-analyzer more than these descriptions. Given that Zed needs experimental capabilities set to get these lens:60cd01864a/editors/code/src/client.ts (L318-L327)
and that the lens may contain other odd tasks (e.g. docs opening or references lookup), a protocol extension to get runnables looks more preferred than lens: https://rust-analyzer.github.io/book/contributing/lsp-extensions.html#runnables This PR does not include any work on this direction, limiting to the general code lens support. As a proof of concept, it's possible to get the lens and even attempt to run it, to no avail:  Release Notes: - Used `textDocument/codeLens` data in the actions menu when applicable
This commit is contained in:
parent
0b492c11de
commit
b61171f152
13 changed files with 618 additions and 19 deletions
|
@ -280,6 +280,7 @@ pub enum Event {
|
|||
Reshared,
|
||||
Rejoined,
|
||||
RefreshInlayHints,
|
||||
RefreshCodeLens,
|
||||
RevealInProjectPanel(ProjectEntryId),
|
||||
SnippetEdit(BufferId, Vec<(lsp::Range, Snippet)>),
|
||||
ExpandedAllForEntry(WorktreeId, ProjectEntryId),
|
||||
|
@ -509,6 +510,8 @@ pub struct CodeAction {
|
|||
/// The raw code action provided by the language server.
|
||||
/// Can be either an action or a command.
|
||||
pub lsp_action: LspAction,
|
||||
/// Whether the action needs to be resolved using the language server.
|
||||
pub resolved: bool,
|
||||
}
|
||||
|
||||
/// An action sent back by a language server.
|
||||
|
@ -519,6 +522,8 @@ pub enum LspAction {
|
|||
Action(Box<lsp::CodeAction>),
|
||||
/// A command data to run as an action.
|
||||
Command(lsp::Command),
|
||||
/// A code lens data to run as an action.
|
||||
CodeLens(lsp::CodeLens),
|
||||
}
|
||||
|
||||
impl LspAction {
|
||||
|
@ -526,6 +531,11 @@ impl LspAction {
|
|||
match self {
|
||||
Self::Action(action) => &action.title,
|
||||
Self::Command(command) => &command.title,
|
||||
Self::CodeLens(lens) => lens
|
||||
.command
|
||||
.as_ref()
|
||||
.map(|command| command.title.as_str())
|
||||
.unwrap_or("Unknown command"),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -533,6 +543,7 @@ impl LspAction {
|
|||
match self {
|
||||
Self::Action(action) => action.kind.clone(),
|
||||
Self::Command(_) => Some(lsp::CodeActionKind::new("command")),
|
||||
Self::CodeLens(_) => Some(lsp::CodeActionKind::new("code lens")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -540,6 +551,7 @@ impl LspAction {
|
|||
match self {
|
||||
Self::Action(action) => action.edit.as_ref(),
|
||||
Self::Command(_) => None,
|
||||
Self::CodeLens(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -547,6 +559,7 @@ impl LspAction {
|
|||
match self {
|
||||
Self::Action(action) => action.command.as_ref(),
|
||||
Self::Command(command) => Some(command),
|
||||
Self::CodeLens(lens) => lens.command.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2483,6 +2496,7 @@ impl Project {
|
|||
};
|
||||
}
|
||||
LspStoreEvent::RefreshInlayHints => cx.emit(Event::RefreshInlayHints),
|
||||
LspStoreEvent::RefreshCodeLens => cx.emit(Event::RefreshCodeLens),
|
||||
LspStoreEvent::LanguageServerPrompt(prompt) => {
|
||||
cx.emit(Event::LanguageServerPrompt(prompt.clone()))
|
||||
}
|
||||
|
@ -3163,6 +3177,34 @@ impl Project {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn code_lens<T: Clone + ToOffset>(
|
||||
&mut self,
|
||||
buffer_handle: &Entity<Buffer>,
|
||||
range: Range<T>,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Task<Result<Vec<CodeAction>>> {
|
||||
let snapshot = buffer_handle.read(cx).snapshot();
|
||||
let range = snapshot.anchor_before(range.start)..snapshot.anchor_after(range.end);
|
||||
let code_lens_actions = self
|
||||
.lsp_store
|
||||
.update(cx, |lsp_store, cx| lsp_store.code_lens(buffer_handle, cx));
|
||||
|
||||
cx.background_spawn(async move {
|
||||
let mut code_lens_actions = code_lens_actions.await?;
|
||||
code_lens_actions.retain(|code_lens_action| {
|
||||
range
|
||||
.start
|
||||
.cmp(&code_lens_action.range.start, &snapshot)
|
||||
.is_ge()
|
||||
&& range
|
||||
.end
|
||||
.cmp(&code_lens_action.range.end, &snapshot)
|
||||
.is_le()
|
||||
});
|
||||
Ok(code_lens_actions)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn apply_code_action(
|
||||
&self,
|
||||
buffer_handle: Entity<Buffer>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue