kakoune/rc/extra/clang.kak

179 lines
8.1 KiB
Plaintext
Raw Normal View History

decl -docstring "options to pass to the `clang` shell command" \
str clang_options
decl -hidden str clang_tmp_dir
decl -hidden completions clang_completions
decl -hidden line-specs clang_flags
decl -hidden line-specs clang_errors
def -params ..1 \
-docstring %{Parse the contents of the current buffer
The syntaxic errors detected during parsing are shown when auto-diagnostics are enabled} \
clang-parse %{
%sh{
dir=$(mktemp -d "${TMPDIR:-/tmp}"/kak-clang.XXXXXXXX)
mkfifo ${dir}/fifo
printf %s\\n "set buffer clang_tmp_dir ${dir}"
printf %s\\n "eval -no-hooks write ${dir}/buf"
}
# end the previous %sh{} so that its output gets interpreted by kakoune
# before launching the following as a background task.
%sh{
dir=${kak_opt_clang_tmp_dir}
printf %s\\n "eval -draft %{
edit! -fifo ${dir}/fifo -debug *clang-output*
set buffer filetype make
set buffer make_current_error_line 0
hook -group fifo buffer BufCloseFifo .* %{
nop %sh{ rm -r ${dir} }
2017-01-04 01:07:45 +01:00
remove-hooks buffer fifo
}
}"
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
# displayed.
(
case ${kak_opt_filetype} in
2015-09-22 00:37:49 +02:00
c) ft=c ;;
cpp) ft=c++ ;;
obj-c) ft=objective-c ;;
*) ft=c++ ;;
esac
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 ': ' '
2015-02-03 01:42:40 +01:00
/^COMPLETION:/ && ! /\(Hidden\)/ {
id=$2
gsub(/ +$/, "", id)
gsub(/:/, "\\:", id)
gsub(/\|/, "\\|", id)
2015-02-03 01:42:40 +01:00
gsub(/[[{<]#|#[}>]/, "", $3)
gsub(/#]/, " ", $3)
gsub(/:: /, "::", $3)
gsub(/ +$/, "", $3)
desc=$4 ? $3 "\\n" $4 : $3
2015-02-03 01:42:40 +01:00
gsub(/:/, "\\:", desc)
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 {
for (id in docstrings) {
menu=id
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)
print id "|" docstrings[id] "|" menu
}
}' | paste -s -d ':' - | sed -e "s/\\\\n/\\n/g; s/'/\\\\'/g")
printf %s\\n "eval -client ${kak_client} echo 'clang completion done'
set 'buffer=${kak_buffile}' clang_completions '${header}:${compl}'" | kak -p ${kak_session}
else
2015-02-03 01:42:40 +01:00
clang++ -x ${ft} -fsyntax-only ${kak_opt_clang_options} - < ${dir}/buf 2> ${dir}/stderr
printf %s\\n "eval -client ${kak_client} echo 'clang parsing done'" | kak -p ${kak_session}
fi
2014-10-31 21:53:36 +01:00
flags=$(cat ${dir}/stderr | sed -rne "
/^<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 }
" | paste -s -d ':' -)
2014-11-19 14:53:31 +01:00
errors=$(cat ${dir}/stderr | sed -rne "
/^<stdin>:[0-9]+:([0-9]+:)? ((fatal )?error|warning)/ {
s/^<stdin>:([0-9]+):([0-9]+:)? (.*)/\1|\3/
s/'/\\\\'/g; s/:/\\\\:/g; p
}" | sort -n | paste -s -d ':' -)
2014-11-19 14:53:31 +01:00
sed -e "s|<stdin>|${kak_bufname}|g" < ${dir}/stderr > ${dir}/fifo
2014-11-19 14:53:31 +01:00
printf %s\\n "set 'buffer=${kak_buffile}' clang_flags %{${kak_timestamp}:${flags}}
set 'buffer=${kak_buffile}' clang_errors '${kak_timestamp}:${errors}'" | kak -p ${kak_session}
) > /dev/null 2>&1 < /dev/null &
}
}
def clang-complete -docstring "Complete the current selection" %{ clang-parse -complete }
def -hidden clang-show-completion-info %[ try %[
eval -draft %[
2016-05-12 00:57:21 +02:00
exec <space>{( <a-k> ^\( <ret> b <a-k> \`\w+\' <ret>
%sh[
desc=$(printf %s\\n "${kak_opt_clang_completions}" | sed -e "{ s/\([^\\]\):/\1\n/g }" | sed -ne "/^${kak_selection}|/ { s/^[^|]\+|//; s/|.*$//; s/\\\:/:/g; p }")
if [ -n "$desc" ]; then
printf %s\\n "eval -client $kak_client %{info -anchor ${kak_cursor_line}.${kak_cursor_column} -placement above %{${desc}}}"
fi
] ]
] ]
def clang-enable-autocomplete -docstring "Enable automatic clang completion" %{
set window completers "option=clang_completions:%opt{completers}"
hook window -group clang-autocomplete InsertIdle .* %{
try %{
exec -draft <a-h><a-k>(\.|->|::).\'<ret>
echo 'completing...'
clang-complete
}
clang-show-completion-info
}
alias window complete clang-complete
}
2013-04-11 14:30:02 +02:00
def clang-disable-autocomplete -docstring "Disable automatic clang completion" %{
set window completers %sh{ printf %s\\n "'${kak_opt_completers}'" | sed -e 's/option=clang_completions://g' }
2017-01-04 01:07:45 +01:00
remove-hooks window clang-autocomplete
unalias window complete clang-complete
}
2014-11-19 14:53:31 +01:00
def -hidden clang-show-error-info %{
update-option buffer clang_errors # Ensure we are up to date with buffer changes
%sh{
desc=$(printf %s\\n "${kak_opt_clang_errors}" |
sed -e "s/\([^\\]\):/\1\n/g" |
sed -ne "/^${kak_cursor_line}|.*/ { s/^[[:digit:]]\+|//g; s/'/\\\\'/g; s/\\\\:/:/g; p }")
if [ -n "$desc" ]; then
printf %s\\n "info -anchor ${kak_cursor_line}.${kak_cursor_column} '${desc}'"
fi
} }
2014-11-19 14:53:31 +01:00
def clang-enable-diagnostics -docstring %{Activate automatic error reporting and diagnostics
Information about the analysis are showned after the buffer has been parsed with the clang-parse function} \
%{
2017-01-04 01:07:45 +01:00
add-highlighter flag_lines default clang_flags
2014-11-19 14:53:31 +01:00
hook window -group clang-diagnostics NormalIdle .* %{ clang-show-error-info }
2016-05-12 00:57:21 +02:00
hook window -group clang-diagnostics WinSetOption ^clang_errors=.* %{ info; clang-show-error-info }
2014-11-19 14:53:31 +01:00
}
def clang-disable-diagnostics -docstring "Disable automatic error reporting and diagnostics" %{
2017-01-04 01:07:45 +01:00
remove-highlighter hlflags_clang_flags
remove-hooks window clang-diagnostics
2014-11-19 14:53:31 +01:00
}
def clang-diagnostics-next -docstring "Jump to the next line that contains an error" %{
update-option buffer clang_errors # Ensure we are up to date with buffer changes
%sh{
printf "%s\n" "${kak_opt_clang_errors}" | sed -e 's/\([^\\]\):/\1\n/g' | tail -n +2 | (
while IFS='|' read candidate rest; do
first_line=${first_line-$candidate}
2017-10-08 05:20:07 +02:00
if [ "$candidate" -gt $kak_cursor_line ]; then
line=$candidate
break
fi
done
line=${line-$first_line}
if [ -n "$line" ]; then
printf %s\\n "exec ${line} g"
else
echo "echo -markup '{Error}no next clang diagnostic'"
fi
)
} }