tazel
A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.
Clonegit clone https://git.progmasoft.com/tazel
Split ShellCompletion into partial files to satisfy Codacy file length check
- Commit
59a2e616bab08219e95fc4d285e8815c61d8f6f3- Author
- Leitwolf11 ·
- Parents
83f2f0d07164
Changes
.../Cli/ShellCompletion.FishPowerShell.cs | 244 +++++++++++++++++++++ src/Progmasoft.Tazel/Cli/ShellCompletion.cs | 235 +------------------- 2 files changed, 245 insertions(+), 234 deletions(-) diff --git a/src/Progmasoft.Tazel/Cli/ShellCompletion.FishPowerShell.cs b/src/Progmasoft.Tazel/Cli/ShellCompletion.FishPowerShell.cs new file mode 100644 index 0000000..2be656f --- /dev/null +++ b/src/Progmasoft.Tazel/Cli/ShellCompletion.FishPowerShell.cs @@ -0,0 +1,244 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MIT + +using System; +using System.Linq; +using System.Text; + +namespace Progmasoft.Tazel.Cli; + +internal static partial class ShellCompletion +{ + private static string Fish() + { + var sb = new StringBuilder(); + sb.AppendLine("# Tazel shell completion for Fish."); + sb.AppendLine("# Install: tazel shell-completion fish > ~/.config/fish/completions/tazel.fish"); + sb.AppendLine(); + sb.AppendLine("complete -c tazel -e"); + sb.AppendLine(); + sb.AppendLine("function __tazel_complete_colon_opt"); + sb.AppendLine(" set -l tok (commandline -ct)"); + sb.AppendLine(" set -l opt $argv[1]"); + sb.AppendLine(" if string match -q -- \"$opt:*\" \"$tok\""); + sb.AppendLine(" for v in $argv[2..-1]"); + sb.AppendLine(" echo \"$opt:$v\""); + sb.AppendLine(" end"); + sb.AppendLine(" end"); + sb.AppendLine("end"); + sb.AppendLine(); + sb.AppendLine("function __tazel_needs_subcmd"); + sb.AppendLine(" set -l cmd $argv[1]"); + sb.AppendLine(" set -l tokens (commandline -opc)"); + sb.AppendLine(" set -l seen 0"); + sb.AppendLine(" set -l skip 0"); + sb.AppendLine(" for i in (seq 2 (count $tokens))"); + sb.AppendLine(" set -l t $tokens[$i]"); + sb.AppendLine(" if test $skip -eq 1"); + sb.AppendLine(" set skip 0"); + sb.AppendLine(" continue"); + sb.AppendLine(" end"); + sb.AppendLine(" switch $t"); + sb.AppendLine(" case --color --progress --dotnet-settings --message-format --mode --platform --jobs --feature --no-feature --compiler-options --c-compiler-options --cpp-compiler-options --linker-options --zig-compiler-options --output --output-dir -o --algorithm"); + sb.AppendLine(" set skip 1"); + sb.AppendLine(" continue"); + sb.AppendLine(" case '-*'"); + sb.AppendLine(" continue"); + sb.AppendLine(" end"); + sb.AppendLine(" if test $seen -eq 0"); + sb.AppendLine(" if test \"$t\" = \"$cmd\""); + sb.AppendLine(" set seen 1"); + sb.AppendLine(" else"); + sb.AppendLine(" return 1"); + sb.AppendLine(" end"); + sb.AppendLine(" else"); + sb.AppendLine(" return 1"); + sb.AppendLine(" end"); + sb.AppendLine(" end"); + sb.AppendLine(" test $seen -eq 1"); + sb.AppendLine("end"); + sb.AppendLine(); + sb.AppendLine("complete -c tazel -f"); + sb.AppendLine(); + sb.AppendLine("# Global options"); + AppendFishValueOption(sb, null, "color", "always auto never", "Color output"); + AppendFishValueOption(sb, null, "progress", "always auto never", "Progress output"); + sb.AppendLine("complete -c tazel -l quiet -d 'Suppress status output'"); + AppendFishValueOption(sb, null, "dotnet-settings", null, "Set .NET runtime settings"); + AppendFishValueOption(sb, null, "message-format", "rich-stdio script-friendly-stdio jsonl xml", "Message output format"); + sb.AppendLine("complete -c tazel -s h -l help -d 'Show help'"); + sb.AppendLine(); + sb.AppendLine("# Commands"); + foreach (var (cmd, desc) in CommandDescriptions) + { + sb.Append("complete -c tazel -n '__fish_use_subcommand' -a '").Append(cmd) + .Append("' -d '").Append(desc.Replace("'", "\\'", StringComparison.Ordinal)).AppendLine("'"); + } + + sb.AppendLine(); + sb.AppendLine("# Command-specific completions"); + const string buildSubcmds = "__fish_seen_subcommand_from build run test coverage cquery aquery"; + AppendFishValueOption(sb, buildSubcmds, "mode", "debug release", "Build mode"); + AppendFishValueOption(sb, buildSubcmds, "platform", null, "Target platform"); + AppendFishValueOption(sb, buildSubcmds, "jobs", null, "Parallel jobs"); + AppendFishValueOption(sb, buildSubcmds, "feature", null, "Enable toolchain feature"); + AppendFishValueOption(sb, buildSubcmds, "no-feature", null, "Disable toolchain feature"); + AppendFishValueOption(sb, buildSubcmds, "compiler-options", null, "C-family compiler options"); + AppendFishValueOption(sb, buildSubcmds, "c-compiler-options", null, "C/ObjC compiler options"); + AppendFishValueOption(sb, buildSubcmds, "cpp-compiler-options", null, "C++/ObjC++ compiler options"); + AppendFishValueOption(sb, buildSubcmds, "linker-options", null, "Linker options"); + AppendFishValueOption(sb, buildSubcmds, "zig-compiler-options", null, "Zig compiler options"); + sb.AppendLine($"complete -c tazel -n '{buildSubcmds}' -l keep-going -d 'Continue after failures'"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from build run test coverage' -l recompile -d 'Recompile all sources'"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from build run test coverage' -l no-cache -d 'Bypass cache'"); + AppendFishValueOption(sb, "__fish_seen_subcommand_from query cquery aquery", "output", "label label_kind", "Output format"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from clean' -l expunge -d 'Remove all of .tazel'"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from help' -l short -d 'Short help'"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from help' -l long -d 'Detailed help'"); + sb.AppendLine($"complete -c tazel -n '__tazel_needs_subcmd help' -a '{string.Join(' ', CommandLine.Commands)}'"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from explain' -l short -d 'Short explanation'"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from explain' -l long -d 'Detailed explanation'"); + AppendFishValueOption(sb, "__fish_seen_subcommand_from license", "output", "text md markdown rst", "License format", shortOpt: "o"); + sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd shell-completion' -a 'bash zsh fish powershell'"); + sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd cache' -a 'keygen validate repair'"); + AppendFishValueOption(sb, "__fish_seen_subcommand_from cache", "algorithm", "ed25519 rsa-4096 ml-dsa", "Signing algorithm"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from cache' -l remote -d 'Include remote cache'"); + sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd dotnet' -a 'get what-is start-daemon restart-daemon stop-daemon'"); + sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd options' -a 'validate get what-is update'"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options custom-cmds envs' -l user -d 'User layer'"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options custom-cmds envs' -l project -d 'Project layer'"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options custom-cmds envs' -l local -d 'Local layer'"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options' -l create -d 'Create file if missing'"); + sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd custom-cmds' -a 'validate'"); + sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd envs' -a 'validate'"); + sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd tool' -a 'launch create-wrapper'"); + AppendFishValueOption(sb, "__fish_seen_subcommand_from tool", "output-dir", null, "Output directory", shortOpt: "o", requireFiles: true); + sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd ignore' -a 'create-from validate add delete del'"); + sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd portal-modules' -a 'search info'"); + sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from portal-modules' -s e -l exact -d 'Exact match'"); + sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd limited-time' -a 'C#'"); + return sb.ToString().TrimEnd(); + } + + private static void AppendFishValueOption( + StringBuilder sb, string? condition, string longOpt, string? values, string description, + string? shortOpt = null, bool requireFiles = false) + { + var cond = condition is null ? "" : $" -n '{condition}'"; + var shortPart = shortOpt is null ? "" : $" -s {shortOpt}"; + var mode = requireFiles ? "-r" : "-x"; + var valuesArg = values is null ? "" : $" -a '{values}'"; + sb.AppendLine($"complete -c tazel{cond}{shortPart} -l {longOpt} {mode}{valuesArg} -d '{description}'"); + if (values is not null) + { + sb.AppendLine($"complete -c tazel{cond} -a '(__tazel_complete_colon_opt --{longOpt} {values})' -d '{description}'"); + } + } + + private static string PowerShell() + { + // Strictly compatible with Windows PowerShell 5.1 and later (no PowerShell 7-only syntax). + var commandList = string.Join(", ", CommandLine.Commands.Select(c => $"'{c}'")); + var globalList = string.Join(", ", GlobalFlags.Select(f => $"'{f}'")); + var buildList = string.Join(", ", BuildFlags.Select(f => $"'{f}'")); + return $$""" + # Tazel shell completion for PowerShell (requires PowerShell 5.1 or later). + # Load in the current shell: tazel shell-completion powershell | Out-String | Invoke-Expression + + Register-ArgumentCompleter -Native -CommandName 'tazel' -ScriptBlock { + param($wordToComplete, $commandAst, $cursorPosition) + + $commands = @({{commandList}}) + $globalOptions = @({{globalList}}) + $buildOptions = @({{buildList}}) + + $elements = @() + foreach ($el in $commandAst.CommandElements) { + $elements += $el.Extent.Text + } + + $valueOpts = @('--color', '--progress', '--dotnet-settings', '--message-format', '--mode', '--platform', '--jobs', '--feature', '--no-feature', '--compiler-options', '--c-compiler-options', '--cpp-compiler-options', '--linker-options', '--zig-compiler-options', '--output', '--output-dir', '-o', '--algorithm') + $cmd = $null + $subcmd = $null + $skipNext = $false + for ($i = 1; $i -lt $elements.Count; $i++) { + $token = $elements[$i] + if ($token -eq $wordToComplete -and $i -eq ($elements.Count - 1)) { + continue + } + if ($skipNext) { + $skipNext = $false + continue + } + if ($valueOpts -contains $token) { + $skipNext = $true + continue + } + if ($token.StartsWith('-')) { + continue + } + if ($null -eq $cmd) { + $cmd = $token + } elseif ($null -eq $subcmd) { + $subcmd = $token + } + } + + $prev = '' + if ($elements.Count -ge 2) { + if ([string]::IsNullOrEmpty($wordToComplete)) { + $prev = $elements[$elements.Count - 1] + } elseif ($elements.Count -ge 3) { + $prev = $elements[$elements.Count - 2] + } + } + + $candidates = @() + if ($prev -eq '--color' -or $prev -eq '--progress') { + $candidates = @('always', 'auto', 'never') + } elseif ($prev -eq '--message-format') { + $candidates = @('rich-stdio', 'script-friendly-stdio', 'jsonl', 'xml') + } elseif ($prev -eq '--mode') { + $candidates = @('debug', 'release') + } elseif ($prev -eq '--algorithm') { + $candidates = @('ed25519', 'rsa-4096', 'ml-dsa') + } elseif (($prev -eq '--output' -or $prev -eq '-o') -and $cmd -eq 'license') { + $candidates = @('text', 'md', 'markdown', 'rst') + } elseif ($prev -eq '--output' -and ($cmd -eq 'query' -or $cmd -eq 'cquery' -or $cmd -eq 'aquery')) { + $candidates = @('label', 'label_kind') + } elseif ($null -eq $cmd) { + $candidates = $commands + $globalOptions + } else { + $candidates = $globalOptions + switch ($cmd) { + 'build' { $candidates += $buildOptions } + 'run' { $candidates += $buildOptions } + 'test' { $candidates += $buildOptions } + 'coverage' { $candidates += $buildOptions } + 'query' { $candidates += @('--output') } + 'cquery' { $candidates += @('--output') + $buildOptions } + 'aquery' { $candidates += @('--output') + $buildOptions } + 'clean' { $candidates += @('--expunge') } + 'help' { $candidates += @('--short', '--long'); if ($null -eq $subcmd) { $candidates += $commands } } + 'explain' { $candidates += @('--short', '--long') } + 'license' { $candidates += @('--output', '-o') } + 'shell-completion' { if ($null -eq $subcmd) { $candidates += @('bash', 'zsh', 'fish', 'powershell') } } + 'cache' { $candidates += @('--algorithm', '--remote'); if ($null -eq $subcmd) { $candidates += @('keygen', 'validate', 'repair') } } + 'dotnet' { if ($null -eq $subcmd) { $candidates += @('get', 'what-is', 'start-daemon', 'restart-daemon', 'stop-daemon') } } + 'options' { $candidates += @('--user', '--project', '--local', '--create'); if ($null -eq $subcmd) { $candidates += @('validate', 'get', 'what-is', 'update') } } + 'custom-cmds' { $candidates += @('--user', '--project', '--local'); if ($null -eq $subcmd) { $candidates += @('validate') } } + 'envs' { $candidates += @('--user', '--project', '--local'); if ($null -eq $subcmd) { $candidates += @('validate') } } + 'tool' { $candidates += @('--output-dir', '-o'); if ($null -eq $subcmd) { $candidates += @('launch', 'create-wrapper') } } + 'ignore' { if ($null -eq $subcmd) { $candidates += @('create-from', 'validate', 'add', 'delete', 'del') } } + 'portal-modules' { $candidates += @('--exact', '-e'); if ($null -eq $subcmd) { $candidates += @('search', 'info') } } + 'limited-time' { if ($null -eq $subcmd) { $candidates += @('C#') } } + } + } + + $candidates | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) + } + } + """; + } +} diff --git a/src/Progmasoft.Tazel/Cli/ShellCompletion.cs b/src/Progmasoft.Tazel/Cli/ShellCompletion.cs index cf85ece..b9810dc 100644 --- a/src/Progmasoft.Tazel/Cli/ShellCompletion.cs +++ b/src/Progmasoft.Tazel/Cli/ShellCompletion.cs @@ -18,7 +18,7 @@ internal enum ShellKind } /// <summary>Generates shell completion scripts for <c>tazel</c>.</summary> -internal static class ShellCompletion +internal static partial class ShellCompletion { /// <summary>The accepted shell names, case-sensitive.</summary> public static IReadOnlyList<string> Names { get; } = ["bash", "zsh", "fish", "powershell"]; @@ -402,237 +402,4 @@ internal static class ShellCompletion sb.AppendLine("fi"); return sb.ToString().TrimEnd(); } - - private static string Fish() - { - var sb = new StringBuilder(); - sb.AppendLine("# Tazel shell completion for Fish."); - sb.AppendLine("# Install: tazel shell-completion fish > ~/.config/fish/completions/tazel.fish"); - sb.AppendLine(); - sb.AppendLine("complete -c tazel -e"); - sb.AppendLine(); - sb.AppendLine("function __tazel_complete_colon_opt"); - sb.AppendLine(" set -l tok (commandline -ct)"); - sb.AppendLine(" set -l opt $argv[1]"); - sb.AppendLine(" if string match -q -- \"$opt:*\" \"$tok\""); - sb.AppendLine(" for v in $argv[2..-1]"); - sb.AppendLine(" echo \"$opt:$v\""); - sb.AppendLine(" end"); - sb.AppendLine(" end"); - sb.AppendLine("end"); - sb.AppendLine(); - sb.AppendLine("function __tazel_needs_subcmd"); - sb.AppendLine(" set -l cmd $argv[1]"); - sb.AppendLine(" set -l tokens (commandline -opc)"); - sb.AppendLine(" set -l seen 0"); - sb.AppendLine(" set -l skip 0"); - sb.AppendLine(" for i in (seq 2 (count $tokens))"); - sb.AppendLine(" set -l t $tokens[$i]"); - sb.AppendLine(" if test $skip -eq 1"); - sb.AppendLine(" set skip 0"); - sb.AppendLine(" continue"); - sb.AppendLine(" end"); - sb.AppendLine(" switch $t"); - sb.AppendLine(" case --color --progress --dotnet-settings --message-format --mode --platform --jobs --feature --no-feature --compiler-options --c-compiler-options --cpp-compiler-options --linker-options --zig-compiler-options --output --output-dir -o --algorithm"); - sb.AppendLine(" set skip 1"); - sb.AppendLine(" continue"); - sb.AppendLine(" case '-*'"); - sb.AppendLine(" continue"); - sb.AppendLine(" end"); - sb.AppendLine(" if test $seen -eq 0"); - sb.AppendLine(" if test \"$t\" = \"$cmd\""); - sb.AppendLine(" set seen 1"); - sb.AppendLine(" else"); - sb.AppendLine(" return 1"); - sb.AppendLine(" end"); - sb.AppendLine(" else"); - sb.AppendLine(" return 1"); - sb.AppendLine(" end"); - sb.AppendLine(" end"); - sb.AppendLine(" test $seen -eq 1"); - sb.AppendLine("end"); - sb.AppendLine(); - sb.AppendLine("complete -c tazel -f"); - sb.AppendLine(); - sb.AppendLine("# Global options"); - AppendFishValueOption(sb, null, "color", "always auto never", "Color output"); - AppendFishValueOption(sb, null, "progress", "always auto never", "Progress output"); - sb.AppendLine("complete -c tazel -l quiet -d 'Suppress status output'"); - AppendFishValueOption(sb, null, "dotnet-settings", null, "Set .NET runtime settings"); - AppendFishValueOption(sb, null, "message-format", "rich-stdio script-friendly-stdio jsonl xml", "Message output format"); - sb.AppendLine("complete -c tazel -s h -l help -d 'Show help'"); - sb.AppendLine(); - sb.AppendLine("# Commands"); - foreach (var (cmd, desc) in CommandDescriptions) - { - sb.Append("complete -c tazel -n '__fish_use_subcommand' -a '").Append(cmd) - .Append("' -d '").Append(desc.Replace("'", "\\'", StringComparison.Ordinal)).AppendLine("'"); - } - - sb.AppendLine(); - sb.AppendLine("# Command-specific completions"); - const string buildSubcmds = "__fish_seen_subcommand_from build run test coverage cquery aquery"; - AppendFishValueOption(sb, buildSubcmds, "mode", "debug release", "Build mode"); - AppendFishValueOption(sb, buildSubcmds, "platform", null, "Target platform"); - AppendFishValueOption(sb, buildSubcmds, "jobs", null, "Parallel jobs"); - AppendFishValueOption(sb, buildSubcmds, "feature", null, "Enable toolchain feature"); - AppendFishValueOption(sb, buildSubcmds, "no-feature", null, "Disable toolchain feature"); - AppendFishValueOption(sb, buildSubcmds, "compiler-options", null, "C-family compiler options"); - AppendFishValueOption(sb, buildSubcmds, "c-compiler-options", null, "C/ObjC compiler options"); - AppendFishValueOption(sb, buildSubcmds, "cpp-compiler-options", null, "C++/ObjC++ compiler options"); - AppendFishValueOption(sb, buildSubcmds, "linker-options", null, "Linker options"); - AppendFishValueOption(sb, buildSubcmds, "zig-compiler-options", null, "Zig compiler options"); - sb.AppendLine($"complete -c tazel -n '{buildSubcmds}' -l keep-going -d 'Continue after failures'"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from build run test coverage' -l recompile -d 'Recompile all sources'"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from build run test coverage' -l no-cache -d 'Bypass cache'"); - AppendFishValueOption(sb, "__fish_seen_subcommand_from query cquery aquery", "output", "label label_kind", "Output format"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from clean' -l expunge -d 'Remove all of .tazel'"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from help' -l short -d 'Short help'"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from help' -l long -d 'Detailed help'"); - sb.AppendLine($"complete -c tazel -n '__tazel_needs_subcmd help' -a '{string.Join(' ', CommandLine.Commands)}'"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from explain' -l short -d 'Short explanation'"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from explain' -l long -d 'Detailed explanation'"); - AppendFishValueOption(sb, "__fish_seen_subcommand_from license", "output", "text md markdown rst", "License format", shortOpt: "o"); - sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd shell-completion' -a 'bash zsh fish powershell'"); - sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd cache' -a 'keygen validate repair'"); - AppendFishValueOption(sb, "__fish_seen_subcommand_from cache", "algorithm", "ed25519 rsa-4096 ml-dsa", "Signing algorithm"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from cache' -l remote -d 'Include remote cache'"); - sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd dotnet' -a 'get what-is start-daemon restart-daemon stop-daemon'"); - sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd options' -a 'validate get what-is update'"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options custom-cmds envs' -l user -d 'User layer'"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options custom-cmds envs' -l project -d 'Project layer'"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options custom-cmds envs' -l local -d 'Local layer'"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options' -l create -d 'Create file if missing'"); - sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd custom-cmds' -a 'validate'"); - sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd envs' -a 'validate'"); - sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd tool' -a 'launch create-wrapper'"); - AppendFishValueOption(sb, "__fish_seen_subcommand_from tool", "output-dir", null, "Output directory", shortOpt: "o", requireFiles: true); - sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd ignore' -a 'create-from validate add delete del'"); - sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd portal-modules' -a 'search info'"); - sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from portal-modules' -s e -l exact -d 'Exact match'"); - sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd limited-time' -a 'C#'"); - return sb.ToString().TrimEnd(); - } - - private static void AppendFishValueOption( - StringBuilder sb, string? condition, string longOpt, string? values, string description, - string? shortOpt = null, bool requireFiles = false) - { - var cond = condition is null ? "" : $" -n '{condition}'"; - var shortPart = shortOpt is null ? "" : $" -s {shortOpt}"; - var mode = requireFiles ? "-r" : "-x"; - var valuesArg = values is null ? "" : $" -a '{values}'"; - sb.AppendLine($"complete -c tazel{cond}{shortPart} -l {longOpt} {mode}{valuesArg} -d '{description}'"); - if (values is not null) - { - sb.AppendLine($"complete -c tazel{cond} -a '(__tazel_complete_colon_opt --{longOpt} {values})' -d '{description}'"); - } - } - - private static string PowerShell() - { - // Strictly compatible with Windows PowerShell 5.1 and later (no PowerShell 7-only syntax). - var commandList = string.Join(", ", CommandLine.Commands.Select(c => $"'{c}'")); - var globalList = string.Join(", ", GlobalFlags.Select(f => $"'{f}'")); - var buildList = string.Join(", ", BuildFlags.Select(f => $"'{f}'")); - return $$""" - # Tazel shell completion for PowerShell (requires PowerShell 5.1 or later). - # Load in the current shell: tazel shell-completion powershell | Out-String | Invoke-Expression - - Register-ArgumentCompleter -Native -CommandName 'tazel' -ScriptBlock { - param($wordToComplete, $commandAst, $cursorPosition) - - $commands = @({{commandList}}) - $globalOptions = @({{globalList}}) - $buildOptions = @({{buildList}}) - - $elements = @() - foreach ($el in $commandAst.CommandElements) { - $elements += $el.Extent.Text - } - - $valueOpts = @('--color', '--progress', '--dotnet-settings', '--message-format', '--mode', '--platform', '--jobs', '--feature', '--no-feature', '--compiler-options', '--c-compiler-options', '--cpp-compiler-options', '--linker-options', '--zig-compiler-options', '--output', '--output-dir', '-o', '--algorithm') - $cmd = $null - $subcmd = $null - $skipNext = $false - for ($i = 1; $i -lt $elements.Count; $i++) { - $token = $elements[$i] - if ($token -eq $wordToComplete -and $i -eq ($elements.Count - 1)) { - continue - } - if ($skipNext) { - $skipNext = $false - continue - } - if ($valueOpts -contains $token) { - $skipNext = $true - continue - } - if ($token.StartsWith('-')) { - continue - } - if ($null -eq $cmd) { - $cmd = $token - } elseif ($null -eq $subcmd) { - $subcmd = $token - } - } - - $prev = '' - if ($elements.Count -ge 2) { - if ([string]::IsNullOrEmpty($wordToComplete)) { - $prev = $elements[$elements.Count - 1] - } elseif ($elements.Count -ge 3) { - $prev = $elements[$elements.Count - 2] - } - } - - $candidates = @() - if ($prev -eq '--color' -or $prev -eq '--progress') { - $candidates = @('always', 'auto', 'never') - } elseif ($prev -eq '--message-format') { - $candidates = @('rich-stdio', 'script-friendly-stdio', 'jsonl', 'xml') - } elseif ($prev -eq '--mode') { - $candidates = @('debug', 'release') - } elseif ($prev -eq '--algorithm') { - $candidates = @('ed25519', 'rsa-4096', 'ml-dsa') - } elseif (($prev -eq '--output' -or $prev -eq '-o') -and $cmd -eq 'license') { - $candidates = @('text', 'md', 'markdown', 'rst') - } elseif ($prev -eq '--output' -and ($cmd -eq 'query' -or $cmd -eq 'cquery' -or $cmd -eq 'aquery')) { - $candidates = @('label', 'label_kind') - } elseif ($null -eq $cmd) { - $candidates = $commands + $globalOptions - } else { - $candidates = $globalOptions - switch ($cmd) { - 'build' { $candidates += $buildOptions } - 'run' { $candidates += $buildOptions } - 'test' { $candidates += $buildOptions } - 'coverage' { $candidates += $buildOptions } - 'query' { $candidates += @('--output') } - 'cquery' { $candidates += @('--output') + $buildOptions } - 'aquery' { $candidates += @('--output') + $buildOptions } - 'clean' { $candidates += @('--expunge') } - 'help' { $candidates += @('--short', '--long'); if ($null -eq $subcmd) { $candidates += $commands } } - 'explain' { $candidates += @('--short', '--long') } - 'license' { $candidates += @('--output', '-o') } - 'shell-completion' { if ($null -eq $subcmd) { $candidates += @('bash', 'zsh', 'fish', 'powershell') } } - 'cache' { $candidates += @('--algorithm', '--remote'); if ($null -eq $subcmd) { $candidates += @('keygen', 'validate', 'repair') } } - 'dotnet' { if ($null -eq $subcmd) { $candidates += @('get', 'what-is', 'start-daemon', 'restart-daemon', 'stop-daemon') } } - 'options' { $candidates += @('--user', '--project', '--local', '--create'); if ($null -eq $subcmd) { $candidates += @('validate', 'get', 'what-is', 'update') } } - 'custom-cmds' { $candidates += @('--user', '--project', '--local'); if ($null -eq $subcmd) { $candidates += @('validate') } } - 'envs' { $candidates += @('--user', '--project', '--local'); if ($null -eq $subcmd) { $candidates += @('validate') } } - 'tool' { $candidates += @('--output-dir', '-o'); if ($null -eq $subcmd) { $candidates += @('launch', 'create-wrapper') } } - 'ignore' { if ($null -eq $subcmd) { $candidates += @('create-from', 'validate', 'add', 'delete', 'del') } } - 'portal-modules' { $candidates += @('--exact', '-e'); if ($null -eq $subcmd) { $candidates += @('search', 'info') } } - 'limited-time' { if ($null -eq $subcmd) { $candidates += @('C#') } } - } - } - - $candidates | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { - [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) - } - } - """; - } }