summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Cross <ncross@redhat.com>2019-07-03 21:21:25 +0100
committerDavid Lord <davidism@gmail.com>2020-02-24 06:46:25 -0800
commit3106951b6fc733385f08b2a397621dec756a2989 (patch)
tree8065cf8019717e9d1a2d7cafaeff9292a2789703
parent37d897069f58f7f2a016c14b0620c6d387430b4b (diff)
downloadclick-3106951b6fc733385f08b2a397621dec756a2989.tar.gz
Correct ZSH completion definitions
-rw-r--r--CHANGES.rst2
-rw-r--r--click/_bashcomplete.py4
-rw-r--r--tests/test_compat.py8
3 files changed, 13 insertions, 1 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 0d4f409..8beb27d 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -57,7 +57,7 @@ Unreleased
``click.edit()``. :pr:`1469`
- Arguments to system calls such as the executable path passed to
``click.edit`` can contains spaces. :pr:`1470`
-
+- Add ZSH completion autoloading and error handling. :issue:`1348`
Version 7.0
-----------
diff --git a/click/_bashcomplete.py b/click/_bashcomplete.py
index a5f1084..e97c539 100644
--- a/click/_bashcomplete.py
+++ b/click/_bashcomplete.py
@@ -39,10 +39,14 @@ COMPLETION_SCRIPT_BASH = '''
'''
COMPLETION_SCRIPT_ZSH = '''
+#compdef %(script_names)s
+
%(complete_func)s() {
local -a completions
local -a completions_with_descriptions
local -a response
+ (( ! $+commands[%(script_names)s] )) && return 1
+
response=("${(@f)$( env COMP_WORDS=\"${words[*]}\" \\
COMP_CWORD=$((CURRENT-1)) \\
%(autocomplete_var)s=\"complete_zsh\" \\
diff --git a/tests/test_compat.py b/tests/test_compat.py
index abc733b..674cded 100644
--- a/tests/test_compat.py
+++ b/tests/test_compat.py
@@ -25,6 +25,14 @@ def test_bash_func_name():
assert '_COMPLETE_VAR=complete $1' in script
+def test_zsh_func_name():
+ from click._bashcomplete import get_completion_script
+ script = get_completion_script('foo-bar', '_COMPLETE_VAR', 'zsh').strip()
+ assert script.startswith('#compdef foo-bar')
+ assert 'compdef _foo_bar_completion foo-bar;' in script
+ assert '(( ! $+commands[foo-bar] )) && return 1' in script
+
+
@pytest.mark.xfail(WIN, reason="Jupyter not tested/supported on Windows")
def test_is_jupyter_kernel_output():
class JupyterKernelFakeStream(object):