From 6198ebe37d42a20f520bab275937f6475b7fba4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Ros=C3=A9n?= Date: Tue, 4 May 2021 09:18:19 +0200 Subject: [PATCH] Fix jedi off-by-one and refactor jedi The off-by-one was introduced by cd9b1e66 which changed `column-1` to `column`. The refactoring solves some esoteric quoting errors: I think cases like unbalanced braces in the bufname and client were not supported. --- rc/tools/python/jedi.kak | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/rc/tools/python/jedi.kak b/rc/tools/python/jedi.kak index 1efdfe3a..14c05124 100644 --- a/rc/tools/python/jedi.kak +++ b/rc/tools/python/jedi.kak @@ -21,16 +21,37 @@ define-command jedi-complete -docstring "Complete the current selection" %{ printf %s\\n "evaluate-commands -draft %{ edit! -fifo ${dir}/fifo *jedi-output* }" (( cd $(dirname ${kak_buffile}) - header="${kak_cursor_line}.${kak_cursor_column}@${kak_timestamp}" export PYTHONPATH="$kak_opt_jedi_python_path:$PYTHONPATH" - compl=$(python 2> "${dir}/fifo" <<-END - import jedi - script=jedi.Script(code=open('$dir/buf', 'r').read(), path='$kak_buffile') - print(' '.join(["'" + (str(c.name).replace("|", "\\|") + "|info -style menu %!" + str(c.docstring()).replace("|", "\\|").replace("!", "!!") + "!|" + str(c.name).replace("|", "\\|")).replace("~", "~~").replace("'", "''") + "'" for c in script.complete(line=$kak_cursor_line, column=$kak_cursor_column)])) - END - ) - printf %s\\n "evaluate-commands -client ${kak_client} %~echo completed; set-option %{buffer=${kak_buffile}} jedi_completions ${header} ${compl}~" | kak -p ${kak_session} + 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}" rm -r ${dir} ) & ) > /dev/null 2>&1 < /dev/null }