2020-09-02 23:21:49 +02:00
|
|
|
# gopls.kak: gopls bindings for kakoune
|
|
|
|
|
|
|
|
define-command -params 1 -docstring %{
|
2020-09-16 21:58:22 +02:00
|
|
|
gopls <command>: gopls command wrapper
|
2020-09-02 23:21:49 +02:00
|
|
|
|
|
|
|
All commands are forwarded to gopls utility
|
|
|
|
Available commands are:
|
|
|
|
format
|
|
|
|
imports
|
|
|
|
definition
|
|
|
|
references
|
|
|
|
} -shell-script-candidates %{
|
|
|
|
printf "format\nimports\ndefinition\nreferences\n"
|
|
|
|
} \
|
|
|
|
gopls %{
|
|
|
|
require-module gopls
|
|
|
|
evaluate-commands %sh{
|
|
|
|
case "$1" in
|
|
|
|
format|imports)
|
|
|
|
printf %s\\n "gopls-cmd $1"
|
|
|
|
;;
|
|
|
|
definition)
|
|
|
|
printf %s\\n "gopls-def"
|
|
|
|
;;
|
|
|
|
references)
|
|
|
|
printf %s\\n "gopls-ref"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
printf "fail Unknown gopls command '%s'\n" "$1"
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
provide-module gopls %§
|
|
|
|
|
|
|
|
evaluate-commands %sh{
|
|
|
|
if ! command -v gopls > /dev/null 2>&1; then
|
|
|
|
echo "fail Please install gopls or add to PATH!"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-09-16 21:58:22 +02:00
|
|
|
# Temp dir preparation
|
2020-09-02 23:21:49 +02:00
|
|
|
declare-option -hidden str gopls_tmp_dir
|
|
|
|
define-command -hidden -params 0 gopls-prepare %{
|
|
|
|
evaluate-commands %sh{
|
|
|
|
dir=$(mktemp -d "${TMPDIR:-/tmp}"/kak-gopls.XXXXXXXX)
|
|
|
|
printf %s\\n "set-option buffer gopls_tmp_dir ${dir}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# gopls format/imports
|
|
|
|
define-command -hidden -params 1 gopls-cmd %{
|
|
|
|
gopls-prepare
|
|
|
|
evaluate-commands %sh{
|
|
|
|
dir=${kak_opt_gopls_tmp_dir}
|
2020-10-16 22:40:58 +02:00
|
|
|
gopls "$1" -w "${kak_buffile}" 2> "${dir}/stderr"
|
2020-09-02 23:21:49 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
# show error messages in *debug* buffer
|
2020-10-16 22:40:58 +02:00
|
|
|
printf %s\\n "echo -debug %file{${dir}/stderr}"
|
2020-09-02 23:21:49 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
edit!
|
2020-10-17 09:47:07 +02:00
|
|
|
nop %sh{ rm -rf "${kak_opt_gopls_tmp_dir}" }
|
2020-09-02 23:21:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# gopls definition
|
|
|
|
define-command -hidden -params 0 gopls-def %{
|
|
|
|
evaluate-commands %sh{
|
2020-10-16 22:40:58 +02:00
|
|
|
jump=$( gopls definition "${kak_buffile}:${kak_cursor_line}:${kak_cursor_column}" 2> /dev/null \
|
|
|
|
|sed -e 's/-.*//; s/:/ /g; q' )
|
|
|
|
if [ -n "${jump}" ]; then
|
2020-09-02 23:21:49 +02:00
|
|
|
printf %s\\n "evaluate-commands -try-client '${kak_opt_jumpclient}' %{
|
2020-09-16 21:58:22 +02:00
|
|
|
edit ${jump}
|
2020-09-02 23:21:49 +02:00
|
|
|
}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# gopls references
|
|
|
|
define-command -hidden -params 0 gopls-ref %{
|
|
|
|
gopls-prepare
|
|
|
|
evaluate-commands %sh{
|
|
|
|
dir=${kak_opt_gopls_tmp_dir}
|
|
|
|
mkfifo "${dir}/fifo"
|
2020-10-16 22:40:58 +02:00
|
|
|
( gopls references "${kak_buffile}:${kak_cursor_line}:${kak_cursor_column}" \
|
2020-09-02 23:21:49 +02:00
|
|
|
> "${dir}/fifo" 2> /dev/null & ) > /dev/null 2>&1 < /dev/null
|
2020-09-16 21:58:22 +02:00
|
|
|
# using filetype=grep for nice hilight and <ret> mapping
|
2020-09-02 23:21:49 +02:00
|
|
|
printf %s\\n "evaluate-commands -try-client '${kak_opt_toolsclient}' %{
|
|
|
|
edit! -fifo '${dir}/fifo' *gopls-refs*
|
2020-09-16 21:58:22 +02:00
|
|
|
set-option buffer filetype grep
|
2020-09-02 23:21:49 +02:00
|
|
|
hook -always -once buffer BufCloseFifo .* %{ nop %sh{ rm -r '${dir}' } }
|
|
|
|
}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
§
|