Skip to content

Commit

Permalink
move to a typed command
Browse files Browse the repository at this point in the history
  • Loading branch information
kirawi committed Mar 15, 2024
1 parent f586557 commit 6edf0e8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
28 changes: 0 additions & 28 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,48 @@ fn move_buffer(
Ok(())
}

fn yank_diagnostic(
cx: &mut compositor::Context,
args: &[Cow<str>],
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",
Expand Down Expand Up @@ -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<HashMap<&'static str, &'static TypableCommand>> =
Expand Down

0 comments on commit 6edf0e8

Please sign in to comment.