Move virtualtext handling to own file

This allows cursor and virtualtext to be independently autoloaded.
This commit is contained in:
Luan Santos 2018-11-06 22:31:35 -08:00
parent 25068de91d
commit f58a5cba05
No known key found for this signature in database
GPG Key ID: CD0E63A032001A8A
6 changed files with 138 additions and 60 deletions

View File

@ -52,39 +52,6 @@ function! ale#cursor#TruncatedEcho(original_message) abort
endtry
endfunction
function! ale#cursor#ClearVirtualText() abort
if !has('nvim-0.3.2')
return
endif
let l:buffer = bufnr('')
call nvim_buf_clear_highlight(l:buffer, 1000, 0, -1)
endfunction
function! ale#cursor#ShowVirtualText(message, hl_group) abort
if !has('nvim-0.3.2')
return
endif
let l:cursor_position = getcurpos()
let l:line = line('.')
let l:buffer = bufnr('')
let l:prefix = get(g:, 'ale_virtualtext_prefix', '> ')
call nvim_buf_set_virtual_text(l:buffer, 1000, l:line-1, [[l:prefix.a:message, a:hl_group]], {})
endfunction
function! s:FindItemAtCursor(buffer) abort
let l:info = get(g:ale_buffer_info, a:buffer, {})
let l:loclist = get(l:info, 'loclist', [])
let l:pos = getcurpos()
let l:index = ale#util#BinarySearch(l:loclist, a:buffer, l:pos[1], l:pos[2])
let l:loc = l:index >= 0 ? l:loclist[l:index] : {}
return [l:info, l:loc]
endfunction
function! s:StopCursorTimer() abort
if s:cursor_timer != -1
call timer_stop(s:cursor_timer)
@ -95,7 +62,7 @@ endfunction
function! ale#cursor#EchoCursorWarning(...) abort
let l:buffer = bufnr('')
if !g:ale_echo_cursor && !g:ale_cursor_detail && !g:ale_virtualtext_cursor
if !g:ale_echo_cursor && !g:ale_cursor_detail
return
endif
@ -108,7 +75,7 @@ function! ale#cursor#EchoCursorWarning(...) abort
return
endif
let [l:info, l:loc] = s:FindItemAtCursor(l:buffer)
let [l:info, l:loc] = ale#util#FindItemAtCursor(l:buffer)
if g:ale_echo_cursor
if !empty(l:loc)
@ -131,30 +98,12 @@ function! ale#cursor#EchoCursorWarning(...) abort
call ale#preview#CloseIfTypeMatches('ale-preview')
endif
endif
if g:ale_virtualtext_cursor
call ale#cursor#ClearVirtualText()
if !empty(l:loc)
let l:msg = get(l:loc, 'detail', l:loc.text)
let l:hl_group = 'ALEInfo'
let l:type = get(l:loc, 'type', 'E')
if l:type is# 'E'
let l:hl_group = 'ALEError'
elseif l:type is# 'W'
let l:hl_group = 'ALEWarning'
endif
call ale#cursor#ShowVirtualText(l:msg, l:hl_group)
endif
endif
endfunction
function! ale#cursor#EchoCursorWarningWithDelay() abort
let l:buffer = bufnr('')
if !g:ale_echo_cursor && !g:ale_cursor_detail && !g:ale_virtualtext_cursor
if !g:ale_echo_cursor && !g:ale_cursor_detail
return
endif
@ -210,7 +159,7 @@ function! ale#cursor#ShowCursorDetail() abort
call s:StopCursorTimer()
let [l:info, l:loc] = s:FindItemAtCursor(l:buffer)
let [l:info, l:loc] = ale#util#FindItemAtCursor(l:buffer)
if !empty(l:loc)
call s:ShowCursorDetailForItem(l:loc, {'stay_here': 0})

View File

@ -298,12 +298,18 @@ function! ale#engine#SetResults(buffer, loclist) abort
endif
if l:linting_is_done
if g:ale_echo_cursor || g:ale_virtualtext_cursor
if g:ale_echo_cursor
" Try and echo the warning now.
" This will only do something meaningful if we're in normal mode.
call ale#cursor#EchoCursorWarning()
endif
if g:ale_virtualtext_cursor
" Try and show the warning now.
" This will only do something meaningful if we're in normal mode.
call ale#virtualtext#ShowCursorWarning()
endif
" Reset the save event marker, used for opening windows, etc.
call setbufvar(a:buffer, 'ale_save_event_fired', 0)
" Set a marker showing how many times a buffer has been checked.

View File

@ -131,7 +131,7 @@ function! ale#events#Init() abort
autocmd InsertLeave * call ale#Queue(0)
endif
if g:ale_echo_cursor || g:ale_cursor_detail || g:ale_virtualtext_cursor
if g:ale_echo_cursor || g:ale_cursor_detail
autocmd CursorMoved,CursorHold * if exists('*ale#engine#Cleanup') | call ale#cursor#EchoCursorWarningWithDelay() | endif
" Look for a warning to echo as soon as we leave Insert mode.
" The script's position variable used when moving the cursor will
@ -139,6 +139,14 @@ function! ale#events#Init() abort
autocmd InsertLeave * if exists('*ale#engine#Cleanup') | call ale#cursor#EchoCursorWarning() | endif
endif
if g:ale_virtualtext_cursor
autocmd CursorMoved,CursorHold * if exists('*ale#engine#Cleanup') | call ale#virtualtext#ShowCursorWarningWithDelay() | endif
" Look for a warning to echo as soon as we leave Insert mode.
" The script's position variable used when moving the cursor will
" not be changed here.
autocmd InsertLeave * if exists('*ale#engine#Cleanup') | call ale#virtualtext#ShowCursorWarning() | endif
endif
if g:ale_close_preview_on_insert
autocmd InsertEnter * if exists('*ale#preview#CloseIfTypeMatches') | call ale#preview#CloseIfTypeMatches('ale-preview') | endif
endif

View File

@ -452,3 +452,14 @@ function! ale#util#Col(str, chr) abort
return strlen(join(split(a:str, '\zs')[0:a:chr - 2], '')) + 1
endfunction
function! ale#util#FindItemAtCursor(buffer) abort
let l:info = get(g:ale_buffer_info, a:buffer, {})
let l:loclist = get(l:info, 'loclist', [])
let l:pos = getcurpos()
let l:index = ale#util#BinarySearch(l:loclist, a:buffer, l:pos[1], l:pos[2])
let l:loc = l:index >= 0 ? l:loclist[l:index] : {}
return [l:info, l:loc]
endfunction

View File

@ -0,0 +1,104 @@
scriptencoding utf-8
" Author: w0rp <devw0rp@gmail.com>
" Author: Luan Santos <cfcluan@gmail.com>
" Description: Shows lint message for the current line as virtualtext, if any
" Controls the milliseconds delay before showing a message.
let g:ale_virtualtext_delay = get(g:, 'ale_virtualtext_delay', 10)
let s:cursor_timer = -1
let s:last_pos = [0, 0, 0]
function! ale#virtualtext#Clear() abort
if !has('nvim-0.3.2')
return
endif
let l:buffer = bufnr('')
call nvim_buf_clear_highlight(l:buffer, 1000, 0, -1)
endfunction
function! ale#virtualtext#ShowMessage(message, hl_group) abort
if !has('nvim-0.3.2')
return
endif
let l:cursor_position = getcurpos()
let l:line = line('.')
let l:buffer = bufnr('')
let l:prefix = get(g:, 'ale_virtualtext_prefix', '> ')
call nvim_buf_set_virtual_text(l:buffer, 1000, l:line-1, [[l:prefix.a:message, a:hl_group]], {})
endfunction
function! s:StopCursorTimer() abort
if s:cursor_timer != -1
call timer_stop(s:cursor_timer)
let s:cursor_timer = -1
endif
endfunction
function! ale#virtualtext#ShowCursorWarning(...) abort
if !g:ale_virtualtext_cursor
return
endif
let l:buffer = bufnr('')
if mode(1) isnot# 'n'
return
endif
if ale#ShouldDoNothing(l:buffer)
return
endif
let [l:info, l:loc] = ale#util#FindItemAtCursor(l:buffer)
call ale#virtualtext#Clear()
if !empty(l:loc)
let l:msg = get(l:loc, 'detail', l:loc.text)
let l:hl_group = 'ALEInfo'
let l:type = get(l:loc, 'type', 'E')
if l:type is# 'E'
let l:hl_group = 'ALEError'
elseif l:type is# 'W'
let l:hl_group = 'ALEWarning'
endif
call ale#virtualtext#ShowMessage(l:msg, l:hl_group)
endif
endfunction
function! ale#virtualtext#ShowCursorWarningWithDelay() abort
let l:buffer = bufnr('')
if !g:ale_virtualtext_cursor
return
endif
if mode(1) isnot# 'n'
return
endif
call s:StopCursorTimer()
let l:pos = getcurpos()[0:2]
" Check the current buffer, line, and column number against the last
" recorded position. If the position has actually changed, *then*
" we should show something. Otherwise we can end up doing processing
" the show message far too frequently.
if l:pos != s:last_pos
let l:delay = ale#Var(l:buffer, 'virtualtext_delay')
let s:last_pos = l:pos
let s:cursor_timer = timer_start(
\ l:delay,
\ function('ale#virtualtext#ShowCursorWarning')
\)
endif
endfunction

View File

@ -109,12 +109,12 @@ let g:ale_set_highlights = get(g:, 'ale_set_highlights', has('syntax'))
" This flag can be set to 0 to disable echoing when the cursor moves.
let g:ale_echo_cursor = get(g:, 'ale_echo_cursor', 1)
" This flag can be set to 1 to enable virtual text when the cursor moves.
let g:ale_virtualtext_cursor = get(g:, 'ale_virtualtext_cursor', 0)
" This flag can be set to 1 to automatically show errors in the preview window.
let g:ale_cursor_detail = get(g:, 'ale_cursor_detail', 0)
" This flag can be set to 1 to enable virtual text when the cursor moves.
let g:ale_virtualtext_cursor = get(g:, 'ale_virtualtext_cursor', 0)
" This flag can be set to 1 to automatically close the preview window upon
" entering Insert Mode.
let g:ale_close_preview_on_insert = get(g:, 'ale_close_preview_on_insert', 0)