2017-11-03 10:34:41 +03:00
|
|
|
declare-option -hidden str racer_tmp_dir
|
|
|
|
declare-option -hidden completions racer_completions
|
2017-03-01 15:11:35 +03:00
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
define-command racer-complete -docstring "Complete the current selection with racer" %{
|
2017-03-01 15:11:35 +03:00
|
|
|
%sh{
|
2017-06-09 14:05:31 +03:00
|
|
|
dir=$(mktemp -d "${TMPDIR:-/tmp}"/kak-racer.XXXXXXXX)
|
2017-11-03 10:34:41 +03:00
|
|
|
printf %s\\n "set-option buffer racer_tmp_dir ${dir}"
|
2017-11-03 11:09:45 +03:00
|
|
|
printf %s\\n "evaluate-commands -no-hooks %{ write ${dir}/buf }"
|
2017-03-01 15:11:35 +03:00
|
|
|
}
|
|
|
|
%sh{
|
|
|
|
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
|
|
|
|
print ENVIRON["kak_cursor_line"] "." column "\\@" ENVIRON["kak_timestamp"]
|
|
|
|
}
|
|
|
|
/^MATCH/ {
|
|
|
|
word = $2
|
|
|
|
type = $7
|
|
|
|
desc = substr($9, 2, length($9) - 2)
|
2017-03-07 00:30:59 +03:00
|
|
|
gsub(/\|/, "\\|", desc)
|
2017-03-01 15:11:35 +03:00
|
|
|
gsub(/\\n/, "\n", desc)
|
|
|
|
menu = $8
|
|
|
|
sub(/^pub /, "", menu)
|
|
|
|
gsub(/\|/, "\\|", menu)
|
|
|
|
if (type == "Function") {
|
|
|
|
sub(word, "{default+e}" word "{default+d}", menu)
|
2018-02-26 16:07:41 +03: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 15:11:35 +03:00
|
|
|
menu = "{default+d}" menu
|
2017-03-07 11:55:22 +03:00
|
|
|
} else if (type == "Enum") {
|
|
|
|
menu = substr(menu, 0, length(menu) - 2)
|
|
|
|
sub(word, "{default+e}" word "{default+d}", menu)
|
2018-02-26 16:07:41 +03: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
|
|
|
|
}
|
|
|
|
menu = " mod {default+e}" word "{default+d} .." 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 == "Trait") {
|
|
|
|
sub(word, "{default+e}" word "{default+d}", menu)
|
|
|
|
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") {
|
|
|
|
sub(word, "{default+e}" word "{default+d}", menu)
|
|
|
|
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") {
|
|
|
|
sub(word, "{default+e}" word "{default+d}", menu) # Struct doesn''t have extra spaces because it''s
|
|
|
|
# the longest keyword
|
2017-03-01 15:11:35 +03:00
|
|
|
} else {
|
|
|
|
menu = "{default+e}" word "{default+d} " menu
|
|
|
|
}
|
|
|
|
candidate = word "|" desc "|" menu
|
|
|
|
gsub(/:/, "\\:", candidate)
|
|
|
|
print candidate
|
|
|
|
}'
|
|
|
|
)
|
2017-11-03 11:09:45 +03:00
|
|
|
printf %s\\n "evaluate-commands -client '${kak_client}' %{
|
2018-02-26 15:24:12 +03:00
|
|
|
set-option buffer=${kak_bufname} racer_completions %@${compl: : -1}@
|
2017-03-01 15:11:35 +03:00
|
|
|
}" | kak -p ${kak_session}
|
|
|
|
rm -r ${dir}
|
|
|
|
) > /dev/null 2>&1 < /dev/null &
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
define-command racer-enable-autocomplete -docstring "Add racer completion candidates to the completer" %{
|
|
|
|
set-option window completers "option=racer_completions:%opt{completers}"
|
2017-03-01 15:11:35 +03:00
|
|
|
hook window -group racer-autocomplete InsertIdle .* %{ try %{
|
2017-11-03 11:09:45 +03:00
|
|
|
execute-keys -draft <a-h><a-k>([\w\.]|::).\z<ret>
|
2017-03-01 15:11:35 +03:00
|
|
|
racer-complete
|
|
|
|
} }
|
|
|
|
alias window complete racer-complete
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
define-command racer-disable-autocomplete -docstring "Disable racer completion" %{
|
|
|
|
set-option window completers %sh{ printf %s\\n "'${kak_opt_completers}'" | sed 's/option=racer_completions://g' }
|
|
|
|
remove-hooks window racer-autocomplete
|
2017-03-01 15:11:35 +03:00
|
|
|
unalias window complete racer-complete
|
|
|
|
}
|