mirror of
1
0
Fork 0
ultimate-vim/sources_non_forked/ale/autoload/ale/preview.vim

138 lines
3.8 KiB
VimL
Raw Normal View History

" Author: w0rp <devw0rp@gmail.com>
" Description: Preview windows for showing whatever information in.
2021-07-04 16:47:44 -04:00
if !has_key(s:, 'last_list')
2020-12-04 16:15:32 -05:00
let s:last_list = []
2020-04-25 21:56:16 -04:00
endif
2020-12-04 16:15:32 -05:00
if !has_key(s:, 'last_options')
let s:last_options = {}
2020-04-25 21:56:16 -04:00
endif
2020-12-04 16:15:32 -05:00
function! ale#preview#SetLastSelection(item_list, options) abort
let s:last_list = a:item_list
let s:last_options = {
\ 'open_in': get(a:options, 'open_in', 'current-buffer'),
\ 'use_relative_paths': get(a:options, 'use_relative_paths', 0),
\}
endfunction
" Open a preview window and show some lines in it.
2018-07-04 06:53:25 -04:00
" A second argument can be passed as a Dictionary with options. They are...
"
" filetype - The filetype to use, defaulting to 'ale-preview'
" stay_here - If 1, stay in the window you came from.
2018-06-14 06:31:12 -04:00
function! ale#preview#Show(lines, ...) abort
2018-07-04 06:53:25 -04:00
let l:options = get(a:000, 0, {})
2018-06-14 06:31:12 -04:00
silent pedit ALEPreviewWindow
wincmd P
2018-07-04 06:53:25 -04:00
setlocal modifiable
setlocal noreadonly
setlocal nobuflisted
setlocal buftype=nofile
setlocal bufhidden=wipe
:%d
call setline(1, a:lines)
setlocal nomodifiable
setlocal readonly
2018-09-24 20:40:17 -04:00
let &l:filetype = get(l:options, 'filetype', 'ale-preview')
2018-07-04 06:53:25 -04:00
2020-12-04 16:15:32 -05:00
for l:command in get(l:options, 'commands', [])
call execute(l:command)
endfor
2018-07-04 06:53:25 -04:00
if get(l:options, 'stay_here')
wincmd p
endif
endfunction
" Close the preview window if the filetype matches the given one.
function! ale#preview#CloseIfTypeMatches(filetype) abort
for l:win in getwininfo()
let l:wintype = gettabwinvar(l:win.tabnr, l:win.winnr, '&filetype')
if l:wintype is# a:filetype
silent! pclose!
endif
endfor
endfunction
2018-06-14 06:31:12 -04:00
" Show a location selection preview window, given some items.
" Each item should have 'filename', 'line', and 'column' keys.
2019-03-08 06:04:56 -05:00
function! ale#preview#ShowSelection(item_list, ...) abort
let l:options = get(a:000, 0, {})
let l:sep = has('win32') ? '\' : '/'
2018-06-14 06:31:12 -04:00
let l:lines = []
" Create lines to display to users.
for l:item in a:item_list
2018-11-01 06:03:42 -04:00
let l:match = get(l:item, 'match', '')
2019-03-08 06:04:56 -05:00
let l:filename = l:item.filename
if get(l:options, 'use_relative_paths')
let l:cwd = getcwd() " no-custom-checks
let l:filename = substitute(l:filename, '^' . l:cwd . l:sep, '', '')
endif
2018-11-01 06:03:42 -04:00
2018-06-14 06:31:12 -04:00
call add(
\ l:lines,
2019-03-08 06:04:56 -05:00
\ l:filename
2018-06-14 06:31:12 -04:00
\ . ':' . l:item.line
2018-11-01 06:03:42 -04:00
\ . ':' . l:item.column
\ . (!empty(l:match) ? ' ' . l:match : ''),
2018-06-14 06:31:12 -04:00
\)
endfor
2018-07-04 06:53:25 -04:00
call ale#preview#Show(l:lines, {'filetype': 'ale-preview-selection'})
2018-06-14 06:31:12 -04:00
let b:ale_preview_item_list = a:item_list
2020-04-25 21:56:16 -04:00
let b:ale_preview_item_open_in = get(l:options, 'open_in', 'current-buffer')
2021-07-04 16:47:44 -04:00
" Jump to an index for a previous selection, if set.
if has_key(l:options, 'jump_to_index')
let l:pos = getpos('.')
let l:pos[1] = l:options.jump_to_index + 1
call setpos('.', l:pos)
endif
2020-12-04 16:15:32 -05:00
" Remember preview state, so we can repeat it later.
call ale#preview#SetLastSelection(a:item_list, l:options)
2018-06-14 06:31:12 -04:00
endfunction
2020-04-25 21:56:16 -04:00
function! ale#preview#RepeatSelection() abort
2020-12-04 16:15:32 -05:00
if !empty(s:last_list)
call ale#preview#ShowSelection(s:last_list, s:last_options)
2020-04-25 21:56:16 -04:00
endif
endfunction
function! s:Open(open_in) abort
2018-06-14 06:31:12 -04:00
let l:item_list = get(b:, 'ale_preview_item_list', [])
2021-07-04 16:47:44 -04:00
let l:index = getpos('.')[1] - 1
let l:item = get(l:item_list, l:index, {})
2018-06-14 06:31:12 -04:00
if empty(l:item)
return
endif
2021-07-04 16:47:44 -04:00
" Remember an index to jump to when repeating a selection.
let s:last_options.jump_to_index = l:index
2020-04-25 21:56:16 -04:00
:q!
2018-06-14 06:31:12 -04:00
call ale#util#Open(
\ l:item.filename,
\ l:item.line,
\ l:item.column,
2020-04-25 21:56:16 -04:00
\ {'open_in': a:open_in},
2018-06-14 06:31:12 -04:00
\)
endfunction
2020-04-25 21:56:16 -04:00
function! ale#preview#OpenSelection() abort
call s:Open(b:ale_preview_item_open_in)
2018-06-14 06:31:12 -04:00
endfunction
function! ale#preview#OpenSelectionInTab() abort
2020-04-25 21:56:16 -04:00
call s:Open('tab')
2018-06-14 06:31:12 -04:00
endfunction