kakoune/rc/extra/flake8.kak

88 lines
3.3 KiB
Plaintext
Raw Normal View History

decl str flake8_options
decl -hidden str flake8_tmp_dir
decl -hidden line-flags flake8_flags
decl -hidden str flake8_errors
def flake8-lint -docstring "Lint the contents of the current buffer" %{
%sh{
dir=$(mktemp -d -t kak-flake8.XXXXXXXX)
mkfifo ${dir}/fifo
echo "set buffer flake8_tmp_dir ${dir}"
echo "eval -no-hooks write ${dir}/buf"
}
# end the previous %sh{} so that its output gets interpreted by kakoune
# before launching the following as a background task.
%sh{
dir=${kak_opt_flake8_tmp_dir}
echo "eval -draft %{
edit! -fifo ${dir}/fifo *flake8-output*
set buffer filetype make
set buffer _make_current_error_line 0
hook -group fifo buffer BufCloseFifo .* %{
nop %sh{ rm -r ${dir} }
rmhooks buffer fifo
}
}"
# this runs in a detached shell, asynchronously, so that kakoune does
# not hang while flake8 is running.
(
flake8 --ignore=${kak_opt_flake8_options} - < ${dir}/buf > ${dir}/stderr
flags=$(cat ${dir}/stderr | sed -rne "
/^stdin:[0-9]+:[0-9]+:? (W|F|C|N).*/ { s/^stdin:([0-9]+):.*/\1|{yellow}█/; p }
/^stdin:[0-9]+:[0-9]+:? E.*/ { s/^stdin:([0-9]+):.*/\1|{red}█/; p }
" | paste -s -d ':')
errors=$(cat ${dir}/stderr | sed -rne "
/^stdin:[0-9]+:[0-9]+:?.*/ { s/^stdin:([0-9]+):([0-9]+:)? (.*) /\1,\3/; s/'/\\\\'/g; p }
" | sort -n)
sed -e "s|<stdin>|${kak_bufname}|g" < ${dir}/stderr > ${dir}/fifo
echo "set 'buffer=${kak_buffile}' flake8_flags %{${kak_timestamp}:${flags}}
set 'buffer=${kak_buffile}' flake8_errors '${errors}'" | kak -p ${kak_session}
) > /dev/null 2>&1 < /dev/null &
}
}
def -hidden flake8-show-error-info %{ %sh{
desc=$(printf %s\\n "${kak_opt_flake8_errors}" | sed -ne "/^${kak_cursor_line},.*/ { s/^[[:digit:]]\+,//g; s/'/\\\\'/g; p }")
if [ -n "$desc" ]; then
echo "info -anchor ${kak_cursor_line}.${kak_cursor_column} '${desc}'"
fi
} }
def flake8-enable-diagnostics -docstring "Activate automatic diagnostics of the code" %{
addhl flag_lines default flake8_flags
hook window -group flake8-diagnostics NormalIdle .* %{ flake8-show-error-info }
}
def flake8-disable-diagnostics -docstring "Disable automatic diagnostics of the code" %{
rmhl hlflags_flake8_flags
rmhooks window flake8-diagnostics
}
def flake8-diagnostics-next -docstring "Jump to the next line that contains an error" %{ %sh{
printf "%s\n" "${kak_opt_flake8_errors}" | (
line=-1
first_line=-1
while read line_content; do
candidate=${line_content%%,*}
if [ -n "$candidate" ]; then
2016-09-24 11:49:19 +02:00
if [ $first_line -eq -1 ]; then
first_line=$candidate
fi
if [ $candidate -gt $kak_cursor_line ]; then
if [ $candidate -lt $line ] || [ $line -eq -1 ]; then
line=$candidate
fi
fi
fi
done
2016-09-24 11:49:19 +02:00
if [ $line -eq -1 ];
line=$first_line
echo 'echo -color Error no next flake8 diagnostic'
2016-09-24 11:49:19 +02:00
else
echo "exec ${line} g"
fi
)
}}