2016-04-12 21:17:50 +02:00
|
|
|
decl str flake8_options
|
|
|
|
decl -hidden str flake8_tmp_dir
|
|
|
|
decl -hidden line-flags flake8_flags
|
|
|
|
decl -hidden str flake8_errors
|
|
|
|
|
2016-10-11 09:03:41 +02:00
|
|
|
def flake8-lint -docstring "Lint the contents of the current buffer" %{
|
2016-04-12 21:17:50 +02:00
|
|
|
%sh{
|
|
|
|
dir=$(mktemp -d -t kak-flake8.XXXXXXXX)
|
|
|
|
mkfifo ${dir}/fifo
|
|
|
|
echo "set buffer flake8_tmp_dir ${dir}"
|
2016-10-03 21:29:54 +02:00
|
|
|
echo "eval -no-hooks write ${dir}/buf"
|
2016-04-12 21:17:50 +02:00
|
|
|
}
|
2016-04-13 02:29:56 +02:00
|
|
|
|
2016-04-12 21:17:50 +02:00
|
|
|
# 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 %{
|
2016-04-13 02:29:56 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}"
|
2016-04-12 21:17:50 +02:00
|
|
|
# this runs in a detached shell, asynchronously, so that kakoune does
|
|
|
|
# not hang while flake8 is running.
|
2016-04-13 02:29:56 +02:00
|
|
|
(
|
|
|
|
flake8 --ignore=${kak_opt_flake8_options} - < ${dir}/buf > ${dir}/stderr
|
2016-04-12 21:17:50 +02:00
|
|
|
flags=$(cat ${dir}/stderr | sed -rne "
|
2016-04-13 02:29:56 +02:00
|
|
|
/^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 ':')
|
2016-04-12 21:17:50 +02:00
|
|
|
errors=$(cat ${dir}/stderr | sed -rne "
|
2016-04-13 02:29:56 +02:00
|
|
|
/^stdin:[0-9]+:[0-9]+:?.*/ { s/^stdin:([0-9]+):([0-9]+:)? (.*) /\1,\3/; s/'/\\\\'/g; p }
|
|
|
|
" | sort -n)
|
2016-04-12 21:17:50 +02:00
|
|
|
|
|
|
|
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{
|
2016-04-23 07:47:01 +02:00
|
|
|
desc=$(printf %s\\n "${kak_opt_flake8_errors}" | sed -ne "/^${kak_cursor_line},.*/ { s/^[[:digit:]]\+,//g; s/'/\\\\'/g; p }")
|
2016-04-12 21:17:50 +02:00
|
|
|
if [ -n "$desc" ]; then
|
|
|
|
echo "info -anchor ${kak_cursor_line}.${kak_cursor_column} '${desc}'"
|
|
|
|
fi
|
|
|
|
} }
|
|
|
|
|
2016-10-11 09:03:41 +02:00
|
|
|
def flake8-enable-diagnostics -docstring "Activate automatic diagnostics of the code" %{
|
2016-04-12 21:17:50 +02:00
|
|
|
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
|
2016-04-12 21:17:50 +02:00
|
|
|
fi
|
|
|
|
done
|
2016-10-12 14:35:25 +02:00
|
|
|
if [ $line -eq -1 ]; then
|
2016-09-24 11:49:19 +02:00
|
|
|
line=$first_line
|
2016-04-12 21:17:50 +02:00
|
|
|
echo 'echo -color Error no next flake8 diagnostic'
|
2016-09-24 11:49:19 +02:00
|
|
|
else
|
|
|
|
echo "exec ${line} g"
|
2016-04-12 21:17:50 +02:00
|
|
|
fi
|
|
|
|
)
|
2016-04-13 02:29:56 +02:00
|
|
|
}}
|