Skip to content

Commit 5d1d1ef

Browse files
authored
feat(icons): use virtual text if possible #255
Closes #145 Closes #253
1 parent c87a8e1 commit 5d1d1ef

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

autoload/dirvish.vim

+24-5
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,20 @@ func! s:apply_icons() abort
426426
if 0 == len(s:cb_map)
427427
return
428428
endif
429-
highlight clear Conceal
429+
let l:feat = has('nvim-0.8') ? 'extmark' : ((v:version >= 901 && has('textprop'))? 'textprop': 'conceal')
430+
if l:feat ==# 'extmark'
431+
if !exists('s:ns_id')
432+
let s:ns_id = nvim_create_namespace('dirvish.icons')
433+
endif
434+
elseif l:feat ==# 'textprop'
435+
if !exists('s:prop_type')
436+
let s:prop_type = 'dirvish.icons'
437+
call prop_type_add(s:prop_type, {})
438+
endif
439+
else
440+
highlight clear Conceal
441+
endif
442+
430443
let i = 0
431444
for f in getline(1, '$')
432445
let i += 1
@@ -438,10 +451,16 @@ func! s:apply_icons() abort
438451
endif
439452
endfor
440453
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
454+
if l:feat ==# 'extmark'
455+
call nvim_buf_set_extmark(0, s:ns_id, i-1, 0, #{virt_text: [[icon, 'DirvishColumnHead']], virt_text_pos: 'inline'})
456+
elseif l:feat ==# 'textprop'
457+
call prop_add(i, 1, #{type: s:prop_type, text: icon})
458+
else
459+
let isdir = (f[-1:] == s:sep)
460+
let f = substitute(s:f(f), escape(s:sep,'\').'$', '', 'g') " Full path, trim slash.
461+
let tail_esc = escape(fnamemodify(f,':t').(isdir?(s:sep):''), '[,*.^$~\')
462+
exe 'syntax match DirvishColumnHead =\%'.i.'l^.\{-}\ze'.tail_esc.'$= conceal cchar='.icon
463+
endif
445464
endif
446465
endfor
447466
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)