From 2279d8fc230515a768dfe525bb366cfddb2713b4 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 18 Mar 2021 14:49:45 +0100 Subject: Restore config access in commands.parser See https://github.com/qutebrowser/qutebrowser/pull/5967#issuecomment-791373157 but no issues with circular imports here, from what I can see... --- qutebrowser/commands/parser.py | 19 +++++++++---------- qutebrowser/commands/runners.py | 3 +-- qutebrowser/config/config.py | 3 +-- tests/unit/commands/test_parser.py | 16 ++++++++-------- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/qutebrowser/commands/parser.py b/qutebrowser/commands/parser.py index ceebad57e..710c31717 100644 --- a/qutebrowser/commands/parser.py +++ b/qutebrowser/commands/parser.py @@ -24,6 +24,7 @@ from typing import Optional, List, Mapping, Iterator, List, Union from qutebrowser.commands import cmdexc, command from qutebrowser.misc import split, objects +from qutebrowser.config import config @dataclasses.dataclass @@ -47,7 +48,7 @@ class CommandParser: def __init__(self, partial_match: bool = False) -> None: self._partial_match = partial_match - def _get_alias(self, text: str, aliases: Mapping[str, str], *, default: str) -> str: + def _get_alias(self, text: str, *, default: str) -> str: """Get an alias from the config. Args: @@ -60,6 +61,7 @@ class CommandParser: otherwise. """ parts = text.strip().split(maxsplit=1) + aliases = config.cache['aliases'] if parts[0] not in aliases: return default alias = aliases[parts[0]] @@ -75,7 +77,7 @@ class CommandParser: def _parse_all_gen( self, text: str, - aliases: Mapping[str, str] = None, + aliases: bool = True, **kwargs: bool, ) -> Iterator[ParseResult]: """Split a command on ;; and parse all parts. @@ -85,7 +87,7 @@ class CommandParser: Args: text: Text to parse. - aliases: A map of aliases to commands. + aliases: Whether to handle aliases. **kwargs: Passed to parse(). Yields: @@ -96,7 +98,7 @@ class CommandParser: raise cmdexc.NoSuchCommandError("No command given") if aliases: - text = self._get_alias(text, aliases, default=text) + text = self._get_alias(text, default=text) if ';;' in text: # Get the first command and check if it doesn't want to have ;; @@ -122,7 +124,6 @@ class CommandParser: *, fallback: bool = False, keep: bool = False, - use_best_match: bool = False, ) -> ParseResult: """Split the commandline text into command and arguments. @@ -131,7 +132,6 @@ class CommandParser: fallback: Whether to do a fallback splitting when the command was unknown. keep: Whether to keep special chars and whitespace - use_best_match: Whether to use the best match even if incomplete. Return: A ParseResult tuple. @@ -142,7 +142,7 @@ class CommandParser: raise cmdexc.NoSuchCommandError("No command given") if self._partial_match: - cmdstr = self._completion_match(cmdstr, use_best_match) + cmdstr = self._completion_match(cmdstr) try: cmd = objects.commands[cmdstr] @@ -163,12 +163,11 @@ class CommandParser: return ParseResult(cmd=cmd, args=args, cmdline=cmdline) - def _completion_match(self, cmdstr: str, use_best_match: bool) -> str: + def _completion_match(self, cmdstr: str) -> str: """Replace cmdstr with a matching completion if there's only one match. Args: cmdstr: The string representing the entered command so far. - use_best_match: Whether to use the best match even if incomplete. Return: cmdstr modified to the matching completion or unmodified @@ -177,7 +176,7 @@ class CommandParser: if cmdstr in cmd] if len(matches) == 1: cmdstr = matches[0] - elif len(matches) > 1 and use_best_match: + elif len(matches) > 1 and config.val.completion.use_best_match: cmdstr = matches[0] return cmdstr diff --git a/qutebrowser/commands/runners.py b/qutebrowser/commands/runners.py index 6d4ccdfc2..7386d7d3b 100644 --- a/qutebrowser/commands/runners.py +++ b/qutebrowser/commands/runners.py @@ -172,8 +172,7 @@ class CommandRunner(AbstractCommandRunner): parsed = None with self._handle_error(safely): - parsed = self._parser.parse_all( - text, use_best_match=config.val.completion.use_best_match) + parsed = self._parser.parse_all(text) if parsed is None: return diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 719721cb7..07d16ea92 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -166,8 +166,7 @@ class KeyConfig: def _implied_cmd(self, cmdline: str) -> Optional[str]: """Return cmdline, or the implied cmd if cmdline is a set-cmd-text.""" try: - results = parser.CommandParser().parse_all( - cmdline, aliases=cache['aliases']) + results = parser.CommandParser().parse_all(cmdline) except cmdexc.NoSuchCommandError: return None diff --git a/tests/unit/commands/test_parser.py b/tests/unit/commands/test_parser.py index 3a0c5f314..b851ad3b0 100644 --- a/tests/unit/commands/test_parser.py +++ b/tests/unit/commands/test_parser.py @@ -37,24 +37,24 @@ class TestCommandParser: """ p = parser.CommandParser() if cmdline_test.valid: - p.parse_all(cmdline_test.cmd) + p.parse_all(cmdline_test.cmd, aliases=False) else: with pytest.raises(cmdexc.NoSuchCommandError): - p.parse_all(cmdline_test.cmd) + p.parse_all(cmdline_test.cmd, aliases=False) def test_parse_all_with_alias(self, cmdline_test, monkeypatch, config_stub): if not cmdline_test.cmd: pytest.skip("Empty command") - aliases = {'alias_name': cmdline_test.cmd} + config_stub.val.aliases = {'alias_name': cmdline_test.cmd} p = parser.CommandParser() if cmdline_test.valid: - assert len(p.parse_all("alias_name", aliases=aliases)) > 0 + assert len(p.parse_all("alias_name")) > 0 else: with pytest.raises(cmdexc.NoSuchCommandError): - p.parse_all("alias_name", aliases=aliases) + p.parse_all("alias_name") @pytest.mark.parametrize('command', ['', ' ']) def test_parse_empty_with_alias(self, command): @@ -65,7 +65,7 @@ class TestCommandParser: """ p = parser.CommandParser() with pytest.raises(cmdexc.NoSuchCommandError): - p.parse_all(command, aliases={"foo": "bar"}) + p.parse_all(command) @pytest.mark.parametrize('command, name, args', [ ("set-cmd-text -s :open", "set-cmd-text", ["-s", ":open"]), @@ -85,7 +85,7 @@ class TestCommandParser: ("set-cmd-text ?", "set-cmd-text", ["?"]), ("set-cmd-text :", "set-cmd-text", [":"]), ]) - def test_parse_result(self, command, name, args): + def test_parse_result(self, config_stub, command, name, args): p = parser.CommandParser() result = p.parse_all(command)[0] assert result.cmd.name == name @@ -133,5 +133,5 @@ class TestCompletions: config_stub.val.completion.use_best_match = True p = parser.CommandParser(partial_match=True) - result = p.parse('tw', best_match=True) + result = p.parse('tw') assert result.cmd.name == 'two' -- cgit v1.2.3-54-g00ecf