New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add alias completion #8053
base: main
Are you sure you want to change the base?
Add alias completion #8053
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think test_models
would be the appropriate place to add tests.
qutebrowser/completion/completer.py
Outdated
@@ -90,8 +90,8 @@ def _get_new_completion(self, before_cursor, under_cursor): | |||
log.completion.debug('Starting command completion') | |||
return miscmodels.command | |||
try: | |||
cmd = objects.commands[before_cursor[0]] | |||
except KeyError: | |||
cmd = parser.CommandParser().parse_all(before_cursor[0])[0].cmd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, though I wonder if it's overkill and we should just use config.cache['aliases'][before_cursor[0]]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, both of looking stuff up in objects.commands and config.cache['aliases'] are an order of magnitude faster than parsing a command line. And that's just what the command parser does to resolve aliases.
@knezi you should be able to plop a config_stub.val.aliases = {"openalias": "open"}
, or whatever, in test_update_completion
and add some more parameterized test entries.
Partially fixes qutebrowser#32.
Thanks for the feedback, I've updated the commit accordingly. The failed tests doesn't seem to be caused by this commit. |
cmdname = before_cursor[0] | ||
if before_cursor[0] in config.cache['aliases']: | ||
cmdname = config.cache['aliases'][before_cursor[0]].split( | ||
maxsplit=1)[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should only need one dict lookup here, something like:
cmdname = before_cursor[0] | |
if before_cursor[0] in config.cache['aliases']: | |
cmdname = config.cache['aliases'][before_cursor[0]].split( | |
maxsplit=1)[0] | |
cmdname = before_cursor[0] | |
alias = config.cache['aliases'].get(cmdname) | |
if alias: | |
cmdname = alias.split(maxsplit=1)[0] |
Actually, this doesn't work for obscure cases like alias Other examples are So I could replace before_cursor[0] with the entire alias split on spaces, or already pass expanded alias to |
I vaguely remember cases like those are why I removed alias completion again (b703028, #358) after initially implementing it, some 9 years ago. I don't remember the details, but this seemed to be surprisingly complex to get right. Maybe something changed about that with the refactorings done since then, but maybe not. There are some ideas floating around regarding passing the command-line around internally as some kind of token objects rather than a string, which I suppose might make it easier. |
Partially fixes #32.
I wasn't able to add tests. I feel like it could be added in
tests/unit/completion/test_models.py
just like bind command (test_bind_completion
and others).Or in
test_completer.py
by adding more parameters in parametrizedtest_update_completion
.Can you give me a hint?