1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
" Author: Jan-Grimo Sobez <jan-grimo.sobez@phys.chem.ethz.ch>
" Author: Kevin Clark <kevin.clark@gmail.com>
" Author: D. Ben Knoble <ben.knoble+github@gmail.com>
" Author: Shaun Duncan <shaun.duncan@gmail.com>
" Description: Floating preview window for showing whatever information in.
" Precondition: exists('*nvim_open_win') || has('popupwin')
function! ale#floating_preview#Show(lines, ...) abort
if !exists('*nvim_open_win') && !has('popupwin')
" no-custom-checks
echom 'Floating windows not supported in this vim instance.'
return
endif
let l:options = get(a:000, 0, {})
if has('nvim')
call s:NvimShow(a:lines, l:options)
else
call s:VimShow(a:lines, l:options)
endif
endfunction
function! s:NvimShow(lines, options) abort
" Remove the close autocmd so it doesn't happen mid update
augroup ale_floating_preview_window
autocmd!
augroup END
" Only create a new window if we need it
if !exists('w:preview') || index(nvim_list_wins(), w:preview['id']) is# -1
call s:NvimCreate(a:options)
else
call nvim_buf_set_option(w:preview['buffer'], 'modifiable', v:true)
endif
" Execute commands in window context
if exists('*win_execute')
for l:command in get(a:options, 'commands', [])
call win_execute(w:preview['id'], l:command)
endfor
else
let l:parent_window = nvim_get_current_win()
call nvim_set_current_win(w:preview['id'])
for l:command in get(a:options, 'commands', [])
call execute(l:command)
endfor
call nvim_set_current_win(l:parent_window)
endif
" Return to parent context on move
augroup ale_floating_preview_window
autocmd!
if g:ale_close_preview_on_insert
autocmd CursorMoved,TabLeave,WinLeave,BufWinLeave,WinScrolled,InsertEnter <buffer> ++once call s:NvimClose()
else
autocmd CursorMoved,TabLeave,WinLeave,BufWinLeave,WinScrolled <buffer> ++once call s:NvimClose()
endif
augroup END
let [l:lines, l:width, l:height] = s:NvimPrepareWindowContent(a:lines)
call nvim_win_set_width(w:preview['id'], l:width)
call nvim_win_set_height(w:preview['id'], l:height)
call nvim_buf_set_lines(w:preview['buffer'], 0, -1, v:false, l:lines)
call nvim_buf_set_option(w:preview['buffer'], 'modified', v:false)
call nvim_buf_set_option(w:preview['buffer'], 'modifiable', v:false)
endfunction
function! s:VimShow(lines, options) abort
if g:ale_close_preview_on_insert
" Remove the close autocmd so it doesn't happen mid update
silent! autocmd! ale_floating_preview_window
endif
" Only create a new window if we need it
if !exists('w:preview') || index(popup_list(), w:preview['id']) is# -1
call s:VimCreate(a:options)
endif
" Execute commands in window context
for l:command in get(a:options, 'commands', [])
call win_execute(w:preview['id'], l:command)
endfor
call popup_settext(w:preview['id'], a:lines)
if g:ale_close_preview_on_insert
augroup ale_floating_preview_window
autocmd!
autocmd InsertEnter * ++once call s:VimClose()
augroup END
endif
endfunction
function! s:NvimPrepareWindowContent(lines) abort
let l:max_height = 10
let l:width = max(map(copy(a:lines), 'strdisplaywidth(v:val)'))
let l:height = min([len(a:lines), l:max_height])
return [a:lines[0:l:height-1], l:width, l:height]
endfunction
function! s:NvimCreate(options) abort
let l:left = get(g:ale_floating_window_border, 0, '|')
let l:top = get(g:ale_floating_window_border, 1, '-')
let l:popup_opts = extend({
\ 'relative': 'cursor',
\ 'row': 1,
\ 'col': 0,
\ 'width': 42,
\ 'height': 4,
\ 'style': 'minimal',
\ 'border': empty(g:ale_floating_window_border) ? 'none' : [
\ get(g:ale_floating_window_border, 2, '+'),
\ l:top,
\ get(g:ale_floating_window_border, 3, '+'),
\ get(g:ale_floating_window_border, 6, l:left),
\ get(g:ale_floating_window_border, 4, '+'),
\ get(g:ale_floating_window_border, 7, l:top),
\ get(g:ale_floating_window_border, 5, '+'),
\ l:left,
\ ],
\ }, s:GetPopupOpts())
let l:buffer = nvim_create_buf(v:false, v:false)
let l:winid = nvim_open_win(l:buffer, v:false, l:popup_opts)
call nvim_buf_set_option(l:buffer, 'buftype', 'acwrite')
call nvim_buf_set_option(l:buffer, 'bufhidden', 'delete')
call nvim_buf_set_option(l:buffer, 'swapfile', v:false)
call nvim_buf_set_option(l:buffer, 'filetype', get(a:options, 'filetype', 'ale-preview'))
let w:preview = {'id': l:winid, 'buffer': l:buffer}
endfunction
function! s:VimCreate(options) abort
" default options
let l:popup_opts = extend({
\ 'line': 'cursor+1',
\ 'col': 'cursor',
\ 'drag': v:true,
\ 'resize': v:true,
\ 'close': 'button',
\ 'padding': [0, 1, 0, 1],
\ 'border': [],
\ 'borderchars': empty(g:ale_floating_window_border) ? [' '] : [
\ get(g:ale_floating_window_border, 1, '-'),
\ get(g:ale_floating_window_border, 6, '|'),
\ get(g:ale_floating_window_border, 7, '-'),
\ get(g:ale_floating_window_border, 0, '|'),
\ get(g:ale_floating_window_border, 2, '+'),
\ get(g:ale_floating_window_border, 3, '+'),
\ get(g:ale_floating_window_border, 4, '+'),
\ get(g:ale_floating_window_border, 5, '+'),
\ ],
\ 'moved': 'any',
\ }, s:GetPopupOpts())
let l:popup_id = popup_create([], l:popup_opts)
call setbufvar(winbufnr(l:popup_id), '&filetype', get(a:options, 'filetype', 'ale-preview'))
let w:preview = {'id': l:popup_id}
endfunction
function! s:NvimClose() abort
let l:mode = mode()
let l:restore_visual = l:mode is# 'v' || l:mode is# 'V' || l:mode is# "\<C-V>"
if !exists('w:preview')
return
endif
call setbufvar(w:preview['buffer'], '&modified', 0)
if win_id2win(w:preview['id']) > 0
execute win_id2win(w:preview['id']).'wincmd c'
endif
unlet w:preview
if l:restore_visual
normal! gv
endif
endfunction
function! s:VimClose() abort
if !exists('w:preview')
return
endif
call popup_close(w:preview['id'])
unlet w:preview
endfunction
" get either the results of a function callback or dictionary for popup overrides
function! s:GetPopupOpts() abort
if exists('g:ale_floating_preview_popup_opts')
let l:ref = g:ale_floating_preview_popup_opts
if type(l:ref) is# v:t_dict
return l:ref
elseif type(l:ref) is# v:t_string
try
return function(l:ref)()
catch /E700/
endtry
endif
endif
return {}
endfunction
|