Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix PowerShell completion with incomplete word #360

Merged
merged 10 commits into from
Aug 24, 2024
5 changes: 4 additions & 1 deletion typer/_completion_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,11 @@ def source_vars(self) -> Dict[str, Any]:
def get_completion_args(self) -> Tuple[List[str], str]:
completion_args = os.getenv("_TYPER_COMPLETE_ARGS", "")
incomplete = os.getenv("_TYPER_COMPLETE_WORD_TO_COMPLETE", "")
cursor = os.getenv("_TYPER_CURSOR_POSITION")
if cursor:
completion_args = completion_args[: int(cursor)]
cwords = click.parser.split_arg_string(completion_args)
args = cwords[1:]
args = cwords[1:-1] if incomplete else cwords[1:]
return args, incomplete

def format_completion(self, item: click.shell_completion.CompletionItem) -> str:
Expand Down
2 changes: 2 additions & 0 deletions typer/_completion_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Shells(str, Enum):
$Env:%(autocomplete_var)s = "complete_powershell"
$Env:_TYPER_COMPLETE_ARGS = $commandAst.ToString()
$Env:_TYPER_COMPLETE_WORD_TO_COMPLETE = $wordToComplete
$Env:_TYPER_CURSOR_POSITION = $cursorPosition
%(prog_name)s | ForEach-Object {
$commandArray = $_ -Split ":::"
$command = $commandArray[0]
Expand All @@ -63,6 +64,7 @@ class Shells(str, Enum):
$Env:%(autocomplete_var)s = ""
$Env:_TYPER_COMPLETE_ARGS = ""
$Env:_TYPER_COMPLETE_WORD_TO_COMPLETE = ""
$Env:_TYPER_CURSOR_POSITION = ""
}
Register-ArgumentCompleter -Native -CommandName %(prog_name)s -ScriptBlock $scriptblock
"""
Expand Down
Loading