Skip to content

Commit 4fac9d3

Browse files
JohnnyWombwellJohn Wilson
andauthored
Added ZSH Completion Shim Script (#1643)
* Add zsh completion shim and documentation. * ZSH shim script updated to use modern completions (thanks to @baronfel for the work on this). Co-authored-by: John Wilson <[email protected]>
1 parent 3e0db9e commit 4fac9d3

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

docs/dotnet-suggest.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ On the machine where you'd like to enable completion, you'll need to do two thin
1414

1515
* For bash, add the contents of [dotnet-suggest-shim.bash](https://github.com/dotnet/command-line-api/blob/master/src/System.CommandLine.Suggest/dotnet-suggest-shim.bash) to `~/.bash_profile`.
1616

17+
* For zsh, add the contents of [dotnet-suggest-shim.zsh](https://github.com/dotnet/command-line-api/blob/master/src/System.CommandLine.Suggest/dotnet-suggest-shim.zsh) to `~/.zshrc`.
18+
1719
* For PowerShell, add the contents of [dotnet-suggest-shim.ps1](https://github.com/dotnet/command-line-api/blob/master/src/System.CommandLine.Suggest/dotnet-suggest-shim.ps1) to your PowerShell profile. You can find the expected path to your PowerShell profile by running the following in your console:
1820

1921
```console

src/System.CommandLine.Suggest.Tests/SuggestionShellScriptHandlerTest.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.CommandLine.Invocation;
54
using System.CommandLine.IO;
65
using System.CommandLine.Parsing;
76
using System.Threading.Tasks;
@@ -53,5 +52,15 @@ await _parser.InvokeAsync(
5352

5453
_console.Out.ToString().Should().Contain("Register-ArgumentCompleter");
5554
}
55+
56+
[Fact]
57+
public async Task It_should_print_zsh_shell_script()
58+
{
59+
await _parser.InvokeAsync(
60+
"script zsh",
61+
_console);
62+
63+
_console.Out.ToString().Should().Contain("_dotnet_zsh_complete()");
64+
}
5665
}
5766
}

src/System.CommandLine.Suggest/ShellType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace System.CommandLine.Suggest
66
public enum ShellType
77
{
88
Bash,
9-
PowerShell
9+
PowerShell,
10+
Zsh
1011
}
1112
}

src/System.CommandLine.Suggest/SuggestionShellScriptHandler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public static void Handle(IConsole console, ShellType shellType)
1818
case ShellType.PowerShell:
1919
PrintToConsoleFrom(console, "dotnet-suggest-shim.ps1");
2020
break;
21+
case ShellType.Zsh:
22+
PrintToConsoleFrom(console, "dotnet-suggest-shim.zsh");
23+
break;
2124
default:
2225
throw new SuggestionShellScriptException($"Shell '{shellType}' is not supported.");
2326
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# dotnet suggest shell complete script start
2+
_dotnet_zsh_complete()
3+
{
4+
# debug lines, uncomment to get state variables passed to this function
5+
# echo "\n\n\nstate:\t'$state'"
6+
# echo "line:\t'$line'"
7+
# echo "words:\t$words"
8+
9+
# Get full path to script because dotnet-suggest needs it
10+
# NOTE: this requires a command registered with dotnet-suggest be
11+
# on the PATH
12+
full_path=`which ${words[1]}` # zsh arrays are 1-indexed
13+
# Get the full line
14+
# $words array when quoted like this gets expanded out into the full line
15+
full_line="$words"
16+
17+
# Get the completion results, will be newline-delimited
18+
completions=$(dotnet suggest get --executable "$full_path" -- "$full_line")
19+
# explode the completions by linefeed instead of by spaces into the descriptions for the
20+
# _values helper function.
21+
22+
exploded=(${(f)completions})
23+
# for later - once we have descriptions from dotnet suggest, we can stitch them
24+
# together like so:
25+
# described=()
26+
# for i in {1..$#exploded}; do
27+
# argument="${exploded[$i]}"
28+
# description="hello description $i"
29+
# entry=($argument"["$description"]")
30+
# described+=("$entry")
31+
# done
32+
_values 'suggestions' $exploded
33+
}
34+
35+
# apply this function to each command the dotnet-suggest knows about
36+
compdef _dotnet_zsh_complete $(dotnet-suggest list)
37+
38+
export DOTNET_SUGGEST_SCRIPT_VERSION="1.0.0"
39+
# dotnet suggest shell complete script end

src/System.CommandLine.Suggest/dotnet-suggest.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
<Content Include="dotnet-suggest-shim.bash">
3838
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3939
</Content>
40+
<None Remove="dotnet-suggest-shim.zsh" />
41+
<Content Include="dotnet-suggest-shim.zsh">
42+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
43+
</Content>
4044
</ItemGroup>
4145

4246
<!-- Workaround https://github.com/dotnet/arcade/issues/2233 -->

0 commit comments

Comments
 (0)