From a071e5b226b92316dcbd3e62bb89db6061ef031b Mon Sep 17 00:00:00 2001 From: Delapouite Date: Mon, 13 Nov 2017 08:34:02 +0100 Subject: [PATCH] Add count support to jumps ( and ). Add jumps tests --- src/context.cc | 18 ++++++++++++------ src/context.hh | 4 ++-- src/normal.cc | 7 ++++--- test/normal/jump/backward-count/cmd | 5 +++++ test/normal/jump/backward-count/in | 3 +++ test/normal/jump/backward-count/out | 3 +++ test/normal/jump/backward-invalid-count/cmd | 5 +++++ test/normal/jump/backward-invalid-count/in | 3 +++ test/normal/jump/backward-invalid-count/out | 3 +++ test/normal/jump/backward/cmd | 5 +++++ test/normal/jump/backward/in | 3 +++ test/normal/jump/backward/out | 3 +++ test/normal/jump/forward-count/cmd | 6 ++++++ test/normal/jump/forward-count/in | 3 +++ test/normal/jump/forward-count/out | 3 +++ test/normal/jump/forward-invalid-count/cmd | 6 ++++++ test/normal/jump/forward-invalid-count/in | 3 +++ test/normal/jump/forward-invalid-count/out | 3 +++ test/normal/jump/forward/cmd | 6 ++++++ test/normal/jump/forward/in | 3 +++ test/normal/jump/forward/out | 3 +++ 21 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 test/normal/jump/backward-count/cmd create mode 100644 test/normal/jump/backward-count/in create mode 100644 test/normal/jump/backward-count/out create mode 100644 test/normal/jump/backward-invalid-count/cmd create mode 100644 test/normal/jump/backward-invalid-count/in create mode 100644 test/normal/jump/backward-invalid-count/out create mode 100644 test/normal/jump/backward/cmd create mode 100644 test/normal/jump/backward/in create mode 100644 test/normal/jump/backward/out create mode 100644 test/normal/jump/forward-count/cmd create mode 100644 test/normal/jump/forward-count/in create mode 100644 test/normal/jump/forward-count/out create mode 100644 test/normal/jump/forward-invalid-count/cmd create mode 100644 test/normal/jump/forward-invalid-count/in create mode 100644 test/normal/jump/forward-invalid-count/out create mode 100644 test/normal/jump/forward/cmd create mode 100644 test/normal/jump/forward/in create mode 100644 test/normal/jump/forward/out diff --git a/src/context.cc b/src/context.cc index e3e20e0c..ef2a999f 100644 --- a/src/context.cc +++ b/src/context.cc @@ -86,12 +86,13 @@ void JumpList::push(SelectionList jump) m_current = m_jumps.size(); } -const SelectionList& JumpList::forward(Context& context) +const SelectionList& JumpList::forward(Context& context, int count) { if (m_current != m_jumps.size() and - m_current + 1 != m_jumps.size()) + m_current + count < m_jumps.size()) { - SelectionList& res = m_jumps[++m_current]; + m_current += count; + SelectionList& res = m_jumps[m_current]; res.update(); context.print_status({ format("jumped to #{} ({})", m_current, m_jumps.size() - 1), @@ -101,14 +102,18 @@ const SelectionList& JumpList::forward(Context& context) throw runtime_error("no next jump"); } -const SelectionList& JumpList::backward(Context& context) +const SelectionList& JumpList::backward(Context& context, int count) { + if ((int)m_current - count < 0) + throw runtime_error("no previous jump"); + const SelectionList& current = context.selections(); if (m_current != m_jumps.size() and m_jumps[m_current] != current) { push(current); - SelectionList& res = m_jumps[--m_current]; + m_current -= count; + SelectionList& res = m_jumps[m_current]; res.update(); context.print_status({ format("jumped to #{} ({})", m_current, m_jumps.size() - 1), @@ -123,7 +128,8 @@ const SelectionList& JumpList::backward(Context& context) if (--m_current == 0) throw runtime_error("no previous jump"); } - SelectionList& res = m_jumps[--m_current]; + m_current -= count; + SelectionList& res = m_jumps[m_current]; res.update(); context.print_status({ format("jumped to #{} ({})", m_current, m_jumps.size() - 1), diff --git a/src/context.hh b/src/context.hh index 6c2a4641..754dfd77 100644 --- a/src/context.hh +++ b/src/context.hh @@ -20,8 +20,8 @@ class AliasRegistry; struct JumpList { void push(SelectionList jump); - const SelectionList& forward(Context& context); - const SelectionList& backward(Context& context); + const SelectionList& forward(Context& context, int count); + const SelectionList& backward(Context& context, int count); void forget_buffer(Buffer& buffer); friend bool operator==(const JumpList& lhs, const JumpList& rhs) diff --git a/src/normal.cc b/src/normal.cc index 483be93c..9a407ec1 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1403,11 +1403,12 @@ void replay_macro(Context& context, NormalParams params) } template -void jump(Context& context, NormalParams) +void jump(Context& context, NormalParams params) { + const int count = std::max(1, params.count); auto jump = (direction == Forward) ? - context.jump_list().forward(context) : - context.jump_list().backward(context); + context.jump_list().forward(context, count) : + context.jump_list().backward(context, count); Buffer* oldbuf = &context.buffer(); Buffer& buffer = const_cast(jump.buffer()); diff --git a/test/normal/jump/backward-count/cmd b/test/normal/jump/backward-count/cmd new file mode 100644 index 00000000..10b59f14 --- /dev/null +++ b/test/normal/jump/backward-count/cmd @@ -0,0 +1,5 @@ +gj +/bar +/qux +3 +aend diff --git a/test/normal/jump/backward-count/in b/test/normal/jump/backward-count/in new file mode 100644 index 00000000..7f7f3f94 --- /dev/null +++ b/test/normal/jump/backward-count/in @@ -0,0 +1,3 @@ +%(foo) +bar +qux diff --git a/test/normal/jump/backward-count/out b/test/normal/jump/backward-count/out new file mode 100644 index 00000000..71f62cb4 --- /dev/null +++ b/test/normal/jump/backward-count/out @@ -0,0 +1,3 @@ +fooend +bar +qux diff --git a/test/normal/jump/backward-invalid-count/cmd b/test/normal/jump/backward-invalid-count/cmd new file mode 100644 index 00000000..b2eb7fa0 --- /dev/null +++ b/test/normal/jump/backward-invalid-count/cmd @@ -0,0 +1,5 @@ +gj +/bar +/qux +42 +aend diff --git a/test/normal/jump/backward-invalid-count/in b/test/normal/jump/backward-invalid-count/in new file mode 100644 index 00000000..7f7f3f94 --- /dev/null +++ b/test/normal/jump/backward-invalid-count/in @@ -0,0 +1,3 @@ +%(foo) +bar +qux diff --git a/test/normal/jump/backward-invalid-count/out b/test/normal/jump/backward-invalid-count/out new file mode 100644 index 00000000..72594ed9 --- /dev/null +++ b/test/normal/jump/backward-invalid-count/out @@ -0,0 +1,3 @@ +foo +bar +qux diff --git a/test/normal/jump/backward/cmd b/test/normal/jump/backward/cmd new file mode 100644 index 00000000..b380353c --- /dev/null +++ b/test/normal/jump/backward/cmd @@ -0,0 +1,5 @@ +gj +/bar +/qux + +aend diff --git a/test/normal/jump/backward/in b/test/normal/jump/backward/in new file mode 100644 index 00000000..7f7f3f94 --- /dev/null +++ b/test/normal/jump/backward/in @@ -0,0 +1,3 @@ +%(foo) +bar +qux diff --git a/test/normal/jump/backward/out b/test/normal/jump/backward/out new file mode 100644 index 00000000..71f62cb4 --- /dev/null +++ b/test/normal/jump/backward/out @@ -0,0 +1,3 @@ +fooend +bar +qux diff --git a/test/normal/jump/forward-count/cmd b/test/normal/jump/forward-count/cmd new file mode 100644 index 00000000..915d01a2 --- /dev/null +++ b/test/normal/jump/forward-count/cmd @@ -0,0 +1,6 @@ +gj +/bar +/qux + +3 +aend diff --git a/test/normal/jump/forward-count/in b/test/normal/jump/forward-count/in new file mode 100644 index 00000000..7f7f3f94 --- /dev/null +++ b/test/normal/jump/forward-count/in @@ -0,0 +1,3 @@ +%(foo) +bar +qux diff --git a/test/normal/jump/forward-count/out b/test/normal/jump/forward-count/out new file mode 100644 index 00000000..48308187 --- /dev/null +++ b/test/normal/jump/forward-count/out @@ -0,0 +1,3 @@ +foo +bar +quxend diff --git a/test/normal/jump/forward-invalid-count/cmd b/test/normal/jump/forward-invalid-count/cmd new file mode 100644 index 00000000..3dcb3ba2 --- /dev/null +++ b/test/normal/jump/forward-invalid-count/cmd @@ -0,0 +1,6 @@ +gj +/bar +/qux + +42 +aend diff --git a/test/normal/jump/forward-invalid-count/in b/test/normal/jump/forward-invalid-count/in new file mode 100644 index 00000000..7f7f3f94 --- /dev/null +++ b/test/normal/jump/forward-invalid-count/in @@ -0,0 +1,3 @@ +%(foo) +bar +qux diff --git a/test/normal/jump/forward-invalid-count/out b/test/normal/jump/forward-invalid-count/out new file mode 100644 index 00000000..72594ed9 --- /dev/null +++ b/test/normal/jump/forward-invalid-count/out @@ -0,0 +1,3 @@ +foo +bar +qux diff --git a/test/normal/jump/forward/cmd b/test/normal/jump/forward/cmd new file mode 100644 index 00000000..e3679b24 --- /dev/null +++ b/test/normal/jump/forward/cmd @@ -0,0 +1,6 @@ +gj +/bar +/qux + + +aend diff --git a/test/normal/jump/forward/in b/test/normal/jump/forward/in new file mode 100644 index 00000000..7f7f3f94 --- /dev/null +++ b/test/normal/jump/forward/in @@ -0,0 +1,3 @@ +%(foo) +bar +qux diff --git a/test/normal/jump/forward/out b/test/normal/jump/forward/out new file mode 100644 index 00000000..48308187 --- /dev/null +++ b/test/normal/jump/forward/out @@ -0,0 +1,3 @@ +foo +bar +quxend