Clear jumplist implementation

pull/9215/head
Jackson 11 months ago
parent a6ed104ea2
commit bd4d1466ac

@ -84,5 +84,6 @@
| `:run-shell-command`, `:sh` | Run a shell command | | `:run-shell-command`, `:sh` | Run a shell command |
| `:reset-diff-change`, `:diffget`, `:diffg` | Reset the diff change at the cursor position. | | `:reset-diff-change`, `:diffget`, `:diffg` | Reset the diff change at the cursor position. |
| `:clear-register` | Clear given register. If no argument is provided, clear all registers. | | `:clear-register` | Clear given register. If no argument is provided, clear all registers. |
| `:clear-jumplist`, `:clj` | Pops items from the jump list. If not argument is provided, clear the jump list. |
| `:redraw` | Clear and re-render the whole UI | | `:redraw` | Clear and re-render the whole UI |
| `:move` | Move the current buffer and its corresponding file to a different path | | `:move` | Move the current buffer and its corresponding file to a different path |

@ -2385,6 +2385,32 @@ fn clear_register(
Ok(()) Ok(())
} }
fn clear_jumplist(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
ensure!(args.len() <= 1, ":clear-jumplist takes at most 1 argument");
let doc = doc!(cx.editor);
let view = view_mut!(cx.editor);
let selection = doc.selection(view.id);
if args.len() == 1 {
let to_remove = args[0].parse::<usize>()?;
view.jumps.remove_from_head(to_remove);
} else {
view.jumps.clear();
}
if view.jumps.len() == 0 {
// Fixup cases where the jumplist is emptied into an invalid state
view.jumps.push((doc.id(), selection.clone()));
}
Ok(())
}
fn redraw( fn redraw(
cx: &mut compositor::Context, cx: &mut compositor::Context,
_args: &[Cow<str>], _args: &[Cow<str>],
@ -3075,6 +3101,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: clear_register, fun: clear_register,
signature: CommandSignature::none(), signature: CommandSignature::none(),
}, },
TypableCommand {
name: "clear-jumplist",
aliases: &["clj"],
doc: "Pops items from the jump list. If not argument is provided, clear the jump list.",
fun: clear_jumplist,
signature: CommandSignature::none(),
},
TypableCommand { TypableCommand {
name: "redraw", name: "redraw",
aliases: &[], aliases: &[],

@ -84,6 +84,23 @@ impl JumpList {
self.jumps.iter() self.jumps.iter()
} }
pub fn remove_from_head(&mut self, to_remove: usize) {
for _ in 0..to_remove {
if self.jumps.pop_front().is_none() {
break;
}
}
self.current = self.jumps.len();
}
pub fn clear(&mut self) {
self.jumps.clear();
}
pub fn len(&self) -> usize {
self.jumps.len()
}
/// Applies a [`Transaction`] of changes to the jumplist. /// Applies a [`Transaction`] of changes to the jumplist.
/// This is necessary to ensure that changes to documents do not leave jump-list /// This is necessary to ensure that changes to documents do not leave jump-list
/// selections pointing to parts of the text which no longer exist. /// selections pointing to parts of the text which no longer exist.

Loading…
Cancel
Save