Embed the structurally defined flows into ZSH

This commit is contained in:
Batuhan Taskaya 2022-05-19 16:06:37 +03:00
parent 0dce332b16
commit b8e0be241c
5 changed files with 32 additions and 40 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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()

View File

@ -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
}

View File

@ -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))