Extend tab-move's direction argument to take an index (fixes #1701)

This commit is contained in:
Marshall Lochbaum 2016-07-29 23:06:40 -04:00
parent f9c09a1f43
commit a7438f4f15
2 changed files with 77 additions and 17 deletions

View File

@ -932,32 +932,40 @@ class CommandDispatcher:
idx)) idx))
@cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('direction', choices=['+', '-']) @cmdutils.argument('index', choices=['+', '-'])
@cmdutils.argument('count', count=True) @cmdutils.argument('count', count=True)
def tab_move(self, direction: str=None, count=None): def tab_move(self, index: typing.Union[str, int]=None, count=None):
"""Move the current tab. """Move the current tab according to the argument and [count].
If neither is given, move it to the first position.
Args: Args:
direction: `+` or `-` for relative moving, not given for absolute index: `+` or `-` to move relative to the current tab by
moving. count, or a default of 1 space.
count: If moving absolutely: New position (default: 0) A tab index to move to that index.
If moving relatively: Offset. count: If moving relatively: Offset.
If moving absolutely: New position (default: 0). This
overrides the index argument, if given.
""" """
if direction is None: if index in ['+', '-']:
# absolute moving
new_idx = 0 if count is None else count - 1
elif direction in '+-':
# relative moving # relative moving
new_idx = self._current_index()
delta = 1 if count is None else count delta = 1 if count is None else count
if direction == '-': if index == '-':
new_idx = self._current_index() - delta new_idx -= delta
elif direction == '+': # pragma: no branch elif index == '+': # pragma: no branch
new_idx = self._current_index() + delta new_idx += delta
if config.get('tabs', 'wrap'): if config.get('tabs', 'wrap'):
new_idx %= self._count() new_idx %= self._count()
else: # pragma: no cover else:
raise ValueError("Invalid direction '{}'!".format(direction)) # absolute moving
if count is not None:
new_idx = count - 1
elif index is not None:
new_idx = index - 1 if index >= 0 else index + self._count()
else:
new_idx = 0
if not 0 <= new_idx < self._count(): if not 0 <= new_idx < self._count():
raise cmdexc.CommandError("Can't move tab to position {}!".format( raise cmdexc.CommandError("Can't move tab to position {}!".format(

View File

@ -380,6 +380,58 @@ Feature: Tab management
- data/numbers/2.txt - data/numbers/2.txt
- data/numbers/3.txt (active) - data/numbers/3.txt (active)
Scenario: :tab-move with index.
When I open data/numbers/1.txt
And I open data/numbers/2.txt in a new tab
And I open data/numbers/3.txt in a new tab
And I run :tab-move 2
Then the following tabs should be open:
- data/numbers/1.txt
- data/numbers/3.txt (active)
- data/numbers/2.txt
Scenario: :tab-move with negative index.
When I open data/numbers/1.txt
And I open data/numbers/2.txt in a new tab
And I open data/numbers/3.txt in a new tab
And I run :tab-move -3
Then the following tabs should be open:
- data/numbers/3.txt (active)
- data/numbers/1.txt
- data/numbers/2.txt
Scenario: :tab-move with invalid index.
When I open data/numbers/1.txt
And I open data/numbers/2.txt in a new tab
And I open data/numbers/3.txt in a new tab
And I run :tab-move -5
Then the error "Can't move tab to position -1!" should be shown.
And the following tabs should be open:
- data/numbers/1.txt
- data/numbers/2.txt
- data/numbers/3.txt (active)
Scenario: :tab-move with index and count.
When I open data/numbers/1.txt
And I open data/numbers/2.txt in a new tab
And I open data/numbers/3.txt in a new tab
And I run :tab-move 1 with count 2
Then the following tabs should be open:
- data/numbers/1.txt
- data/numbers/3.txt (active)
- data/numbers/2.txt
Scenario: :tab-move with index and invalid count.
When I open data/numbers/1.txt
And I open data/numbers/2.txt in a new tab
And I open data/numbers/3.txt in a new tab
And I run :tab-move -2 with count 4
Then the error "Can't move tab to position 4!" should be shown.
And the following tabs should be open:
- data/numbers/1.txt
- data/numbers/2.txt
- data/numbers/3.txt (active)
Scenario: :tab-move with relative position (negative). Scenario: :tab-move with relative position (negative).
When I open data/numbers/1.txt When I open data/numbers/1.txt
And I open data/numbers/2.txt in a new tab And I open data/numbers/2.txt in a new tab