Skip to content
Permalink
Browse files
Merge branch 'adamwethington7-modifcation-for-issue-1386'
  • Loading branch information
The-Compiler committed Jun 6, 2016
2 parents 4e86a88 + a38ec6e commit 57a1847e3a1d18cfdf60c627e04cb66c88d29167
@@ -59,6 +59,7 @@ Changed
a new page.
- `:zoom-in` or `:zoom-out` (`+`/`-`) with a too large count now zooms to the
smallest/largest zoom instead of doing nothing.
- The commandline now accepts partially typed commands if they're unique.
Fixed
-----
@@ -195,6 +195,7 @@ Contributors, sorted by the number of commits in descending order:
* Larry Hynes
* Johannes Altmanninger
* Ismail
* adam
* Samir Benmendil
* Regina Hug
* Mathias Fussenegger
@@ -72,10 +72,12 @@ class CommandRunner(QObject):
Attributes:
_win_id: The window this CommandRunner is associated with.
_partial_match: Whether to allow partial command matches.
"""

def __init__(self, win_id, parent=None):
def __init__(self, win_id, partial_match=False, parent=None):
super().__init__(parent)
self._partial_match = partial_match
self._win_id = win_id

def _get_alias(self, text):
@@ -172,6 +174,10 @@ def parse(self, text, *, aliases=True, fallback=False, keep=False):
log.commands.debug("Re-parsing with '{}'.".format(new_cmd))
return self.parse(new_cmd, aliases=False, fallback=fallback,
keep=keep)

if self._partial_match:
cmdstr = self._completion_match(cmdstr)

try:
cmd = cmdutils.cmd_dict[cmdstr]
except KeyError:
@@ -196,6 +202,23 @@ def parse(self, text, *, aliases=True, fallback=False, keep=False):
cmdline = [cmdstr] + args[:]
return ParseResult(cmd=cmd, args=args, cmdline=cmdline, count=count)

def _completion_match(self, cmdstr):
"""Replace cmdstr with a matching completion if there's only one match.
Args:
cmdstr: The string representing the entered command so far
Return:
cmdstr modified to the matching completion or unmodified
"""
matches = []
for valid_command in cmdutils.cmd_dict.keys():
if valid_command.find(cmdstr) == 0:
matches.append(valid_command)
if len(matches) == 1:
cmdstr = matches[0]
return cmdstr

def _split_args(self, cmd, argstr, keep):
"""Split the arguments from an arg string.
@@ -158,7 +158,8 @@ def __init__(self, geometry=None, parent=None):

self._completion = completionwidget.CompletionView(self.win_id, self)

self._commandrunner = runners.CommandRunner(self.win_id)
self._commandrunner = runners.CommandRunner(self.win_id,
partial_match=True)

self._keyhint = keyhintwidget.KeyHintView(self.win_id, self)

@@ -296,7 +297,6 @@ def _connect_signals(self):
status.keystring.setText)
cmd.got_cmd.connect(self._commandrunner.run_safely)
cmd.returnPressed.connect(tabs.on_cmd_return_pressed)
tabs.got_cmd.connect(self._commandrunner.run_safely)

# key hint popup
for mode, parser in keyparsers.items():
@@ -99,7 +99,6 @@ class TabbedBrowser(tabwidget.TabWidget):
cur_load_status_changed = pyqtSignal(str)
close_window = pyqtSignal()
resized = pyqtSignal('QRect')
got_cmd = pyqtSignal(str)
current_tab_changed = pyqtSignal(webview.WebView)
new_tab = pyqtSignal(webview.WebView, int)

@@ -123,10 +123,18 @@ def run_command(quteproc, httpbin, command):
count = int(count)
else:
count = None

invalid_tag = ' (invalid command)'
if command.endswith(invalid_tag):
command = command[:-len(invalid_tag)]
invalid = True
else:
invalid = False

command = command.replace('(port)', str(httpbin.port))
command = command.replace('(testdata)', utils.abs_datapath())

quteproc.send_cmd(command, count=count)
quteproc.send_cmd(command, count=count, invalid=invalid)


@bdd.when(bdd.parsers.parse("I reload"))
@@ -441,3 +441,17 @@ Feature: Various utility commands.
Scenario: Completing a single option argument
When I run :set-cmd-text -s :--
Then no crash should happen

## https://github.com/The-Compiler/qutebrowser/issues/1386

Scenario: Partial commandline matching with startup command
When I run :message-i "Hello World" (invalid command)
Then the error "message-i: no such command" should be shown

# We can't run :message-i as startup command, so we use
# :set-cmd-text

Scenario: Partial commandline matching
When I run :set-cmd-text :message-i "Hello World"
And I run :command-accept
Then the message "Hello World" should be shown
@@ -330,8 +330,13 @@ def after_test(self, did_fail):
finally:
super().after_test()

def send_cmd(self, command, count=None):
"""Send a command to the running qutebrowser instance."""
def send_cmd(self, command, count=None, invalid=False):
"""Send a command to the running qutebrowser instance.
Args:
count: The count to pass to the command.
invalid: If True, we don't wait for "command called: ..." in the log
"""
summary = command
if count is not None:
summary += ' (count {})'.format(count)
@@ -346,8 +351,9 @@ def send_cmd(self, command, count=None):

ipc.send_to_running_instance(self._ipc_socket, [command],
target_arg='')
self.wait_for(category='commands', module='command', function='run',
message='command called: *')
if not invalid:
self.wait_for(category='commands', module='command',
function='run', message='command called: *')

def get_setting(self, sect, opt):
"""Get the value of a qutebrowser setting."""
@@ -146,6 +146,8 @@ def _generate_cmdline_tests():
# Command with no_cmd_split combined with an "invalid" command -> valid
for item in itertools.product(['bind x open'], separators, invalid):
yield TestCase(''.join(item), True)
# Partial command
yield TestCase('message-i', False)


@pytest.fixture(params=_generate_cmdline_tests(), ids=lambda e: e.cmd)
@@ -51,3 +51,12 @@ def test_parse_with_count(self):
assert result.count == 20
assert result.args == ['down']
assert result.cmdline == ['scroll', 'down']

def test_partial_parsing(self):
"""Test partial parsing with a runner where it's enabled.
The same with it being disabled is tested by test_parse_all.
"""
cr = runners.CommandRunner(0, partial_match=True)
result = cr.parse('message-i', aliases=False)
assert result.cmd.name == 'message-info'

0 comments on commit 57a1847

Please sign in to comment.