Skip to content

Commit fc6b544

Browse files
committed
feat(icons): use virtual text if possible (has('nvim') or has('textprop'))
Closes justinmk#145, justinmk#253
1 parent c87a8e1 commit fc6b544

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

autoload/dirvish.vim

+23-5
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,19 @@ func! s:apply_icons() abort
426426
if 0 == len(s:cb_map)
427427
return
428428
endif
429-
highlight clear Conceal
429+
if has('nvim')
430+
if !exists('s:ns_id')
431+
let s:ns_id = nvim_create_namespace('dirvish.icons')
432+
endif
433+
elseif v:version >= 901 && has('textprop')
434+
if !exists('s:prop_type')
435+
let s:prop_type = 'dirvish.icons'
436+
call prop_type_add(s:prop_type, {})
437+
endif
438+
else
439+
highlight clear Conceal
440+
endif
441+
430442
let i = 0
431443
for f in getline(1, '$')
432444
let i += 1
@@ -438,10 +450,16 @@ func! s:apply_icons() abort
438450
endif
439451
endfor
440452
if icon != ''
441-
let isdir = (f[-1:] == s:sep)
442-
let f = substitute(s:f(f), escape(s:sep,'\').'$', '', 'g') " Full path, trim slash.
443-
let tail_esc = escape(fnamemodify(f,':t').(isdir?(s:sep):''), '[,*.^$~\')
444-
exe 'syntax match DirvishColumnHead =\%'.i.'l^.\{-}\ze'.tail_esc.'$= conceal cchar='.icon
453+
if has('nvim-0.8')
454+
call nvim_buf_set_extmark(0, s:ns_id, i-1, 0, #{virt_text: [[icon, 'DirvishColumnHead']], virt_text_pos: 'inline'})
455+
elseif v:version >= 901 && has('textprop')
456+
call prop_add(i, 1, #{type: s:prop_type, text: icon})
457+
else
458+
let isdir = (f[-1:] == s:sep)
459+
let f = substitute(s:f(f), escape(s:sep,'\').'$', '', 'g') " Full path, trim slash.
460+
let tail_esc = escape(fnamemodify(f,':t').(isdir?(s:sep):''), '[,*.^$~\')
461+
exe 'syntax match DirvishColumnHead =\%'.i.'l^.\{-}\ze'.tail_esc.'$= conceal cchar='.icon
462+
endif
445463
endif
446464
endfor
447465
endf

doc/dirvish.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,12 @@ dirvish#add_icon_fn(fn)
114114
a given path, wins. Best practice: if you don't have anything meaningful
115115
to show for a given path, return empty string (or whitespace).
116116

117-
{fn} is any |Funcref| that takes a path (string) and returns a single
118-
character (the "icon"). Example: >
117+
{fn} is any |Funcref| that takes a path (string) and returns a string
118+
(the "icon"). Example: >vim
119119
call dirvish#add_icon_fn({p -> p[-1:]=='/'?'📂':'📄'})
120120
<
121+
Note: multi-character icons are only supported on Nvim 0.8+ or Vim 9.1+
122+
with |+textprop|.
121123

122124
*dirvish#remove_icon_fn()*
123125
dirvish#remove_icon_fn(fn_id)

0 commit comments

Comments
 (0)