Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add alias completion #8053

wants to merge 1 commit into from

Conversation

knezi
Copy link
Contributor

@knezi knezi commented Jan 1, 2024

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 parametrized test_update_completion.

Can you give me a hint?

@knezi knezi requested a review from rcorre as a code owner January 1, 2024 09:45
Copy link
Contributor

@rcorre rcorre left a 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.

@@ -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
Copy link
Contributor

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]]?

Copy link
Member

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.

@toofar toofar added this to the 3.2.0 milestone Jan 3, 2024
@The-Compiler The-Compiler added this to Inbox in Pull request backlog via automation Jan 10, 2024
@The-Compiler The-Compiler moved this from Inbox to WIP in Pull request backlog Jan 10, 2024
@knezi
Copy link
Contributor Author

knezi commented Jan 11, 2024

Thanks for the feedback, I've updated the commit accordingly. The failed tests doesn't seem to be caused by this commit.

Comment on lines +93 to +96
cmdname = before_cursor[0]
if before_cursor[0] in config.cache['aliases']:
cmdname = config.cache['aliases'][before_cursor[0]].split(
maxsplit=1)[0]
Copy link
Contributor

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:

Suggested change
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]

@knezi
Copy link
Contributor Author

knezi commented Jan 16, 2024

Actually, this doesn't work for obscure cases like alias ba for bind <ctrl-x>. If you run ba |, it won't complete commands as it expects key short cut first.

Other examples are set if you want to quickly set some particular settings. Or possibly
config-dict-add and hint, but it's not applicable for those as the completion is not done for these commands.

So I could replace before_cursor[0] with the entire alias split on spaces, or already pass expanded alias to _get_new_completion. What do you think?

@The-Compiler
Copy link
Member

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.

@The-Compiler The-Compiler removed this from the v3.2.0 milestone Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

More completions
4 participants