Skip to content

Commit d1d5d06

Browse files
author
skywind3000
committed
new: quickui#tools#preview_tag
1 parent de10502 commit d1d5d06

File tree

4 files changed

+108
-3
lines changed

4 files changed

+108
-3
lines changed

autoload/quickui/core.vim

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ endfunc
213213

214214

215215
"----------------------------------------------------------------------
216-
" object
216+
" tabpage instance
217217
"----------------------------------------------------------------------
218218
function! quickui#core#instance()
219219
if exists('t:__quickui__')
@@ -224,6 +224,18 @@ function! quickui#core#instance()
224224
endfunc
225225

226226

227+
"----------------------------------------------------------------------
228+
" buffer instance
229+
"----------------------------------------------------------------------
230+
function! quickui#core#object()
231+
if exists('b:__quickui__')
232+
return b:__quickui__
233+
endif
234+
let b:__quickui__ = {}
235+
return b:__quickui__
236+
endfunc
237+
238+
227239
"----------------------------------------------------------------------
228240
" object cache: acquire
229241
"----------------------------------------------------------------------

autoload/quickui/style.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let g:quickui#style#border = get(g:, 'quickui#style#border', 1)
1414

1515
let g:quickui#style#tip_head = '[tip]'
1616

17-
let g:quickui#style#preview_w = 90
17+
let g:quickui#style#preview_w = 85
1818
let g:quickui#style#preview_h = 10
1919
let g:quickui#style#preview_number = 1
2020

autoload/quickui/tools.vim

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,64 @@ function! quickui#tools#preview_quickfix(...)
269269
endfunc
270270

271271

272+
"----------------------------------------------------------------------
273+
" preview tag
274+
"----------------------------------------------------------------------
275+
function! quickui#tools#preview_tag(tagname)
276+
let tagname = (a:tagname == '')? expand('<cword>') : a:tagname
277+
if tagname == ''
278+
call quickui#utils#errmsg('Error: empty tagname')
279+
return 0
280+
endif
281+
let obj = quickui#core#object()
282+
let reuse = 0
283+
if has_key(obj, 'ptag')
284+
let ptag = obj.ptag
285+
if get(ptag, 'tagname', '') == tagname
286+
let reuse = 1
287+
endif
288+
endif
289+
if reuse == 0
290+
let obj.ptag = {}
291+
let ptag = obj.ptag
292+
let ptag.taglist = quickui#tags#tagfind(tagname)
293+
let ptag.tagname = tagname
294+
let ptag.index = 0
295+
else
296+
let ptag = obj.ptag
297+
let ptag.index += 1
298+
if ptag.index >= len(ptag.taglist)
299+
let ptag.index = 0
300+
endif
301+
endif
302+
if len(ptag.taglist) == 0
303+
call quickui#utils#errmsg('E257: preview: tag not find "' . a:tagname . '"')
304+
return 1
305+
endif
306+
if ptag.index >= len(ptag.taglist) || ptag.index < 0
307+
let ptag.index = 0
308+
endif
309+
let taginfo = ptag.taglist[ptag.index]
310+
let filename = taginfo.filename
311+
if !filereadable(filename)
312+
call quickui#utils#errmsg('E484: Can not open file '.filename)
313+
return 2
314+
endif
315+
if !has_key(taginfo, 'line')
316+
call quickui#utils#errmsg('Error: no "line" information in your tags, regenerate with -n')
317+
return 3
318+
endif
319+
call quickui#preview#open(filename, taginfo.line)
320+
let text = taginfo.name
321+
let text.= ' ('.(ptag.index + 1).'/'.len(ptag.taglist).') '
322+
let text.= filename
323+
if has_key(taginfo, 'line')
324+
let text .= ':'.taginfo.line
325+
endif
326+
call quickui#utils#print(text, 1)
327+
return 0
328+
endfunc
329+
272330

273331
"----------------------------------------------------------------------
274332
" display vim help in popup
@@ -316,3 +374,4 @@ function! quickui#tools#display_help(tag)
316374
endfunc
317375

318376

377+

autoload/quickui/utils.vim

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,45 @@ endfunc
229229
function! quickui#utils#errmsg(what)
230230
redraw
231231
echohl ErrorMsg
232-
echo a:what
232+
echom a:what
233233
echohl None
234234
endfunc
235235

236236

237+
"----------------------------------------------------------------------
238+
" safe print
239+
"----------------------------------------------------------------------
240+
function! quickui#utils#print(content, highlight, ...)
241+
let saveshow = &showmode
242+
set noshowmode
243+
let wincols = &columns
244+
let statusline = (&laststatus==1 && winnr('$')>1) || (&laststatus==2)
245+
let reqspaces_lastline = (statusline || !&ruler) ? 12 : 29
246+
let width = len(a:content)
247+
let limit = wincols - reqspaces_lastline
248+
let l:content = a:content
249+
if width + 1 > limit
250+
let l:content = strpart(l:content, 0, limit - 1)
251+
let width = len(l:content)
252+
endif
253+
" prevent scrolling caused by multiple echo
254+
let needredraw = (a:0 >= 1)? a:1 : 1
255+
if needredraw != 0
256+
redraw
257+
endif
258+
if a:highlight != 0
259+
echohl Type
260+
echo l:content
261+
echohl NONE
262+
else
263+
echo l:content
264+
endif
265+
if saveshow != 0
266+
set showmode
267+
endif
268+
endfunc
269+
270+
237271
"----------------------------------------------------------------------
238272
" max height
239273
"----------------------------------------------------------------------

0 commit comments

Comments
 (0)