lint.kak (15491B)
1 # require-module jump 2 3 declare-option \ 4 -docstring %{ 5 The shell command used by lint-buffer and lint-selections. 6 7 See `:doc lint` for details. 8 } \ 9 str lintcmd 10 11 declare-option -hidden line-specs lint_flags 12 declare-option -hidden line-specs lint_messages 13 declare-option -hidden int lint_error_count 14 declare-option -hidden int lint_warning_count 15 16 define-command -hidden -params 1 lint-open-output-buffer %{ 17 evaluate-commands -try-client %opt{toolsclient} %{ 18 edit! -fifo "%arg{1}/fifo" -debug *lint-output* 19 set-option buffer filetype make 20 set-option buffer jump_current_line 0 21 } 22 } 23 24 define-command \ 25 -hidden \ 26 -params 1 \ 27 -docstring %{ 28 lint-cleaned-selections <linter>: Check each selection with <linter>. 29 30 Assumes selections all have anchor before cursor, and that 31 %val{selections} and %val{selections_desc} are in the same order. 32 } \ 33 lint-cleaned-selections \ 34 %{ 35 # Create a temporary directory to keep all our state. 36 evaluate-commands %sh{ 37 # This is going to come in handy later. 38 kakquote() { printf "%s" "$*" | sed "s/'/''/g; 1s/^/'/; \$s/\$/'/"; } 39 40 # Before we clobber our arguments, 41 # let's record the lintcmd we were given. 42 lintcmd="$1" 43 44 # Some linters care about the name or extension 45 # of the file being linted, so we'll store the text we want to lint 46 # in a file with the same name as the original buffer. 47 filename="${kak_buffile##*/}" 48 49 # A directory to keep all our temporary data. 50 dir=$(mktemp -d "${TMPDIR:-/tmp}"/kak-lint.XXXXXXXX) 51 52 # Write all the selection descriptions to files. 53 eval set -- "$kak_selections_desc" 54 i=0 55 for desc; do 56 mkdir -p "$dir"/sel-"$i" 57 printf "%s" "$desc" > "$dir"/sel-$i/desc 58 i=$(( i + 1 )) 59 done 60 61 # Write all the selection contents to files. 62 eval set -- "$kak_quoted_selections" 63 i=0 64 for text; do 65 # The selection text needs to be stored in a subdirectory, 66 # so we can be sure the filename won't clash with one of ours. 67 mkdir -p "$dir"/sel-"$i"/text/ 68 printf "%s" "$text" > "$dir"/sel-$i/text/"$filename" 69 i=$(( i + 1 )) 70 done 71 72 # We do redirection trickiness to record stderr from 73 # this background task and route it back to Kakoune, 74 # but shellcheck isn't a fan. 75 # shellcheck disable=SC2094 76 ({ # do the parsing in the background and when ready send to the session 77 trap - INT QUIT 78 79 for selpath in "$dir"/sel-*; do 80 # Read in the line and column offset of this selection. 81 IFS=".," read -r start_line start_byte _ < "$selpath"/desc 82 83 # Run the linter, and record the exit-code. 84 eval "$lintcmd '$selpath/text/$filename'" | 85 sort -t: -k2,2 -n | 86 awk \ 87 -v line_offset=$(( start_line - 1 )) \ 88 -v first_line_byte_offset=$(( start_byte - 1 )) \ 89 ' 90 BEGIN { OFS=":"; FS=":" } 91 92 /:[1-9][0-9]*:[1-9][0-9]*:/ { 93 $1 = ENVIRON["kak_bufname"] 94 if ( $2 == 1 ) { 95 $3 += first_line_byte_offset 96 } 97 $2 += line_offset 98 print $0 99 } 100 ' >>"$dir"/result 101 done 102 103 # Load all the linter messages into Kakoune options. 104 # Inside this block, shellcheck warns us that the shell doesn't 105 # need backslash-continuation chars in a single-quoted string, 106 # but awk still needs them. 107 # shellcheck disable=SC1004 108 awk -v file="$kak_buffile" -v stamp="$kak_timestamp" -v client="$kak_client" ' 109 function kakquote(text) { 110 # \x27 is apostrophe, escaped for shell-quoting reasons. 111 gsub(/\x27/, "\x27\x27", text) 112 return "\x27" text "\x27" 113 } 114 115 BEGIN { 116 OFS=":" 117 FS=":" 118 error_count = 0 119 warning_count = 0 120 } 121 122 /:[1-9][0-9]*:[1-9][0-9]*:/ { 123 # Remember that an error or a warning occurs on this line.. 124 if ($4 ~ /[Ee]rror/) { 125 # We definitely have an error on this line. 126 flags_by_line[$2] = "{Error}x" 127 error_count++ 128 } else if (flags_by_line[$2] ~ /Error/) { 129 # We have a warning on this line, 130 # but we already have an error, so do nothing. 131 warning_count++ 132 } else { 133 # We have a warning on this line, 134 # and no previous error. 135 flags_by_line[$2] = "{Information}!" 136 warning_count++ 137 } 138 139 # The message starts with the severity indicator. 140 msg = substr($4, 2) 141 142 # fix case where $5 is not the last field 143 # because of extra colons in the message 144 for (i=5; i<=NF; i++) msg = msg ":" $i 145 146 # Mention the column where this problem occurs, 147 # so that information is not lost. 148 msg = msg "(col " $3 ")" 149 150 # Messages will be stored in a line-specs option, 151 # and each record in the option uses "|" 152 # as a field delimiter, so we need to escape them. 153 gsub(/\|/, "\\|", msg) 154 155 if ($2 in messages_by_line) { 156 # We already have a message on this line, 157 # so append our new message. 158 messages_by_line[$2] = messages_by_line[$2] "\n" msg 159 } else { 160 # A brand-new message on this line. 161 messages_by_line[$2] = msg 162 } 163 } 164 165 END { 166 printf("set-option %s lint_flags %s", kakquote("buffer=" file), stamp); 167 for (line in flags_by_line) { 168 flag = flags_by_line[line] 169 printf(" %s", kakquote(line "|" flag)); 170 } 171 printf("\n"); 172 173 printf("set-option %s lint_messages %s", kakquote("buffer=" file), stamp); 174 for (line in messages_by_line) { 175 msg = messages_by_line[line] 176 printf(" %s", kakquote(line "|" msg)); 177 } 178 printf("\n"); 179 180 print "set-option " \ 181 kakquote("buffer=" file) " " \ 182 "lint_error_count " \ 183 error_count 184 print "set-option " \ 185 kakquote("buffer=" file) " " \ 186 "lint_warning_count " \ 187 warning_count 188 } 189 ' "$dir"/result | kak -p "$kak_session" 190 191 # Send any linting errors to the debug buffer, 192 # for visibility. 193 if [ -s "$dir"/stderr ]; then 194 # Errors were detected!" 195 printf "echo -debug Linter errors: <<<\n" 196 while read -r LINE; do 197 printf "echo -debug %s\n" "$(kakquote " $LINE")" 198 done < "$dir"/stderr 199 printf "echo -debug >>>\n" 200 # FIXME: When #3254 is fixed, this can become a "fail" 201 printf "eval -client %s echo -markup {Error}%s\n" \ 202 "$kak_client" \ 203 "lint failed, see *debug* for details" 204 else 205 # No errors detected, show the results. 206 printf "eval -client %s 'lint-show-diagnostics; lint-show-counters'" \ 207 "$kak_client" 208 fi | kak -p "$kak_session" 209 210 # A fifo to send the results back to a Kakoune buffer. 211 mkfifo "$dir"/fifo 212 # Send the results to kakoune if the session is still valid. 213 if printf 'lint-open-output-buffer %s' "$(kakquote "$dir")" | kak -p "$kak_session"; then 214 cat "$dir"/result > "$dir"/fifo 215 fi 216 # Clean up. 217 rm -rf "$dir" 218 219 } & ) >"$dir"/stderr 2>&1 </dev/null 220 } 221 } 222 223 define-command \ 224 -params 0..2 \ 225 -docstring %{ 226 lint-selections [<switches>]: Check each selection with a linter. 227 228 Switches: 229 -command <cmd> Use the given linter. 230 If not given, the lintcmd option is used. 231 232 See `:doc lint` for details. 233 } \ 234 lint-selections \ 235 %{ 236 evaluate-commands -draft %{ 237 # Make sure all the selections are "forward" (anchor before cursor) 238 execute-keys <a-:> 239 240 # Make sure the selections are in document order. 241 evaluate-commands %sh{ 242 printf "select " 243 printf "%s\n" "$kak_selections_desc" | 244 tr ' ' '\n' | 245 sort -n -t. | 246 tr '\n' ' ' 247 } 248 249 evaluate-commands %sh{ 250 # This is going to come in handy later. 251 kakquote() { printf "%s" "$*" | sed "s/'/''/g; 1s/^/'/; \$s/\$/'/"; } 252 253 if [ "$1" = "-command" ]; then 254 if [ -z "$2" ]; then 255 echo 'fail -- -command option requires a value' 256 exit 1 257 fi 258 lintcmd="$2" 259 elif [ -n "$1" ]; then 260 echo "fail -- Unrecognised parameter $(kakquote "$1")" 261 exit 1 262 elif [ -z "${kak_opt_lintcmd}" ]; then 263 echo 'fail The lintcmd option is not set' 264 exit 1 265 else 266 lintcmd="$kak_opt_lintcmd" 267 fi 268 269 printf 'lint-cleaned-selections %s\n' "$(kakquote "$lintcmd")" 270 } 271 } 272 } 273 274 define-command \ 275 -docstring %{ 276 lint-buffer: Check the current buffer with a linter. 277 278 See `:doc lint` for details. 279 } \ 280 lint-buffer \ 281 %{ 282 evaluate-commands %sh{ 283 if [ -z "${kak_opt_lintcmd}" ]; then 284 echo 'fail The lintcmd option is not set' 285 exit 1 286 fi 287 } 288 evaluate-commands -draft %{ 289 execute-keys '%' 290 lint-cleaned-selections %opt{lintcmd} 291 } 292 } 293 294 alias global lint lint-buffer 295 296 define-command -hidden lint-show-current-line %{ 297 update-option buffer lint_messages 298 evaluate-commands %sh{ 299 # This is going to come in handy later. 300 kakquote() { printf "%s" "$*" | sed "s/'/''/g; 1s/^/'/; \$s/\$/'/"; } 301 302 eval set -- "${kak_quoted_opt_lint_messages}" 303 shift # skip the timestamp 304 305 while [ $# -gt 0 ]; do 306 lineno=${1%%|*} 307 msg=${1#*|} 308 309 if [ "$lineno" -eq "$kak_cursor_line" ]; then 310 printf "info -anchor %d.%d %s\n" \ 311 "$kak_cursor_line" \ 312 "$kak_cursor_column" \ 313 "$(kakquote "$msg")" 314 break 315 fi 316 shift 317 done 318 } 319 } 320 321 define-command -hidden lint-show-counters %{ 322 echo -markup "linting results: {Error} %opt{lint_error_count} error(s) {Information} %opt{lint_warning_count} warning(s) " 323 } 324 325 define-command -hidden lint-show-diagnostics %{ 326 try %{ 327 # Assume that if the highlighter is set, then hooks also are 328 add-highlighter window/lint flag-lines default lint_flags 329 hook window -group lint-diagnostics NormalIdle .* %{ lint-show-current-line } 330 hook window -group lint-diagnostics WinSetOption lint_flags=.* %{ info; lint-show-current-line } 331 } 332 } 333 334 define-command lint-hide-diagnostics -docstring "Hide line markers and disable automatic diagnostic displaying" %{ 335 remove-highlighter window/lint 336 remove-hooks window lint-diagnostics 337 } 338 339 # FIXME: Is there some way we can re-use make-next-error 340 # instead of re-implementing it? 341 define-command \ 342 -docstring "Jump to the next line that contains a lint message" \ 343 lint-next-message \ 344 %{ 345 update-option buffer lint_messages 346 347 evaluate-commands %sh{ 348 # This is going to come in handy later. 349 kakquote() { printf "%s" "$*" | sed "s/'/''/g; 1s/^/'/; \$s/\$/'/"; } 350 351 eval "set -- ${kak_quoted_opt_lint_messages}" 352 shift 353 354 if [ "$#" -eq 0 ]; then 355 printf 'fail no lint messages' 356 exit 357 fi 358 359 first_lineno="" 360 first_msg="" 361 362 for lint_message; do 363 lineno="${lint_message%%|*}" 364 msg="${lint_message#*|}" 365 366 if [ -z "$first_lineno" ]; then 367 first_lineno=$lineno 368 first_msg=$msg 369 fi 370 371 if [ "$lineno" -gt "$kak_cursor_line" ]; then 372 printf "execute-keys %dg\n" "$lineno" 373 printf "info -anchor %d.%d %s\n" \ 374 "$lineno" "1" "$(kakquote "$msg")" 375 exit 376 fi 377 done 378 379 # We didn't find any messages after the current line, 380 # let's wrap around to the beginning. 381 printf "execute-keys %dg\n" "$first_lineno" 382 printf "info -anchor %d.%d %s\n" \ 383 "$first_lineno" "1" "$(kakquote "$first_msg")" 384 printf "echo -markup \ 385 {Information}lint message search wrapped around buffer\n" 386 387 } 388 } 389 390 # FIXME: Is there some way we can re-use make-previous-error 391 # instead of re-implementing it? 392 define-command \ 393 -docstring "Jump to the previous line that contains a lint message" \ 394 lint-previous-message \ 395 %{ 396 update-option buffer lint_messages 397 398 evaluate-commands %sh{ 399 # This is going to come in handy later. 400 kakquote() { printf "%s" "$*" | sed "s/'/''/g; 1s/^/'/; \$s/\$/'/"; } 401 402 eval "set -- ${kak_quoted_opt_lint_messages}" 403 shift 404 405 if [ "$#" -eq 0 ]; then 406 printf 'fail no lint messages' 407 exit 408 fi 409 410 prev_lineno="" 411 prev_msg="" 412 413 for lint_message; do 414 lineno="${lint_message%%|*}" 415 msg="${lint_message#*|}" 416 417 # If this message comes on or after the cursor position... 418 if [ "$lineno" -ge "${kak_cursor_line}" ]; then 419 # ...and we had a previous message... 420 if [ -n "$prev_lineno" ]; then 421 # ...then go to the previous message and display it. 422 printf "execute-keys %dg\n" "$prev_lineno" 423 printf "info -anchor %d.%d %s\n" \ 424 "$lineno" "1" "$(kakquote "$prev_msg")" 425 exit 426 427 # We are after the cursor position, but there has been 428 # no previous message; we'll need to do something else. 429 else 430 break 431 fi 432 fi 433 434 # We have not yet reached the cursor position, stash this message 435 # and try the next. 436 prev_lineno="$lineno" 437 prev_msg="$msg" 438 done 439 440 # There is no message before the cursor position, 441 # let's wrap around to the end. 442 shift $(( $# - 1 )) 443 last_lineno="${1%%|*}" 444 last_msg="${1#*|}" 445 446 printf "execute-keys %dg\n" "$last_lineno" 447 printf "info -anchor %d.%d %s\n" \ 448 "$last_lineno" "1" "$(kakquote "$last_msg")" 449 printf "echo -markup \ 450 {Information}lint message search wrapped around buffer\n" 451 } 452 }