clang.kak (9003B)
1 hook -once global BufSetOption filetype=(c|cpp) %{ 2 require-module clang 3 } 4 5 provide-module clang %[ 6 7 declare-option -docstring "options to pass to the `clang` shell command" \ 8 str clang_options 9 10 declare-option -docstring "directory from which to invoke clang" \ 11 str clang_directory 12 13 declare-option -hidden completions clang_completions 14 declare-option -hidden line-specs clang_flags 15 declare-option -hidden line-specs clang_errors 16 17 define-command -params ..1 \ 18 -docstring %{ 19 Parse the contents of the current buffer 20 The syntaxic errors detected during parsing are shown when auto-diagnostics are enabled 21 } clang-parse %{ 22 evaluate-commands %sh{ 23 dir=$(mktemp -d "${TMPDIR:-/tmp}"/kak-clang.XXXXXXXX) 24 mkfifo ${dir}/fifo 25 printf %s\\n " 26 evaluate-commands -no-hooks write -sync -method replace ${dir}/buf 27 evaluate-commands -draft %{ 28 edit! -fifo ${dir}/fifo -debug *clang-output* 29 set-option buffer filetype make 30 set-option buffer jump_current_line 0 31 hook -once -always buffer BufCloseFifo .* %{ nop %sh{ rm -r ${dir} } } 32 }" 33 34 # this runs in a detached shell, asynchronously, so that kakoune does 35 # not hang while clang is running. As completions references a cursor 36 # position and a buffer timestamp, only valid completions should be 37 # displayed. 38 (( 39 trap - INT QUIT 40 until [ -f ${dir}/buf ]; do :; done # wait for the buffer to be written 41 42 if [ -n "$kak_opt_clang_directory" ]; then 43 cd "$kak_opt_clang_directory" 44 fi 45 case ${kak_opt_filetype} in 46 (c) ft=c ;; 47 (cpp) ft=c++ ;; 48 (obj-c) ft=objective-c ;; 49 (*) ft=c++ ;; 50 esac 51 52 if [ "$1" = "-complete" ]; then 53 pos=-:${kak_cursor_line}:${kak_cursor_column} 54 header="${kak_cursor_line}.${kak_cursor_column}@${kak_timestamp}" 55 compl=$(clang++ -x ${ft} -fsyntax-only ${kak_opt_clang_options} \ 56 -Xclang -code-completion-brief-comments -Xclang -code-completion-at=${pos} - < ${dir}/buf 2> ${dir}/stderr | 57 awk -F ': ' ' 58 /^COMPLETION:/ && $2 !~ /[(,](Hidden|Inaccessible)[),]/ { 59 candidate=$3 60 gsub(/[[<{]#[^#]*#[]>}]/, "", candidate) 61 gsub(/~/, "~~", candidate) 62 gsub(/\|/, "\\|", candidate) 63 64 gsub(/[[{<]#|#[]}>]/, " ", $3) 65 gsub(/:: /, "::", $3) 66 gsub(/ ,/, ",", $3) 67 gsub(/^ +| +$/, "", $3) 68 docstring=$4 ? $3 "\n" $4 : $3 69 70 gsub(/~|!/, "&&", docstring) 71 gsub(/\|/, "\\|", docstring) 72 if (candidate in candidates) 73 candidates[candidate]=candidates[candidate] "\n" docstring 74 else 75 candidates[candidate]=docstring 76 } 77 END { 78 for (candidate in candidates) { 79 menu=candidate 80 gsub(/(^|[^[:alnum:]_])(operator|new|delete)($|[^{}_[:alnum:]]+)/, "{keyword}&{}", menu) 81 gsub(/(^|[[:space:]])(int|size_t|bool|char|unsigned|signed|long)($|[[:space:]])/, "{type}&{}", menu) 82 gsub(/[^{}_[:alnum:]]+/, "{operator}&{}", menu) 83 printf "%%~%s|info -style menu %!%s!|%s~ ", candidate, candidates[candidate], menu 84 } 85 }') 86 printf %s\\n "evaluate-commands -client ${kak_client} echo 'clang completion done' 87 set-option 'buffer=${kak_buffile}' clang_completions ${header} ${compl}" | kak -p ${kak_session} 88 else 89 clang++ -x ${ft} -fsyntax-only ${kak_opt_clang_options} - < ${dir}/buf 2> ${dir}/stderr 90 printf %s\\n "evaluate-commands -client ${kak_client} echo 'clang parsing done'" | kak -p ${kak_session} 91 fi 92 93 flags=$(cat ${dir}/stderr | sed -Ene " 94 /^<stdin>:[0-9]+:([0-9]+:)? (fatal )?error/ { s/^<stdin>:([0-9]+):.*/'\1|{red}█'/; p } 95 /^<stdin>:[0-9]+:([0-9]+:)? warning/ { s/^<stdin>:([0-9]+):.*/'\1|{yellow}█'/; p } 96 " | paste -s -d ' ' -) 97 98 errors=$(cat ${dir}/stderr | sed -Ene " 99 /^<stdin>:[0-9]+:([0-9]+:)? ((fatal )?error|warning)/ { 100 s/'/''/g; s/^<stdin>:([0-9]+):([0-9]+:)? (.*)/'\1|\3'/; p 101 }" | sort -n | paste -s -d ' ' -) 102 103 sed -e "s|<stdin>|${kak_bufname}|g" < ${dir}/stderr > ${dir}/fifo 104 105 printf %s\\n "set-option 'buffer=${kak_buffile}' clang_flags ${kak_timestamp} ${flags} 106 set-option 'buffer=${kak_buffile}' clang_errors ${kak_timestamp} ${errors}" | kak -p ${kak_session} 107 ) & ) > /dev/null 2>&1 < /dev/null 108 } 109 } 110 111 define-command clang-complete -docstring "Complete the current selection" %{ clang-parse -complete } 112 113 define-command -hidden clang-show-completion-info %[ try %[ 114 evaluate-commands -draft %[ 115 execute-keys ,{( <a-k> ^\( <ret> b <a-k> \A\w+\z <ret> 116 evaluate-commands %sh[ 117 desc=$(printf %s\\n "${kak_opt_clang_completions}" | sed -e "{ s/\([^\\]\):/\1\n/g }" | sed -ne "/^${kak_selection}|/ { s/^[^|]\+|//; s/|.*$//; s/\\\:/:/g; p }") 118 if [ -n "$desc" ]; then 119 printf %s\\n "evaluate-commands -client $kak_client %{info -anchor ${kak_cursor_line}.${kak_cursor_column} -style above %{${desc}}}" 120 fi 121 ] ] 122 ] ] 123 124 define-command clang-enable-autocomplete -docstring "Enable automatic clang completion" %{ 125 set-option window completers "option=clang_completions" %opt{completers} 126 hook window -group clang-autocomplete InsertIdle .* %{ 127 try %{ 128 execute-keys -draft <a-h><a-k>(\.|->|::).\z<ret> 129 echo 'completing...' 130 clang-complete 131 } 132 clang-show-completion-info 133 } 134 alias window complete clang-complete 135 } 136 137 define-command clang-disable-autocomplete -docstring "Disable automatic clang completion" %{ 138 evaluate-commands %sh{ printf "set-option window completers %s\n" $(printf %s "${kak_opt_completers}" | sed -e "s/'option=clang_completions'//g") } 139 remove-hooks window clang-autocomplete 140 unalias window complete clang-complete 141 } 142 143 define-command -hidden clang-show-error-info %{ 144 update-option buffer clang_errors # Ensure we are up to date with buffer changes 145 evaluate-commands %sh{ 146 eval "set -- ${kak_quoted_opt_clang_errors}" 147 shift # skip timestamp 148 desc=$(for error in "$@"; do 149 if [ "${error%%|*}" = "$kak_cursor_line" ]; then 150 printf '%s\n' "${error##*|}" 151 fi 152 done) 153 if [ -n "$desc" ]; then 154 desc=$(printf %s "${desc}" | sed "s/'/''/g") 155 printf "info -anchor %d.%d '%s'\n" "${kak_cursor_line}" "${kak_cursor_column}" "${desc}" 156 fi 157 } } 158 159 define-command clang-enable-diagnostics -docstring %{ 160 Activate automatic error reporting and diagnostics 161 Information about the analysis will be shown after the buffer has been parsed with the clang-parse function 162 } %{ 163 add-highlighter window/clang_flags flag-lines default clang_flags 164 hook window -group clang-diagnostics NormalIdle .* %{ clang-show-error-info } 165 hook window -group clang-diagnostics WinSetOption ^clang_errors=.* %{ info; clang-show-error-info } 166 } 167 168 define-command clang-disable-diagnostics -docstring "Disable automatic error reporting and diagnostics" %{ 169 remove-highlighter window/clang_flags 170 remove-hooks window clang-diagnostics 171 } 172 173 define-command clang-diagnostics-next -docstring "Jump to the next line that contains an error" %{ 174 update-option buffer clang_errors # Ensure we are up to date with buffer changes 175 evaluate-commands %sh{ 176 eval "set -- ${kak_quoted_opt_clang_errors}" 177 shift # skip timestamp 178 unset line 179 unset first_line 180 for error in "$@"; do 181 candidate=${error%%|*} 182 first_line=${first_line-$candidate} 183 if [ "$candidate" -gt $kak_cursor_line ]; then 184 line=$candidate 185 break 186 fi 187 done 188 line=${line-$first_line} 189 if [ -n "$line" ]; then 190 printf %s\\n "execute-keys ${line} g" 191 else 192 echo "fail no next clang diagnostic" 193 fi 194 } } 195 196 ]