2019-03-13 22:00:59 +01:00
|
|
|
hook -once global BufSetOption filetype=python %{
|
|
|
|
require-module jedi
|
|
|
|
}
|
|
|
|
|
|
|
|
provide-module jedi %{
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
declare-option -hidden str jedi_tmp_dir
|
|
|
|
declare-option -hidden completions jedi_completions
|
|
|
|
declare-option -docstring "colon separated list of path added to `python`'s $PYTHONPATH environment variable" \
|
2018-05-30 15:28:50 +02:00
|
|
|
str jedi_python_path
|
2014-11-05 22:02:34 +01:00
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command jedi-complete -docstring "Complete the current selection" %{
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2017-06-09 13:05:31 +02:00
|
|
|
dir=$(mktemp -d "${TMPDIR:-/tmp}"/kak-jedi.XXXXXXXX)
|
2014-11-05 22:02:34 +01:00
|
|
|
mkfifo ${dir}/fifo
|
2017-11-03 08:34:41 +01:00
|
|
|
printf %s\\n "set-option buffer jedi_tmp_dir ${dir}"
|
2018-07-04 08:55:05 +02:00
|
|
|
printf %s\\n "evaluate-commands -no-hooks write -sync ${dir}/buf"
|
2014-11-05 22:02:34 +01:00
|
|
|
}
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2014-11-05 22:02:34 +01:00
|
|
|
dir=${kak_opt_jedi_tmp_dir}
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -draft %{ edit! -fifo ${dir}/fifo *jedi-output* }"
|
2019-06-10 18:25:51 +02:00
|
|
|
((
|
2024-03-29 17:27:27 +01:00
|
|
|
trap - INT QUIT
|
2014-11-05 22:02:34 +01:00
|
|
|
cd $(dirname ${kak_buffile})
|
|
|
|
|
2019-03-13 22:00:59 +01:00
|
|
|
export PYTHONPATH="$kak_opt_jedi_python_path:$PYTHONPATH"
|
2021-05-04 09:18:19 +02:00
|
|
|
python 2> "${dir}/fifo" -c 'if 1:
|
|
|
|
import os
|
|
|
|
dir = os.environ["kak_opt_jedi_tmp_dir"]
|
|
|
|
buffile = os.environ["kak_buffile"]
|
|
|
|
line = int(os.environ["kak_cursor_line"])
|
|
|
|
column = int(os.environ["kak_cursor_column"])
|
|
|
|
timestamp = os.environ["kak_timestamp"]
|
|
|
|
client = os.environ["kak_client"]
|
|
|
|
pipe_escape = lambda s: s.replace("|", "\\|")
|
|
|
|
def quote(s):
|
|
|
|
c = chr(39) # single quote
|
|
|
|
return c + s.replace(c, c+c) + c
|
|
|
|
import jedi
|
|
|
|
script = jedi.Script(code=open(dir + "/buf", "r").read(), path=buffile)
|
|
|
|
completions = (
|
|
|
|
quote(
|
|
|
|
pipe_escape(str(c.name)) + "|" +
|
|
|
|
pipe_escape("info -style menu -- " + quote(c.docstring())) + "|" +
|
|
|
|
pipe_escape(str(c.name))
|
|
|
|
)
|
|
|
|
for c in script.complete(line=line, column=column-1)
|
|
|
|
)
|
|
|
|
header = str(line) + "." + str(column) + "@" + timestamp
|
|
|
|
cmds = [
|
|
|
|
"echo completed",
|
|
|
|
" ".join(("set-option", quote("buffer=" + buffile), "jedi_completions", header, *completions)),
|
|
|
|
]
|
|
|
|
print("evaluate-commands -client", quote(client), quote("\n".join(cmds)))
|
|
|
|
' | kak -p "${kak_session}"
|
2014-11-19 14:54:36 +01:00
|
|
|
rm -r ${dir}
|
2019-06-10 18:25:51 +02:00
|
|
|
) & ) > /dev/null 2>&1 < /dev/null
|
2014-11-05 22:02:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command jedi-enable-autocomplete -docstring "Add jedi completion candidates to the completer" %{
|
2018-06-20 13:01:34 +02:00
|
|
|
set-option window completers option=jedi_completions %opt{completers}
|
2014-11-05 22:02:34 +01:00
|
|
|
hook window -group jedi-autocomplete InsertIdle .* %{ try %{
|
2017-11-03 09:09:45 +01:00
|
|
|
execute-keys -draft <a-h><a-k>\..\z<ret>
|
2014-11-05 22:02:34 +01:00
|
|
|
echo 'completing...'
|
|
|
|
jedi-complete
|
|
|
|
} }
|
2014-11-18 14:55:52 +01:00
|
|
|
alias window complete jedi-complete
|
2014-11-05 22:02:34 +01:00
|
|
|
}
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command jedi-disable-autocomplete -docstring "Disable jedi completion" %{
|
|
|
|
set-option window completers %sh{ printf %s\\n "'${kak_opt_completers}'" | sed -e 's/option=jedi_completions://g' }
|
2017-01-04 01:07:45 +01:00
|
|
|
remove-hooks window jedi-autocomplete
|
2014-11-18 14:55:52 +01:00
|
|
|
unalias window complete jedi-complete
|
2014-11-05 22:02:34 +01:00
|
|
|
}
|
2019-03-13 22:00:59 +01:00
|
|
|
|
|
|
|
}
|