|
| 1 | +import 'dart:io' show IOSink; |
| 2 | + |
| 3 | +import 'package:config/config.dart'; |
| 4 | + |
| 5 | +import 'usage_representation.dart'; |
| 6 | + |
| 7 | +/// Generates usage representation for a command in the YAML format |
| 8 | +/// of the `carapace` tool. |
| 9 | +/// https://github.com/carapace-sh/carapace |
| 10 | +class CarapaceYamlGenerator implements UsageRepresentationGenerator { |
| 11 | + @override |
| 12 | + void generate( |
| 13 | + final IOSink out, |
| 14 | + final CommandUsage usage, |
| 15 | + ) { |
| 16 | + out.writeln( |
| 17 | + r'# yaml-language-server: $schema=https://carapace.sh/schemas/command.json'); |
| 18 | + |
| 19 | + _generateForCommand(out, usage, 0); |
| 20 | + } |
| 21 | + |
| 22 | + void _generateForCommand( |
| 23 | + final IOSink out, |
| 24 | + final CommandUsage usage, |
| 25 | + int indentLevel, |
| 26 | + ) { |
| 27 | + if (indentLevel == 0) { |
| 28 | + _writeWithIndent(out, 'name: ${usage.commandSequence.last}', indentLevel); |
| 29 | + } else { |
| 30 | + _writeWithIndent( |
| 31 | + out, '- name: ${usage.commandSequence.last}', indentLevel); |
| 32 | + indentLevel += 1; |
| 33 | + } |
| 34 | + |
| 35 | + if (usage.persistentOptions.isNotEmpty) { |
| 36 | + _writeWithIndent(out, 'persistentFlags:', indentLevel); |
| 37 | + _declareOptions(out, usage.persistentOptions, indentLevel + 1); |
| 38 | + } |
| 39 | + |
| 40 | + if (usage.options.isNotEmpty) { |
| 41 | + _writeWithIndent(out, 'flags:', indentLevel); |
| 42 | + _declareOptions(out, usage.options, indentLevel + 1); |
| 43 | + } |
| 44 | + |
| 45 | + final allOptions = [...usage.persistentOptions, ...usage.options]; |
| 46 | + |
| 47 | + final exclusiveGroups = _generateExclusiveOptionGroups(allOptions); |
| 48 | + if (exclusiveGroups.isNotEmpty) { |
| 49 | + _writeWithIndent(out, 'exclusiveFlags:', indentLevel); |
| 50 | + for (final group in exclusiveGroups) { |
| 51 | + _writeWithIndent(out, '- [${group.join(', ')}]', indentLevel + 1); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + final specs = _generateOptionCompletionSpecs(allOptions); |
| 56 | + if (specs.isNotEmpty) { |
| 57 | + _writeWithIndent(out, 'completion:', indentLevel); |
| 58 | + _writeWithIndent(out, 'flag:', indentLevel + 1); |
| 59 | + for (final spec in specs) { |
| 60 | + _writeWithIndent(out, spec, indentLevel + 2); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + out.writeln(); |
| 65 | + |
| 66 | + if (usage.subcommands.isNotEmpty) { |
| 67 | + _writeWithIndent(out, 'commands:', indentLevel); |
| 68 | + for (final subcommand in usage.subcommands) { |
| 69 | + _generateForCommand(out, subcommand, indentLevel + 1); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + static void _writeWithIndent( |
| 75 | + final IOSink out, |
| 76 | + final String text, |
| 77 | + final int indentLevel, |
| 78 | + ) { |
| 79 | + out.writeln('${_getIndent(indentLevel)}$text'); |
| 80 | + } |
| 81 | + |
| 82 | + static String _getIndent(final int indentLevel) => ' ' * indentLevel; |
| 83 | + |
| 84 | + static void _declareOptions( |
| 85 | + final IOSink out, |
| 86 | + final List<OptionDefinition> options, |
| 87 | + final int indentLevel, |
| 88 | + ) { |
| 89 | + // options |
| 90 | + for (final option in options) { |
| 91 | + _declareOption(out, option.option, indentLevel); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + static void _declareOption( |
| 96 | + final IOSink out, |
| 97 | + final ConfigOptionBase option, |
| 98 | + final int indentLevel, |
| 99 | + ) { |
| 100 | + final names = [ |
| 101 | + if (option.argAbbrev != null) '-${option.argAbbrev}', |
| 102 | + if (option.argName != null) '--${option.argName}', |
| 103 | + ]; |
| 104 | + |
| 105 | + String attributes = ''; |
| 106 | + if (option is! FlagOption) { |
| 107 | + attributes += '='; |
| 108 | + } |
| 109 | + if (option is MultiOption) { |
| 110 | + attributes += '*'; |
| 111 | + } |
| 112 | + if (option.mandatory && option is! FlagOption) { |
| 113 | + attributes += '!'; |
| 114 | + } |
| 115 | + |
| 116 | + _writeWithIndent( |
| 117 | + out, |
| 118 | + '${names.join(', ')}$attributes: ${option.helpText ?? ''}', |
| 119 | + indentLevel, |
| 120 | + ); |
| 121 | + |
| 122 | + if (option case final FlagOption flagOption) { |
| 123 | + if (flagOption.negatable && !flagOption.hideNegatedUsage) { |
| 124 | + _writeWithIndent( |
| 125 | + out, |
| 126 | + '--no-${option.argName}$attributes: ${option.helpText ?? ''}', |
| 127 | + indentLevel, |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + static List<List<String>> _generateExclusiveOptionGroups( |
| 134 | + final List<OptionDefinition> options, |
| 135 | + ) { |
| 136 | + final groups = <List<String>>[]; |
| 137 | + for (final option in options) { |
| 138 | + if (option.option case final FlagOption flagOption) { |
| 139 | + final argName = flagOption.argName; |
| 140 | + if (argName != null && |
| 141 | + flagOption.negatable && |
| 142 | + !flagOption.hideNegatedUsage) { |
| 143 | + groups.add([argName, 'no-$argName']); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + return groups; |
| 148 | + } |
| 149 | + |
| 150 | + static List<String> _generateOptionCompletionSpecs( |
| 151 | + final List<OptionDefinition> options, |
| 152 | + ) { |
| 153 | + final specs = <String>[]; |
| 154 | + for (final option in options) { |
| 155 | + final name = option.option.argName ?? option.option.argAbbrev; |
| 156 | + if (name == null) { |
| 157 | + continue; |
| 158 | + } |
| 159 | + |
| 160 | + final values = _getOptionValues(option.option); |
| 161 | + if (values.isNotEmpty) { |
| 162 | + final valueSpec = values.map((final v) => '"$v"').join(', '); |
| 163 | + specs.add('$name: [$valueSpec]'); |
| 164 | + } |
| 165 | + } |
| 166 | + return specs; |
| 167 | + } |
| 168 | + |
| 169 | + static List<String> _getOptionValues( |
| 170 | + final ConfigOptionBase option, |
| 171 | + ) { |
| 172 | + if (option case final MultiOption multiOption) { |
| 173 | + if (multiOption.allowedElementValues case final List allowedValues) { |
| 174 | + return allowedValues |
| 175 | + .map<String>(multiOption.multiParser.elementParser.format) |
| 176 | + .toList(); |
| 177 | + } |
| 178 | + } else if (option.allowedValues case final List allowedValues) { |
| 179 | + return allowedValues.map(option.valueParser.format).toList(); |
| 180 | + } |
| 181 | + |
| 182 | + switch (option.option) { |
| 183 | + case EnumOption(): |
| 184 | + final enumParser = option.option.valueParser as EnumParser; |
| 185 | + return enumParser.enumValues.map(enumParser.format).toList(); |
| 186 | + case FileOption(): |
| 187 | + return [r'$files']; |
| 188 | + case DirOption(): |
| 189 | + return [r'$directories']; |
| 190 | + default: |
| 191 | + return []; |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments