Skip to content

Add zsh compdef completion script. - #14239

Merged
marcpopMSFT merged 3 commits into
dotnet:mainfrom
Esgariot:master
Jun 20, 2023
Merged

marcpopMSFT merged 3 commits into
dotnet:mainfrom
Esgariot:master

Conversation

@Esgariot

Copy link
Copy Markdown
Contributor

Taking another look at stuff mentioned in dotnet/cli#13384
I have no knowledge of zsh and completion scripts, just followed the suggestions from aforementioned PR.
Because of that there's few things left to do that come to mind:

  • integrate this script with others so that it conforms to both zsh sourcing/autoloading of completion files (gets included into fpath somehow) and what is expected of script files here, in scripts folder
  • add descriptions (when applicable) to dotnet complete output using zsh completion system's mechanisms of which I know exactly nothing.

@marcpopMSFT
marcpopMSFT requested a review from wli3 October 28, 2020 21:20
@omajid

omajid commented Jan 7, 2021

Copy link
Copy Markdown
Member

IMO, this should replace the existing scripts/register-completions.zsh file (having both is redundant). I wonder if anything else in the installer needs to be updated to make sure this file is installed when appropriate?

@wli3

wli3 commented Jan 7, 2021

Copy link
Copy Markdown

@omajid we don't include this script "in the box". So far we only point user to the instruction in Github to manually add it. Btw, have you tried this script? Or could you help review this script? I don't know too much about zsh, so it takes a while for me to look into it.

@wli3

wli3 commented Jan 7, 2021

Copy link
Copy Markdown

@sfoslund how much auto completion of system commandline is "online" in SDK?

@sfoslund

sfoslund commented Jan 7, 2021

Copy link
Copy Markdown
Member

how much auto completion of system commandline is "online" in SDK?

It should be enabled for all commands, I recently fixed a "dotnet add package" specific issue but otherwise it has been supported since the original PR.

@wli3

wli3 commented Jan 7, 2021

Copy link
Copy Markdown

with some configure, the library should support dotnet-suggest for completion. But it is unlikely to work on SDK. Since there are so much customization in SDK

@sfoslund

sfoslund commented Jan 7, 2021

Copy link
Copy Markdown
Member

The CLI supports the dotnet complete command and dotnet [suggest] directive for getting suggestions, if that's what you're looking for.

@omajid

omajid commented Jan 7, 2021

Copy link
Copy Markdown
Member

Btw, have you tried this script? Or could you help review this script?

No, sorry, I dont use zsh so I haven't reviewed or test it :(

@omajid

omajid commented Jan 7, 2021

Copy link
Copy Markdown
Member

we don't include this script "in the box". So far we only point user to the instruction in Github to manually add it.

Interesting. Is that true for bash completion too?

For the Fedora .NET Core 3.1 package, we can install/use it in from the source repo: https://src.fedoraproject.org/rpms/dotnet3.1/blob/7d66170eeba7d2f13cd15d1c05d1c259ba48c324/f/dotnet3.1.spec#_481

@wli3

wli3 commented Jan 7, 2021

Copy link
Copy Markdown

@omajid it is the same for bash. It is something to be put in user's bashrc. We need an explicit agreement from the user. We need to design a UX for that, which is not trivial

@omajid

omajid commented Jan 7, 2021

Copy link
Copy Markdown
Member

@omajid it is the same for bash. It is something to be put in user's bashrc. We need an explicit agreement from the user.

If you are installing the SDK system wide, why not put the bash completion system-wide too? And if the user agrees with installing the software in the system, why wouldn't they agree with the auto-completion? If there are privacy implications, then I guess that would explain why the need for explicit agreement and UX design.

@wli3

wli3 commented Jan 7, 2021 •

Copy link
Copy Markdown

My understanding is bashrc belongs to the user. No program should directly edit it. Also, simply append more lines to it will not always work.

@omajid

omajid commented Jan 7, 2021 •

Copy link
Copy Markdown
Member

My understanding is bashrc belongs to the user. No program should directly edit it. Also, simply append more lines to it will not always work.

That's right. You shouldn't edit the user's bashrc.

But you can add system-wide completion by dropping completion files into the system-wide completion directory. That's what it's meant for. That's where programs like git install their completion file.

$ pkg-config --variable=completionsdir bash-completion
/usr/share/bash-completion/completions
$ ls -l $(pkg-config --variable=completionsdir bash-completion)/git
-rw-r--r--. 1 root root 78880 Oct 29 18:11 /usr/share/bash-completion/completions/git
$ head $(pkg-config --variable=completionsdir bash-completion)/git
# bash/zsh completion support for core Git.
#
# Copyright (C) 2006,2007 Shawn O. Pearce <[email protected]>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# Distributed under the GNU General Public License, version 2.0.
#
# The contained completion routines provide support for completing:
#
#    *) local and remote branch names
#    *) local and remote tag names


From the FAQ:

Q. I author/maintain package X and would like to maintain my own completion code for this package. Where should I put it to be sure that interactive bash shells will find it and source it?

A. Install it in one of the directories pointed to by bash-completion's pkgconfig file variables. There are two alternatives:

The recommended directory is completionsdir, which you can get with pkg-config --variable=completionsdir bash-completion. From this directory, completions are automatically loaded on demand based on invoked commands' names, so be sure to name your completion file accordingly, and to include (for example) symbolic links in case the file provides completions for more than one command. The completion filename for command foo in this directory should be either foo, or foo.bash. (Underscore prefixed _foo works too, but is reserved for bash-completion internal use as a deprecation/fallback marker.)

@wli3

wli3 commented Jan 7, 2021

Copy link
Copy Markdown

That's a good news. Is /usr/share/bash-completion/ available on all shells and all distros?

@omajid

omajid commented Jan 7, 2021

Copy link
Copy Markdown
Member

Is /usr/share/bash-completion/ available on all shells and all distros?

Probably not. The bash completion FAQ I linked above says that pkg-config --variable=completionsdir bash-completion should compute the desired path.

I think this location is for bash only.

I dont know enough about zsh to figure out where that lies. Google says the environment variables $FPATH or $fpath should list all the paths where zsh looks for completion files and the system-wide location should be one of those paths.

Base automatically changed from master to main March 18, 2021 21:05
@marcpopMSFT

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@Esgariot

Esgariot commented Jun 6, 2021

Copy link
Copy Markdown
Contributor Author

FYI I'm still looking at this issue from time to time and I'm still a noob when it comes to zsh stuff. If there's anybody you know that's more knowledgeable in these things and would like to take a look that would be great, I don't believe it should be a big problem because package maintainers include their completions for all kinds of stuff

@Esgariot

Esgariot commented Jun 8, 2021 •

Copy link
Copy Markdown
Contributor Author

ultimately I think that the zsh completion scripts should land in
($datadir|$datarootdir)/zsh/site-functions/_dotnet

https://www.gnu.org/prep/standards/html_node/Directory-Variables.html

(thanks Megame50)

@omajid

omajid commented Jul 2, 2021

Copy link
Copy Markdown
Member

I don't believe it should be a big problem because package maintainers include their completions for all kinds of stuff

I am a package maintainer in Fedora and I would love to include zsh shell completions for dotnet too! Currently we only include dotnet completions for bash.

@baronfel

Copy link
Copy Markdown
Member

Based on some new work on System.CommandLine's completions for dotnet suggest, I suggest we look into a completion function similar to this:

#compdef dotnet

# debug lines, uncomment to get state variables passed to this function
# echo "\n\n\nstate:\t'$state'"
# echo "line:\t'$line'"
# echo "words:\t$words"

# Get the full line
# $words array when quoted like this gets expanded out into the full line
full_line="$words"

# Get the completion results, will be newline-delimited
completions=$(dotnet complete "$full_line")
# explode the completions by linefeed instead of by spaces into the descriptions for the
# _values helper function.
exploded=(${(f)completions})
_values 'suggestions' $exploded

When we add description support to dotnet complete the descriptions can be added using

described=()
for i in {1..$#exploded}; do
    argument="${exploded[$i]}"
    description="hello description $i"
    entry=($argument"["$description"]")
    described+=("$entry")
done

and the described array can be used with _values instead of exploded.

@ayakael ayakael mentioned this pull request Oct 25, 2022
13 of 18 tasks
@ayakael

ayakael commented Nov 8, 2022

Copy link
Copy Markdown
Contributor

For what it's worth, at Alpine Linux we''ve been applying the following patch for zsh completion support:

From 4c74721e1f5da9642adab03075dda2f55719ac7b Mon Sep 17 00:00:00 2001
Patch-Source: https://github.com/dotnet/sdk/pull/14239
From: Esgariot <[email protected]>
Date: Mon, 19 Oct 2020 23:42:37 +0200
Subject: [PATCH 1/1] Add zsh compdef completion script.

Continuing off of PR 13384 in upstream.
This commit is ready to be rebased to include appropriate co-authors
before merging PR.

---
 scripts/register-completions.zsh | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/src/sdk/scripts/register-completions.zsh b/src/sdk/scripts/register-completions.zsh
index 8eebe9a867..faa32fef71 100644
--- a/src/sdk/scripts/register-completions.zsh
+++ b/src/sdk/scripts/register-completions.zsh
@@ -1,10 +1,3 @@
-# zsh parameter completion for the dotnet CLI
-
-_dotnet_zsh_complete() 
-{
-  local completions=("$(dotnet complete "$words")")
-
-  reply=( "${(ps:\n:)completions}" )
-}
-
-compctl -K _dotnet_zsh_complete dotnet
+#compdef dotnet
+local completions=("$(dotnet complete "$words")")
+[[ -n "$completions" ]] && compadd -- "${(ps:\n:)completions}" 
-- 
2.34.1

@marcpopMSFT

Copy link
Copy Markdown
Member

Pinging on old PRs for status. Is this PR still relevant and what would be needed to get it prepped for merge? @omajid @baronfel @Esgariot

@Esgariot

Esgariot commented Jun 6, 2023

Copy link
Copy Markdown
Contributor Author

It should be still applicable, I'll check and update the PR over the weekend

Continuing off of PR 13384 in upstream.
This commit is ready to be rebased to include appropriate co-authors
before merging PR.
@Esgariot Esgariot closed this Jun 6, 2023
@Esgariot
Esgariot deleted the master branch June 6, 2023 20:36
@Esgariot Esgariot reopened this Jun 7, 2023
@Esgariot

Esgariot commented Jun 7, 2023

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@Esgariot

Esgariot commented Jun 7, 2023

Copy link
Copy Markdown
Contributor Author

@marcpopMSFT @wli3 Please take a look now, I've added _files completions to the list, because dotnet complete doesn't include filenames in it's output.

@Esgariot

Esgariot commented Jun 7, 2023

Copy link
Copy Markdown
Contributor Author

I've also dropped scripts/_dotnet and replaced the existing completion script. Looking at e.g. Arch's PKGBUILD or Homebrew Formula, I see that they just copy the completion file into _dotnet, in appropriate location

@marcpopMSFT

Copy link
Copy Markdown
Member

I'll defer to @baronfel and @omajid for reviews as they are more knowledgeable in this space than I am.

@baronfel

baronfel commented Jun 7, 2023

Copy link
Copy Markdown
Member

This works and works well! I tested by installing zsh in a WSL ubuntu instance and adding the _dotnet script to the appropriate site-functions directory, then rebuilding my zsh completions cache. Future enhancements might include filtering the _files to just project files, solutions, and solution filters for the common cases, but this is already a nice step up 👍

@marcpopMSFT

Copy link
Copy Markdown
Member

Chatted with baronfel and we'll merge this and if Omajid has additional feedback we can update with a new PR. Thank you for the contribution and sorry for the delay.

@marcpopMSFT
marcpopMSFT merged commit b69c850 into dotnet:main Jun 20, 2023
@omajid

omajid commented Jun 20, 2023

Copy link
Copy Markdown
Member

Sorry about the lack of response. I don't use zsh, so I don't have actionable specific advice for this PR itself.

I am glad that we are finally shipping a compdef-style completion support with .NET! AFAIK compdef-style completion files are required for completion to work if maintainers want to install them system-wide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants