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
|
|
|
import pytest
|
|
|
|
|
|
|
|
from xonsh.completers.bash import complete_from_bash
|
2022-01-31 21:26:34 +05:30
|
|
|
from xonsh.completers.tools import RichCompletion
|
2021-05-20 15:44:26 +05:30
|
|
|
from xonsh.parsers.completion_context import (
|
|
|
|
CommandArg,
|
2022-01-31 21:26:34 +05:30
|
|
|
CommandContext,
|
|
|
|
CompletionContext,
|
2021-05-20 15:44:26 +05:30
|
|
|
)
|
2022-03-24 00:46:50 +05:30
|
|
|
from xonsh.pytest.tools import skip_if_on_darwin, skip_if_on_windows
|
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.fixture(autouse=True)
|
2021-05-20 15:44:26 +05:30
|
|
|
def setup(monkeypatch, tmp_path, xession):
|
|
|
|
if not xession.env.get("BASH_COMPLETIONS"):
|
|
|
|
monkeypatch.setitem(
|
|
|
|
xession.env,
|
|
|
|
"BASH_COMPLETIONS",
|
|
|
|
["/usr/share/bash-completion/bash_completion"],
|
|
|
|
)
|
2021-04-12 22:37:35 +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
|
|
|
(tmp_path / "testdir").mkdir()
|
|
|
|
(tmp_path / "spaced dir").mkdir()
|
|
|
|
monkeypatch.chdir(str(tmp_path))
|
|
|
|
|
|
|
|
|
|
|
|
@skip_if_on_darwin
|
|
|
|
@skip_if_on_windows
|
2021-05-20 15:44:26 +05:30
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"command_context, completions, lprefix",
|
|
|
|
(
|
|
|
|
(
|
|
|
|
CommandContext(args=(CommandArg("bash"),), arg_index=1, prefix="--deb"),
|
|
|
|
{"--debug", "--debugger"},
|
|
|
|
5,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
CommandContext(args=(CommandArg("ls"),), arg_index=1, prefix=""),
|
|
|
|
{"'testdir/'", "'spaced dir/'"},
|
|
|
|
0,
|
|
|
|
),
|
2022-02-11 13:01:41 +01:00
|
|
|
# tar replaces "~/" with "/home/user/", the change should be rolledback by us.
|
|
|
|
(
|
|
|
|
CommandContext(args=(CommandArg("tar"),), arg_index=1, prefix="~/"),
|
|
|
|
{"~/c", "~/u", "~/t", "~/d", "~/A", "~/r", "~/x"},
|
|
|
|
2,
|
|
|
|
),
|
2021-05-20 15:44:26 +05:30
|
|
|
(
|
|
|
|
CommandContext(
|
|
|
|
args=(CommandArg("ls"),), arg_index=1, prefix="", opening_quote="'"
|
|
|
|
),
|
|
|
|
{"'testdir/'", "'spaced dir/'"},
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2021-04-12 22:37:35 +03:00
|
|
|
def test_bash_completer(command_context, completions, lprefix):
|
2021-05-20 15:44:26 +05:30
|
|
|
bash_completions, bash_lprefix = complete_from_bash(
|
|
|
|
CompletionContext(command_context)
|
|
|
|
)
|
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 bash_completions == completions and bash_lprefix == lprefix
|
|
|
|
|
2021-04-12 22:37:35 +03:00
|
|
|
|
|
|
|
@skip_if_on_darwin
|
|
|
|
@skip_if_on_windows
|
2021-05-20 15:44:26 +05:30
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"command_context, completions, lprefix",
|
|
|
|
(
|
2021-04-12 22:37:35 +03:00
|
|
|
# ls /pro<TAB> -> ls /proc/
|
2021-05-20 15:44:26 +05:30
|
|
|
(
|
|
|
|
CommandContext(args=(CommandArg("ls"),), arg_index=1, prefix="/pro"),
|
|
|
|
{"/proc/"},
|
|
|
|
4,
|
|
|
|
),
|
2021-04-12 22:37:35 +03:00
|
|
|
# ls '/pro<TAB> -> ls '/proc/'
|
2021-05-20 15:44:26 +05:30
|
|
|
(
|
|
|
|
CommandContext(
|
|
|
|
args=(CommandArg("ls"),), arg_index=1, prefix="/pro", opening_quote="'"
|
|
|
|
),
|
|
|
|
{"'/proc/'"},
|
|
|
|
5,
|
|
|
|
),
|
2021-04-12 22:37:35 +03:00
|
|
|
# ls '/pro<TAB>' -> ls '/proc/'
|
2021-05-20 15:44:26 +05:30
|
|
|
(
|
|
|
|
CommandContext(
|
|
|
|
args=(CommandArg("ls"),),
|
|
|
|
arg_index=1,
|
|
|
|
prefix="/pro",
|
|
|
|
opening_quote="'",
|
|
|
|
closing_quote="'",
|
|
|
|
),
|
|
|
|
{"'/proc/"},
|
|
|
|
5,
|
|
|
|
),
|
2021-04-12 22:37:35 +03:00
|
|
|
# ls '/pro'<TAB> -> ls '/proc/'
|
2021-05-20 15:44:26 +05:30
|
|
|
(
|
|
|
|
CommandContext(
|
|
|
|
args=(CommandArg("ls"),),
|
|
|
|
arg_index=1,
|
|
|
|
prefix="/pro",
|
|
|
|
opening_quote="'",
|
|
|
|
closing_quote="'",
|
|
|
|
is_after_closing_quote=True,
|
|
|
|
),
|
|
|
|
{"'/proc/'"},
|
|
|
|
6,
|
|
|
|
),
|
2021-04-12 22:37:35 +03:00
|
|
|
# ls """/pro"""<TAB> -> ls """/proc/"""
|
2021-05-20 15:44:26 +05:30
|
|
|
(
|
|
|
|
CommandContext(
|
|
|
|
args=(CommandArg("ls"),),
|
|
|
|
arg_index=1,
|
|
|
|
prefix="/pro",
|
|
|
|
opening_quote='"""',
|
|
|
|
closing_quote='"""',
|
|
|
|
is_after_closing_quote=True,
|
|
|
|
),
|
|
|
|
{'"""/proc/"""'},
|
|
|
|
10,
|
|
|
|
),
|
2021-04-12 22:37:35 +03:00
|
|
|
# Completions that have to be quoted:
|
|
|
|
# ls ./sp -> ls './spaced dir/'
|
2021-05-20 15:44:26 +05:30
|
|
|
(
|
|
|
|
CommandContext(args=(CommandArg("ls"),), arg_index=1, prefix="./sp"),
|
|
|
|
{"'./spaced dir/'"},
|
|
|
|
4,
|
|
|
|
),
|
2021-04-12 22:37:35 +03:00
|
|
|
# ls './sp<TAB> -> ls './spaced dir/'
|
2021-05-20 15:44:26 +05:30
|
|
|
(
|
|
|
|
CommandContext(
|
|
|
|
args=(CommandArg("ls"),), arg_index=1, prefix="./sp", opening_quote="'"
|
|
|
|
),
|
|
|
|
{"'./spaced dir/'"},
|
|
|
|
5,
|
|
|
|
),
|
2021-04-12 22:37:35 +03:00
|
|
|
# ls './sp<TAB>' -> ls './spaced dir/'
|
2021-05-20 15:44:26 +05:30
|
|
|
(
|
|
|
|
CommandContext(
|
|
|
|
args=(CommandArg("ls"),),
|
|
|
|
arg_index=1,
|
|
|
|
prefix="./sp",
|
|
|
|
opening_quote="'",
|
|
|
|
closing_quote="'",
|
|
|
|
),
|
|
|
|
{"'./spaced dir/"},
|
|
|
|
5,
|
|
|
|
),
|
2021-04-12 22:37:35 +03:00
|
|
|
# ls './sp'<TAB> -> ls './spaced dir/'
|
2021-05-20 15:44:26 +05:30
|
|
|
(
|
|
|
|
CommandContext(
|
|
|
|
args=(CommandArg("ls"),),
|
|
|
|
arg_index=1,
|
|
|
|
prefix="./sp",
|
|
|
|
opening_quote="'",
|
|
|
|
closing_quote="'",
|
|
|
|
is_after_closing_quote=True,
|
|
|
|
),
|
|
|
|
{"'./spaced dir/'"},
|
|
|
|
6,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2021-04-12 22:37:35 +03:00
|
|
|
def test_quote_handling(command_context, completions, lprefix):
|
2021-05-20 15:44:26 +05:30
|
|
|
bash_completions, bash_lprefix = complete_from_bash(
|
|
|
|
CompletionContext(command_context)
|
|
|
|
)
|
2021-04-12 22:37:35 +03:00
|
|
|
assert bash_completions == completions and bash_lprefix == lprefix
|
2021-05-20 15:44:26 +05:30
|
|
|
assert all(
|
|
|
|
isinstance(comp, RichCompletion) and not comp.append_closing_quote
|
|
|
|
for comp in bash_completions
|
|
|
|
) # make sure the completer handles the closing quote by itself
|
2021-06-20 12:54:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
@skip_if_on_darwin
|
|
|
|
@skip_if_on_windows
|
|
|
|
def test_bash_completer_empty_prefix():
|
|
|
|
context = CompletionContext(
|
|
|
|
CommandContext(args=(CommandArg("git"),), arg_index=1, prefix="")
|
|
|
|
)
|
|
|
|
bash_completions, bash_lprefix = complete_from_bash(context)
|
|
|
|
assert {"clean", "show"}.issubset(bash_completions)
|
2022-04-15 22:52:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
@skip_if_on_darwin
|
|
|
|
@skip_if_on_windows
|
|
|
|
@pytest.mark.parametrize(
|
2022-04-21 11:38:30 -04:00
|
|
|
"command_context, completions, lprefix, exp_append_space",
|
2022-04-15 22:52:54 -04:00
|
|
|
(
|
|
|
|
# dd sta -> dd status=
|
|
|
|
(
|
|
|
|
CommandContext(args=(CommandArg("dd"),), arg_index=1, prefix="sta"),
|
|
|
|
{"status="},
|
|
|
|
3,
|
2022-04-21 11:38:30 -04:00
|
|
|
False,
|
2022-04-15 22:52:54 -04:00
|
|
|
),
|
2023-12-31 18:08:28 +03:00
|
|
|
# date --u -> date --utc
|
2022-04-15 22:52:54 -04:00
|
|
|
(
|
2023-12-31 18:08:28 +03:00
|
|
|
CommandContext(args=(CommandArg("date"),), arg_index=1, prefix="--u"),
|
|
|
|
{"--utc"},
|
|
|
|
3,
|
|
|
|
True,
|
2022-04-21 11:38:30 -04:00
|
|
|
),
|
|
|
|
# dd status=pr -> dd status=progress
|
|
|
|
(
|
|
|
|
CommandContext(args=(CommandArg("dd"),), arg_index=1, prefix="status=pr"),
|
|
|
|
{"progress"},
|
|
|
|
2,
|
|
|
|
True,
|
|
|
|
),
|
|
|
|
# dd if=/et -> dd if=/etc/
|
|
|
|
(
|
|
|
|
CommandContext(args=(CommandArg("dd"),), arg_index=1, prefix="if=/et"),
|
|
|
|
{"/etc/"},
|
|
|
|
3,
|
|
|
|
False,
|
|
|
|
),
|
|
|
|
# dd of=/dev/nul -> dd of=/dev/null
|
|
|
|
(
|
|
|
|
CommandContext(args=(CommandArg("dd"),), arg_index=1, prefix="of=/dev/nul"),
|
|
|
|
{"/dev/null"},
|
|
|
|
8,
|
|
|
|
True,
|
2022-04-15 22:52:54 -04:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2022-04-21 11:38:30 -04:00
|
|
|
def test_equal_sign_arg(command_context, completions, lprefix, exp_append_space):
|
2022-04-15 22:52:54 -04:00
|
|
|
bash_completions, bash_lprefix = complete_from_bash(
|
|
|
|
CompletionContext(command_context)
|
|
|
|
)
|
|
|
|
assert bash_completions == completions and bash_lprefix == lprefix
|
|
|
|
assert all(
|
2022-04-21 11:38:30 -04:00
|
|
|
isinstance(comp, RichCompletion) and comp.append_space == exp_append_space
|
2022-04-15 22:52:54 -04:00
|
|
|
for comp in bash_completions
|
2022-04-21 11:38:30 -04:00
|
|
|
)
|
2022-08-06 04:07:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def bash_completer(fake_process):
|
|
|
|
fake_process.register_subprocess(
|
|
|
|
command=["bash", fake_process.any()],
|
|
|
|
# completion for "git push origin :dev-b"
|
|
|
|
stdout=b"""\
|
|
|
|
complete -o bashdefault -o default -o nospace -F __git_wrap__git_main git
|
|
|
|
dev-branch
|
|
|
|
""",
|
|
|
|
)
|
|
|
|
|
|
|
|
return fake_process
|
|
|
|
|
|
|
|
|
|
|
|
# git push origin :dev-b<TAB> -> git push origin :dev-branch
|
|
|
|
def test_git_delete_remote_branch(bash_completer):
|
|
|
|
command_context = CommandContext(
|
|
|
|
args=(
|
|
|
|
CommandArg("git"),
|
|
|
|
CommandArg("push"),
|
|
|
|
CommandArg("origin"),
|
|
|
|
),
|
|
|
|
arg_index=3,
|
|
|
|
prefix=":dev-b",
|
|
|
|
)
|
|
|
|
bash_completions, bash_lprefix = complete_from_bash(
|
|
|
|
CompletionContext(command_context)
|
|
|
|
)
|
|
|
|
assert bash_completions == {"dev-branch"} and bash_lprefix == 5
|
|
|
|
assert all(
|
|
|
|
isinstance(comp, RichCompletion) and comp.append_space is False
|
|
|
|
for comp in bash_completions
|
|
|
|
)
|