2016-10-03 22:56:29 +02:00
|
|
|
# Provides integration of the following tools:
|
|
|
|
# - gocode for code completion (github.com/nsf/gocode)
|
2016-11-05 18:40:37 +01:00
|
|
|
# - goimports for code formatting on save
|
2016-10-03 22:56:29 +02:00
|
|
|
# - gogetdoc for documentation display and source jump (needs jq) (github.com/zmb3/gogetdoc)
|
|
|
|
# Needs the following tools in the path:
|
|
|
|
# - jq for json deserializaton
|
|
|
|
|
2019-03-13 06:24:33 +01:00
|
|
|
hook -once global BufSetOption filetype=go %{
|
|
|
|
require-module go-tools
|
|
|
|
}
|
|
|
|
|
|
|
|
provide-module go-tools %{
|
|
|
|
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2017-08-29 08:57:19 +02:00
|
|
|
for dep in gocode goimports gogetdoc jq; do
|
|
|
|
if ! command -v $dep > /dev/null 2>&1; then
|
|
|
|
echo "echo -debug %{Dependency unmet: $dep, please install it to use go-tools}"
|
|
|
|
fi
|
|
|
|
done
|
2017-08-18 16:06:17 +02:00
|
|
|
}
|
|
|
|
|
2016-10-03 22:56:29 +02:00
|
|
|
# Auto-completion
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
declare-option -hidden str go_complete_tmp_dir
|
|
|
|
declare-option -hidden completions gocode_completions
|
2016-10-03 22:56:29 +02:00
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command go-complete -docstring "Complete the current selection with gocode" %{
|
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-go.XXXXXXXX)
|
2017-11-03 08:34:41 +01:00
|
|
|
printf %s\\n "set-option buffer go_complete_tmp_dir ${dir}"
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -no-hooks write ${dir}/buf"
|
2016-10-03 22:56:29 +02:00
|
|
|
}
|
2018-05-06 23:29:52 +02:00
|
|
|
nop %sh{
|
2016-10-03 22:56:29 +02:00
|
|
|
dir=${kak_opt_go_complete_tmp_dir}
|
|
|
|
(
|
|
|
|
gocode_data=$(gocode -f=godit --in=${dir}/buf autocomplete ${kak_cursor_byte_offset})
|
|
|
|
rm -r ${dir}
|
2018-09-10 13:22:32 +02:00
|
|
|
column_offset=$(printf %s "${gocode_data}" | head -n1 | cut -d, -f1)
|
2016-10-03 22:56:29 +02:00
|
|
|
|
|
|
|
header="${kak_cursor_line}.$((${kak_cursor_column} - $column_offset))@${kak_timestamp}"
|
2018-09-10 13:22:32 +02:00
|
|
|
compl=$(echo "${gocode_data}" | sed 1d | awk -F ",," '{
|
|
|
|
gsub(/~/, "~~", $1)
|
|
|
|
gsub(/~/, "~~", $2)
|
|
|
|
print "%~" $2 "||" $1 "~"
|
|
|
|
}' | paste -s -)
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -client '${kak_client}' %{
|
2018-09-04 20:41:35 +02:00
|
|
|
set-option 'buffer=${kak_bufname}' gocode_completions ${header} ${compl}
|
2016-10-03 22:56:29 +02:00
|
|
|
}" | kak -p ${kak_session}
|
|
|
|
) > /dev/null 2>&1 < /dev/null &
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command go-enable-autocomplete -docstring "Add gocode completion candidates to the completer" %{
|
2018-09-04 19:58:57 +02:00
|
|
|
set-option window completers "option=gocode_completions" %opt{completers}
|
2016-10-03 22:56:29 +02:00
|
|
|
hook window -group go-autocomplete InsertIdle .* %{ try %{
|
2017-11-03 09:09:45 +01:00
|
|
|
execute-keys -draft <a-h><a-k>[\w\.].\z<ret>
|
2016-10-03 22:56:29 +02:00
|
|
|
go-complete
|
|
|
|
} }
|
|
|
|
alias window complete go-complete
|
|
|
|
}
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command go-disable-autocomplete -docstring "Disable gocode completion" %{
|
2018-07-08 13:02:51 +02:00
|
|
|
set-option window completers %sh{ printf %s\\n "${kak_opt_completers}" | sed "s/'option=gocode_completions'//g" }
|
2017-01-04 01:07:45 +01:00
|
|
|
remove-hooks window go-autocomplete
|
2016-10-03 22:56:29 +02:00
|
|
|
unalias window complete go-complete
|
|
|
|
}
|
|
|
|
|
|
|
|
# Auto-format
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
declare-option -hidden str go_format_tmp_dir
|
2016-10-03 22:56:29 +02:00
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command -params ..1 go-format \
|
2016-11-06 12:31:27 +01:00
|
|
|
-docstring "go-format [-use-goimports]: custom formatter for go files" %{
|
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-go.XXXXXXXX)
|
2017-11-03 08:34:41 +01:00
|
|
|
printf %s\\n "set-option buffer go_format_tmp_dir ${dir}"
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -no-hooks write ${dir}/buf"
|
2016-10-03 22:56:29 +02:00
|
|
|
}
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2016-10-03 22:56:29 +02:00
|
|
|
dir=${kak_opt_go_format_tmp_dir}
|
2016-11-06 12:31:27 +01:00
|
|
|
if [ "$1" = "-use-goimports" ]; then
|
2016-10-03 22:56:29 +02:00
|
|
|
fmt_cmd="goimports -srcdir '${kak_buffile}'"
|
|
|
|
else
|
|
|
|
fmt_cmd="gofmt -s"
|
|
|
|
fi
|
|
|
|
eval "${fmt_cmd} -e -w ${dir}/buf 2> ${dir}/stderr"
|
|
|
|
if [ $? ]; then
|
|
|
|
cp ${dir}/buf "${kak_buffile}"
|
|
|
|
else
|
|
|
|
# we should report error if linting isn't active
|
|
|
|
printf %s\\n "echo -debug '$(cat ${dir}/stderr)'"
|
|
|
|
fi
|
|
|
|
rm -r ${dir}
|
|
|
|
}
|
|
|
|
edit!
|
|
|
|
}
|
|
|
|
|
|
|
|
# Documentation
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
declare-option -hidden str go_doc_tmp_dir
|
2016-10-03 22:56:29 +02:00
|
|
|
|
|
|
|
# FIXME text escaping
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command -hidden -params 1..2 gogetdoc-cmd %{
|
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-go.XXXXXXXX)
|
2017-11-03 08:34:41 +01:00
|
|
|
printf %s\\n "set-option buffer go_doc_tmp_dir ${dir}"
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -no-hooks write ${dir}/buf"
|
2016-10-03 22:56:29 +02:00
|
|
|
}
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2016-10-03 22:56:29 +02:00
|
|
|
dir=${kak_opt_go_doc_tmp_dir}
|
|
|
|
(
|
|
|
|
printf %s\\n "${kak_buffile}" > ${dir}/modified
|
|
|
|
cat ${dir}/buf | wc -c >> ${dir}/modified
|
|
|
|
cat ${dir}/buf >> ${dir}/modified
|
|
|
|
|
|
|
|
if [ "$2" = "1" ]; then
|
|
|
|
args="-json"
|
|
|
|
fi
|
|
|
|
output=$(cat ${dir}/modified \
|
|
|
|
| gogetdoc $args -pos "${kak_buffile}:#${kak_cursor_byte_offset}" -modified \
|
|
|
|
| sed 's/%/%%/g')
|
|
|
|
rm -r ${dir}
|
|
|
|
printf %s "${output}" | grep -v -q "^gogetdoc: "
|
|
|
|
status=$?
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
"info")
|
|
|
|
if [ ${status} -eq 0 ]; then
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -client '${kak_client}' %{
|
2016-10-03 22:56:29 +02:00
|
|
|
info -anchor ${kak_cursor_line}.${kak_cursor_column} %@${output}@
|
|
|
|
}" | kak -p ${kak_session}
|
|
|
|
else
|
|
|
|
msg=$(printf %s "${output}" | cut -d' ' -f2-)
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -client '${kak_client}' %{
|
2016-10-03 22:56:29 +02:00
|
|
|
echo '${msg}'
|
|
|
|
}" | kak -p ${kak_session}
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
"echo")
|
|
|
|
if [ ${status} -eq 0 ]; then
|
|
|
|
signature=$(printf %s "${output}" | sed -n 3p)
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -client '${kak_client}' %{
|
2016-10-03 22:56:29 +02:00
|
|
|
echo '${signature}'
|
|
|
|
}" | kak -p ${kak_session}
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
"jump")
|
|
|
|
if [ ${status} -eq 0 ]; then
|
|
|
|
pos=$(printf %s "${output}" | jq -r .pos)
|
|
|
|
file=$(printf %s "${pos}" | cut -d: -f1)
|
|
|
|
line=$(printf %s "${pos}" | cut -d: -f2)
|
|
|
|
col=$(printf %s "${pos}" | cut -d: -f3)
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -client '${kak_client}' %{
|
|
|
|
evaluate-commands -try-client '${kak_opt_jumpclient}' edit -existing ${file} ${line} ${col}
|
2016-11-05 18:40:37 +01:00
|
|
|
try %{ focus '${kak_opt_jumpclient}' }
|
2016-10-03 22:56:29 +02:00
|
|
|
}" | kak -p ${kak_session}
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -client '${kak_client}' %{
|
2016-10-03 22:56:29 +02:00
|
|
|
echo -error %{unkown command '$1'}
|
|
|
|
}" | kak -p ${kak_session}
|
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|
|
|
|
) > /dev/null 2>&1 < /dev/null &
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command go-doc-info -docstring "Show the documention of the symbol under the cursor" %{
|
2017-01-13 01:56:30 +01:00
|
|
|
gogetdoc-cmd "info"
|
2016-10-03 22:56:29 +02:00
|
|
|
}
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command go-print-signature -docstring "Print the signature of the symbol under the cursor" %{
|
2017-01-13 01:56:30 +01:00
|
|
|
gogetdoc-cmd "echo"
|
2016-10-03 22:56:29 +02:00
|
|
|
}
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command go-jump -docstring "Jump to the symbol definition" %{
|
2017-01-13 01:56:30 +01:00
|
|
|
gogetdoc-cmd "jump" 1
|
2016-10-03 22:56:29 +02:00
|
|
|
}
|
|
|
|
|
2018-05-06 23:29:52 +02:00
|
|
|
define-command go-share-selection -docstring "Share the selection using the Go Playground" %{ evaluate-commands %sh{
|
2016-10-03 22:56:29 +02:00
|
|
|
snippet_id=$(printf %s\\n "${kak_selection}" | curl -s https://play.golang.org/share --data-binary @-)
|
2016-11-05 18:40:37 +01:00
|
|
|
printf "echo https://play.golang.org/p/%s" ${snippet_id}
|
2016-10-03 22:56:29 +02:00
|
|
|
} }
|
2019-03-13 06:24:33 +01:00
|
|
|
|
|
|
|
}
|