diff --git a/extras/completion/completion.zsh b/extras/completion/completion.zsh index 6ed6c131..02ca886c 100644 --- a/extras/completion/completion.zsh +++ b/extras/completion/completion.zsh @@ -11,24 +11,24 @@ _httpie_params () { local predecessor=$words[(( $CURRENT - 1 ))] fi - - if (( CURRENT == NORMARG + 0 )); then - _httpie_method && ret=0 - fi - if (( CURRENT == NORMARG + 0 )); then - _httpie_url && ret=0 - fi - if (( CURRENT == NORMARG + 1 )) && [[ ${METHODS[(ie)$predecessor]} -le ${#METHODS} ]]; then - _httpie_url && ret=0 - fi - if (( CURRENT >= NORMARG + 2 )) && ! [[ $current == -* ]]; then - _httpie_request_item && ret=0 - fi - if (( CURRENT >= NORMARG + 1 )) && ! [[ ${METHODS[(ie)$predecessor]} -le ${#METHODS} ]] && ! [[ $current == -* ]]; then - _httpie_request_item && ret=0 - fi - - + if ! [[ $current == -* ]]; then + if (( CURRENT == NORMARG + 0 )); then + _httpie_method && ret=0 +fi + if (( CURRENT == NORMARG + 0 )); then + _httpie_url && ret=0 +fi + if (( CURRENT == NORMARG + 1 )) && [[ ${METHODS[(ie)$predecessor]} -le ${#METHODS} ]]; then + _httpie_url && ret=0 +fi + if (( CURRENT >= NORMARG + 2 )); then + _httpie_request_item && ret=0 +fi + if (( CURRENT >= NORMARG + 1 )) && ! [[ ${METHODS[(ie)$predecessor]} -le ${#METHODS} ]]; then + _httpie_request_item && ret=0 +fi + fi + return $ret } diff --git a/extras/completion/templates/completion.zsh.j2 b/extras/completion/templates/completion.zsh.j2 index bd77351d..2fb5b241 100755 --- a/extras/completion/templates/completion.zsh.j2 +++ b/extras/completion/templates/completion.zsh.j2 @@ -11,23 +11,11 @@ _httpie_params () { local predecessor=$words[(( $CURRENT - 1 ))] fi - {% raw %} - if (( CURRENT == NORMARG + 0 )); then - _httpie_method && ret=0 + if ! [[ $current == -* ]]; then + {% for flow_item in generate_flow() -%} + {{ compile_zsh(flow_item) }} + {% endfor -%} fi - if (( CURRENT == NORMARG + 0 )); then - _httpie_url && ret=0 - fi - if (( CURRENT == NORMARG + 1 )) && [[ ${METHODS[(ie)$predecessor]} -le ${#METHODS} ]]; then - _httpie_url && ret=0 - fi - if (( CURRENT >= NORMARG + 2 )) && ! [[ $current == -* ]]; then - _httpie_request_item && ret=0 - fi - if (( CURRENT >= NORMARG + 1 )) && ! [[ ${METHODS[(ie)$predecessor]} -le ${#METHODS} ]] && ! [[ $current == -* ]]; then - _httpie_request_item && ret=0 - fi - {% endraw %} return $ret diff --git a/extras/scripts/completion/completion_flow.py b/extras/scripts/completion/completion_flow.py index 538ed681..f6d1faa4 100644 --- a/extras/scripts/completion/completion_flow.py +++ b/extras/scripts/completion/completion_flow.py @@ -66,7 +66,7 @@ class Not(Node): check: Node -def main_flow() -> Iterator[Node]: +def generate_flow() -> Iterator[Node]: # yield from suggest_option() yield from suggest_method() yield from suggest_url() diff --git a/extras/scripts/completion/generate_completion.py b/extras/scripts/completion/generate_completion.py index 37eca5fc..fef64f72 100644 --- a/extras/scripts/completion/generate_completion.py +++ b/extras/scripts/completion/generate_completion.py @@ -11,6 +11,8 @@ from httpie.cli.constants import SEPARATOR_FILE_UPLOAD from httpie.cli.definition import options from httpie.cli.options import Argument, ParserSpec +from completion_flow import generate_flow + T = TypeVar('T') EXTRAS_DIR = Path(__file__).parent.parent.parent @@ -77,6 +79,7 @@ def prepare_objects(spec: ParserSpec) -> Dict[str, Any]: if not argument.is_positional ] global_objects['methods'] = COMMON_HTTP_METHODS + global_objects['generate_flow'] = generate_flow return global_objects @@ -194,9 +197,12 @@ def find_argument_by_target_name(spec: ParserSpec, name: str) -> Argument: @use_template('zsh') def zsh_completer(spec: ParserSpec) -> Dict[str, Any]: + from zsh import compile_zsh + return { 'escape_zsh': escape_zsh, 'serialize_argument_to_zsh': serialize_argument_to_zsh, + 'compile_zsh': compile_zsh } diff --git a/extras/scripts/completion/zsh.py b/extras/scripts/completion/zsh.py index 97fef5d0..1a952e4f 100644 --- a/extras/scripts/completion/zsh.py +++ b/extras/scripts/completion/zsh.py @@ -1,5 +1,6 @@ from functools import singledispatch from enum import Enum +from lib2to3.pgen2.pgen import generate_grammar from completion_flow import ( Node, Check, @@ -10,7 +11,7 @@ from completion_flow import ( If, And, Not, - main_flow, + generate_flow, ) @@ -59,7 +60,7 @@ def compile_check(node: Check) -> str: parts = [ '[[ ${', args[0], - '[(ie)', + '[(ie)$', ZSHVariable.PREDECESSOR, ']}', ' -le ${#', @@ -82,6 +83,3 @@ def compile_not(node: Not) -> str: @compile_zsh.register(Suggest) def compile_suggest(node: Suggest) -> str: return SUGGESTION_TO_FUNCTION[node.suggestion] - -for item in main_flow(): - print(compile_zsh(item))