All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 " Author: w0rp <devw0rp@gmail.com>
2 " Description: Preview windows for showing whatever information in.
4 if !has_key(s:, 'last_list')
8 if !has_key(s:, 'last_options')
9 let s:last_options = {}
12 function! ale#preview#SetLastSelection(item_list, options) abort
13 let s:last_list = a:item_list
14 let s:last_options = {
15 \ 'open_in': get(a:options, 'open_in', 'current-buffer'),
16 \ 'use_relative_paths': get(a:options, 'use_relative_paths', 0),
20 " Open a preview window and show some lines in it.
21 " A second argument can be passed as a Dictionary with options. They are...
23 " filetype - The filetype to use, defaulting to 'ale-preview'
24 " stay_here - If 1, stay in the window you came from.
25 function! ale#preview#Show(lines, ...) abort
26 let l:options = get(a:000, 0, {})
28 silent pedit ALEPreviewWindow
34 setlocal buftype=nofile
35 setlocal bufhidden=wipe
37 call setline(1, a:lines)
40 let &l:filetype = get(l:options, 'filetype', 'ale-preview')
42 for l:command in get(l:options, 'commands', [])
43 call execute(l:command)
46 if get(l:options, 'stay_here')
51 " Close the preview window if the filetype matches the given one.
52 function! ale#preview#CloseIfTypeMatches(filetype) abort
53 for l:win in getwininfo()
54 let l:wintype = gettabwinvar(l:win.tabnr, l:win.winnr, '&filetype')
56 if l:wintype is# a:filetype
62 " Show a location selection preview window, given some items.
63 " Each item should have 'filename', 'line', and 'column' keys.
64 function! ale#preview#ShowSelection(item_list, ...) abort
65 let l:options = get(a:000, 0, {})
66 let l:sep = has('win32') ? '\' : '/'
69 " Create lines to display to users.
70 for l:item in a:item_list
71 let l:match = get(l:item, 'match', '')
72 let l:filename = l:item.filename
74 if get(l:options, 'use_relative_paths')
75 let l:cwd = getcwd() " no-custom-checks
76 let l:filename = substitute(l:filename, '^' . l:cwd . l:sep, '', '')
83 \ . ':' . l:item.column
84 \ . (!empty(l:match) ? ' ' . l:match : ''),
88 call ale#preview#Show(l:lines, {'filetype': 'ale-preview-selection'})
89 let b:ale_preview_item_list = a:item_list
90 let b:ale_preview_item_open_in = get(l:options, 'open_in', 'current-buffer')
92 " Jump to an index for a previous selection, if set.
93 if has_key(l:options, 'jump_to_index')
94 let l:pos = getpos('.')
95 let l:pos[1] = l:options.jump_to_index + 1
96 call setpos('.', l:pos)
99 " Remember preview state, so we can repeat it later.
100 call ale#preview#SetLastSelection(a:item_list, l:options)
103 function! ale#preview#RepeatSelection() abort
104 if !empty(s:last_list)
105 call ale#preview#ShowSelection(s:last_list, s:last_options)
109 function! s:Open(open_in) abort
110 let l:item_list = get(b:, 'ale_preview_item_list', [])
111 let l:index = getpos('.')[1] - 1
112 let l:item = get(l:item_list, l:index, {})
118 " Remember an index to jump to when repeating a selection.
119 let s:last_options.jump_to_index = l:index
127 \ {'open_in': a:open_in},
131 function! ale#preview#OpenSelection() abort
132 call s:Open(b:ale_preview_item_open_in)
135 function! ale#preview#OpenSelectionInTab() abort