2013-03-14 14:12:14 +01:00
|
|
|
decl str clang_options
|
2014-04-03 20:00:42 +02:00
|
|
|
|
2014-10-24 19:46:42 +02:00
|
|
|
decl -hidden str clang_tmp_dir
|
2014-04-03 20:00:42 +02:00
|
|
|
decl -hidden str-list clang_completions
|
2015-12-12 12:45:45 +01:00
|
|
|
decl -hidden line-flags clang_flags
|
2014-11-19 14:53:31 +01:00
|
|
|
decl -hidden str clang_errors
|
2013-03-14 14:12:14 +01:00
|
|
|
|
2015-12-02 02:18:20 +01:00
|
|
|
def clang-parse -params 0..1 -docstring "Parse the contents of the current buffer with clang" %{
|
2013-03-14 14:12:14 +01:00
|
|
|
%sh{
|
2014-10-24 19:46:42 +02:00
|
|
|
dir=$(mktemp -d -t kak-clang.XXXXXXXX)
|
|
|
|
mkfifo ${dir}/fifo
|
|
|
|
echo "set buffer clang_tmp_dir ${dir}"
|
2014-10-31 21:53:36 +01:00
|
|
|
echo "write ${dir}/buf"
|
2013-03-14 14:12:14 +01:00
|
|
|
}
|
|
|
|
# end the previous %sh{} so that its output gets interpreted by kakoune
|
|
|
|
# before launching the following as a background task.
|
|
|
|
%sh{
|
2014-10-24 19:46:42 +02:00
|
|
|
dir=${kak_opt_clang_tmp_dir}
|
|
|
|
echo "eval -draft %{
|
|
|
|
edit! -fifo ${dir}/fifo *clang-output*
|
|
|
|
set buffer filetype make
|
|
|
|
set buffer _make_current_error_line 0
|
2015-03-04 00:24:12 +01:00
|
|
|
hook -group fifo buffer BufCloseFifo .* %{
|
|
|
|
nop %sh{ rm -r ${dir} }
|
|
|
|
rmhooks buffer fifo
|
|
|
|
}
|
2014-10-24 19:46:42 +02:00
|
|
|
}"
|
2014-11-06 20:17:13 +01:00
|
|
|
# this runs in a detached shell, asynchronously, so that kakoune does
|
|
|
|
# not hang while clang is running. As completions references a cursor
|
|
|
|
# position and a buffer timestamp, only valid completions should be
|
2013-03-14 14:12:14 +01:00
|
|
|
# displayed.
|
|
|
|
(
|
2014-11-21 14:27:43 +01:00
|
|
|
case ${kak_opt_filetype} in
|
2015-09-22 00:37:49 +02:00
|
|
|
c) ft=c ;;
|
2014-11-21 14:27:43 +01:00
|
|
|
cpp) ft=c++ ;;
|
|
|
|
obj-c) ft=objective-c ;;
|
|
|
|
*) ft=c++ ;;
|
|
|
|
esac
|
|
|
|
|
2016-03-14 21:59:23 +01:00
|
|
|
if [ "$1" = "-complete" ]; then
|
2015-02-03 01:42:40 +01:00
|
|
|
pos=-:${kak_cursor_line}:${kak_cursor_column}
|
|
|
|
header="${kak_cursor_line}.${kak_cursor_column}@${kak_timestamp}"
|
|
|
|
compl=$(clang++ -x ${ft} -fsyntax-only ${kak_opt_clang_options} \
|
|
|
|
-Xclang -code-completion-brief-comments -Xclang -code-completion-at=${pos} - < ${dir}/buf 2> ${dir}/stderr |
|
|
|
|
awk -F ': ' -e '
|
|
|
|
/^COMPLETION:/ && ! /\(Hidden\)/ {
|
|
|
|
gsub(/[[{<]#|#[}>]/, "", $3)
|
|
|
|
gsub(/#]/, " ", $3)
|
|
|
|
gsub(/:: /, "::", $3)
|
|
|
|
gsub(/ +$/, "", $3)
|
|
|
|
id=substr($2, 1, length($2)-1)
|
|
|
|
gsub(/:/, "\\:", id)
|
|
|
|
desc=$4 ? $3 "\\n" $4 : $3
|
|
|
|
gsub(/:/, "\\:", desc)
|
2015-10-06 23:50:51 +02:00
|
|
|
if (id in docstrings)
|
|
|
|
docstrings[id]=docstrings[id] "\\n" desc
|
2015-02-03 01:42:40 +01:00
|
|
|
else
|
2015-10-06 23:50:51 +02:00
|
|
|
docstrings[id]=desc
|
2015-02-03 01:42:40 +01:00
|
|
|
}
|
|
|
|
END {
|
2015-10-06 23:50:51 +02:00
|
|
|
for (id in docstrings)
|
2015-10-06 14:39:09 +02:00
|
|
|
{
|
|
|
|
menu=id
|
2015-10-07 00:22:36 +02:00
|
|
|
gsub(/(^|[^[:alnum:]_])(operator|new|delete)($|[^[:alnum:]_])/, "{keyword}&{}", menu)
|
|
|
|
gsub(/(^|[[:space:]])(int|size_t|bool|char|unsigned|signed|long)($|[[:space:]])/, "{type}&{}", menu)
|
|
|
|
gsub(/[^[:alnum:]{}_]+/, "{operator}&{}", menu)
|
2015-10-06 23:50:51 +02:00
|
|
|
print id "@" docstrings[id] "@" menu
|
2015-10-06 14:39:09 +02:00
|
|
|
}
|
2015-03-04 00:24:12 +01:00
|
|
|
}' | sort | paste -s -d ':' | sed -e "s/\\\\n/\\n/g; s/'/\\\\'/g")
|
2015-03-14 12:27:35 +01:00
|
|
|
echo "eval -client ${kak_client} echo 'clang completion done'
|
2015-03-04 00:24:12 +01:00
|
|
|
set 'buffer=${kak_buffile}' clang_completions '${header}:${compl}'" | kak -p ${kak_session}
|
2014-11-27 14:42:36 +01:00
|
|
|
else
|
2015-02-03 01:42:40 +01:00
|
|
|
clang++ -x ${ft} -fsyntax-only ${kak_opt_clang_options} - < ${dir}/buf 2> ${dir}/stderr
|
2015-03-14 12:27:35 +01:00
|
|
|
echo "eval -client ${kak_client} echo 'clang parsing done'" | kak -p ${kak_session}
|
2014-11-27 14:42:36 +01:00
|
|
|
fi
|
2014-10-31 21:53:36 +01:00
|
|
|
|
2014-11-27 14:42:36 +01:00
|
|
|
flags=$(cat ${dir}/stderr | sed -rne "
|
2015-12-13 00:16:07 +01:00
|
|
|
/^<stdin>:[0-9]+:([0-9]+:)? (fatal )?error/ { s/^<stdin>:([0-9]+):.*/\1|{red}█/; p }
|
|
|
|
/^<stdin>:[0-9]+:([0-9]+:)? warning/ { s/^<stdin>:([0-9]+):.*/\1|{yellow}█/; p }
|
2014-11-19 14:53:31 +01:00
|
|
|
" | paste -s -d ':')
|
|
|
|
|
2014-11-27 14:42:36 +01:00
|
|
|
errors=$(cat ${dir}/stderr | sed -rne "
|
2015-08-04 19:41:56 +02:00
|
|
|
/^<stdin>:[0-9]+:([0-9]+:)? ((fatal )?error|warning)/ { s/^<stdin>:([0-9]+):([0-9]+:)? (.*)/\1,\3/; s/'/\\\\'/g; p }
|
2015-02-18 14:56:13 +01:00
|
|
|
" | sort -n)
|
2014-11-19 14:53:31 +01:00
|
|
|
|
2014-11-27 14:42:36 +01:00
|
|
|
sed -e "s|<stdin>|${kak_bufname}|g" < ${dir}/stderr > ${dir}/fifo
|
2014-11-19 14:53:31 +01:00
|
|
|
|
2015-12-12 12:45:45 +01:00
|
|
|
echo "set 'buffer=${kak_buffile}' clang_flags %{${kak_timestamp}:${flags}}
|
2015-02-18 14:55:47 +01:00
|
|
|
set 'buffer=${kak_buffile}' clang_errors '${errors}'" | kak -p ${kak_session}
|
2014-03-06 04:35:38 +01:00
|
|
|
) > /dev/null 2>&1 < /dev/null &
|
2013-03-14 14:12:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 20:44:27 +02:00
|
|
|
def clang-complete -docstring "Complete the current selection with clang" %{ clang-parse -complete }
|
2014-11-27 14:42:36 +01:00
|
|
|
|
2015-11-30 14:56:01 +01:00
|
|
|
def -hidden clang-show-completion-info %[ try %[
|
|
|
|
eval -draft %[
|
|
|
|
exec '<space>{(<a-k>^\(<ret>b'
|
|
|
|
%sh[
|
2016-03-12 17:44:08 +01:00
|
|
|
# desc=$(echo "${kak_opt_clang_completions}" | sed -e 's/\([^\\]\):/\1\n/g;' | sed -ne "/^${kak_selection}@/ { s/^[^@]\+@//; s/@.*$//; s/\\:/:/g; p }")
|
|
|
|
desc=$(echo "${kak_opt_clang_completions}" | sed -ne "{ s/\([^\\]\):/\1\n/g }; /^${kak_selection}@/ { s/^[^@]\+@//; s/@.*$//; s/\\\:/:/g; P }; D")
|
2015-11-30 14:56:01 +01:00
|
|
|
if [ -n "$desc" ]; then
|
2016-03-04 14:35:27 +01:00
|
|
|
echo "eval -client $kak_client %{info -anchor ${kak_cursor_line}.${kak_cursor_column} -placement above %{${desc}}}"
|
2015-11-30 14:56:01 +01:00
|
|
|
fi
|
|
|
|
] ]
|
|
|
|
] ]
|
|
|
|
|
2015-08-03 20:44:27 +02:00
|
|
|
def clang-enable-autocomplete -docstring "Enable completion with clang" %{
|
2014-11-11 14:53:57 +01:00
|
|
|
set window completers "option=clang_completions:%opt{completers}"
|
2015-11-30 14:56:01 +01:00
|
|
|
hook window -group clang-autocomplete InsertIdle .* %{
|
|
|
|
try %{
|
|
|
|
exec -draft <a-h><a-k>(\.|->|::).\'<ret>
|
|
|
|
echo 'completing...'
|
|
|
|
clang-complete
|
|
|
|
}
|
|
|
|
clang-show-completion-info
|
|
|
|
}
|
2014-11-18 14:55:52 +01:00
|
|
|
alias window complete clang-complete
|
2013-03-14 14:12:14 +01:00
|
|
|
}
|
2013-04-11 14:30:02 +02:00
|
|
|
|
2015-08-03 20:44:27 +02:00
|
|
|
def clang-disable-autocomplete -docstring "Disable automatic clang completion" %{
|
2016-01-10 21:41:46 +01:00
|
|
|
set window completers %sh{ printf %s "'${kak_opt_completers}'" | sed -e 's/option=clang_completions://g' }
|
2014-03-15 04:14:23 +01:00
|
|
|
rmhooks window clang-autocomplete
|
2014-11-18 14:55:52 +01:00
|
|
|
unalias window complete clang-complete
|
2014-03-15 04:14:23 +01:00
|
|
|
}
|
2014-11-19 14:53:31 +01:00
|
|
|
|
2016-03-03 20:28:45 +01:00
|
|
|
def -hidden clang-show-error-info %{ %sh{
|
2016-01-10 21:41:46 +01:00
|
|
|
printf %s "${kak_opt_clang_errors}" | grep "^${kak_cursor_line},.*" | if read line; then
|
2016-01-26 07:32:23 +01:00
|
|
|
desc=$(printf %s "${line}" | sed -e "s/^[[:digit:]]\+,//g; s/'/\\\\'/g")
|
2015-11-21 13:07:53 +01:00
|
|
|
echo "info -anchor ${kak_cursor_line}.${kak_cursor_column} '${desc}'"
|
2015-11-30 14:56:01 +01:00
|
|
|
fi
|
2014-11-19 14:53:31 +01:00
|
|
|
} }
|
|
|
|
|
2015-08-03 20:44:27 +02:00
|
|
|
def clang-enable-diagnostics -docstring "Activate automatic diagnostics of the code by clang" %{
|
2015-12-13 00:16:07 +01:00
|
|
|
addhl flag_lines default clang_flags
|
2014-11-19 14:53:31 +01:00
|
|
|
hook window -group clang-diagnostics NormalIdle .* %{ clang-show-error-info }
|
|
|
|
}
|
|
|
|
|
2015-08-03 20:44:27 +02:00
|
|
|
def clang-disable-diagnostics -docstring "Disable automatic diagnostics of the code" %{
|
2014-11-19 14:53:31 +01:00
|
|
|
rmhl hlflags_clang_flags
|
|
|
|
rmhooks window clang-diagnostics
|
|
|
|
}
|
2015-02-18 14:56:13 +01:00
|
|
|
|
2015-08-03 20:44:27 +02:00
|
|
|
def clang-diagnostics-next -docstring "Jump to the next line that contains an error" %{ %sh{
|
2016-03-03 20:28:45 +01:00
|
|
|
printf "%s\n" "${kak_opt_clang_errors}" | (
|
2015-02-18 14:56:13 +01:00
|
|
|
line=-1
|
|
|
|
first_line=-1
|
|
|
|
while read line_content; do
|
|
|
|
candidate=${line_content%%,*}
|
2015-08-04 19:41:56 +02:00
|
|
|
if [ -n "$candidate" ]; then
|
|
|
|
first_line=$(( first_line == -1 ? candidate : first_line ))
|
|
|
|
line=$((candidate > kak_cursor_line && (candidate < line || line == -1) ? candidate : line ))
|
|
|
|
fi
|
2015-02-18 14:56:13 +01:00
|
|
|
done
|
|
|
|
line=$((line == -1 ? first_line : line))
|
|
|
|
if [ ${line} -ne -1 ]; then
|
|
|
|
echo "exec ${line} g"
|
|
|
|
else
|
|
|
|
echo 'echo -color Error no next clang diagnostic'
|
|
|
|
fi
|
|
|
|
)
|
|
|
|
} }
|