home/rc/extra/flake8.kak
Frank LENORMAND f6a2925950 Fix, complete and add docstring documentation to builtin commands
Level out the builtin commands loaded at startup in terms of format and
expressiveness. The following convention was followed:
* commands that take more than one argument have to be described along
  with their parameters prior to the actual documentation, otherwise the
  docstring consists in a capitalized sentence
  e.g. `command <arg1>: do something`
* optional arguments are enclosed in square brackets, to comply with the
  format used for hardcoded commands
  e.g. `cd [<directory>]`
* describe the effects of the command in the documentation string and
  omit implementation details unless they are relevant. Usually command
  names include the name of the tool they use, so they don't need to be
  redundantly mentioned
  e.g. `tmux-new-pane <arguments>: open a new pane`
* document the format the parameters to the commands, or list them if
  they are to be chosen among a list of static values (c.f. `spell.kak`)
2016-10-11 10:26:17 +03:00

88 lines
3.3 KiB
Plaintext

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
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
if [ $line -eq -1 ];
line=$first_line
echo 'echo -color Error no next flake8 diagnostic'
else
echo "exec ${line} g"
fi
)
}}