'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const breadc = require('breadc'); const generatePowershell = (breadc, commands, globalOptions) => { const bin = breadc.name; const subcommands = generateSubcommands(breadc, commands, globalOptions); const template = `using namespace System.Management.Automation using namespace System.Management.Automation.Language Register-ArgumentCompleter -Native -CommandName '${bin}' -ScriptBlock { param($wordToComplete, $commandAst, $cursorPosition) $commandElements = $commandAst.CommandElements $command = @( '${bin}' for ($i = 1; $i -lt $commandElements.Count; $i++) { $element = $commandElements[$i] if ($element -isnot [StringConstantExpressionAst] -or $element.StringConstantType -ne [StringConstantType]::BareWord -or $element.Value.StartsWith('-') -or $element.Value -eq $wordToComplete) { break } $element.Value }) -join ';' $completions = @(switch ($command) {${subcommands} }) $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | Sort-Object -Property ListItemText }`; return template; }; function generateSubcommands(breadc, commands, globalOptions) { const cases = []; cases.push( [ "", `'${breadc.name}' {`, ...commands.map( (c) => ` [CompletionResult]::new('${c._arguments[0].name}', '${c._arguments[0].name}', [CompletionResultType]::ParameterValue, '${c.description}')` ), ...globalOptions.map( (o) => ` [CompletionResult]::new('--${o.name}', '${o.name}', [CompletionResultType]::ParameterName, '${o.description}')` ), ` break`, `}` ].map((t) => " " + t).join("\n") ); for (const command of commands) { const args = command._arguments.filter((a) => a.type === "const").map((a) => a.name).join(";"); cases.push( [ "", `'${breadc.name};${args}' {`, ...command._options.map( (o) => ` [CompletionResult]::new('--${o.name}', '${o.name}', [CompletionResultType]::ParameterName, '${o.description}')` ), ...globalOptions.map( (o) => ` [CompletionResult]::new('--${o.name}', '${o.name}', [CompletionResultType]::ParameterName, '${o.description}')` ), ` break`, `}` ].map((t) => " " + t).join("\n") ); } return cases.join("\n"); } function generate(shell, breadc$1, allCommands, globalOptions) { if (shell === "powershell") { return generatePowershell(breadc$1, allCommands, globalOptions); } else { throw new breadc.ParseError(`Unsupport shell ${shell}`); } } function complete() { return { onInit(breadc, allCommands, globalOptions) { globalOptions.push( makeCompleteOption(breadc, allCommands, globalOptions) ); } }; } function makeCompleteOption(breadc$1, allCommands, globalOptions) { const node = breadc.makeTreeNode({ next() { return false; }, finish() { return (result) => { const shell = detectShellType(result.options["shell"]); const text = generate(shell, breadc$1, allCommands, globalOptions); console.log(text); return text; }; } }); const option = { format: "-c, --complete ", name: "complete", short: "c", type: "string", initial: "", order: 999999999 - 1, description: "Export shell complete script", parse() { return node; } }; return option; } function detectShellType(shell) { if (!shell) { return "powershell"; } else { return shell; } } exports.complete = complete; exports.default = complete;