-
Notifications
You must be signed in to change notification settings - Fork 61
/
zsh_codex.plugin.zsh
24 lines (20 loc) · 849 Bytes
/
zsh_codex.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/zsh
# This ZSH plugin reads the text from the current buffer
# and uses a Python script to complete the text.
create_completion() {
# Get the text typed until now.
text=${BUFFER}
#echo $cursor_line $cursor_col
ZSH_CODEX_PYTHON="${ZSH_CODEX_PYTHON:-python3}"
completion=$(echo -n "$text" | $ZSH_CODEX_PYTHON $ZSH_CUSTOM/plugins/zsh_codex/create_completion.py $CURSOR)
text_before_cursor=${text:0:$CURSOR}
text_after_cursor=${text:$CURSOR}
# Add completion to the current buffer.
#BUFFER="${text}${completion}"
BUFFER="${text_before_cursor}${completion}${text_after_cursor}"
prefix_and_completion="${text_before_cursor}${completion}"
# Put the cursor at the end of the completion
CURSOR=${#prefix_and_completion}
}
# Bind the create_completion function to a key.
zle -N create_completion