xonsh/tests/test_completer.py

176 lines
5.1 KiB
Python
Raw Normal View History

Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
"""Tests for the base completer's logic (xonsh/completer.py)"""
import pytest
2022-01-31 21:26:34 +05:30
from xonsh.completer import Completer
from xonsh.completers.tools import (
RichCompletion,
contextual_command_completer,
non_exclusive_completer,
)
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
from xonsh.parsers.completion_context import CommandContext
@pytest.fixture(scope="session")
def completer():
return Completer()
@pytest.fixture
def completers_mock(xession, monkeypatch):
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
completers = {}
monkeypatch.setattr(xession, "_completers", completers)
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
return completers
def test_sanity(completer, completers_mock):
# no completions:
completers_mock["a"] = lambda *a: None
assert completer.complete("", "", 0, 0) == ((), 0)
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
# simple completion:
completers_mock["a"] = lambda *a: {"comp"}
assert completer.complete("pre", "", 0, 0) == (("comp",), 3)
# multiple completions:
completers_mock["a"] = lambda *a: {"comp1", "comp2"}
assert completer.complete("pre", "", 0, 0) == (("comp1", "comp2"), 3)
# custom lprefix:
completers_mock["a"] = lambda *a: ({"comp"}, 2)
assert completer.complete("pre", "", 0, 0) == (("comp",), 2)
# RichCompletion:
completers_mock["a"] = lambda *a: {RichCompletion("comp", prefix_len=5)}
assert completer.complete("pre", "", 0, 0) == (
(RichCompletion("comp", prefix_len=5),),
3,
)
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
def test_cursor_after_closing_quote(completer, completers_mock):
"""See ``Completer.complete`` in ``xonsh/completer.py``"""
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
@contextual_command_completer
def comp(context: CommandContext):
return {context.prefix + "1", context.prefix + "2"}
completers_mock["a"] = comp
assert completer.complete(
"", "", 0, 0, {}, multiline_text="'test'", cursor_index=6
) == (("test1'", "test2'"), 5)
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
assert completer.complete(
"", "", 0, 0, {}, multiline_text="'''test'''", cursor_index=10
) == (("test1'''", "test2'''"), 7)
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
def test_cursor_after_closing_quote_override(completer, completers_mock):
"""Test overriding the default values"""
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
@contextual_command_completer
def comp(context: CommandContext):
return {
# replace the closing quote with "a"
RichCompletion(
"a", prefix_len=len(context.closing_quote), append_closing_quote=False
),
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
# add text after the closing quote
RichCompletion(context.prefix + "_no_quote", append_closing_quote=False),
# sanity
RichCompletion(context.prefix + "1"),
}
completers_mock["a"] = comp
assert completer.complete(
"", "", 0, 0, {}, multiline_text="'test'", cursor_index=6
) == (
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
(
"a",
"test1'",
"test_no_quote",
),
5,
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
)
assert completer.complete(
"", "", 0, 0, {}, multiline_text="'''test'''", cursor_index=10
) == (
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
(
"a",
"test1'''",
"test_no_quote",
),
7,
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
)
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
def test_append_space(completer, completers_mock):
@contextual_command_completer
def comp(context: CommandContext):
return {
RichCompletion(context.prefix + "a", append_space=True),
RichCompletion(context.prefix + " ", append_space=False), # bad usage
RichCompletion(
context.prefix + "b", append_space=True, append_closing_quote=False
),
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
}
completers_mock["a"] = comp
assert completer.complete(
"", "", 0, 0, {}, multiline_text="'test'", cursor_index=6
) == (
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
(
"test '",
"testa' ",
"testb ",
),
5,
Completion context (#4017) * 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
2021-03-30 20:37:56 +03:00
)
@pytest.mark.parametrize(
"middle_result, exp",
(
(
# stop at the first exclusive result
({"b1", "b2"}, ("a1", "a2", "b1", "b2")),
# pass empty exclusive results
({}, ("a1", "a2", "c1", "c2")),
# pass empty exclusive results
(None, ("a1", "a2", "c1", "c2")),
# stop at StopIteration
(StopIteration, ("a1", "a2")),
)
),
)
def test_non_exclusive(completer, completers_mock, middle_result, exp):
completers_mock["a"] = non_exclusive_completer(lambda *a: {"a1", "a2"})
def middle(*a):
if middle_result is StopIteration:
raise StopIteration()
return middle_result
completers_mock["b"] = middle
completers_mock["c"] = non_exclusive_completer(lambda *a: {"c1", "c2"})
assert completer.complete("", "", 0, 0, {})[0] == exp
def test_env_completer_sort(completer, completers_mock):
@contextual_command_completer
def comp(context: CommandContext):
return {"$SUPER_WOW", "$WOW1", "$WOW0", "$MID_WOW"}
completers_mock["a"] = comp
comps = completer.complete(
"$WOW", "$WOW", 4, 0, {}, multiline_text="'$WOW'", cursor_index=4
)
assert set(comps[0]) == {"$WOW0", "$WOW1", "$MID_WOW", "$SUPER_WOW"}
def test_python_only_context(completer, completers_mock):
assert completer.complete_line("echo @(") != ()
assert completer.complete("", "echo @(", 0, 0, {}, "echo @(", 7) != ()