-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Support the carapace CLI completions tool #67
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2b9381d
refactor: Moved completion command to own folder
christerswahn 0141510
refactor: Broke out completely generator into own file
christerswahn 8d78a85
feat(config): multiParser getter in MultiOption
christerswahn eda37c6
feat(cli_tools): Support the carapace CLI completions tool
christerswahn 11a152f
fix: Made --target option mandatory
christerswahn 1a1c539
refactor: Renamed carapace_generator.dart
christerswahn f4defaf
fix(cli_tools): Fixed omission in completion representation
christerswahn e315705
fix(cli_tools): Fixed pattern bug in completely generator
christerswahn 1c32c64
test(cli_tools): Added completion subcommand tests
christerswahn baaf40a
refactor(cli_tools): Organized completion as parent/subcommand
christerswahn 1392e6f
docs(cli_tools): Updated READMEs
christerswahn ca6dbe3
fix(cli_tools): Fixed parameter name lint info
christerswahn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
packages/cli_tools/lib/src/better_command_runner/completion/carapace_generator.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import 'dart:io' show IOSink; | ||
|
|
||
| import 'package:config/config.dart'; | ||
|
|
||
| import 'usage_representation.dart'; | ||
|
|
||
| /// Generates usage representation for a command in the YAML format | ||
| /// of the `carapace` tool. | ||
| /// https://github.com/carapace-sh/carapace | ||
| class CarapaceYamlGenerator implements UsageRepresentationGenerator { | ||
| @override | ||
| void generate( | ||
| final IOSink out, | ||
| final CommandUsage usage, | ||
| ) { | ||
| out.writeln( | ||
| r'# yaml-language-server: $schema=https://carapace.sh/schemas/command.json'); | ||
|
|
||
| _generateForCommand(out, usage, 0); | ||
| } | ||
|
|
||
| void _generateForCommand( | ||
| final IOSink out, | ||
| final CommandUsage usage, | ||
| int indentLevel, | ||
| ) { | ||
| if (indentLevel == 0) { | ||
| _writeWithIndent(out, 'name: ${usage.commandSequence.last}', indentLevel); | ||
| } else { | ||
| _writeWithIndent( | ||
| out, '- name: ${usage.commandSequence.last}', indentLevel); | ||
| indentLevel += 1; | ||
| } | ||
|
|
||
| if (usage.persistentOptions.isNotEmpty) { | ||
| _writeWithIndent(out, 'persistentFlags:', indentLevel); | ||
| _declareOptions(out, usage.persistentOptions, indentLevel + 1); | ||
| } | ||
|
|
||
| if (usage.options.isNotEmpty) { | ||
| _writeWithIndent(out, 'flags:', indentLevel); | ||
| _declareOptions(out, usage.options, indentLevel + 1); | ||
| } | ||
|
|
||
| final allOptions = [...usage.persistentOptions, ...usage.options]; | ||
|
|
||
| final exclusiveGroups = _generateExclusiveOptionGroups(allOptions); | ||
| if (exclusiveGroups.isNotEmpty) { | ||
| _writeWithIndent(out, 'exclusiveFlags:', indentLevel); | ||
| for (final group in exclusiveGroups) { | ||
| _writeWithIndent(out, '- [${group.join(', ')}]', indentLevel + 1); | ||
| } | ||
| } | ||
|
|
||
| final specs = _generateOptionCompletionSpecs(allOptions); | ||
| if (specs.isNotEmpty) { | ||
| _writeWithIndent(out, 'completion:', indentLevel); | ||
| _writeWithIndent(out, 'flag:', indentLevel + 1); | ||
| for (final spec in specs) { | ||
| _writeWithIndent(out, spec, indentLevel + 2); | ||
| } | ||
| } | ||
|
|
||
| out.writeln(); | ||
|
|
||
| if (usage.subcommands.isNotEmpty) { | ||
| _writeWithIndent(out, 'commands:', indentLevel); | ||
| for (final subcommand in usage.subcommands) { | ||
| _generateForCommand(out, subcommand, indentLevel + 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static void _writeWithIndent( | ||
| final IOSink out, | ||
| final String text, | ||
| final int indentLevel, | ||
| ) { | ||
| out.writeln('${_getIndent(indentLevel)}$text'); | ||
| } | ||
|
|
||
| static String _getIndent(final int indentLevel) => ' ' * indentLevel; | ||
|
|
||
| static void _declareOptions( | ||
| final IOSink out, | ||
| final List<OptionDefinition> options, | ||
| final int indentLevel, | ||
| ) { | ||
| // options | ||
| for (final option in options) { | ||
| _declareOption(out, option.option, indentLevel); | ||
| } | ||
| } | ||
|
|
||
| static void _declareOption( | ||
| final IOSink out, | ||
| final ConfigOptionBase option, | ||
| final int indentLevel, | ||
| ) { | ||
| final names = [ | ||
| if (option.argAbbrev != null) '-${option.argAbbrev}', | ||
| if (option.argName != null) '--${option.argName}', | ||
| ]; | ||
|
|
||
| String attributes = ''; | ||
| if (option is! FlagOption) { | ||
| attributes += '='; | ||
| } | ||
| if (option is MultiOption) { | ||
| attributes += '*'; | ||
| } | ||
| if (option.mandatory && option is! FlagOption) { | ||
| attributes += '!'; | ||
| } | ||
|
|
||
nielsenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _writeWithIndent( | ||
| out, | ||
| '${names.join(', ')}$attributes: ${option.helpText ?? ''}', | ||
| indentLevel, | ||
| ); | ||
|
|
||
| if (option case final FlagOption flagOption) { | ||
| if (flagOption.negatable && !flagOption.hideNegatedUsage) { | ||
| _writeWithIndent( | ||
| out, | ||
| '--no-${option.argName}$attributes: ${option.helpText ?? ''}', | ||
| indentLevel, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static List<List<String>> _generateExclusiveOptionGroups( | ||
| final List<OptionDefinition> options, | ||
| ) { | ||
| final groups = <List<String>>[]; | ||
| for (final option in options) { | ||
| if (option.option case final FlagOption flagOption) { | ||
| final argName = flagOption.argName; | ||
| if (argName != null && | ||
| flagOption.negatable && | ||
| !flagOption.hideNegatedUsage) { | ||
| groups.add([argName, 'no-$argName']); | ||
| } | ||
| } | ||
| } | ||
| return groups; | ||
| } | ||
|
|
||
| static List<String> _generateOptionCompletionSpecs( | ||
| final List<OptionDefinition> options, | ||
| ) { | ||
| final specs = <String>[]; | ||
| for (final option in options) { | ||
| final name = option.option.argName ?? option.option.argAbbrev; | ||
| if (name == null) { | ||
| continue; | ||
| } | ||
|
|
||
| final values = _getOptionValues(option.option); | ||
| if (values.isNotEmpty) { | ||
| final valueSpec = values.map((final v) => '"$v"').join(', '); | ||
| specs.add('$name: [$valueSpec]'); | ||
| } | ||
| } | ||
| return specs; | ||
| } | ||
|
|
||
| static List<String> _getOptionValues( | ||
| final ConfigOptionBase option, | ||
| ) { | ||
| if (option case final MultiOption multiOption) { | ||
| if (multiOption.allowedElementValues case final List allowedValues) { | ||
| return allowedValues | ||
| .map<String>(multiOption.multiParser.elementParser.format) | ||
| .toList(); | ||
| } | ||
| } else if (option.allowedValues case final List allowedValues) { | ||
| return allowedValues.map(option.valueParser.format).toList(); | ||
| } | ||
|
|
||
| switch (option.option) { | ||
| case EnumOption(): | ||
| final enumParser = option.option.valueParser as EnumParser; | ||
| return enumParser.enumValues.map(enumParser.format).toList(); | ||
| case FileOption(): | ||
| return [r'$files']; | ||
| case DirOption(): | ||
| return [r'$directories']; | ||
| default: | ||
| return []; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quote help text and support required flags ('!') too
Apply this diff:
Also applies to: 116-121, 122-130
🤖 Prompt for AI Agents