daily pastebin goal
62%
SHARE
TWEET

Untitled

a guest May 16th, 2018 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. declare-option -docstring %{shell command to which the path of a copy of the current buffer will be passed
  2. The output returned by this command is expected to comply with the following format:
  3.  {filename}:{line}:{column}: {kind}: {message}} \
  4.     str lintcmd
  5.  
  6. declare-option -hidden line-specs lint_flags
  7. declare-option -hidden range-specs lint_errors
  8.  
  9. define-command lint -docstring 'Parse the current buffer with a linter' %{
  10.     %sh{
  11.         dir=$(mktemp -d "${TMPDIR:-/tmp}"/kak-lint.XXXXXXXX)
  12.         mkfifo "$dir"/fifo
  13.         # some linters doesn't work well on displaced copies
  14.         # e.g. eslint demands local eslint and checks imports
  15.         #printf '%s\n' "evaluate-commands -no-hooks write $dir/buf"
  16.         printf '%s\n' "evaluate-commands -no-hooks write"
  17.  
  18.         printf '%s\n' "evaluate-commands -draft %{
  19.                   edit! -fifo $dir/fifo -debug *lint-output*
  20.                   set-option buffer filetype make
  21.                   set-option buffer make_current_error_line 0
  22.                   hook -group fifo buffer BufCloseFifo .* %{
  23.                       nop %sh{ rm -r '$dir' }
  24.                       remove-hooks buffer fifo
  25.                   }
  26.               }"
  27.  
  28.         { # do the parsing in the background and when ready send to the session
  29.  
  30.         # some linters doesn't work well on displaced copies
  31.         # e.g. eslint demands local eslint and checks imports
  32.         #eval "$kak_opt_lintcmd '$dir'/buf" | sort -t: -k2,2 -n > "$dir"/stderr
  33.         cd $(dirname $kak_buffile)
  34.         eval "$kak_opt_lintcmd '$kak_buffile'" | sort -t: -k2,2 -n > "$dir"/stderr
  35.         printf '%s\n' "evaluate-commands -client $kak_client echo 'Linted'" | kak -p "$kak_session"
  36.  
  37.         # Flags for the gutter:
  38.         #   line3|{red}:line11|{yellow}
  39.         # Contextual error messages:
  40.         #   l1,c1,err1
  41.         #   ln,cn,err2
  42.         awk -F: -v file="$kak_buffile" -v stamp="$kak_timestamp" '
  43.             /:[0-9]+:[0-9]+: ([Ff]atal )?[Ee]rror/ {
  44.                 flags = flags $2 "|{red}█:"
  45.             }
  46.             /:[0-9]+:[0-9]+:/ {
  47.                 if ($4 !~ /[Ee]rror/) {
  48.                     flags = flags $2 "|{yellow}█:"
  49.                 }
  50.             }
  51.             /:[0-9]+:[0-9]+:/ {
  52.                 errors = errors ":" $2 "." $3 "," $2 "." $3 "|" substr($4,2)
  53.                 # fix case where $5 is not the last field because of extra :s in the message
  54.                 for (i=5; i<=NF; i++) errors = errors "\\:" $i
  55.                 errors = substr(errors, 1, length(errors)-1) " (col " $3 ")"
  56.             }
  57.             END {
  58.                 print "set-option \"buffer=" file "\" lint_flags  %{" stamp ":" substr(flags,  1, length(flags)-1)  "}"
  59.                 errors = substr(errors, 1, length(errors)-1)
  60.                 gsub("~", "\\~", errors)
  61.                 print "set-option \"buffer=" file "\" lint_errors %~" stamp errors "~"
  62.             }
  63.         ' "$dir"/stderr | kak -p "$kak_session"
  64.  
  65.         cut -d: -f2- "$dir"/stderr | sed "s@^@$kak_bufname:@" > "$dir"/fifo
  66.  
  67.         } >/dev/null 2>&1 </dev/null &
  68.     }
  69. }
  70.  
  71. define-command -hidden lint-show %{
  72.     update-option buffer lint_errors
  73.     %sh{
  74.         desc=$(printf '%s\n' "$kak_opt_lint_errors" | sed -e 's/\([^\\]\):/\1\n/g' | tail -n +2 |
  75.                sed -ne "/^$kak_cursor_line\.[^|]\+|.*/ { s/^[^|]\+|//g; s/'/\\\\'/g; s/\\\\:/:/g; p; }")
  76.         if [ -n "$desc" ]; then
  77.             printf '%s\n' "info -anchor $kak_cursor_line.$kak_cursor_column '$desc'"
  78.         fi
  79.     } }
  80.  
  81. define-command lint-enable -docstring "Activate automatic diagnostics of the code" %{
  82.     add-highlighter window flag_lines default lint_flags
  83.     hook window -group lint-diagnostics NormalIdle .* %{ lint-show }
  84.     hook window -group lint-diagnostics WinSetOption lint_flags=.* %{ info; lint-show }
  85. }
  86.  
  87. define-command lint-disable -docstring "Disable automatic diagnostics of the code" %{
  88.     remove-highlighter window/hlflags_lint_flags
  89.     remove-hooks window lint-diagnostics
  90. }
  91.  
  92. define-command lint-next-error -docstring "Jump to the next line that contains an error" %{
  93.     update-option buffer lint_errors
  94.     %sh{
  95.         printf '%s\n' "$kak_opt_lint_errors" | sed -e 's/\([^\\]\):/\1\n/g' | tail -n +2 | {
  96.             while IFS='|' read -r candidate rest
  97.             do
  98.                 first_range=${first_range-$candidate}
  99.                 if [ "${candidate%%.*}" -gt "$kak_cursor_line" ]; then
  100.                     range=$candidate
  101.                     break
  102.                 fi
  103.             done
  104.             range=${range-$first_range}
  105.             if [ -n "$range" ]; then
  106.                 printf '%s\n' "select $range"
  107.             else
  108.                 printf 'echo -markup "{Error}no lint diagnostics"\n'
  109.             fi
  110.         }
  111.     }}
  112.  
  113. define-command lint-previous-error -docstring "Jump to the previous line that contains an error" %{
  114.     update-option buffer lint_errors
  115.     %sh{
  116.         printf '%s\n' "$kak_opt_lint_errors" | sed -e 's/\([^\\]\):/\1\n/g' | tail -n +2 | sort -t. -k1,1 -rn | {
  117.             while IFS='|' read -r candidate rest
  118.             do
  119.                 first_range=${first_range-$candidate}
  120.                 if [ "${candidate%%.*}" -lt "$kak_cursor_line" ]; then
  121.                     range=$candidate
  122.                     break
  123.                 fi
  124.             done
  125.             range=${range-$first_range}
  126.             if [ -n "$range" ]; then
  127.                 printf '%s\n' "select $range"
  128.             else
  129.                 printf 'echo -markup "{Error}no lint diagnostics"\n'
  130.             fi
  131.         }
  132.     }}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top