Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear jumplist implementation #9215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@
| `:run-shell-command`, `:sh` | Run a shell command |
| `: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-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 |
| `:move` | Move the current buffer and its corresponding file to a different path |
33 changes: 33 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2385,6 +2385,32 @@ fn clear_register(
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(
cx: &mut compositor::Context,
_args: &[Cow<str>],
Expand Down Expand Up @@ -3075,6 +3101,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: clear_register,
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 {
name: "redraw",
aliases: &[],
Expand Down
17 changes: 17 additions & 0 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ impl JumpList {
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.
/// 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.
Expand Down