Skip to content

Commit a335f8f

Browse files
committed
dotnet: set up tab completions for SDK 10+
Generate bash, zsh, and fish completion scripts using 'dotnet completions script' and place them in the standard system-wide completion directories: - /usr/share/bash-completion/completions/dotnet - /usr/share/zsh/site-functions/_dotnet - /usr/share/fish/vendor_completions.d/dotnet.fish Gated behind SDK 10+ via version check since the 'dotnet completions script' command is only available starting with .NET 10. Skipped for runtime-only installs. Reference: https://learn.microsoft.com/en-us/dotnet/core/tools/enable-tab-autocomplete
1 parent 0c2cd3f commit a335f8f

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/dotnet/install.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ if [ ! -e /usr/bin/dotnet ]; then
146146
ln --symbolic "$DOTNET_ROOT/dotnet" /usr/bin/dotnet
147147
fi
148148

149+
install_completions
150+
149151
# Add .NET Core SDK tools to PATH for bash and zsh users
150152
# This is where 'dotnet tool install --global <tool>' installs tools to
151153
# Use single-quoted EOF to defer $PATH expansion until sourcing the file

src/dotnet/scripts/dotnet-helpers.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,46 @@ parse_version_and_quality() {
184184
quality=""
185185
fi
186186
echo "$clean_version" "$quality"
187+
}
188+
189+
# Checks if the installed .NET SDK is at least the given major version.
190+
# Returns 0 (true) if the SDK major version >= the specified version, 1 otherwise.
191+
# Also returns 1 if no SDK is installed (e.g. runtime-only installs).
192+
# Usage: is_at_least_sdk_version <major_version>
193+
# Example: is_at_least_sdk_version 10
194+
is_at_least_sdk_version() {
195+
local required_major="$1"
196+
local dotnet_version
197+
dotnet_version=$("$DOTNET_ROOT/dotnet" --version 2>/dev/null || true)
198+
local major_version="${dotnet_version%%.*}"
199+
[[ "$major_version" =~ ^[0-9]+$ ]] && [ "$major_version" -ge "$required_major" ]
200+
}
201+
202+
# Sets up dotnet tab completions for bash, zsh, and fish.
203+
# The 'dotnet completions script' command is only available in .NET SDK 10+.
204+
# Older SDKs and runtime-only installs will naturally skip this since the
205+
# command won't be available.
206+
# Reference: https://learn.microsoft.com/en-us/dotnet/core/tools/enable-tab-autocomplete
207+
# Completion scripts are generated at install time and placed in the standard
208+
# system-wide completion directories, which are auto-discovered by
209+
# bash-completion and zsh without modifying any rc files.
210+
install_completions() {
211+
if ! is_at_least_sdk_version 10; then
212+
echo "Skipping dotnet tab completions (requires SDK 10+)."
213+
return
214+
fi
215+
216+
echo "Setting up dotnet tab completions..."
217+
218+
# Bash: drop into the standard bash-completion directory
219+
mkdir -p /usr/share/bash-completion/completions
220+
"$DOTNET_ROOT/dotnet" completions script bash > /usr/share/bash-completion/completions/dotnet
221+
222+
# Zsh: drop into the standard site-functions directory
223+
mkdir -p /usr/share/zsh/site-functions
224+
"$DOTNET_ROOT/dotnet" completions script zsh > /usr/share/zsh/site-functions/_dotnet
225+
226+
# Fish: drop into the standard vendor completions directory
227+
mkdir -p /usr/share/fish/vendor_completions.d
228+
"$DOTNET_ROOT/dotnet" completions script fish > /usr/share/fish/vendor_completions.d/dotnet.fish
187229
}

0 commit comments

Comments
 (0)