2019-03-13 06:24:33 +01:00
|
|
|
hook -once global BufSetOption filetype=(c|cpp) %{
|
|
|
|
require-module clang
|
|
|
|
}
|
|
|
|
|
|
|
|
provide-module clang %[
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
declare-option -docstring "options to pass to the `clang` shell command" \
|
2017-05-16 13:35:43 +02:00
|
|
|
str clang_options
|
2014-04-03 20:00:42 +02:00
|
|
|
|
2019-09-17 13:50:21 +02:00
|
|
|
declare-option -docstring "directory from which to invoke clang" \
|
|
|
|
str clang_directory
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
declare-option -hidden completions clang_completions
|
|
|
|
declare-option -hidden line-specs clang_flags
|
|
|
|
declare-option -hidden line-specs clang_errors
|
2013-03-14 14:12:14 +01:00
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command -params ..1 \
|
2020-02-03 20:53:28 +01:00
|
|
|
-docstring %{
|
|
|
|
Parse the contents of the current buffer
|
|
|
|
The syntaxic errors detected during parsing are shown when auto-diagnostics are enabled
|
|
|
|
} clang-parse %{
|
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-clang.XXXXXXXX)
|
2014-10-24 19:46:42 +02:00
|
|
|
mkfifo ${dir}/fifo
|
2019-11-21 13:36:38 +01:00
|
|
|
printf %s\\n "
|
2020-10-27 10:20:18 +01:00
|
|
|
evaluate-commands -no-hooks write -sync -method replace ${dir}/buf
|
2019-11-21 13:36:38 +01:00
|
|
|
evaluate-commands -draft %{
|
|
|
|
edit! -fifo ${dir}/fifo -debug *clang-output*
|
|
|
|
set-option buffer filetype make
|
2024-02-25 11:12:58 +01:00
|
|
|
set-option buffer jump_current_line 0
|
2019-11-21 13:36:38 +01:00
|
|
|
hook -once -always buffer BufCloseFifo .* %{ nop %sh{ rm -r ${dir} } }
|
|
|
|
}"
|
|
|
|
|
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.
|
2019-06-10 18:25:51 +02:00
|
|
|
((
|
2024-03-29 17:27:27 +01:00
|
|
|
trap - INT QUIT
|
2019-11-21 13:36:38 +01:00
|
|
|
until [ -f ${dir}/buf ]; do :; done # wait for the buffer to be written
|
|
|
|
|
2019-09-17 13:50:21 +02:00
|
|
|
if [ -n "$kak_opt_clang_directory" ]; then
|
|
|
|
cd "$kak_opt_clang_directory"
|
|
|
|
fi
|
2014-11-21 14:27:43 +01:00
|
|
|
case ${kak_opt_filetype} in
|
2024-03-29 17:27:27 +01:00
|
|
|
(c) ft=c ;;
|
|
|
|
(cpp) ft=c++ ;;
|
|
|
|
(obj-c) ft=objective-c ;;
|
|
|
|
(*) ft=c++ ;;
|
2014-11-21 14:27:43 +01:00
|
|
|
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 |
|
2016-06-21 20:00:41 +02:00
|
|
|
awk -F ': ' '
|
2019-09-15 03:48:25 +02:00
|
|
|
/^COMPLETION:/ && $2 !~ /[(,](Hidden|Inaccessible)[),]/ {
|
|
|
|
candidate=$3
|
2019-12-15 00:32:14 +01:00
|
|
|
gsub(/[[<{]#[^#]*#[]>}]/, "", candidate)
|
2019-09-15 03:48:25 +02:00
|
|
|
gsub(/~/, "~~", candidate)
|
|
|
|
gsub(/\|/, "\\|", candidate)
|
2016-04-04 14:37:28 +02:00
|
|
|
|
2019-12-15 00:32:14 +01:00
|
|
|
gsub(/[[{<]#|#[]}>]/, " ", $3)
|
2015-02-03 01:42:40 +01:00
|
|
|
gsub(/:: /, "::", $3)
|
2019-12-15 00:32:14 +01:00
|
|
|
gsub(/ ,/, ",", $3)
|
|
|
|
gsub(/^ +| +$/, "", $3)
|
2019-09-15 03:48:25 +02:00
|
|
|
docstring=$4 ? $3 "\n" $4 : $3
|
2016-04-04 14:37:28 +02:00
|
|
|
|
2019-11-22 09:43:39 +01:00
|
|
|
gsub(/~|!/, "&&", docstring)
|
2019-09-15 03:48:25 +02:00
|
|
|
gsub(/\|/, "\\|", docstring)
|
|
|
|
if (candidate in candidates)
|
|
|
|
candidates[candidate]=candidates[candidate] "\n" docstring
|
2015-02-03 01:42:40 +01:00
|
|
|
else
|
2019-09-15 03:48:25 +02:00
|
|
|
candidates[candidate]=docstring
|
2015-02-03 01:42:40 +01:00
|
|
|
}
|
|
|
|
END {
|
2019-09-15 03:48:25 +02:00
|
|
|
for (candidate in candidates) {
|
|
|
|
menu=candidate
|
2018-02-07 09:40:45 +01:00
|
|
|
gsub(/(^|[^[:alnum:]_])(operator|new|delete)($|[^{}_[:alnum:]]+)/, "{keyword}&{}", menu)
|
2015-10-07 00:22:36 +02:00
|
|
|
gsub(/(^|[[:space:]])(int|size_t|bool|char|unsigned|signed|long)($|[[:space:]])/, "{type}&{}", menu)
|
2018-02-07 09:40:45 +01:00
|
|
|
gsub(/[^{}_[:alnum:]]+/, "{operator}&{}", menu)
|
2019-09-15 03:48:25 +02:00
|
|
|
printf "%%~%s|info -style menu %!%s!|%s~ ", candidate, candidates[candidate], menu
|
2015-10-06 14:39:09 +02:00
|
|
|
}
|
2018-05-30 15:28:50 +02:00
|
|
|
}')
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -client ${kak_client} echo 'clang completion done'
|
2018-05-30 15:28:50 +02:00
|
|
|
set-option '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
|
2017-11-03 09:09:45 +01:00
|
|
|
printf %s\\n "evaluate-commands -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
|
|
|
|
2020-01-21 14:45:33 +01:00
|
|
|
flags=$(cat ${dir}/stderr | sed -Ene "
|
2018-05-30 15:28:50 +02: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 }
|
|
|
|
" | paste -s -d ' ' -)
|
2014-11-19 14:53:31 +01:00
|
|
|
|
2020-01-21 14:45:33 +01:00
|
|
|
errors=$(cat ${dir}/stderr | sed -Ene "
|
2017-05-25 13:17:22 +02:00
|
|
|
/^<stdin>:[0-9]+:([0-9]+:)? ((fatal )?error|warning)/ {
|
2018-05-30 15:28:50 +02:00
|
|
|
s/'/''/g; s/^<stdin>:([0-9]+):([0-9]+:)? (.*)/'\1|\3'/; p
|
|
|
|
}" | sort -n | paste -s -d ' ' -)
|
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
|
|
|
|
2018-05-30 15:28:50 +02:00
|
|
|
printf %s\\n "set-option 'buffer=${kak_buffile}' clang_flags ${kak_timestamp} ${flags}
|
|
|
|
set-option 'buffer=${kak_buffile}' clang_errors ${kak_timestamp} ${errors}" | kak -p ${kak_session}
|
2019-06-10 18:25:51 +02:00
|
|
|
) & ) > /dev/null 2>&1 < /dev/null
|
2013-03-14 14:12:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command clang-complete -docstring "Complete the current selection" %{ clang-parse -complete }
|
2014-11-27 14:42:36 +01:00
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command -hidden clang-show-completion-info %[ try %[
|
2017-11-03 09:09:45 +01:00
|
|
|
evaluate-commands -draft %[
|
2022-04-15 00:14:17 +02:00
|
|
|
execute-keys ,{( <a-k> ^\( <ret> b <a-k> \A\w+\z <ret>
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh[
|
2016-08-31 00:23:33 +02: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 14:56:01 +01:00
|
|
|
if [ -n "$desc" ]; then
|
2019-04-17 08:31:06 +02:00
|
|
|
printf %s\\n "evaluate-commands -client $kak_client %{info -anchor ${kak_cursor_line}.${kak_cursor_column} -style above %{${desc}}}"
|
2015-11-30 14:56:01 +01:00
|
|
|
fi
|
|
|
|
] ]
|
|
|
|
] ]
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command clang-enable-autocomplete -docstring "Enable automatic clang completion" %{
|
2018-05-30 15:28:50 +02:00
|
|
|
set-option window completers "option=clang_completions" %opt{completers}
|
2015-11-30 14:56:01 +01:00
|
|
|
hook window -group clang-autocomplete InsertIdle .* %{
|
|
|
|
try %{
|
2017-11-03 09:09:45 +01:00
|
|
|
execute-keys -draft <a-h><a-k>(\.|->|::).\z<ret>
|
2015-11-30 14:56:01 +01:00
|
|
|
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
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command clang-disable-autocomplete -docstring "Disable automatic clang completion" %{
|
2018-06-30 01:49:57 +02:00
|
|
|
evaluate-commands %sh{ printf "set-option window completers %s\n" $(printf %s "${kak_opt_completers}" | sed -e "s/'option=clang_completions'//g") }
|
2017-01-04 01:07:45 +01:00
|
|
|
remove-hooks 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
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command -hidden clang-show-error-info %{
|
2017-05-25 13:17:22 +02:00
|
|
|
update-option buffer clang_errors # Ensure we are up to date with buffer changes
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2019-06-19 14:59:42 +02:00
|
|
|
eval "set -- ${kak_quoted_opt_clang_errors}"
|
2018-05-30 15:28:50 +02:00
|
|
|
shift # skip timestamp
|
2018-09-26 20:22:30 +02:00
|
|
|
desc=$(for error in "$@"; do
|
2018-06-21 00:46:38 +02:00
|
|
|
if [ "${error%%|*}" = "$kak_cursor_line" ]; then
|
2018-09-26 20:22:30 +02:00
|
|
|
printf '%s\n' "${error##*|}"
|
2018-05-30 15:28:50 +02:00
|
|
|
fi
|
2018-09-26 20:22:30 +02:00
|
|
|
done)
|
2017-05-25 13:17:22 +02:00
|
|
|
if [ -n "$desc" ]; then
|
2018-09-26 20:22:30 +02:00
|
|
|
desc=$(printf %s "${desc}" | sed "s/'/''/g")
|
|
|
|
printf "info -anchor %d.%d '%s'\n" "${kak_cursor_line}" "${kak_cursor_column}" "${desc}"
|
2017-05-25 13:17:22 +02:00
|
|
|
fi
|
|
|
|
} }
|
2014-11-19 14:53:31 +01:00
|
|
|
|
2020-09-02 10:57:34 +02:00
|
|
|
define-command clang-enable-diagnostics -docstring %{
|
|
|
|
Activate automatic error reporting and diagnostics
|
2020-09-06 02:56:37 +02:00
|
|
|
Information about the analysis will be shown after the buffer has been parsed with the clang-parse function
|
2020-09-02 10:57:34 +02:00
|
|
|
} %{
|
2018-07-08 11:11:04 +02:00
|
|
|
add-highlighter window/clang_flags 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
|
|
|
}
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command clang-disable-diagnostics -docstring "Disable automatic error reporting and diagnostics" %{
|
2018-06-30 01:49:57 +02:00
|
|
|
remove-highlighter window/clang_flags
|
2017-01-04 01:07:45 +01:00
|
|
|
remove-hooks window clang-diagnostics
|
2014-11-19 14:53:31 +01:00
|
|
|
}
|
2015-02-18 14:56:13 +01:00
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command clang-diagnostics-next -docstring "Jump to the next line that contains an error" %{
|
2017-05-25 13:17:22 +02:00
|
|
|
update-option buffer clang_errors # Ensure we are up to date with buffer changes
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2019-06-19 14:59:42 +02:00
|
|
|
eval "set -- ${kak_quoted_opt_clang_errors}"
|
2018-05-30 15:28:50 +02:00
|
|
|
shift # skip timestamp
|
2020-08-23 18:55:50 +02:00
|
|
|
unset line
|
|
|
|
unset first_line
|
2018-05-30 15:28:50 +02:00
|
|
|
for error in "$@"; do
|
|
|
|
candidate=${error%%|*}
|
|
|
|
first_line=${first_line-$candidate}
|
|
|
|
if [ "$candidate" -gt $kak_cursor_line ]; then
|
|
|
|
line=$candidate
|
|
|
|
break
|
2015-08-04 19:41:56 +02:00
|
|
|
fi
|
2018-05-30 15:28:50 +02:00
|
|
|
done
|
|
|
|
line=${line-$first_line}
|
|
|
|
if [ -n "$line" ]; then
|
|
|
|
printf %s\\n "execute-keys ${line} g"
|
|
|
|
else
|
2019-11-14 08:32:55 +01:00
|
|
|
echo "fail no next clang diagnostic"
|
2018-05-30 15:28:50 +02:00
|
|
|
fi
|
2017-05-25 13:17:22 +02:00
|
|
|
} }
|
2019-03-13 06:24:33 +01:00
|
|
|
|
|
|
|
]
|