Merge branch 'unix_filename_rubout' of https://github.com/rcorre/qutebrowser into rcorre-unix_filename_rubout

This commit is contained in:
Florian Bruhin 2016-08-01 10:40:34 +02:00
commit 7f311a46ae
2 changed files with 71 additions and 49 deletions

View File

@ -148,14 +148,8 @@ class ReadlineBridge:
self._deleted[widget] = widget.selectedText()
widget.del_()
@cmdutils.register(instance='readline-bridge', hide=True,
modes=[typ.KeyMode.command, typ.KeyMode.prompt])
def rl_unix_word_rubout(self):
"""Remove chars from the cursor to the beginning of the word.
This acts like readline's unix-word-rubout. Whitespace is used as a
word delimiter.
"""
def _rubout(self, delim):
"""Delete backwards using the characters in delim as boundaries."""
widget = self._widget()
if widget is None:
return
@ -164,14 +158,14 @@ class ReadlineBridge:
target_position = cursor_position
is_word_boundary = True
while is_word_boundary and target_position > 0:
is_word_boundary = text[target_position - 1] == " "
is_boundary = True
while is_boundary and target_position > 0:
is_boundary = text[target_position - 1] in delim
target_position -= 1
is_word_boundary = False
while not is_word_boundary and target_position > 0:
is_word_boundary = text[target_position - 1] == " "
is_boundary = False
while not is_boundary and target_position > 0:
is_boundary = text[target_position - 1] in delim
target_position -= 1
moveby = cursor_position - target_position - 1
@ -179,6 +173,25 @@ class ReadlineBridge:
self._deleted[widget] = widget.selectedText()
widget.del_()
@cmdutils.register(instance='readline-bridge', hide=True,
modes=[typ.KeyMode.command, typ.KeyMode.prompt])
def rl_unix_word_rubout(self):
"""Remove chars from the cursor to the beginning of the word.
This acts like readline's unix-word-rubout. Whitespace is used as a
word delimiter.
"""
self._rubout([' '])
@cmdutils.register(instance='readline-bridge', hide=True,
modes=[typ.KeyMode.command, typ.KeyMode.prompt])
def rl_unix_filename_rubout(self):
"""Remove chars from the cursor to the previous path separator.
This acts like readline's unix-filename-rubout.
"""
self._rubout([' ', '/'])
@cmdutils.register(instance='readline-bridge', hide=True,
modes=[typ.KeyMode.command, typ.KeyMode.prompt])
def rl_backward_kill_word(self):

View File

@ -96,6 +96,26 @@ class LineEdit(QLineEdit):
return ''.join(chars)
def _validate_deletion(lineedit, bridge, method, text, deleted, rest):
"""Run and validate a text deletion method on the ReadLine bridge.
Args:
lineedit: The LineEdit instance.
bridge: The ReadlineBridge instance.
method: Reference to the method on the bridge to test.
text: The starting 'augmented' text (see LineEdit.set_aug_text)
deleted: The text that should be deleted when the method is invoked.
rest: The augmented text that should remain after method is invoked.
"""
lineedit.set_aug_text(text)
method()
assert bridge._deleted[lineedit] == deleted
assert lineedit.aug_text() == rest
lineedit.clear()
bridge.rl_yank()
assert lineedit.aug_text() == deleted + '|'
@pytest.fixture
def lineedit(qtbot, monkeypatch):
"""Fixture providing a LineEdit."""
@ -206,13 +226,8 @@ def test_rl_backward_delete_char(text, expected, lineedit, bridge):
])
def test_rl_unix_line_discard(lineedit, bridge, text, deleted, rest):
"""Delete from the cursor to the beginning of the line and yank back."""
lineedit.set_aug_text(text)
bridge.rl_unix_line_discard()
assert bridge._deleted[lineedit] == deleted
assert lineedit.aug_text() == rest
lineedit.clear()
bridge.rl_yank()
assert lineedit.aug_text() == deleted + '|'
_validate_deletion(lineedit, bridge, bridge.rl_unix_line_discard, text,
deleted, rest)
@pytest.mark.parametrize('text, deleted, rest', [
@ -222,13 +237,8 @@ def test_rl_unix_line_discard(lineedit, bridge, text, deleted, rest):
])
def test_rl_kill_line(lineedit, bridge, text, deleted, rest):
"""Delete from the cursor to the end of line and yank back."""
lineedit.set_aug_text(text)
bridge.rl_kill_line()
assert bridge._deleted[lineedit] == deleted
assert lineedit.aug_text() == rest
lineedit.clear()
bridge.rl_yank()
assert lineedit.aug_text() == deleted + '|'
_validate_deletion(lineedit, bridge, bridge.rl_kill_line, text, deleted,
rest)
@pytest.mark.parametrize('text, deleted, rest', [
@ -241,13 +251,21 @@ def test_rl_kill_line(lineedit, bridge, text, deleted, rest):
])
def test_rl_unix_word_rubout(lineedit, bridge, text, deleted, rest):
"""Delete to word beginning and see if it comes back with yank."""
lineedit.set_aug_text(text)
bridge.rl_unix_word_rubout()
assert bridge._deleted[lineedit] == deleted
assert lineedit.aug_text() == rest
lineedit.clear()
bridge.rl_yank()
assert lineedit.aug_text() == deleted + '|'
_validate_deletion(lineedit, bridge, bridge.rl_unix_word_rubout, text,
deleted, rest)
@pytest.mark.parametrize('text, deleted, rest', [
('test delete|foobar', 'delete', 'test |foobar'),
('test delete |foobar', 'delete ', 'test |foobar'),
('open -t github.com/foo/bar |', 'bar ', 'open -t github.com/foo/|'),
('open -t |github.com/foo/bar', '-t ', 'open |github.com/foo/bar'),
('open foo/bar.baz|', 'bar.baz', 'open foo/|'),
])
def test_rl_unix_filename_rubout(lineedit, bridge, text, deleted, rest):
"""Delete filename segment and see if it comes back with yank."""
_validate_deletion(lineedit, bridge, bridge.rl_unix_filename_rubout, text,
deleted, rest)
@pytest.mark.parametrize('text, deleted, rest', [
@ -260,13 +278,8 @@ def test_rl_unix_word_rubout(lineedit, bridge, text, deleted, rest):
])
def test_rl_kill_word(lineedit, bridge, text, deleted, rest):
"""Delete to word end and see if it comes back with yank."""
lineedit.set_aug_text(text)
bridge.rl_kill_word()
assert bridge._deleted[lineedit] == deleted
assert lineedit.aug_text() == rest
lineedit.clear()
bridge.rl_yank()
assert lineedit.aug_text() == deleted + '|'
_validate_deletion(lineedit, bridge, bridge.rl_kill_word, text, deleted,
rest)
@pytest.mark.parametrize('text, deleted, rest', [
@ -276,16 +289,12 @@ def test_rl_kill_word(lineedit, bridge, text, deleted, rest):
('open -t |github.com/foo/bar', 't ', 'open -|github.com/foo/bar'),
fixme(('test del<ete>foobar', 'delete', 'test |foobar')),
('test del<ete >foobar', 'del', 'test |ete foobar'), # wrong
('open foo/bar.baz|', 'baz', 'open foo/bar.|'),
])
def test_rl_backward_kill_word(lineedit, bridge, text, deleted, rest):
"""Delete to word beginning and see if it comes back with yank."""
lineedit.set_aug_text(text)
bridge.rl_backward_kill_word()
assert bridge._deleted[lineedit] == deleted
assert lineedit.aug_text() == rest
lineedit.clear()
bridge.rl_yank()
assert lineedit.aug_text() == deleted + '|'
_validate_deletion(lineedit, bridge, bridge.rl_backward_kill_word, text,
deleted, rest)
def test_rl_yank_no_text(lineedit, bridge):