mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00

* completion-context: Add CompletionContextParser placeholder Implements the xonsh (tab-)completion context parser. This parser is meant to parse a (possibly incomplete) command line. * completers: tools: Implement ``contextual_completer`` decorator This is used to mark completers that want to use the parsed completion context. * completers: Enable using contextual completers in xonsh/completer.py * completers: readline, ptk, jupyter: Enable using contextual completers Pass ``multiline_text`` and ``cursor_index`` to ``Completer.complete()`` * parsers: base: Refactor out a ``raise_parse_error`` function * tokenize: Enable ``tolerant`` mode If ``tolerant`` is True, yield ERRORTOKEN instead of throwing an exception when encountering an error. * lexer: Enable ``tolerant`` mode Tokenize without extra checks (e.g. paren matching). When True, ERRORTOKEN contains the erroneous string instead of an error msg. * tests: lexer: Test ``tolerant`` mode * completion-context: Implement simple CommandContext parsing * completion-context: tests: Test simple CommandContext parsing * completion-context: Implement parsing sub-commands * completion-context: tests: Test parsing sub-commands * completion-context: Add news file * completion-context: parser: Add parser table path to relevant locations Code-coverage, mypy ignore list, etc. * completion-context: Implement parsing partial strings and line continuations * completion-context: tests: Test parsing partial strings and line continuations * completion-context: Convert ``Span`` object to a ``slice`` * completion-context: Refactor out ``create_command`` and ``cursor_in_span`` * completion-context: Implement handling empty commands * completion-context: tests: Test handling empty commands * completion-context: Implement handling multiple commands Separated by newlines, `;`, `and`, `or`, `|`, `&&`, `||` * completion-context: tests: Test handling multiple commands Separated by newlines, `;`, `and`, `or`, `|`, `&&`, `||` * completion-context: Implement handling python context * completion-context: tests: Test handling python context * completers: tools: Add `contextual_command_completer` * completers: Make `complete_skipper` contextual * completers: Make `complete_from_man` contextual * completers: Make `complete_from_bash` contextual and add test * completers: Make `complete_pip` contextual and update tests * completers: Keep opening string quote if it exists * completion-context: Handle cursor after a closing quote For example - cursor at the end of ``ls "/usr/"``. 1. The closing quote will be appended to all completions. I.e the completion ``/usr/bin`` will turn into ``/usr/bin"`` 2. If not specified, lprefix will cover the closing prefix. I.e for ``ls "/usr/"``, the default lprefix will be 6 to include the closing quote. * completion-context: tests: Test handling cursor after a closing quote * completion-context: Fix bug with multiple empty commands e.g. `;;;` * completion-context: tests: Speed up tests From ~15 seconds to ~500 ms * completion-context: Expand commands and subcommands * completion-context: Simplify `commands` rules * completion-context: Simplify `sub_expression` rules * completion-context: Simplify editing a multi-command token * completion-context: Inline `create_command` * completion-context: Implement `contextual_command_completer_for` helper * completers: Make `complete_cd`/`complete_rmdir` contextual and add tests * completers: path: Don't append a double-backslash in a raw string When completing a path, if a raw string is used (e.g. `r"C:\Windows\"`), there's no reason to append a double-backslash (e.g. `r"C:\Windows\\"`). * completers: Make `complete_xonfig`/`complete_xontrib` contextual and add tests * completers: Make `complete_completer` contextual and add tests * completers: Make `complete_import` contextual and add tests * completion-context: Add python `ctx` attribute * completion: tools: Simplify `RichCompletion` attributes handling * completers: Make `base`, `python`, and `commands` contextual * Add tests * No need for `python_mode` completer anymore * completion: tools: Add `append_space` attribute to `RichCompletion` * completion-context: Get all lines in a main python context * xontrib: jedi: Make the `jedi` completer contextual * completers: tools: Remove `get_ptk_completer` and `PromptToolkitCompleter.current_document` These aren't needed anymore now that contextual completers can access the multiline code (via `PythonContext.multiline_code`). * completion-context: ptk: Expand aliases * completion-context: jupyter: Expand aliases and fix line handling * completer: Preserve custom prefix after closing quote * completers: bash: Ensure bash completion uses the complete prefix * completers: pip: Append a space after a pip command * completers: pip: Prevent bad package name completions * completers: Remove a common prefix from `RichCompletion` if `display` wasn't provided * completion-context: Treat cursor at edge of `&& || | ;` as normal args This will be used for completing a space * completers: Complete end proc keywords correctly
192 lines
5 KiB
Python
192 lines
5 KiB
Python
import pytest
|
|
from inspect import signature
|
|
from unittest.mock import MagicMock
|
|
from prompt_toolkit.document import Document
|
|
from prompt_toolkit.completion import Completion as PTKCompletion
|
|
|
|
from xonsh.aliases import Aliases
|
|
from xonsh.completer import Completer
|
|
from xonsh.completers.tools import RichCompletion
|
|
from xonsh.ptk_shell.completer import PromptToolkitCompleter
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"completion, lprefix, ptk_completion",
|
|
[
|
|
(RichCompletion("x", 0, "x()", "func"), 0, None),
|
|
(RichCompletion("x", 1, "xx", "instance"), 0, None),
|
|
(
|
|
RichCompletion("x", description="wow"),
|
|
5,
|
|
PTKCompletion(RichCompletion("x"), -5, "x", "wow"),
|
|
),
|
|
(RichCompletion("x"), 5, PTKCompletion(RichCompletion("x"), -5, "x")),
|
|
("x", 5, PTKCompletion("x", -5, "x")),
|
|
],
|
|
)
|
|
def test_rich_completion(
|
|
completion, lprefix, ptk_completion, monkeypatch, xonsh_builtins
|
|
):
|
|
xonsh_completer_mock = MagicMock()
|
|
xonsh_completer_mock.complete.return_value = {completion}, lprefix
|
|
|
|
ptk_completer = PromptToolkitCompleter(xonsh_completer_mock, None, None)
|
|
ptk_completer.reserve_space = lambda: None
|
|
ptk_completer.suggestion_completion = lambda _, __: None
|
|
|
|
document_mock = MagicMock()
|
|
document_mock.text = ""
|
|
document_mock.current_line = ""
|
|
document_mock.cursor_position_col = 0
|
|
|
|
monkeypatch.setattr("builtins.aliases", Aliases())
|
|
|
|
completions = list(ptk_completer.get_completions(document_mock, MagicMock()))
|
|
if isinstance(completion, RichCompletion) and not ptk_completion:
|
|
assert completions == [
|
|
PTKCompletion(
|
|
completion,
|
|
-completion.prefix_len,
|
|
completion.display,
|
|
completion.description,
|
|
)
|
|
]
|
|
else:
|
|
assert completions == [ptk_completion]
|
|
|
|
|
|
EXPANSION_CASES = (
|
|
(
|
|
"sanity", 6,
|
|
dict(
|
|
prefix="sanity",
|
|
line="sanity",
|
|
begidx=0,
|
|
endidx=6,
|
|
multiline_text="sanity",
|
|
cursor_index=6,
|
|
),
|
|
),
|
|
(
|
|
"gb ", 3,
|
|
dict(
|
|
prefix="",
|
|
line="git branch ",
|
|
begidx=11,
|
|
endidx=11,
|
|
multiline_text="git branch ",
|
|
cursor_index=11,
|
|
),
|
|
),
|
|
(
|
|
"gb ", 1,
|
|
dict(
|
|
prefix="g",
|
|
line="gb ",
|
|
begidx=0,
|
|
endidx=1,
|
|
multiline_text="gb ",
|
|
cursor_index=1,
|
|
),
|
|
),
|
|
(
|
|
"gb", 0,
|
|
dict(
|
|
prefix="",
|
|
line="gb",
|
|
begidx=0,
|
|
endidx=0,
|
|
multiline_text="gb",
|
|
cursor_index=0,
|
|
),
|
|
),
|
|
(
|
|
" gb ", 0,
|
|
dict(
|
|
prefix="",
|
|
line=" gb ", # the PTK completer `lstrip`s the line
|
|
begidx=0,
|
|
endidx=0,
|
|
multiline_text=" gb ",
|
|
cursor_index=0,
|
|
),
|
|
),
|
|
(
|
|
"gb --", 5,
|
|
dict(
|
|
prefix="--",
|
|
line="git branch --",
|
|
begidx=11,
|
|
endidx=13,
|
|
multiline_text="git branch --",
|
|
cursor_index=13,
|
|
),
|
|
),
|
|
(
|
|
"nice\ngb --", 10,
|
|
dict(
|
|
prefix="--",
|
|
line="git branch --",
|
|
begidx=11,
|
|
endidx=13,
|
|
multiline_text="nice\ngit branch --",
|
|
cursor_index=18,
|
|
),
|
|
),
|
|
(
|
|
"nice\n gb --", 11,
|
|
dict(
|
|
prefix="--",
|
|
line=" git branch --",
|
|
begidx=12,
|
|
endidx=14,
|
|
multiline_text="nice\n git branch --",
|
|
cursor_index=19,
|
|
),
|
|
),
|
|
(
|
|
"gb -- wow", 5,
|
|
dict(
|
|
prefix="--",
|
|
line="git branch -- wow",
|
|
begidx=11,
|
|
endidx=13,
|
|
multiline_text="git branch -- wow",
|
|
cursor_index=13,
|
|
),
|
|
),
|
|
(
|
|
"gb --wow", 5,
|
|
dict(
|
|
prefix="--",
|
|
line="git branch --wow",
|
|
begidx=11,
|
|
endidx=13,
|
|
multiline_text="git branch --wow",
|
|
cursor_index=13,
|
|
),
|
|
),
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
"code, index, expected_args",
|
|
EXPANSION_CASES
|
|
)
|
|
def test_alias_expansion(
|
|
code, index, expected_args, monkeypatch, xonsh_builtins
|
|
):
|
|
xonsh_completer_mock = MagicMock(spec=Completer)
|
|
xonsh_completer_mock.complete.return_value = set(), 0
|
|
|
|
ptk_completer = PromptToolkitCompleter(xonsh_completer_mock, None, None)
|
|
ptk_completer.reserve_space = lambda: None
|
|
ptk_completer.suggestion_completion = lambda _, __: None
|
|
|
|
monkeypatch.setattr("builtins.aliases", Aliases(gb=["git branch"]))
|
|
|
|
list(ptk_completer.get_completions(Document(code, index), MagicMock()))
|
|
mock_call = xonsh_completer_mock.complete.call_args
|
|
args, kwargs = mock_call
|
|
expected_args["self"] = None
|
|
expected_args["ctx"] = None
|
|
assert signature(Completer.complete).bind(None, *args, **kwargs).arguments == expected_args
|