Fix #1069 Support formatting the loclist messages with g:ale_loclist_msg_format

This commit is contained in:
w0rp
2017-11-14 10:28:36 +00:00
parent d8f9aef84a
commit 16e7dc2371
6 changed files with 215 additions and 30 deletions

View File

@@ -257,3 +257,31 @@ function! ale#Escape(str) abort
return shellescape (a:str)
endfunction
" Get the loclist item message according to a given format string.
"
" See `:help g:ale_loclist_msg_format` and `:help g:ale_echo_msg_format`
function! ale#GetLocItemMessage(item, format_string) abort
let l:msg = a:format_string
let l:severity = g:ale_echo_msg_warning_str
let l:code = get(a:item, 'code', '')
let l:type = get(a:item, 'type', 'E')
let l:linter_name = get(a:item, 'linter_name', '')
let l:code_repl = !empty(l:code) ? '\=submatch(1) . l:code . submatch(2)' : ''
if l:type is# 'E'
let l:severity = g:ale_echo_msg_error_str
elseif l:type is# 'I'
let l:severity = g:ale_echo_msg_info_str
endif
" Replace special markers with certain information.
" \=l:variable is used to avoid escaping issues.
let l:msg = substitute(l:msg, '\V%severity%', '\=l:severity', 'g')
let l:msg = substitute(l:msg, '\V%linter%', '\=l:linter_name', 'g')
let l:msg = substitute(l:msg, '\v\%([^\%]*)code([^\%]*)\%', l:code_repl, 'g')
" Replace %s with the text.
let l:msg = substitute(l:msg, '\V%s', '\=a:item.text', 'g')
return l:msg
endfunction

View File

@@ -4,30 +4,6 @@
let s:cursor_timer = -1
let s:last_pos = [0, 0, 0]
" Return a formatted message according to g:ale_echo_msg_format variable
function! s:GetMessage(item) abort
let l:msg = g:ale_echo_msg_format
let l:severity = g:ale_echo_msg_warning_str
let l:code = get(a:item, 'code', '')
let l:code_repl = !empty(l:code) ? '\=submatch(1) . l:code . submatch(2)' : ''
if a:item.type is# 'E'
let l:severity = g:ale_echo_msg_error_str
elseif a:item.type is# 'I'
let l:severity = g:ale_echo_msg_info_str
endif
" Replace special markers with certain information.
" \=l:variable is used to avoid escaping issues.
let l:msg = substitute(l:msg, '\V%severity%', '\=l:severity', 'g')
let l:msg = substitute(l:msg, '\V%linter%', '\=a:item.linter_name', 'g')
let l:msg = substitute(l:msg, '\v\%([^\%]*)code([^\%]*)\%', l:code_repl, 'g')
" Replace %s with the text.
let l:msg = substitute(l:msg, '\V%s', '\=a:item.text', 'g')
return l:msg
endfunction
function! s:EchoWithShortMess(setting, message) abort
" We need to remember the setting for shormess and reset it again.
let l:shortmess_options = getbufvar('%', '&shortmess')
@@ -96,7 +72,7 @@ function! s:EchoImpl() abort
let [l:info, l:loc] = s:FindItemAtCursor()
if !empty(l:loc)
let l:msg = s:GetMessage(l:loc)
let l:msg = ale#GetLocItemMessage(l:loc, g:ale_echo_msg_format)
call ale#cursor#TruncatedEcho(l:msg)
let l:info.echoed = 1
elseif get(l:info, 'echoed')

View File

@@ -41,13 +41,16 @@ function! s:FixList(list) abort
let l:new_list = []
for l:item in a:list
let l:fixed_item = copy(l:item)
let l:fixed_item.text = ale#GetLocItemMessage(
\ l:item,
\ g:ale_loclist_msg_format,
\)
if l:item.bufnr == -1
" If the buffer number is invalid, remove it.
let l:fixed_item = copy(l:item)
call remove(l:fixed_item, 'bufnr')
else
" Don't copy the Dictionary if we do not need to.
let l:fixed_item = l:item
endif
call add(l:new_list, l:fixed_item)