Skip to content

Commit

Permalink
Improve custom completion args for fish 4+.
Browse files Browse the repository at this point in the history
Resolve apple#737

Signed-off-by: Ross Goldberg <[email protected]>
  • Loading branch information
rgoldberg committed Feb 19, 2025
1 parent 3342f37 commit d89069c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
24 changes: 19 additions & 5 deletions Sources/ArgumentParser/Completions/FishCompletionsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@ extension [ParsableCommand.Type] {
end
end
function \(tokensFunctionName)
if test (string split -m 1 -f 1 . $FISH_VERSION) -gt 3
commandline --tokens-raw $argv
else
commandline -o $argv
end
end
function \(usingCommandFunctionName) -a expected_commands
set COMMANDS
set POSITIONALS (commandline -opc)
set POSITIONALS (\(tokensFunctionName) -pc)
\(commandsAndPositionalsFunctionName)
test "$COMMANDS" = $expected_commands
end
function \(positionalIndexFunctionName)
set POSITIONALS (commandline -opc)
set POSITIONALS (\(tokensFunctionName) -pc)
\(commandsAndPositionalsFunctionName)
math (count $POSITIONALS) + 1
end
Expand All @@ -63,9 +71,9 @@ extension [ParsableCommand.Type] {
set -x \(CompletionShell.shellEnvironmentVariableName) fish
set -x \(CompletionShell.shellVersionEnvironmentVariableName) $FISH_VERSION
set tokens (commandline -op)
if test -z (commandline -ot)
set index (count (commandline -opc))
set tokens (\(tokensFunctionName) -p)
if test -z (\(tokensFunctionName) -t)
set index (count (\(tokensFunctionName) -pc))
set tokens $tokens[..$index] \\'\\' $tokens[$(math $index + 1)..]
end
command $tokens[1] $argv $tokens
Expand Down Expand Up @@ -234,6 +242,12 @@ extension [ParsableCommand.Type] {
"_swift_\(first!._commandName)_commands_and_positionals"
}

private var tokensFunctionName: String {
// swift-format-ignore: NeverForceUnwrap
// Precondition: first is guaranteed to be non-empty
"_swift_\(first!._commandName)_tokens"
}

private var usingCommandFunctionName: String {
// swift-format-ignore: NeverForceUnwrap
// Precondition: first is guaranteed to be non-empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,23 @@ function _swift_math_commands_and_positionals_helper -S -a argparse_options -a o
end
end

function _swift_math_tokens
if test (string split -m 1 -f 1 . $FISH_VERSION) -gt 3
commandline --tokens-raw $argv
else
commandline -o $argv
end
end

function _swift_math_using_command -a expected_commands
set COMMANDS
set POSITIONALS (commandline -opc)
set POSITIONALS (_swift_math_tokens -pc)
_swift_math_commands_and_positionals
test "$COMMANDS" = $expected_commands
end

function _swift_math_positional_index
set POSITIONALS (commandline -opc)
set POSITIONALS (_swift_math_tokens -pc)
_swift_math_commands_and_positionals
math (count $POSITIONALS) + 1
end
Expand All @@ -61,9 +69,9 @@ function _swift_math_custom_completion
set -x SAP_SHELL fish
set -x SAP_SHELL_VERSION $FISH_VERSION

set tokens (commandline -op)
if test -z (commandline -ot)
set index (count (commandline -opc))
set tokens (_swift_math_tokens -p)
if test -z (_swift_math_tokens -t)
set index (count (_swift_math_tokens -pc))
set tokens $tokens[..$index] \'\' $tokens[$(math $index + 1)..]
end
command $tokens[1] $argv $tokens
Expand Down
18 changes: 13 additions & 5 deletions Tests/ArgumentParserUnitTests/Snapshots/testBase_Fish().fish
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ function _swift_base-test_commands_and_positionals_helper -S -a argparse_options
end
end

function _swift_base-test_tokens
if test (string split -m 1 -f 1 . $FISH_VERSION) -gt 3
commandline --tokens-raw $argv
else
commandline -o $argv
end
end

function _swift_base-test_using_command -a expected_commands
set COMMANDS
set POSITIONALS (commandline -opc)
set POSITIONALS (_swift_base-test_tokens -pc)
_swift_base-test_commands_and_positionals
test "$COMMANDS" = $expected_commands
end

function _swift_base-test_positional_index
set POSITIONALS (commandline -opc)
set POSITIONALS (_swift_base-test_tokens -pc)
_swift_base-test_commands_and_positionals
math (count $POSITIONALS) + 1
end
Expand All @@ -45,9 +53,9 @@ function _swift_base-test_custom_completion
set -x SAP_SHELL fish
set -x SAP_SHELL_VERSION $FISH_VERSION

set tokens (commandline -op)
if test -z (commandline -ot)
set index (count (commandline -opc))
set tokens (_swift_base-test_tokens -p)
if test -z (_swift_base-test_tokens -t)
set index (count (_swift_base-test_tokens -pc))
set tokens $tokens[..$index] \'\' $tokens[$(math $index + 1)..]
end
command $tokens[1] $argv $tokens
Expand Down

0 comments on commit d89069c

Please sign in to comment.