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
|
@ -234,6 +234,19 @@ pub(crate) struct InlayHints {
|
|||
pub range: Range<Anchor>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub(crate) struct GetCodeLens;
|
||||
|
||||
impl GetCodeLens {
|
||||
pub(crate) fn can_resolve_lens(capabilities: &ServerCapabilities) -> bool {
|
||||
capabilities
|
||||
.code_lens_provider
|
||||
.as_ref()
|
||||
.and_then(|code_lens_options| code_lens_options.resolve_provider)
|
||||
.unwrap_or(false)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct LinkedEditingRange {
|
||||
pub position: Anchor,
|
||||
|
@ -2229,18 +2242,18 @@ impl LspCommand for GetCodeActions {
|
|||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.filter_map(|entry| {
|
||||
let lsp_action = match entry {
|
||||
let (lsp_action, resolved) = match entry {
|
||||
lsp::CodeActionOrCommand::CodeAction(lsp_action) => {
|
||||
if let Some(command) = lsp_action.command.as_ref() {
|
||||
if !available_commands.contains(&command.command) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
LspAction::Action(Box::new(lsp_action))
|
||||
(LspAction::Action(Box::new(lsp_action)), false)
|
||||
}
|
||||
lsp::CodeActionOrCommand::Command(command) => {
|
||||
if available_commands.contains(&command.command) {
|
||||
LspAction::Command(command)
|
||||
(LspAction::Command(command), true)
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
|
@ -2259,6 +2272,7 @@ impl LspCommand for GetCodeActions {
|
|||
server_id,
|
||||
range: self.range.clone(),
|
||||
lsp_action,
|
||||
resolved,
|
||||
})
|
||||
})
|
||||
.collect())
|
||||
|
@ -3037,6 +3051,152 @@ impl LspCommand for InlayHints {
|
|||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl LspCommand for GetCodeLens {
|
||||
type Response = Vec<CodeAction>;
|
||||
type LspRequest = lsp::CodeLensRequest;
|
||||
type ProtoRequest = proto::GetCodeLens;
|
||||
|
||||
fn display_name(&self) -> &str {
|
||||
"Code Lens"
|
||||
}
|
||||
|
||||
fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
|
||||
capabilities
|
||||
.server_capabilities
|
||||
.code_lens_provider
|
||||
.as_ref()
|
||||
.map_or(false, |code_lens_options| {
|
||||
code_lens_options.resolve_provider.unwrap_or(false)
|
||||
})
|
||||
}
|
||||
|
||||
fn to_lsp(
|
||||
&self,
|
||||
path: &Path,
|
||||
_: &Buffer,
|
||||
_: &Arc<LanguageServer>,
|
||||
_: &App,
|
||||
) -> Result<lsp::CodeLensParams> {
|
||||
Ok(lsp::CodeLensParams {
|
||||
text_document: lsp::TextDocumentIdentifier {
|
||||
uri: file_path_to_lsp_url(path)?,
|
||||
},
|
||||
work_done_progress_params: lsp::WorkDoneProgressParams::default(),
|
||||
partial_result_params: lsp::PartialResultParams::default(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn response_from_lsp(
|
||||
self,
|
||||
message: Option<Vec<lsp::CodeLens>>,
|
||||
lsp_store: Entity<LspStore>,
|
||||
buffer: Entity<Buffer>,
|
||||
server_id: LanguageServerId,
|
||||
mut cx: AsyncApp,
|
||||
) -> anyhow::Result<Vec<CodeAction>> {
|
||||
let snapshot = buffer.update(&mut cx, |buffer, _| buffer.snapshot())?;
|
||||
let language_server = cx.update(|cx| {
|
||||
lsp_store
|
||||
.read(cx)
|
||||
.language_server_for_id(server_id)
|
||||
.with_context(|| {
|
||||
format!("Missing the language server that just returned a response {server_id}")
|
||||
})
|
||||
})??;
|
||||
let server_capabilities = language_server.capabilities();
|
||||
let available_commands = server_capabilities
|
||||
.execute_command_provider
|
||||
.as_ref()
|
||||
.map(|options| options.commands.as_slice())
|
||||
.unwrap_or_default();
|
||||
Ok(message
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.filter(|code_lens| {
|
||||
code_lens
|
||||
.command
|
||||
.as_ref()
|
||||
.is_none_or(|command| available_commands.contains(&command.command))
|
||||
})
|
||||
.map(|code_lens| {
|
||||
let code_lens_range = range_from_lsp(code_lens.range);
|
||||
let start = snapshot.clip_point_utf16(code_lens_range.start, Bias::Left);
|
||||
let end = snapshot.clip_point_utf16(code_lens_range.end, Bias::Right);
|
||||
let range = snapshot.anchor_before(start)..snapshot.anchor_after(end);
|
||||
CodeAction {
|
||||
server_id,
|
||||
range,
|
||||
lsp_action: LspAction::CodeLens(code_lens),
|
||||
resolved: false,
|
||||
}
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetCodeLens {
|
||||
proto::GetCodeLens {
|
||||
project_id,
|
||||
buffer_id: buffer.remote_id().into(),
|
||||
version: serialize_version(&buffer.version()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn from_proto(
|
||||
message: proto::GetCodeLens,
|
||||
_: Entity<LspStore>,
|
||||
buffer: Entity<Buffer>,
|
||||
mut cx: AsyncApp,
|
||||
) -> Result<Self> {
|
||||
buffer
|
||||
.update(&mut cx, |buffer, _| {
|
||||
buffer.wait_for_version(deserialize_version(&message.version))
|
||||
})?
|
||||
.await?;
|
||||
Ok(Self)
|
||||
}
|
||||
|
||||
fn response_to_proto(
|
||||
response: Vec<CodeAction>,
|
||||
_: &mut LspStore,
|
||||
_: PeerId,
|
||||
buffer_version: &clock::Global,
|
||||
_: &mut App,
|
||||
) -> proto::GetCodeLensResponse {
|
||||
proto::GetCodeLensResponse {
|
||||
lens_actions: response
|
||||
.iter()
|
||||
.map(LspStore::serialize_code_action)
|
||||
.collect(),
|
||||
version: serialize_version(buffer_version),
|
||||
}
|
||||
}
|
||||
|
||||
async fn response_from_proto(
|
||||
self,
|
||||
message: proto::GetCodeLensResponse,
|
||||
_: Entity<LspStore>,
|
||||
buffer: Entity<Buffer>,
|
||||
mut cx: AsyncApp,
|
||||
) -> anyhow::Result<Vec<CodeAction>> {
|
||||
buffer
|
||||
.update(&mut cx, |buffer, _| {
|
||||
buffer.wait_for_version(deserialize_version(&message.version))
|
||||
})?
|
||||
.await?;
|
||||
message
|
||||
.lens_actions
|
||||
.into_iter()
|
||||
.map(LspStore::deserialize_code_action)
|
||||
.collect::<Result<Vec<_>>>()
|
||||
.context("deserializing proto code lens response")
|
||||
}
|
||||
|
||||
fn buffer_id_from_proto(message: &proto::GetCodeLens) -> Result<BufferId> {
|
||||
BufferId::new(message.buffer_id)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl LspCommand for LinkedEditingRange {
|
||||
type Response = Vec<Range<Anchor>>;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue