From 6edf0e82d271867ce2b1b98b19764202c6a74b5d Mon Sep 17 00:00:00 2001 From: kirawi <67773714+kirawi@users.noreply.github.com> Date: Fri, 15 Mar 2024 11:00:14 -0400 Subject: [PATCH] move to a typed command --- helix-term/src/commands.rs | 28 ------------------ helix-term/src/commands/typed.rs | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 0d2b8c2e32b06..d44f477b73764 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -388,7 +388,6 @@ impl MappableCommand { yank_main_selection_to_clipboard, "Yank main selection to clipboard", yank_joined_to_primary_clipboard, "Join and yank selections to primary clipboard", yank_main_selection_to_primary_clipboard, "Yank main selection to primary clipboard", - yank_diagnostic, "Yank diagnostic(s) under primary cursor to register, or clipboard by default", replace_with_yanked, "Replace with yanked text", replace_selections_with_clipboard, "Replace selections by clipboard content", replace_selections_with_primary_clipboard, "Replace selections by primary clipboard", @@ -5520,33 +5519,6 @@ fn increment_impl(cx: &mut Context, increment_direction: IncrementDirection) { } } -fn yank_diagnostic(cx: &mut Context) { - let (view, doc) = current_ref!(cx.editor); - let primary = doc.selection(view.id).primary(); - - 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 { - cx.editor - .set_error("No diagnostics under primary selection"); - return; - } - - let reg = cx.register.unwrap_or('+'); - match cx.editor.registers.write(reg, diag) { - Ok(_) => cx.editor.set_status(format!( - "Yanked {n} diagnostic{} to register {reg}", - if n == 1 { "" } else { "s" } - )), - Err(err) => cx.editor.set_error(err.to_string()), - } -} - fn record_macro(cx: &mut Context) { if let Some((reg, mut keys)) = cx.editor.macro_recording.take() { // Remove the keypress which ends the recording diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index b7ceeba59a180..6f21bef159292 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -2414,6 +2414,48 @@ fn move_buffer( Ok(()) } +fn yank_diagnostic( + cx: &mut compositor::Context, + args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + 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"); + } + + let reg = match args.get(0) { + Some(s) => { + ensure!(s.chars().count() == 1, format!("Invalid register {s}")); + s.chars().next().unwrap() + } + None => '+', + }; + + match cx.editor.registers.write(reg, diag) { + Ok(_) => cx.editor.set_status(format!( + "Yanked {n} diagnostic{} to register {reg}", + if n == 1 { "" } else { "s" } + )), + Err(err) => cx.editor.set_error(err.to_string()), + } + Ok(()) +} + pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ TypableCommand { name: "quit", @@ -3021,6 +3063,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ fun: move_buffer, signature: CommandSignature::positional(&[completers::filename]), }, + TypableCommand { + name: "yank-diagnostic", + aliases: &[], + doc: "Yank diagnostic(s) under primary cursor to register, or clipboard by default", + fun: yank_diagnostic, + signature: CommandSignature::none(), + }, ]; pub static TYPABLE_COMMAND_MAP: Lazy> =