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

Refactor Bash completions to use ToolInfo #695

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rauhul
Copy link
Contributor

@rauhul rauhul commented Feb 5, 2025

Rebases the implementation of the BashCompletionGenerator to use
ToolInfo from ArgumentParserToolInfo instead of digging through the
command structure. This helps us decouple the implementation of Argument
parsing from the generation of supplemental content such as docs,
man-pages, completion scripts, help menus and more.

@rauhul rauhul force-pushed the rebase-bash-completion-on-tool-info branch 3 times, most recently from 04ba818 to dae8159 Compare February 6, 2025 17:00
Rebases the implementation of the BashCompletionGenerator to use
ToolInfo from ArgumentParserToolInfo instead of digging through the
command structure. This helps us decouple the implementation of Argument
parsing from the generation of supplemental content such as docs,
man-pages, completion scripts, help menus and more.
@rauhul rauhul force-pushed the rebase-bash-completion-on-tool-info branch from dae8159 to d66a07b Compare February 6, 2025 18:35
@rauhul rauhul changed the title WIP: use toolinfo for completion gen Refactor Bash completions to use ToolInfo Feb 6, 2025
@rauhul rauhul marked this pull request as ready for review February 6, 2025 18:36
@rauhul
Copy link
Contributor Author

rauhul commented Feb 6, 2025

@swift-ci test

@rauhul rauhul requested a review from natecook1000 February 6, 2025 18:36
@rauhul
Copy link
Contributor Author

rauhul commented Feb 6, 2025

@rgoldberg mind reviewing this PR? it should have minimal functional changes.

Copy link
Contributor

@rgoldberg rgoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See this branch on my fork for recommended changes for BashCompletionsGenerator.swift that are local to the file (my other recommendations for the file require updating other Swift files, too, but those aren't in my branch). My recommendations that are local to other files are just in my review comments.

@@ -191,9 +191,17 @@ extension ArgumentSet {
}

func firstPositional(
withKey key: InputKey
withKey key: InputKey,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove , because it breaks the build in Swift 5.7

Comment on lines +169 to 188
var result = [String]()
for argument in self.arguments ?? [] {
// Skip hidden arguments.
guard argument.shouldDisplay else { continue }
// Flags don't take a value, so we don't provide follow-on completions.
guard argument.kind != .flag else { continue }
// Skip if no keys.
let keys = argument.bashCompletionKeys()
guard !keys.isEmpty else { continue }
// Skip if no completions.
guard let completionValues = argument.bashOptionCompletionValues(command: self) else { continue }
result.append("""
\(keys.joined(separator: "|")))
\(completionValues.indentingEachLine(by: 4))
return
;;
"""
}
.joined(separator: "\n")
""")
}
return result
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer compactMap {…}

}

extension ArgumentInfoV0.NameInfoV0 {
func commonCompletionSynopsisString() -> String {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the DocC for NameInfoV0 specifies the double & single dash prefixes, maybe move this extension into ToolInfo.swift, rename commonCompletionSynopsisString as synopsis, and change from func to var.

@@ -139,7 +139,7 @@ _math_stats_quantiles() {
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_math_help() {
opts="--version"
opts=""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be opts="--version", as --version is a valid flag for math help.

) -> String? {
precondition(self.kind == .option)

switch self.completionKind {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent by 2 more spaces.

@rauhul
Copy link
Contributor Author

rauhul commented Feb 7, 2025

@rgoldberg formatting issue will be resolved in a future PR which enables swift-format, im going to mark them as resolved

@rgoldberg
Copy link
Contributor

rgoldberg commented Feb 11, 2025

@rauhul @natecook1000 Is there any way to postpone modifying the completion script code & tests any more until after PRs for #679 have been merged?

That will allow me to save a lot of time by not needing to drag massive changes through (currently) 85 commits; it took a while to drag through the formatting changes from #711 & the testing changes from #698, #699 & #700.

It will take time to drag through changes from this PR (& from any potential similar PRs for zsh & fish).

It would be easiest if all other completion work (including this PR) could be delayed until after #679 is done. I'm happy to submit an equivalent of this PR (& ones for zsh & fish) after #679 is done, to save you the trouble of having to do it.

@natecook1000
Copy link
Member

@rgoldberg Agreed on the order here – go ahead and get your changes lined up, and when that work has wrapped up we can do this migration. Thanks!

@rgoldberg
Copy link
Contributor

@natecook1000 @rauhul My existing commits don't fix up all of the issues that I've found in completions.

I assume that after my existing commits have been merged, it makes sense to migrate to ToolInfo, then resume completion fixes after that has been merged.

Are you guys planning any other large-scale changes to completions or anything with which they interact?

@natecook1000
Copy link
Member

The goal is eventually to have everything going through a single path, rather than the separate generators for tool info, help, and completion scripts that we started with. That should both make things simpler to maintain and guarantee that we're including everything in ToolInfo, so that other tools built on top of that have full access to the information they might need. Other than that I don't believe there are planned changes for completions (other than as required to support other new features).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants