-
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathcursor.vim
More file actions
315 lines (258 loc) · 7.96 KB
/
cursor.vim
File metadata and controls
315 lines (258 loc) · 7.96 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
" Copyright (c) 2019 Liu-Cheng Xu
" MIT License
" vim: ts=2 sw=2 sts=2 et
scriptencoding utf8
let s:find_timer = -1
let s:cursor_timer = -1
let s:find_detail_timer = -1
let s:highlight_timer = -1
let s:echo_cursor_opts = ['echo', 'floating_win', 'scroll', 'both']
let s:echo_strategy = get(g:, 'vista_echo_cursor_strategy', 'echo')
let s:last_vlnum = v:null
function! s:GenericStopTimer(timer) abort
execute 'if '.a:timer.' != -1 |'.
\ ' call timer_stop('.a:timer.') |'.
\ ' let 'a:timer.' = -1 |'.
\ 'endif'
endfunction
function! s:StopFindTimer() abort
call s:GenericStopTimer('s:find_timer')
endfunction
function! s:StopCursorTimer() abort
call s:GenericStopTimer('s:cursor_timer')
endfunction
function! s:StopFindDetailTimer() abort
call s:GenericStopTimer('s:find_detail_timer')
endfunction
function! s:StopHighlightTimer() abort
call s:GenericStopTimer('s:highlight_timer')
endfunction
" Get tag and corresponding source line at current cursor position.
"
" Return: [tag, source_line]
function! s:GetInfoUnderCursor() abort
if g:vista.provider ==# 'ctags'
return vista#cursor#ctags#GetInfo()
else
return vista#cursor#lsp#GetInfo()
endif
endfunction
function! s:Compare(s1, s2) abort
return a:s1.lnum - a:s2.lnum
endfunction
function! s:FindNearestMethodOrFunction(_timer) abort
if !exists('g:vista')
\ || !has_key(g:vista, 'functions')
\ || !has_key(g:vista, 'source')
return
endif
call sort(g:vista.functions, function('s:Compare'))
let result = vista#util#BinarySearch(g:vista.functions, line('.'), 'lnum', 'text')
if empty(result)
let result = ''
endif
call setbufvar(g:vista.source.bufnr, 'vista_nearest_method_or_function', result)
call s:StopHighlightTimer()
if vista#sidebar#IsOpen()
let s:highlight_timer = timer_start(200, function('s:HighlightNearestTag'))
endif
endfunction
function! s:HasVlnum() abort
return exists('g:vista')
\ && has_key(g:vista, 'raw')
\ && !empty(g:vista.raw)
\ && has_key(g:vista.raw[0], 'vlnum')
endfunction
" Highlight the nearest tag in the vista window.
function! s:HighlightNearestTag(_timer) abort
if !exists('g:vista')
return
endif
let winnr = g:vista.winnr()
if winnr == -1
\ || vista#ShouldSkip()
\ || !s:HasVlnum()
\ || mode() !=# 'n'
return
endif
let found = vista#util#BinarySearch(g:vista.raw, line('.'), 'line', '')
if empty(found)
return
endif
let s:vlnum = get(found, 'vlnum', v:null)
" Skip if the vlnum is same with previous one
if empty(s:vlnum) || s:last_vlnum == s:vlnum
return
endif
let s:last_vlnum = s:vlnum
let tag = get(found, 'name', v:null)
call vista#win#Execute(winnr, function('vista#highlight#Add'), s:vlnum, v:true, tag)
endfunction
" Fold or unfold when meets the top level tag line
function! s:TryFoldIsOk() abort
if indent('.') == 0
if !empty(getline('.'))
if foldclosed('.') != -1
normal! zo
return v:true
elseif foldlevel('.') != 0
normal! zc
return v:true
endif
endif
endif
return v:false
endfunction
" Fold scope based on the indent.
" Jump to the target source line or source file.
function! vista#cursor#FoldOrJump() abort
if line('.') == 1
call vista#source#GotoWin()
return
elseif s:TryFoldIsOk()
return
endif
let tag_under_cursor = s:GetInfoUnderCursor()[0]
call vista#jump#TagLine(tag_under_cursor)
endfunction
" This happens when you are in the window of source file
function! vista#cursor#FindNearestMethodOrFunction() abort
if !exists('g:vista')
\ || !has_key(g:vista, 'functions')
\ || bufnr('') != g:vista.source.bufnr
return
endif
call s:StopFindTimer()
if empty(g:vista.functions)
call setbufvar(g:vista.source.bufnr, 'vista_nearest_method_or_function', '')
return
endif
let s:find_timer = timer_start(
\ g:vista_find_nearest_method_or_function_delay,
\ function('s:FindNearestMethodOrFunction'),
\ )
endfunction
function! vista#cursor#NearestSymbol() abort
return vista#util#BinarySearch(g:vista.raw, line('.'), 'line', 'name')
endfunction
" Show the detail of current tag/symbol under cursor.
function! vista#cursor#ShowDetail(_timer) abort
if empty(getline('.'))
\ || vista#echo#EchoScopeInCmdlineIsOk()
\ || vista#win#ShowFoldedDetailInFloatingIsOk()
return
endif
let [tag, source_line] = s:GetInfoUnderCursor()
if empty(tag) || empty(source_line)
echo "\r"
return
endif
let msg = vista#util#Truncate(source_line)
let lnum = s:GetTrailingLnum()
if s:echo_strategy ==# s:echo_cursor_opts[0]
call vista#echo#EchoInCmdline(msg, tag)
elseif s:echo_strategy ==# s:echo_cursor_opts[1]
call vista#win#FloatingDisplay(lnum, tag)
elseif s:echo_strategy ==# s:echo_cursor_opts[2]
call vista#source#PeekSymbol(lnum, tag)
elseif s:echo_strategy ==# s:echo_cursor_opts[3]
call vista#echo#EchoInCmdline(msg, tag)
call vista#win#FloatingDisplayOrPeek(lnum, tag)
else
call vista#error#InvalidOption('g:vista_echo_cursor_strategy', s:echo_cursor_opts)
endif
call vista#highlight#Add(line('.'), v:false, tag)
endfunction
function! vista#cursor#ShowDetailWithDelay() abort
call s:StopCursorTimer()
let s:cursor_timer = timer_start(
\ g:vista_cursor_delay,
\ function('vista#cursor#ShowDetail'),
\ )
endfunction
" Show the detail of current tag/symbol under cursor.
function! vista#cursor#FindDetail(_timer) abort
if empty(getline('.'))
\ || !exists('g:vista')
\ || !has_key(g:vista, 'functions')
\ || !has_key(g:vista, 'source')
call setbufvar(g:vista.source.bufnr, 'vista_cursor_info', {})
return
endif
let found = vista#util#BinarySearch(g:vista.raw, line('.'), 'line', '')
if empty(found)
let found = {}
endif
"echo found
let scope = vista#util#Trim(get(found, 'scope', ''))
if !empty(scope)
let scopeKind = get(found, 'scopeKind', '')
if !empty(scopeKind)
let scope = scopeKind.' '.scope
endif
let access = get(found, 'access', '')
if !empty(access)
let scope = scope.' '.access
endif
else
let scope = vista#util#Trim(get(found, 'namespace', '').' '.get(found, 'class', ''))
endif
let symbolname = vista#util#Trim(get(found, 'name', ''))
call setbufvar(g:vista.source.bufnr, 'vista_cursor_info', {'scope': scope, 'symbol': symbolname})
endfunction
function! vista#cursor#FindDetailWithDelay() abort
call s:StopFindDetailTimer()
let s:find_detail_timer = timer_start(
\ g:vista_update_scope_delay,
\ function('vista#cursor#FindDetail'),
\ )
endfunction
" This happens on calling `:Vista show` but the vista window is still invisible.
function! vista#cursor#ShowTagFor(lnum) abort
if !s:HasVlnum()
return
endif
let found = vista#util#BinarySearch(g:vista.raw, a:lnum, 'line', '')
if empty(found)
return
endif
let s:vlnum = get(found, 'vlnum', v:null)
if empty(s:vlnum)
return
endif
let tag = get(found, 'name', v:null)
call vista#highlight#Add(s:vlnum, v:true, tag)
endfunction
function! vista#cursor#ShowTag() abort
if !s:HasVlnum()
return
endif
let s:vlnum = vista#util#BinarySearch(g:vista.raw, line('.'), 'line', 'vlnum')
if empty(s:vlnum)
return
endif
let winnr = g:vista.winnr()
if winnr() != winnr
execute winnr.'wincmd w'
endif
call cursor(s:vlnum, 1)
normal! zz
endfunction
" Extract the line number from last section of cursor line in the vista window
function! s:GetTrailingLnum() abort
return str2nr(matchstr(getline('.'), '\d\+$'))
endfunction
function! vista#cursor#TogglePreview() abort
if get(g:vista, 'floating_visible', v:false)
\ || get(g:vista, 'popup_visible', v:false)
call vista#win#CloseFloating()
return
endif
let [tag, source_line] = s:GetInfoUnderCursor()
if empty(tag) || empty(source_line)
echo "\r"
return
endif
let lnum = s:GetTrailingLnum()
call vista#win#FloatingDisplay(lnum, tag)
endfunction