From 66858971495dc05634cd1cb2ad58bd59c8924d38 Mon Sep 17 00:00:00 2001 From: kirawi <67773714+kirawi@users.noreply.github.com> Date: Thu, 15 Feb 2024 21:05:34 -0500 Subject: [PATCH 1/4] yank diagnostic command --- helix-term/src/commands.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index d44f477b7376..e8bb7e107ead 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -388,6 +388,7 @@ 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", @@ -5519,6 +5520,24 @@ 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 = doc + .diagnostics() + .iter() + .filter(|d| primary.overlaps(&helix_core::Range::new(d.range.start, d.range.end))) + .map(|d| d.message.clone()); + let reg = cx.register.unwrap_or('+'); + match cx.editor.registers.write(reg, diag.collect()) { + Ok(_) => cx + .editor + .set_status(format!("Yanked diagnostic(s) to register {reg}")), + 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 From f5865571e0345d59f68702d15576313e6821212b Mon Sep 17 00:00:00 2001 From: kirawi <67773714+kirawi@users.noreply.github.com> Date: Sun, 18 Feb 2024 12:04:32 -0500 Subject: [PATCH 2/4] improve success message --- helix-term/src/commands.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index e8bb7e107ead..0d2b8c2e32b0 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -5524,16 +5524,25 @@ fn yank_diagnostic(cx: &mut Context) { let (view, doc) = current_ref!(cx.editor); let primary = doc.selection(view.id).primary(); - let diag = doc + 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()); + .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.collect()) { - Ok(_) => cx - .editor - .set_status(format!("Yanked diagnostic(s) to register {reg}")), + 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()), } } From b810691499c9641815bf04f7804ea2e5b9503de4 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 3/4] move to a typed command --- helix-term/src/commands.rs | 28 ------------------- helix-term/src/commands/typed.rs | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 0d2b8c2e32b0..d44f477b7376 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 b7ceeba59a18..0720303ae858 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -2414,6 +2414,46 @@ 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 => '+', + }; + + 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", @@ -3021,6 +3061,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> = From a03c313835a709582c5285b7a48db63844b51035 Mon Sep 17 00:00:00 2001 From: kirawi <67773714+kirawi@users.noreply.github.com> Date: Fri, 15 Mar 2024 11:49:35 -0400 Subject: [PATCH 4/4] docgen --- book/src/generated/typable-cmd.md | 1 + 1 file changed, 1 insertion(+) diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index f4fcb6f6200e..dbb8b5f380d2 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -86,3 +86,4 @@ | `:clear-register` | Clear given register. If no argument is provided, clear all registers. | | `:redraw` | Clear and re-render the whole UI | | `: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 |