Last active
October 23, 2024 01:04
-
-
Save BoltsJ/5942ecac7f0b0e9811749ef6e19d2176 to your computer and use it in GitHub Desktop.
Automatically place signs based on the quickfix list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if exists('g:loaded_qfsign') | |
finish | |
endif | |
let g:loaded_qfsign=1 | |
sign define QFErr texthl=QFErrMarker text=E | |
sign define QFWarn texthl=QFWarnMarker text=W | |
sign define QFInfo texthl=QFInfoMarker text=I | |
augroup qfsign | |
autocmd! | |
autocmd QuickFixCmdPre [^l]* call s:clear_signs() | |
autocmd QuickFixCmdPost [^l]* call s:place_signs() | |
augroup END | |
nnoremap <Plug>(QfSignPlace) :silent call <SID>place_signs()<CR> | |
nnoremap <Plug>(QfSignClear) :silent call <SID>clear_signs()<CR> | |
let s:sign_count = 0 | |
function! s:place_signs() abort | |
let l:errors = getqflist() | |
for l:error in l:errors | |
if l:error.bufnr < 0 | |
continue | |
endif | |
let s:sign_count = s:sign_count + 1 | |
if l:error.type ==# 'E' | |
let l:err_sign = 'sign place ' . s:sign_count | |
\ . ' line=' . l:error.lnum | |
\ . ' name=QFErr' | |
\ . ' buffer=' . l:error.bufnr | |
elseif l:error.type ==# 'W' | |
let l:err_sign = 'sign place ' . s:sign_count | |
\ . ' line=' . l:error.lnum | |
\ . ' name=QFWarn' | |
\ . ' buffer=' . l:error.bufnr | |
else | |
let l:err_sign = 'sign place ' . s:sign_count | |
\ . ' line=' . l:error.lnum | |
\ . ' name=QFInfo' | |
\ . ' buffer=' . l:error.bufnr | |
endif | |
silent! execute l:err_sign | |
endfor | |
endfunction | |
function! s:clear_signs() abort | |
while s:sign_count > 0 | |
execute 'sign unplace ' . s:sign_count | |
let s:sign_count = s:sign_count - 1 | |
endwhile | |
redraw! | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is really neat! Thank you for sharing.
A few improvements I've made that I would like to share:
Filter out text unmatched by errorformat
In cases where linter/compiler output doesn't match
errorformat
exactly,getqflist()
still reports it, causing the unmatched output to be calculated/reported in the finalelse
control flow.For example, consider the following
tflint.vim
compiler configuration:And the following terraform file:
Running
:make %
produces:Running
:echo getqflist()
in vim produces (note the first 2 elements in the array):A simple fix is to check if
bufnr <= 0
(is buffer number of0
even possible?), like:Alternative, you can check if
lnum
is0
(because why/how would you show signs for something with no line number?), like:Sign Priority
Signs were not showing up in my
signcolumn
. This was because Vim assigns a default priority of10
for signs (:h sign-place
). Priority helps Vim determine which sign to show when multiple signs are placed on the same line. This scenario happes easily & frequently with plugins likevim-signify
orvim-gitgutter
.I fixed this by assigning a priority of
99
to errors,98
to warnings, and97
to info, because I much rather see issues with code than git status indicators in mysigncolumn
, like this:Note:
priority
must be specified/assigned beforefile=
orbuffer=
.Support location lists
Surface counts to vimrc
I placed this script in my
~/.vim/plugin/
directory and wanted to surface the count of errors/warnings/infos to show in my statusline.Then in my
vimrc
:Everything put together: https://gist.github.com/pbnj/dc19a2d0f579933daef7f0fd9604dc0c