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

Custom completion per shell (zsh, bash, or fish) #672

Open
2 tasks done
rgoldberg opened this issue Sep 27, 2024 · 0 comments · May be fixed by #680
Open
2 tasks done

Custom completion per shell (zsh, bash, or fish) #672

rgoldberg opened this issue Sep 27, 2024 · 0 comments · May be fixed by #680

Comments

@rgoldberg
Copy link
Contributor

rgoldberg commented Sep 27, 2024

Problem

Different shells use different syntaxes for their completions.

e.g., zsh's _describe accepts a different syntax than bash's compgen.

The completion: parameter for @Argument & for @Option is used for all shells.

To the best of my knowledge, Swift Argument Parser (SAP) doesn't inform Swift the shell for which completions are being requested.

It is thus difficult to output the correct values to properly configure completion in multiple shells.

Versions

ArgumentParser version:
1.5.0

Swift version:
swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
Target: x86_64-apple-macosx12.0

Checklist

  • If possible, I've reproduced the issue using the main branch of this package
  • I've searched for existing GitHub issues

Steps to Reproduce

  1. Look for some way to know what shell is requesting completions.
  2. Fail to find some way to know what shell is requesting completions.
  3. Profit!

Expected behavior

Ability to easily output completions that are correct for the current shell.

Actual behavior

Cannot easily output completions that are correct for the current shell.

Solution

There are many ways to solve this, but the simplest way to do so without changing the existing CompletionKinds or the completion: parameter of @Argument or @Option seems to be for SAP to provide a singleton CompletionShell named CompletionShell.requesting (requesting for short) that can be used by code supplying an associated value for a CompletionKind to determine the shell for which completions are being requested.

For .custom(:), requesting must be available at runtime when a completion script requests completions. For the other current CompletionKinds, requesting need only be available when a completion script is generated by using --generate-completion-script <shell>.

For .custom(:), each completion script generated by using --generate-completion-script <shell> will export an environment variable SAP_SHELL = <shell> so SAP can use it to populate requesting before it calls completion from .custom(:).

# bash
export SAP_SHELL=bash
# fish
set -x SAP_SHELL fish
# zsh
export SAP_SHELL=zsh

For the other current CompletionKinds, the *CompletionsGenerator.generateCompletionScript(type:) function will populate requesting with the correct value before calling the shell-specific *CompletionsGenerator.generateCompletionScript(:). Any function called to supply the associated value for these CompletionKinds can read requesting to determine the shell for which completions are being requested.

rgoldberg added a commit to rgoldberg/swift-argument-parser that referenced this issue Oct 31, 2024
Swift shell completion functions can determine the shell requesting completions
from a CompletionShell singleton named CompletionShell.requesting.

For the custom(:) CompletionKind, the singleton is populated at runtime (when a
completion script requests completions from the Swift app) from an environment
variable export by each completion script.

For all other CompletionKinds, the singleton is populated when a completion
script is generated.

Resolve apple#672

Signed-off-by: Ross Goldberg <[email protected]>
rgoldberg added a commit to rgoldberg/swift-argument-parser that referenced this issue Nov 1, 2024
A CompletionShell singleton named CompletionShell.requesting has been created
that indicates which shell is requesting completion candidates.

The singleton is populated when a completion script is generated, so functions
used to generate arguments for CompletionKind creation functions can return
completion candidate syntax / shell commands tailored for that shell.

For the custom(:) CompletionKind creation function, the singleton is populated
at runtime (when a completion script requests completions from the Swift app
after a user types tab while composing a command line to call the app).

The requesting shell is communicated to the Swift app via an environment variable
named SAP_SHELL, which is exported by each of the generated completion scripts.

Resolve apple#672

Signed-off-by: Ross Goldberg <[email protected]>
rgoldberg added a commit to rgoldberg/swift-argument-parser that referenced this issue Nov 1, 2024
…ates

A CompletionShell singleton named CompletionShell.requesting has been created
that indicates which shell is requesting completion candidates.

The singleton is populated when a completion script is generated, so functions
used to generate arguments for CompletionKind creation functions can return
completion candidate syntax / shell commands tailored for that shell.

For the custom(:) CompletionKind creation function, the singleton is populated
at runtime (when a completion script requests completions from the Swift app
after a user types tab while composing a command line to call the app).

The requesting shell is communicated to the Swift app via an environment variable
named SAP_SHELL, which is exported by each of the generated completion scripts.

Resolve apple#672

Signed-off-by: Ross Goldberg <[email protected]>
rgoldberg added a commit to rgoldberg/swift-argument-parser that referenced this issue Nov 1, 2024
…ates

A CompletionShell singleton named CompletionShell.requesting has been created
that indicates which shell is requesting completion candidates.

The singleton is populated when a completion script is generated, so functions
used to generate arguments for CompletionKind creation functions can return
completion candidate syntax / shell commands tailored for that shell.

For the custom(:) CompletionKind creation function, the singleton is populated
at runtime (when a completion script requests completions from the Swift app
after a user types tab while composing a command line to call the app).

The requesting shell is communicated to the Swift app via an environment variable
named SAP_SHELL, which is exported by each of the generated completion scripts.

Resolve apple#672

Signed-off-by: Ross Goldberg <[email protected]>
rgoldberg added a commit to rgoldberg/swift-argument-parser that referenced this issue Nov 1, 2024
…ates

A CompletionShell singleton named CompletionShell.requesting has been created
that indicates which shell is requesting completion candidates.

The singleton is populated when a completion script is generated, so functions
used to generate arguments for CompletionKind creation functions can return
completion candidate syntax / shell commands tailored for that shell.

For the custom(:) CompletionKind creation function, the singleton is populated
at runtime (when a completion script requests completions from the Swift app
after a user types tab while composing a command line to call the app).

The requesting shell is communicated to the Swift app via an environment variable
named SAP_SHELL, which is exported by each of the generated completion scripts.

Resolve apple#672

Signed-off-by: Ross Goldberg <[email protected]>
rgoldberg added a commit to rgoldberg/swift-argument-parser that referenced this issue Nov 24, 2024
…ates

A CompletionShell singleton named CompletionShell.requesting has been created
that indicates which shell is requesting completion candidates.

The singleton is populated when a completion script is generated, so functions
used to generate arguments for CompletionKind creation functions can return
completion candidate syntax / shell commands tailored for that shell.

For the custom(:) CompletionKind creation function, the singleton is populated
at runtime (when a completion script requests completions from the Swift app
after a user types tab while composing a command line to call the app).

The requesting shell is communicated to the Swift app via an environment variable
named SAP_SHELL, which is exported by each of the generated completion scripts.

Resolve apple#672

Signed-off-by: Ross Goldberg <[email protected]>
rgoldberg added a commit to rgoldberg/swift-argument-parser that referenced this issue Dec 5, 2024
…ates

A CompletionShell singleton named CompletionShell.requesting has been created
that indicates which shell is requesting completion candidates.

The singleton is populated when a completion script is generated, so functions
used to generate arguments for CompletionKind creation functions can return
completion candidate syntax / shell commands tailored for that shell.

For the custom(:) CompletionKind creation function, the singleton is populated
at runtime (when a completion script requests completions from the Swift app
after a user types tab while composing a command line to call the app).

The requesting shell is communicated to the Swift app via an environment variable
named SAP_SHELL, which is exported by each of the generated completion scripts.

Resolve apple#672

Signed-off-by: Ross Goldberg <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant