Skip to content

Commit

Permalink
Support go to line 1
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhemingi authored and archseer committed Jun 8, 2021
1 parent 4e3a343 commit ae51065
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
65 changes: 33 additions & 32 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use once_cell::sync::Lazy;

pub struct Context<'a> {
pub register: helix_view::RegisterSelection,
pub count: usize,
pub _count: Option<std::num::NonZeroUsize>,
pub editor: &'a mut Editor,

pub callback: Option<crate::compositor::Callback>,
Expand Down Expand Up @@ -97,6 +97,11 @@ impl<'a> Context<'a> {
});
self.callbacks.push(callback);
}

#[inline]
pub fn count(&self) -> usize {
self._count.map_or(1, |v| v.get())
}
}

enum Align {
Expand All @@ -123,7 +128,7 @@ fn align_view(doc: &Document, view: &mut View, align: Align) {
pub type Command = fn(cx: &mut Context);

pub fn move_char_left(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);
let selection = doc.selection(view.id).transform(|range| {
Expand All @@ -139,7 +144,7 @@ pub fn move_char_left(cx: &mut Context) {
}

pub fn move_char_right(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);
let selection = doc.selection(view.id).transform(|range| {
Expand All @@ -155,7 +160,7 @@ pub fn move_char_right(cx: &mut Context) {
}

pub fn move_line_up(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);
let selection = doc.selection(view.id).transform(|range| {
Expand All @@ -171,7 +176,7 @@ pub fn move_line_up(cx: &mut Context) {
}

pub fn move_line_down(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);
let selection = doc.selection(view.id).transform(|range| {
Expand Down Expand Up @@ -240,7 +245,7 @@ pub fn move_first_nonwhitespace(cx: &mut Context) {
// since these all really do the same thing

pub fn move_next_word_start(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);

Expand All @@ -252,7 +257,7 @@ pub fn move_next_word_start(cx: &mut Context) {
}

pub fn move_prev_word_start(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);

Expand All @@ -264,7 +269,7 @@ pub fn move_prev_word_start(cx: &mut Context) {
}

pub fn move_next_word_end(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);

Expand All @@ -290,7 +295,7 @@ pub fn move_file_end(cx: &mut Context) {
}

pub fn extend_next_word_start(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);

Expand All @@ -304,7 +309,7 @@ pub fn extend_next_word_start(cx: &mut Context) {
}

pub fn extend_prev_word_start(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);

Expand All @@ -317,7 +322,7 @@ pub fn extend_prev_word_start(cx: &mut Context) {
}

pub fn extend_next_word_end(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);

Expand All @@ -339,7 +344,7 @@ where
{
// TODO: count is reset to 1 before next key so we move it into the closure here.
// Would be nice to carry over.
let count = cx.count;
let count = cx.count();

// need to wait for next key
cx.on_next_key(move |cx, event| {
Expand Down Expand Up @@ -563,7 +568,7 @@ pub fn half_page_down(cx: &mut Context) {
}

pub fn extend_char_left(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);
let selection = doc.selection(view.id).transform(|range| {
Expand All @@ -579,7 +584,7 @@ pub fn extend_char_left(cx: &mut Context) {
}

pub fn extend_char_right(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);
let selection = doc.selection(view.id).transform(|range| {
Expand All @@ -595,7 +600,7 @@ pub fn extend_char_right(cx: &mut Context) {
}

pub fn extend_line_up(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);
let selection = doc.selection(view.id).transform(|range| {
Expand All @@ -611,7 +616,7 @@ pub fn extend_line_up(cx: &mut Context) {
}

pub fn extend_line_down(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);
let selection = doc.selection(view.id).transform(|range| {
Expand Down Expand Up @@ -795,7 +800,7 @@ pub fn search_selection(cx: &mut Context) {
//

pub fn select_line(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();

let pos = doc.selection(view.id).primary();
Expand All @@ -810,7 +815,7 @@ pub fn select_line(cx: &mut Context) {
doc.set_selection(view.id, Selection::single(start, end));
}
pub fn extend_line(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();

let pos = doc.selection(view.id).primary();
Expand Down Expand Up @@ -1212,7 +1217,7 @@ enum Open {
}

fn open(cx: &mut Context, open: Open) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
enter_insert_mode(doc);

Expand Down Expand Up @@ -1308,15 +1313,11 @@ fn push_jump(editor: &mut Editor) {
}

pub fn goto_mode(cx: &mut Context) {
let count = cx.count;

if count > 1 {
if let Some(count) = cx._count {
push_jump(cx.editor);

// TODO: can't go to line 1 since we can't distinguish between g and 1g, g gets converted
// to 1g
let (view, doc) = cx.current();
let line_idx = std::cmp::min(count - 1, doc.text().len_lines().saturating_sub(2));
let line_idx = std::cmp::min(count.get() - 1, doc.text().len_lines().saturating_sub(2));
let pos = doc.text().line_to_char(line_idx);
doc.set_selection(view.id, Selection::point(pos));
return;
Expand Down Expand Up @@ -1880,7 +1881,7 @@ pub mod insert {

// TODO: handle indent-aware delete
pub fn delete_char_backward(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);
let transaction =
Expand All @@ -1895,7 +1896,7 @@ pub mod insert {
}

pub fn delete_char_forward(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);
let transaction =
Expand All @@ -1910,7 +1911,7 @@ pub mod insert {
}

pub fn delete_word_backward(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let text = doc.text().slice(..);
let transaction =
Expand Down Expand Up @@ -2071,7 +2072,7 @@ fn get_lines(doc: &Document, view_id: ViewId) -> Vec<usize> {
}

pub fn indent(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let lines = get_lines(doc, view.id);

Expand All @@ -2090,7 +2091,7 @@ pub fn indent(cx: &mut Context) {
}

pub fn unindent(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();
let lines = get_lines(doc, view.id);
let mut changes = Vec::with_capacity(lines.len());
Expand Down Expand Up @@ -2428,7 +2429,7 @@ pub fn match_brackets(cx: &mut Context) {
//

pub fn jump_forward(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();

if let Some((id, selection)) = view.jumps.forward(count) {
Expand All @@ -2442,7 +2443,7 @@ pub fn jump_forward(cx: &mut Context) {
}

pub fn jump_backward(cx: &mut Context) {
let count = cx.count;
let count = cx.count();
let (view, doc) = cx.current();

if let Some((id, selection)) = view.jumps.backward(count) {
Expand Down
7 changes: 4 additions & 3 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@ impl EditorView {
// count handling
key!(i @ '0'..='9') => {
let i = i.to_digit(10).unwrap() as usize;
cxt.editor.count = Some(cxt.editor.count.map_or(i, |c| c * 10 + i));
cxt.editor.count =
std::num::NonZeroUsize::new(cxt.editor.count.map_or(i, |c| c.get() * 10 + i));
}
// special handling for repeat operator
key!('.') => {
Expand All @@ -540,7 +541,7 @@ impl EditorView {
}
_ => {
// set the count
cxt.count = cxt.editor.count.take().unwrap_or(1);
cxt._count = cxt.editor.count.take();
// TODO: edge case: 0j -> reset to 1
// if this fails, count was Some(0)
// debug_assert!(cxt.count != 0);
Expand Down Expand Up @@ -587,8 +588,8 @@ impl Component for EditorView {

let mut cxt = commands::Context {
register: helix_view::RegisterSelection::default(),
count: 1,
editor: &mut cx.editor,
_count: None,
callback: None,
on_next_key_callback: None,
callbacks: cx.callbacks,
Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use helix_core::diagnostic::Severity;
pub struct Editor {
pub tree: Tree,
pub documents: SlotMap<DocumentId, Document>,
pub count: Option<usize>,
pub count: Option<std::num::NonZeroUsize>,
pub register: RegisterSelection,
pub theme: Theme,
pub language_servers: helix_lsp::Registry,
Expand Down

0 comments on commit ae51065

Please sign in to comment.