Merge branch 'adamwethington7-modifcation-for-issue-1386'

This commit is contained in:
Florian Bruhin 2016-06-06 16:19:49 +02:00
commit 57a1847e3a
10 changed files with 72 additions and 9 deletions

View File

@ -59,6 +59,7 @@ Changed
a new page. a new page.
- `:zoom-in` or `:zoom-out` (`+`/`-`) with a too large count now zooms to the - `:zoom-in` or `:zoom-out` (`+`/`-`) with a too large count now zooms to the
smallest/largest zoom instead of doing nothing. smallest/largest zoom instead of doing nothing.
- The commandline now accepts partially typed commands if they're unique.
Fixed Fixed
----- -----

View File

@ -195,6 +195,7 @@ Contributors, sorted by the number of commits in descending order:
* Larry Hynes * Larry Hynes
* Johannes Altmanninger * Johannes Altmanninger
* Ismail * Ismail
* adam
* Samir Benmendil * Samir Benmendil
* Regina Hug * Regina Hug
* Mathias Fussenegger * Mathias Fussenegger

View File

@ -72,10 +72,12 @@ class CommandRunner(QObject):
Attributes: Attributes:
_win_id: The window this CommandRunner is associated with. _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) super().__init__(parent)
self._partial_match = partial_match
self._win_id = win_id self._win_id = win_id
def _get_alias(self, text): def _get_alias(self, text):
@ -172,6 +174,10 @@ class CommandRunner(QObject):
log.commands.debug("Re-parsing with '{}'.".format(new_cmd)) log.commands.debug("Re-parsing with '{}'.".format(new_cmd))
return self.parse(new_cmd, aliases=False, fallback=fallback, return self.parse(new_cmd, aliases=False, fallback=fallback,
keep=keep) keep=keep)
if self._partial_match:
cmdstr = self._completion_match(cmdstr)
try: try:
cmd = cmdutils.cmd_dict[cmdstr] cmd = cmdutils.cmd_dict[cmdstr]
except KeyError: except KeyError:
@ -196,6 +202,23 @@ class CommandRunner(QObject):
cmdline = [cmdstr] + args[:] cmdline = [cmdstr] + args[:]
return ParseResult(cmd=cmd, args=args, cmdline=cmdline, count=count) 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): def _split_args(self, cmd, argstr, keep):
"""Split the arguments from an arg string. """Split the arguments from an arg string.

View File

@ -158,7 +158,8 @@ class MainWindow(QWidget):
self._completion = completionwidget.CompletionView(self.win_id, self) 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) self._keyhint = keyhintwidget.KeyHintView(self.win_id, self)
@ -296,7 +297,6 @@ class MainWindow(QWidget):
status.keystring.setText) status.keystring.setText)
cmd.got_cmd.connect(self._commandrunner.run_safely) cmd.got_cmd.connect(self._commandrunner.run_safely)
cmd.returnPressed.connect(tabs.on_cmd_return_pressed) cmd.returnPressed.connect(tabs.on_cmd_return_pressed)
tabs.got_cmd.connect(self._commandrunner.run_safely)
# key hint popup # key hint popup
for mode, parser in keyparsers.items(): for mode, parser in keyparsers.items():

View File

@ -99,7 +99,6 @@ class TabbedBrowser(tabwidget.TabWidget):
cur_load_status_changed = pyqtSignal(str) cur_load_status_changed = pyqtSignal(str)
close_window = pyqtSignal() close_window = pyqtSignal()
resized = pyqtSignal('QRect') resized = pyqtSignal('QRect')
got_cmd = pyqtSignal(str)
current_tab_changed = pyqtSignal(webview.WebView) current_tab_changed = pyqtSignal(webview.WebView)
new_tab = pyqtSignal(webview.WebView, int) new_tab = pyqtSignal(webview.WebView, int)

View File

@ -123,10 +123,18 @@ def run_command(quteproc, httpbin, command):
count = int(count) count = int(count)
else: else:
count = None 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('(port)', str(httpbin.port))
command = command.replace('(testdata)', utils.abs_datapath()) 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")) @bdd.when(bdd.parsers.parse("I reload"))

View File

@ -441,3 +441,17 @@ Feature: Various utility commands.
Scenario: Completing a single option argument Scenario: Completing a single option argument
When I run :set-cmd-text -s :-- When I run :set-cmd-text -s :--
Then no crash should happen 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

View File

@ -330,8 +330,13 @@ class QuteProc(testprocess.Process):
finally: finally:
super().after_test() super().after_test()
def send_cmd(self, command, count=None): def send_cmd(self, command, count=None, invalid=False):
"""Send a command to the running qutebrowser instance.""" """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 summary = command
if count is not None: if count is not None:
summary += ' (count {})'.format(count) summary += ' (count {})'.format(count)
@ -346,8 +351,9 @@ class QuteProc(testprocess.Process):
ipc.send_to_running_instance(self._ipc_socket, [command], ipc.send_to_running_instance(self._ipc_socket, [command],
target_arg='') target_arg='')
self.wait_for(category='commands', module='command', function='run', if not invalid:
message='command called: *') self.wait_for(category='commands', module='command',
function='run', message='command called: *')
def get_setting(self, sect, opt): def get_setting(self, sect, opt):
"""Get the value of a qutebrowser setting.""" """Get the value of a qutebrowser setting."""

View File

@ -146,6 +146,8 @@ def _generate_cmdline_tests():
# Command with no_cmd_split combined with an "invalid" command -> valid # Command with no_cmd_split combined with an "invalid" command -> valid
for item in itertools.product(['bind x open'], separators, invalid): for item in itertools.product(['bind x open'], separators, invalid):
yield TestCase(''.join(item), True) yield TestCase(''.join(item), True)
# Partial command
yield TestCase('message-i', False)
@pytest.fixture(params=_generate_cmdline_tests(), ids=lambda e: e.cmd) @pytest.fixture(params=_generate_cmdline_tests(), ids=lambda e: e.cmd)

View File

@ -51,3 +51,12 @@ class TestCommandRunner:
assert result.count == 20 assert result.count == 20
assert result.args == ['down'] assert result.args == ['down']
assert result.cmdline == ['scroll', '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'