2019-03-13 22:00:59 +01:00
|
|
|
hook -once global BufSetOption filetype=rust %{
|
|
|
|
require-module racer
|
|
|
|
}
|
|
|
|
|
|
|
|
provide-module racer %{
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
declare-option -hidden str racer_tmp_dir
|
|
|
|
declare-option -hidden completions racer_completions
|
2017-03-01 13:11:35 +01:00
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command racer-complete -docstring "Complete the current selection with racer" %{
|
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-racer.XXXXXXXX)
|
2017-11-03 08:34:41 +01:00
|
|
|
printf %s\\n "set-option buffer racer_tmp_dir ${dir}"
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -no-hooks %{ write ${dir}/buf }"
|
2017-03-01 13:11:35 +01:00
|
|
|
}
|
2018-07-10 10:55:47 +02:00
|
|
|
evaluate-commands %sh{
|
2017-03-01 13:11:35 +01:00
|
|
|
dir=${kak_opt_racer_tmp_dir}
|
|
|
|
(
|
|
|
|
cursor="${kak_cursor_line} $((${kak_cursor_column} - 1))"
|
|
|
|
racer_data=$(racer --interface tab-text complete-with-snippet ${cursor} ${kak_buffile} ${dir}/buf)
|
|
|
|
compl=$(printf %s\\n "${racer_data}" | awk '
|
|
|
|
BEGIN { FS = "\t"; ORS = ":" }
|
|
|
|
/^PREFIX/ {
|
|
|
|
column = ENVIRON["kak_cursor_column"] + $2 - $3
|
2018-07-10 10:55:47 +02:00
|
|
|
print ENVIRON["kak_cursor_line"] "." column "@@" ENVIRON["kak_timestamp"]
|
2017-03-01 13:11:35 +01:00
|
|
|
}
|
|
|
|
/^MATCH/ {
|
|
|
|
word = $2
|
|
|
|
type = $7
|
|
|
|
desc = substr($9, 2, length($9) - 2)
|
2017-03-06 22:30:59 +01:00
|
|
|
gsub(/\|/, "\\|", desc)
|
2017-03-01 13:11:35 +01:00
|
|
|
gsub(/\\n/, "\n", desc)
|
|
|
|
menu = $8
|
|
|
|
sub(/^pub /, "", menu)
|
|
|
|
gsub(/\|/, "\\|", menu)
|
|
|
|
if (type == "Function") {
|
2018-10-26 06:29:21 +02:00
|
|
|
sub(word, "{default+F}" word "{default+d}", menu)
|
2018-02-26 14:07:41 +01:00
|
|
|
gsub(/^fn /, " fn ", menu) # The extra spaces are there to vertically align
|
|
|
|
# the type of element on the menu to make it easy
|
|
|
|
# to read
|
2017-03-01 13:11:35 +01:00
|
|
|
menu = "{default+d}" menu
|
2017-03-07 09:55:22 +01:00
|
|
|
} else if (type == "Enum") {
|
|
|
|
menu = substr(menu, 0, length(menu) - 2)
|
2018-10-26 06:29:21 +02:00
|
|
|
sub(word, "{default+F}" word "{default+d}", menu)
|
2018-02-26 14:07:41 +01:00
|
|
|
gsub(/^enum /, " enum ", menu) # The extra spaces are there to vertically align
|
|
|
|
# the type of element on the menu to make it easy
|
|
|
|
# to read
|
|
|
|
} else if (type == "Module") {
|
|
|
|
if (length(menu) > 30) { # The "menu" bit (as returned by racer),
|
|
|
|
# contains the path to the source file
|
|
|
|
# containing the module...
|
|
|
|
|
|
|
|
menu = substr(menu, length(menu) - 29, 30) # ... trimming it, so the completion menu
|
|
|
|
# doesn''t get distorted if it''s too long
|
|
|
|
}
|
2018-10-26 06:29:21 +02:00
|
|
|
menu = " mod {default+F}" word "{default+d} .." menu # The extra spaces are there to vertically align
|
2018-02-26 14:07:41 +01:00
|
|
|
# the type of element on the menu to make it easy
|
|
|
|
# to read
|
|
|
|
} else if (type == "Trait") {
|
2018-10-26 06:29:21 +02:00
|
|
|
sub(word, "{default+F}" word "{default+d}", menu)
|
2018-02-26 14:07:41 +01:00
|
|
|
gsub(/^trait /, " trait ", menu) # The extra spaces are there to vertically align
|
|
|
|
# the type of element on the menu to make it easy
|
|
|
|
# to read
|
|
|
|
} else if (type == "Type") {
|
2018-10-26 06:29:21 +02:00
|
|
|
sub(word, "{default+F}" word "{default+d}", menu)
|
2018-02-26 14:07:41 +01:00
|
|
|
gsub(/^type /, " type ", menu) # The extra spaces are there to vertically align
|
|
|
|
# the type of element on the menu to make it easy
|
|
|
|
# to read
|
|
|
|
} else if (type == "Struct") {
|
2018-10-26 06:29:21 +02:00
|
|
|
sub(word, "{default+F}" word "{default+d}", menu) # Struct doesn''t have extra spaces because it''s
|
2018-02-26 14:07:41 +01:00
|
|
|
# the longest keyword
|
2017-03-01 13:11:35 +01:00
|
|
|
} else {
|
2018-10-26 06:29:21 +02:00
|
|
|
menu = "{default+F}" word "{default+d} " menu
|
2017-03-01 13:11:35 +01:00
|
|
|
}
|
|
|
|
candidate = word "|" desc "|" menu
|
|
|
|
gsub(/:/, "\\:", candidate)
|
|
|
|
print candidate
|
|
|
|
}'
|
|
|
|
)
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -client '${kak_client}' %{
|
2018-03-04 21:14:53 +01:00
|
|
|
set-option buffer=${kak_bufname} racer_completions %@${compl%?}@
|
2017-03-01 13:11:35 +01:00
|
|
|
}" | kak -p ${kak_session}
|
|
|
|
rm -r ${dir}
|
|
|
|
) > /dev/null 2>&1 < /dev/null &
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command racer-enable-autocomplete -docstring "Add racer completion candidates to the completer" %{
|
2018-07-10 10:55:47 +02:00
|
|
|
set-option window completers option=racer_completions %opt{completers}
|
2017-03-01 13:11:35 +01:00
|
|
|
hook window -group racer-autocomplete InsertIdle .* %{ try %{
|
2017-11-03 09:09:45 +01:00
|
|
|
execute-keys -draft <a-h><a-k>([\w\.]|::).\z<ret>
|
2017-03-01 13:11:35 +01:00
|
|
|
racer-complete
|
|
|
|
} }
|
|
|
|
alias window complete racer-complete
|
|
|
|
}
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command racer-disable-autocomplete -docstring "Disable racer completion" %{
|
2018-07-10 10:55:47 +02:00
|
|
|
evaluate-commands %sh{ printf "set-option window completers %s\n" $(printf %s "${kak_opt_completers}" | sed -e "s/'option=racer_completions'//g") }
|
2017-11-03 08:34:41 +01:00
|
|
|
remove-hooks window racer-autocomplete
|
2017-03-01 13:11:35 +01:00
|
|
|
unalias window complete racer-complete
|
|
|
|
}
|
2018-03-12 13:47:47 +01:00
|
|
|
|
|
|
|
define-command racer-go-definition -docstring "Jump to where the rust identifier below the cursor is defined" %{
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2018-03-12 13:47:47 +01:00
|
|
|
dir=$(mktemp -d "${TMPDIR:-/tmp}"/kak-racer.XXXXXXXX)
|
|
|
|
printf %s\\n "set-option buffer racer_tmp_dir ${dir}"
|
|
|
|
printf %s\\n "evaluate-commands -no-hooks %{ write ${dir}/buf }"
|
|
|
|
}
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2018-03-12 13:47:47 +01:00
|
|
|
dir=${kak_opt_racer_tmp_dir}
|
2018-03-14 16:56:25 +01:00
|
|
|
cursor="${kak_cursor_line} $((${kak_cursor_column} - 1))"
|
|
|
|
racer_data=$(racer --interface tab-text find-definition ${cursor} "${kak_buffile}" "${dir}/buf" | head -n 1)
|
2018-03-15 21:37:27 +01:00
|
|
|
|
2018-03-14 16:56:25 +01:00
|
|
|
racer_match=$(printf %s\\n "$racer_data" | cut -f1 )
|
|
|
|
if [ "$racer_match" = "MATCH" ]; then
|
|
|
|
racer_line=$(printf %s\\n "$racer_data" | cut -f3 )
|
|
|
|
racer_column=$(printf %s\\n "$racer_data" | cut -f4 )
|
|
|
|
racer_file=$(printf %s\\n "$racer_data" | cut -f5 )
|
|
|
|
printf %s\\n "edit -existing '$racer_file' $racer_line $racer_column"
|
2018-03-16 13:25:17 +01:00
|
|
|
case ${racer_file} in
|
2018-03-17 21:27:11 +01:00
|
|
|
"${RUST_SRC_PATH}"* | "${CARGO_HOME:-$HOME/.cargo}"/registry/src/*)
|
|
|
|
printf %s\\n "set-option buffer readonly true";;
|
2018-03-16 13:25:17 +01:00
|
|
|
esac
|
2018-03-14 16:56:25 +01:00
|
|
|
else
|
|
|
|
printf %s\\n "echo -debug 'racer could not find a definition'"
|
|
|
|
fi
|
2018-03-12 13:47:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
define-command racer-show-doc -docstring "Show the documentation about the rust identifier below the cursor" %{
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2018-03-12 13:47:47 +01:00
|
|
|
dir=$(mktemp -d "${TMPDIR:-/tmp}"/kak-racer.XXXXXXXX)
|
|
|
|
printf %s\\n "set-option buffer racer_tmp_dir ${dir}"
|
|
|
|
printf %s\\n "evaluate-commands -no-hooks %{ write ${dir}/buf }"
|
|
|
|
}
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2018-03-12 13:47:47 +01:00
|
|
|
dir=${kak_opt_racer_tmp_dir}
|
2018-03-14 16:56:25 +01:00
|
|
|
cursor="${kak_cursor_line} ${kak_cursor_column}"
|
2018-03-15 21:37:27 +01:00
|
|
|
racer_data=$(racer --interface tab-text complete-with-snippet ${cursor} "${kak_buffile}" "${dir}/buf" | sed -n 2p )
|
2018-03-14 16:56:25 +01:00
|
|
|
racer_match=$(printf %s\\n "$racer_data" | cut -f1)
|
|
|
|
if [ "$racer_match" = "MATCH" ]; then
|
2018-03-18 11:46:46 +01:00
|
|
|
racer_doc=$(
|
|
|
|
printf %s\\n "$racer_data" |
|
|
|
|
cut -f9 |
|
2018-03-17 21:27:11 +01:00
|
|
|
sed -e '
|
|
|
|
|
|
|
|
# Remove leading and trailing quotes
|
|
|
|
s/^"\(.*\)"$/\1/g
|
|
|
|
|
|
|
|
# Escape all @ so that it can be properly used in the string expansion
|
|
|
|
s/@/\\@/g
|
|
|
|
|
|
|
|
')
|
2018-03-14 16:56:25 +01:00
|
|
|
printf "info %%@$racer_doc@"
|
|
|
|
else
|
|
|
|
printf %s\\n "echo -debug 'racer could not find a definition'"
|
|
|
|
fi
|
2018-03-12 13:47:47 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-13 22:00:59 +01:00
|
|
|
|
|
|
|
}
|