2017-05-16 14:35:43 +03:00
|
|
|
decl -docstring "options to pass to the `clang` shell command" \
|
|
|
|
str clang_options
|
2014-04-03 22:00:42 +04:00
|
|
|
|
2014-10-24 21:46:42 +04:00
|
|
|
decl -hidden str clang_tmp_dir
|
2016-04-01 03:27:23 +03:00
|
|
|
decl -hidden completions clang_completions
|
2017-05-24 17:41:43 +03:00
|
|
|
decl -hidden line-specs clang_flags
|
2017-05-25 14:17:22 +03:00
|
|
|
decl -hidden line-specs clang_errors
|
2013-03-14 17:12:14 +04:00
|
|
|
|
2016-10-11 10:03:41 +03:00
|
|
|
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 %{
|
2013-03-14 17:12:14 +04:00
|
|
|
%sh{
|
2017-06-09 14:05:31 +03:00
|
|
|
dir=$(mktemp -d "${TMPDIR:-/tmp}"/kak-clang.XXXXXXXX)
|
2014-10-24 21:46:42 +04:00
|
|
|
mkfifo ${dir}/fifo
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "set buffer clang_tmp_dir ${dir}"
|
2016-10-03 22:29:54 +03:00
|
|
|
printf %s\\n "eval -no-hooks write ${dir}/buf"
|
2013-03-14 17:12:14 +04: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 21:46:42 +04:00
|
|
|
dir=${kak_opt_clang_tmp_dir}
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "eval -draft %{
|
2017-03-08 22:33:25 +03:00
|
|
|
edit! -fifo ${dir}/fifo -debug *clang-output*
|
2014-10-24 21:46:42 +04:00
|
|
|
set buffer filetype make
|
2016-12-10 16:36:51 +03:00
|
|
|
set buffer make_current_error_line 0
|
2015-03-04 02:24:12 +03:00
|
|
|
hook -group fifo buffer BufCloseFifo .* %{
|
|
|
|
nop %sh{ rm -r ${dir} }
|
2017-01-04 03:07:45 +03:00
|
|
|
remove-hooks buffer fifo
|
2015-03-04 02:24:12 +03:00
|
|
|
}
|
2014-10-24 21:46:42 +04:00
|
|
|
}"
|
2014-11-06 22:17:13 +03: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 17:12:14 +04:00
|
|
|
# displayed.
|
|
|
|
(
|
2014-11-21 16:27:43 +03:00
|
|
|
case ${kak_opt_filetype} in
|
2015-09-22 01:37:49 +03:00
|
|
|
c) ft=c ;;
|
2014-11-21 16:27:43 +03:00
|
|
|
cpp) ft=c++ ;;
|
|
|
|
obj-c) ft=objective-c ;;
|
|
|
|
*) ft=c++ ;;
|
|
|
|
esac
|
|
|
|
|
2016-03-14 23:59:23 +03:00
|
|
|
if [ "$1" = "-complete" ]; then
|
2015-02-03 03:42:40 +03: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 |
|
2016-06-21 21:00:41 +03:00
|
|
|
awk -F ': ' '
|
2015-02-03 03:42:40 +03:00
|
|
|
/^COMPLETION:/ && ! /\(Hidden\)/ {
|
2016-04-04 15:37:28 +03:00
|
|
|
id=$2
|
|
|
|
gsub(/ +$/, "", id)
|
|
|
|
gsub(/:/, "\\:", id)
|
|
|
|
gsub(/\|/, "\\|", id)
|
|
|
|
|
2015-02-03 03:42:40 +03:00
|
|
|
gsub(/[[{<]#|#[}>]/, "", $3)
|
|
|
|
gsub(/#]/, " ", $3)
|
|
|
|
gsub(/:: /, "::", $3)
|
|
|
|
gsub(/ +$/, "", $3)
|
|
|
|
desc=$4 ? $3 "\\n" $4 : $3
|
2016-04-04 15:37:28 +03:00
|
|
|
|
2015-02-03 03:42:40 +03:00
|
|
|
gsub(/:/, "\\:", desc)
|
2016-04-04 15:37:28 +03:00
|
|
|
gsub(/\|/, "\\|", desc)
|
2015-10-07 00:50:51 +03:00
|
|
|
if (id in docstrings)
|
|
|
|
docstrings[id]=docstrings[id] "\\n" desc
|
2015-02-03 03:42:40 +03:00
|
|
|
else
|
2015-10-07 00:50:51 +03:00
|
|
|
docstrings[id]=desc
|
2015-02-03 03:42:40 +03:00
|
|
|
}
|
|
|
|
END {
|
2016-08-31 01:23:33 +03:00
|
|
|
for (id in docstrings) {
|
2015-10-06 15:39:09 +03:00
|
|
|
menu=id
|
2017-01-04 15:23:25 +03:00
|
|
|
gsub(/(^|[^[:alnum:]_])(operator|new|delete)($|[^[:alnum:]{}_]+)/, "{keyword}&{}", menu)
|
2015-10-07 01:22:36 +03:00
|
|
|
gsub(/(^|[[:space:]])(int|size_t|bool|char|unsigned|signed|long)($|[[:space:]])/, "{type}&{}", menu)
|
|
|
|
gsub(/[^[:alnum:]{}_]+/, "{operator}&{}", menu)
|
2016-04-01 03:27:23 +03:00
|
|
|
print id "|" docstrings[id] "|" menu
|
2015-10-06 15:39:09 +03:00
|
|
|
}
|
2016-12-19 10:18:15 +03:00
|
|
|
}' | paste -s -d ':' - | sed -e "s/\\\\n/\\n/g; s/'/\\\\'/g")
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "eval -client ${kak_client} echo 'clang completion done'
|
2015-03-04 02:24:12 +03:00
|
|
|
set 'buffer=${kak_buffile}' clang_completions '${header}:${compl}'" | kak -p ${kak_session}
|
2014-11-27 16:42:36 +03:00
|
|
|
else
|
2015-02-03 03:42:40 +03:00
|
|
|
clang++ -x ${ft} -fsyntax-only ${kak_opt_clang_options} - < ${dir}/buf 2> ${dir}/stderr
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "eval -client ${kak_client} echo 'clang parsing done'" | kak -p ${kak_session}
|
2014-11-27 16:42:36 +03:00
|
|
|
fi
|
2014-10-31 23:53:36 +03:00
|
|
|
|
2014-11-27 16:42:36 +03:00
|
|
|
flags=$(cat ${dir}/stderr | sed -rne "
|
2015-12-13 02:16:07 +03: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 }
|
2016-12-19 10:18:15 +03:00
|
|
|
" | paste -s -d ':' -)
|
2014-11-19 16:53:31 +03:00
|
|
|
|
2014-11-27 16:42:36 +03:00
|
|
|
errors=$(cat ${dir}/stderr | sed -rne "
|
2017-05-25 14:17:22 +03:00
|
|
|
/^<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 16:53:31 +03:00
|
|
|
|
2014-11-27 16:42:36 +03:00
|
|
|
sed -e "s|<stdin>|${kak_bufname}|g" < ${dir}/stderr > ${dir}/fifo
|
2014-11-19 16:53:31 +03:00
|
|
|
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "set 'buffer=${kak_buffile}' clang_flags %{${kak_timestamp}:${flags}}
|
2017-05-25 14:17:22 +03:00
|
|
|
set 'buffer=${kak_buffile}' clang_errors '${kak_timestamp}:${errors}'" | kak -p ${kak_session}
|
2014-03-06 07:35:38 +04:00
|
|
|
) > /dev/null 2>&1 < /dev/null &
|
2013-03-14 17:12:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 10:03:41 +03:00
|
|
|
def clang-complete -docstring "Complete the current selection" %{ clang-parse -complete }
|
2014-11-27 16:42:36 +03:00
|
|
|
|
2015-11-30 16:56:01 +03:00
|
|
|
def -hidden clang-show-completion-info %[ try %[
|
|
|
|
eval -draft %[
|
2017-09-29 05:39:59 +03:00
|
|
|
exec <space>{( <a-k> ^\( <ret> b <a-k> \A\w+\z <ret>
|
2015-11-30 16:56:01 +03:00
|
|
|
%sh[
|
2016-08-31 01:23:33 +03:00
|
|
|
desc=$(printf %s\\n "${kak_opt_clang_completions}" | sed -e "{ s/\([^\\]\):/\1\n/g }" | sed -ne "/^${kak_selection}|/ { s/^[^|]\+|//; s/|.*$//; s/\\\:/:/g; p }")
|
2015-11-30 16:56:01 +03:00
|
|
|
if [ -n "$desc" ]; then
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "eval -client $kak_client %{info -anchor ${kak_cursor_line}.${kak_cursor_column} -placement above %{${desc}}}"
|
2015-11-30 16:56:01 +03:00
|
|
|
fi
|
|
|
|
] ]
|
|
|
|
] ]
|
|
|
|
|
2016-10-11 10:03:41 +03:00
|
|
|
def clang-enable-autocomplete -docstring "Enable automatic clang completion" %{
|
2014-11-11 16:53:57 +03:00
|
|
|
set window completers "option=clang_completions:%opt{completers}"
|
2015-11-30 16:56:01 +03:00
|
|
|
hook window -group clang-autocomplete InsertIdle .* %{
|
|
|
|
try %{
|
2017-09-29 05:39:59 +03:00
|
|
|
exec -draft <a-h><a-k>(\.|->|::).\z<ret>
|
2015-11-30 16:56:01 +03:00
|
|
|
echo 'completing...'
|
|
|
|
clang-complete
|
|
|
|
}
|
|
|
|
clang-show-completion-info
|
|
|
|
}
|
2014-11-18 16:55:52 +03:00
|
|
|
alias window complete clang-complete
|
2013-03-14 17:12:14 +04:00
|
|
|
}
|
2013-04-11 16:30:02 +04:00
|
|
|
|
2015-08-03 21:44:27 +03:00
|
|
|
def clang-disable-autocomplete -docstring "Disable automatic clang completion" %{
|
2016-04-23 08:47:01 +03:00
|
|
|
set window completers %sh{ printf %s\\n "'${kak_opt_completers}'" | sed -e 's/option=clang_completions://g' }
|
2017-01-04 03:07:45 +03:00
|
|
|
remove-hooks window clang-autocomplete
|
2014-11-18 16:55:52 +03:00
|
|
|
unalias window complete clang-complete
|
2014-03-15 07:14:23 +04:00
|
|
|
}
|
2014-11-19 16:53:31 +03:00
|
|
|
|
2017-05-25 14:17:22 +03: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 16:53:31 +03:00
|
|
|
|
2016-10-11 10:03:41 +03: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 03:07:45 +03:00
|
|
|
add-highlighter flag_lines default clang_flags
|
2014-11-19 16:53:31 +03:00
|
|
|
hook window -group clang-diagnostics NormalIdle .* %{ clang-show-error-info }
|
2016-05-12 01:57:21 +03:00
|
|
|
hook window -group clang-diagnostics WinSetOption ^clang_errors=.* %{ info; clang-show-error-info }
|
2014-11-19 16:53:31 +03:00
|
|
|
}
|
|
|
|
|
2016-10-11 10:03:41 +03:00
|
|
|
def clang-disable-diagnostics -docstring "Disable automatic error reporting and diagnostics" %{
|
2017-01-04 03:07:45 +03:00
|
|
|
remove-highlighter hlflags_clang_flags
|
|
|
|
remove-hooks window clang-diagnostics
|
2014-11-19 16:53:31 +03:00
|
|
|
}
|
2015-02-18 16:56:13 +03:00
|
|
|
|
2017-05-25 14:17:22 +03: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 06:20:07 +03:00
|
|
|
if [ "$candidate" -gt $kak_cursor_line ]; then
|
2017-05-25 14:17:22 +03:00
|
|
|
line=$candidate
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
line=${line-$first_line}
|
|
|
|
if [ -n "$line" ]; then
|
|
|
|
printf %s\\n "exec ${line} g"
|
|
|
|
else
|
2017-07-19 18:18:52 +03:00
|
|
|
echo "echo -markup '{Error}no next clang diagnostic'"
|
2015-08-04 20:41:56 +03:00
|
|
|
fi
|
2017-05-25 14:17:22 +03:00
|
|
|
)
|
|
|
|
} }
|