Skip to content

Commit

Permalink
Add goto preview (#2982)
Browse files Browse the repository at this point in the history
  • Loading branch information
jharrilim authored Oct 3, 2022
1 parent 18f6ec7 commit bcba5d6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
39 changes: 31 additions & 8 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,18 +1196,41 @@ pub(super) fn goto_line_number(
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
match event {
PromptEvent::Abort => {
if let Some(line_number) = cx.editor.last_line_number {
goto_line_impl(cx.editor, NonZeroUsize::new(line_number));
let (view, doc) = current!(cx.editor);
view.ensure_cursor_in_view(doc, line_number);
cx.editor.last_line_number = None;
}
return Ok(());
}
PromptEvent::Validate => {
ensure!(!args.is_empty(), "Line number required");
cx.editor.last_line_number = None;
}
PromptEvent::Update => {
if args.is_empty() {
if let Some(line_number) = cx.editor.last_line_number {
// When a user hits backspace and there are no numbers left,
// we can bring them back to their original line
goto_line_impl(cx.editor, NonZeroUsize::new(line_number));
let (view, doc) = current!(cx.editor);
view.ensure_cursor_in_view(doc, line_number);
cx.editor.last_line_number = None;
}
return Ok(());
}
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let line = doc.selection(view.id).primary().cursor_line(text);
cx.editor.last_line_number.get_or_insert(line + 1);
}
}

ensure!(!args.is_empty(), "Line number required");

let line = args[0].parse::<usize>()?;

goto_line_impl(cx.editor, NonZeroUsize::new(line));

let (view, doc) = current!(cx.editor);

view.ensure_cursor_in_view(doc, line);
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ pub struct Editor {
/// The currently applied editor theme. While previewing a theme, the previewed theme
/// is set here.
pub theme: Theme,

pub last_line_number: Option<usize>,
pub status_msg: Option<(Cow<'static, str>, Severity)>,
pub autoinfo: Option<Info>,

Expand Down Expand Up @@ -717,6 +717,7 @@ impl Editor {
syn_loader,
theme_loader,
last_theme: None,
last_line_number: None,
registers: Registers::default(),
clipboard_provider: get_clipboard_provider(),
status_msg: None,
Expand Down

0 comments on commit bcba5d6

Please sign in to comment.