]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/preview.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / preview.vim
1 " Author: w0rp <devw0rp@gmail.com>
2 " Description: Preview windows for showing whatever information in.
3
4 if !has_key(s:, 'last_list')
5     let s:last_list = []
6 endif
7
8 if !has_key(s:, 'last_options')
9     let s:last_options = {}
10 endif
11
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),
17     \}
18 endfunction
19
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...
22 "
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, {})
27
28     silent pedit ALEPreviewWindow
29     wincmd P
30
31     setlocal modifiable
32     setlocal noreadonly
33     setlocal nobuflisted
34     setlocal buftype=nofile
35     setlocal bufhidden=wipe
36     :%d
37     call setline(1, a:lines)
38     setlocal nomodifiable
39     setlocal readonly
40     let &l:filetype = get(l:options, 'filetype', 'ale-preview')
41
42     for l:command in get(l:options, 'commands', [])
43         call execute(l:command)
44     endfor
45
46     if get(l:options, 'stay_here')
47         wincmd p
48     endif
49 endfunction
50
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')
55
56         if l:wintype is# a:filetype
57             silent! pclose!
58         endif
59     endfor
60 endfunction
61
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') ? '\' : '/'
67     let l:lines = []
68
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
73
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, '', '')
77         endif
78
79         call add(
80         \   l:lines,
81         \   l:filename
82         \       . ':' . l:item.line
83         \       . ':' . l:item.column
84         \       . (!empty(l:match) ? ' ' . l:match : ''),
85         \)
86     endfor
87
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')
91
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)
97     endif
98
99     " Remember preview state, so we can repeat it later.
100     call ale#preview#SetLastSelection(a:item_list, l:options)
101 endfunction
102
103 function! ale#preview#RepeatSelection() abort
104     if !empty(s:last_list)
105         call ale#preview#ShowSelection(s:last_list, s:last_options)
106     endif
107 endfunction
108
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, {})
113
114     if empty(l:item)
115         return
116     endif
117
118     " Remember an index to jump to when repeating a selection.
119     let s:last_options.jump_to_index = l:index
120
121     :q!
122
123     call ale#util#Open(
124     \   l:item.filename,
125     \   l:item.line,
126     \   l:item.column,
127     \   {'open_in': a:open_in},
128     \)
129 endfunction
130
131 function! ale#preview#OpenSelection() abort
132     call s:Open(b:ale_preview_item_open_in)
133 endfunction
134
135 function! ale#preview#OpenSelectionInTab() abort
136     call s:Open('tab')
137 endfunction