From d97f803e55bc58029be25c28e0610c9e28c92aa0 Mon Sep 17 00:00:00 2001 From: Kirawi <67773714+kirawi@users.noreply.github.com> Date: Fri, 15 Mar 2024 12:38:22 -0400 Subject: [PATCH] Add a yank diagnostic command (#9640) * yank diagnostic command * improve success message * move to a typed command * docgen --- book/src/generated/typable-cmd.md | 1 + helix-term/src/commands/typed.rs | 47 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index 59aa8c212598..9636a5424926 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -88,3 +88,4 @@ | `:move` | Move the current buffer and its corresponding file to a different path | | `:yank-diagnostic` | Yank diagnostic(s) under primary cursor to register, or clipboard by default | | `:echo` | Print the processed input to the editor status | +| `:yank-diagnostic` | Yank diagnostic(s) under primary cursor to register, or clipboard by default | diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 364c1080b6ab..f8635978236e 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -2466,6 +2466,46 @@ fn echo(cx: &mut compositor::Context, args: &[Cow], event: PromptEvent) -> Ok(()) } +fn yank_diagnostic( + cx: &mut compositor::Context, + args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + let reg = match args.first() { + Some(s) => { + ensure!(s.chars().count() == 1, format!("Invalid register {s}")); + s.chars().next().unwrap() + } + None => '+', + }; + + let (view, doc) = current_ref!(cx.editor); + let primary = doc.selection(view.id).primary(); + + // Look only for diagnostics that intersect with the primary selection + let diag: Vec<_> = doc + .diagnostics() + .iter() + .filter(|d| primary.overlaps(&helix_core::Range::new(d.range.start, d.range.end))) + .map(|d| d.message.clone()) + .collect(); + let n = diag.len(); + if n == 0 { + bail!("No diagnostics under primary selection"); + } + + cx.editor.registers.write(reg, diag)?; + cx.editor.set_status(format!( + "Yanked {n} diagnostic{} to register {reg}", + if n == 1 { "" } else { "s" } + )); + Ok(()) +} + pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ TypableCommand { name: "quit", @@ -3087,6 +3127,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ fun: echo, signature: CommandSignature::all(completers::variables) }, + TypableCommand { + name: "yank-diagnostic", + aliases: &[], + doc: "Yank diagnostic(s) under primary cursor to register, or clipboard by default", + fun: yank_diagnostic, + signature: CommandSignature::all(completers::register), + }, ]; pub static TYPABLE_COMMAND_MAP: Lazy> =