Fix tab-completion trailing space for cmd arguments that use equals sign (#4750)

* Fix tab-completion trailing space for command arguments that use equals sign

* Add test for completion of path ending with equal sign

* Move test_path_completers.py to tests/completers

* Restore _quote_paths() to original behaviour

* Apply trailing equal sign fix to _bash_quote_paths()

Use _bash_quote_paths instead of _quote_paths in complete_from_bash()

* Do not need to pass quote_paths=_bash_quote_paths
This commit is contained in:
Peter Ye 2022-04-15 22:52:54 -04:00 committed by GitHub
parent cd334e44e3
commit 09a77c15c9
Failed to generate hash of commit
5 changed files with 72 additions and 3 deletions

View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* A trailing space no longer gets appended when tab-completing command arguments that involve equals signs. For example `dd sta` gets completed to `dd status=`, without a space space after the equals sign.
**Security:**
* <news item>

View file

@ -179,3 +179,33 @@ def test_bash_completer_empty_prefix():
)
bash_completions, bash_lprefix = complete_from_bash(context)
assert {"clean", "show"}.issubset(bash_completions)
@skip_if_on_darwin
@skip_if_on_windows
@pytest.mark.parametrize(
"command_context, completions, lprefix",
(
# dd sta -> dd status=
(
CommandContext(args=(CommandArg("dd"),), arg_index=1, prefix="sta"),
{"status="},
3,
),
# date --da -> date --date=
(
CommandContext(args=(CommandArg("date"),), arg_index=1, prefix="--da"),
{"--date="},
4,
),
),
)
def test_equal_sign_arg(command_context, completions, lprefix):
bash_completions, bash_lprefix = complete_from_bash(
CompletionContext(command_context)
)
assert bash_completions == completions and bash_lprefix == lprefix
assert all(
isinstance(comp, RichCompletion) and not comp.append_space
for comp in bash_completions
) # there should not be an appended space after the equal sign

View file

@ -63,6 +63,24 @@ def test_complete_path_when_prefix_is_raw_path_string(
assert expected == out[0].pop()
def test_complete_path_ending_with_equal_sign(xession, completion_context_parse):
xession.env = {
"CASE_SENSITIVE_COMPLETIONS": True,
"GLOB_SORTED": True,
"SUBSEQUENCE_PATH_COMPLETION": False,
"FUZZY_PATH_COMPLETION": False,
"SUGGEST_THRESHOLD": 1,
"CDPATH": set(),
}
with tempfile.NamedTemporaryFile(suffix="=") as tmp:
prefix_file_name = tmp.name.replace("=", "")
prefix = prefix_file_name
line = f"ls {prefix}"
out = xcp.complete_path(completion_context_parse(line, len(line)))
expected = f"{tmp.name} " # has trailing space
assert expected == out[0].pop()
@pytest.mark.parametrize("prefix", ("", "r", "p", "pr", "rp"))
def test_path_from_partial_string(prefix):
string = "hello"

View file

@ -4,7 +4,6 @@ import xonsh.platform as xp
import xonsh.tools as xt
from xonsh.built_ins import XSH
from xonsh.completers.bash_completion import bash_completions
from xonsh.completers.path import _quote_paths
from xonsh.completers.tools import RichCompletion, contextual_command_completer
from xonsh.parsers.completion_context import CommandContext
@ -41,7 +40,6 @@ def complete_from_bash(context: CommandContext):
env=env,
paths=paths,
command=command,
quote_paths=_quote_paths,
line_args=args,
opening_quote=opening_quote,
closing_quote=closing_quote,

View file

@ -186,7 +186,7 @@ def _bash_quote_paths(paths, start, end):
start = end = _bash_quote_to_use(s)
if os.path.isdir(_bash_expand_path(s)):
_tail = slash
elif end == "":
elif end == "" and not s.endswith("="):
_tail = space
else:
_tail = ""