-
Notifications
You must be signed in to change notification settings - Fork 409
fix: completion generation inside $() #1403
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
Draft
richardkmichael
wants to merge
2
commits into
scop:main
Choose a base branch
from
richardkmichael:completion-in-csub
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−3
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import pytest | ||
|
|
||
| from conftest import assert_bash_exec, assert_complete | ||
|
|
||
|
|
||
| @pytest.mark.bashcomp( | ||
| cmd=None, | ||
| ignore_env=r"^[+-](COMPREPLY|REPLY)=", | ||
| ) | ||
| class TestUnitCommandSubstitution: | ||
| """Test completion inside $() command substitutions.""" | ||
|
|
||
| wordlist = ["alpha", "bravo"] | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def functions(self, bash): | ||
| assert_bash_exec( | ||
| bash, | ||
| # csub_cmd: a test command whose completion function returns | ||
| # a fixed word list, filtered by the current word prefix. | ||
| "_csub_compfunc() {" | ||
| ' COMPREPLY=($(compgen -W "%s"' | ||
| ' -- "${COMP_WORDS[COMP_CWORD]}"));' | ||
| "}; " | ||
| "complete -F _csub_compfunc csub_cmd" % " ".join(self.wordlist), | ||
| ) | ||
|
|
||
| def test_closed_substitution(self, bash, functions): | ||
| assert assert_complete( | ||
| bash, "echo $(echo hi) ", cwd="shared/default" | ||
| ) == ["bar", "bar bar.d/", "foo", "foo.d/"] | ||
|
|
||
| def test_single_level(self, bash, functions): | ||
| assert assert_complete(bash, "echo $(csub_cmd ") == self.wordlist | ||
|
|
||
| def test_single_level_partial(self, bash, functions): | ||
| assert assert_complete(bash, "echo $(csub_cmd a") == ["lpha"] | ||
|
|
||
| def test_nested(self, bash, functions): | ||
| assert ( | ||
| assert_complete(bash, "echo $(echo foo $(csub_cmd ") | ||
| == self.wordlist | ||
| ) | ||
|
|
||
| def test_nested_partial(self, bash, functions): | ||
| assert assert_complete(bash, "echo $(echo foo $(csub_cmd b") == [ | ||
| "ravo" | ||
| ] | ||
|
|
||
| def test_closed_then_open(self, bash, functions): | ||
| assert ( | ||
| assert_complete(bash, "echo $(echo hi) $(csub_cmd ") | ||
| == self.wordlist | ||
| ) | ||
|
|
||
| def test_open_then_closed(self, bash, functions): | ||
| assert ( | ||
| assert_complete(bash, "echo $(csub_cmd $(echo hi) ") | ||
| == self.wordlist | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
The current implementation falls into an infinite loop with the following completion attempt:
$ cmd1 $(cmd2 a$(cmd3)b$(cmd4 [tab]Also, the command name is wrongly extracted to be
pwd)/$(cmd3with the following completion attempt:$ cmd1 $(cmd2 $(pwd)/$(cmd3 [tab]Actually, I have difficulty understanding the choice of the present algorithm. Even if the above issues are fixed somehow, is this algorithm stable and robust in general? Since I currently don't see a clear path or picture to understand the construction of the current algorithm, the algorithm appears to be a random trial currently.
Can you explain what is the assumptions of the algorithm, and how it is ensured to work under the given assumptions?
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.
Thank you!
Given the failures, yes, one assumption that words do not contain unbalanced pairs is a mistake, and another being that the first $( in the COMP_WORD is correct one. Evidently not robust. :) I was trying to find the word right-adjacent to the last open
$(but the pair-wise counting by line length difference won't work, and the strip is to the wrong $(. I wouldn't say "random trial" :-) but definitely too naive. I was experimenting with test cases on my command line, and hoping to avoid writing a parens matcher with character position.I'll go through it more carefully, for another approach.
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.
Thank you for the explanation!
Thank you for thinking about another approach! If it turns out to be difficult, we don't necessarily have to support all possible cases. I think the important thing is that we want to gracefully fail in the unsupported cases (instead of causing an infinite loop or producing a strange result); for example, if the required assumptions are clear, we may examine the contents of
curand give up the special treatment when the assumptions are unsatisfied.