mirror of
https://github.com/helix-editor/helix.git
synced 2024-12-03 23:03:31 +04:00
Add 'select_register_history' command to pull items from history
This commit is contained in:
parent
119edfdb03
commit
7cfa548038
@ -529,6 +529,7 @@ pub fn doc(&self) -> &str {
|
||||
command_palette, "Open command palette",
|
||||
goto_word, "Jump to a two-character label",
|
||||
extend_to_word, "Extend to a two-character label",
|
||||
select_register_history, "Select an item from a register's history",
|
||||
);
|
||||
}
|
||||
|
||||
@ -6186,3 +6187,56 @@ fn jump_to_word(cx: &mut Context, behaviour: Movement) {
|
||||
}
|
||||
jump_to_label(cx, words, behaviour)
|
||||
}
|
||||
|
||||
fn select_register_history(cx: &mut Context) {
|
||||
struct HistoryEntry {
|
||||
index: usize,
|
||||
last_value: String,
|
||||
}
|
||||
|
||||
// TODO: only show the registers that support selecting from history.
|
||||
cx.editor.autoinfo = Some(Info::from_registers(&cx.editor.registers));
|
||||
cx.on_next_key(move |cx, event| {
|
||||
cx.editor.autoinfo = None;
|
||||
let Some(register) = event.char() else { return };
|
||||
let Some(history) = cx.editor.registers.history(register) else {
|
||||
cx.editor
|
||||
.set_error(format!("No history for register '{register}'"));
|
||||
return;
|
||||
};
|
||||
|
||||
let items = history.map(|(index, entry)| HistoryEntry {
|
||||
index,
|
||||
last_value: entry
|
||||
.last()
|
||||
.and_then(|s| s.lines().next())
|
||||
.unwrap_or("<empty>")
|
||||
.to_string(),
|
||||
});
|
||||
let columns = vec![
|
||||
PickerColumn::new("entry", |entry: &HistoryEntry, _| {
|
||||
entry.index.to_string().into()
|
||||
}),
|
||||
PickerColumn::new("contents", |entry: &HistoryEntry, _| {
|
||||
entry.last_value.as_str().into()
|
||||
}),
|
||||
];
|
||||
|
||||
let picker = Picker::new(
|
||||
columns,
|
||||
1, // "contents"
|
||||
items,
|
||||
(),
|
||||
move |cx, entry, _action| {
|
||||
if let Err(err) = cx
|
||||
.editor
|
||||
.registers
|
||||
.select_history_entry(register, entry.index)
|
||||
{
|
||||
cx.editor.set_error(err.to_string());
|
||||
}
|
||||
},
|
||||
);
|
||||
cx.push_layer(Box::new(overlaid(picker)));
|
||||
})
|
||||
}
|
||||
|
@ -323,6 +323,7 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
|
||||
},
|
||||
|
||||
"\"" => select_register,
|
||||
"C-r" => select_register_history,
|
||||
"|" => shell_pipe,
|
||||
"A-|" => shell_pipe_to,
|
||||
"!" => shell_insert_output,
|
||||
|
Loading…
Reference in New Issue
Block a user