Skip to content

feat(icons): use virtual text if possible #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions autoload/dirvish.vim
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,20 @@ func! s:apply_icons() abort
if 0 == len(s:cb_map)
return
endif
highlight clear Conceal
let l:feat = has('nvim-0.8') ? 'extmark' : ((v:version >= 901 && has('textprop'))? 'textprop': 'conceal')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be a script-level var.

if l:feat ==# 'extmark'
if !exists('s:ns_id')
let s:ns_id = nvim_create_namespace('dirvish.icons')
endif
elseif l:feat ==# 'textprop'
if !exists('s:prop_type')
let s:prop_type = 'dirvish.icons'
call prop_type_add(s:prop_type, {})
endif
else
highlight clear Conceal
endif

let i = 0
for f in getline(1, '$')
let i += 1
Expand All @@ -438,10 +451,16 @@ func! s:apply_icons() abort
endif
endfor
if icon != ''
let isdir = (f[-1:] == s:sep)
let f = substitute(s:f(f), escape(s:sep,'\').'$', '', 'g') " Full path, trim slash.
let tail_esc = escape(fnamemodify(f,':t').(isdir?(s:sep):''), '[,*.^$~\')
exe 'syntax match DirvishColumnHead =\%'.i.'l^.\{-}\ze'.tail_esc.'$= conceal cchar='.icon
if l:feat ==# 'extmark'
call nvim_buf_set_extmark(0, s:ns_id, i-1, 0, #{virt_text: [[icon, 'DirvishColumnHead']], virt_text_pos: 'inline'})
elseif l:feat ==# 'textprop'
call prop_add(i, 1, #{type: s:prop_type, text: icon})
else
let isdir = (f[-1:] == s:sep)
let f = substitute(s:f(f), escape(s:sep,'\').'$', '', 'g') " Full path, trim slash.
let tail_esc = escape(fnamemodify(f,':t').(isdir?(s:sep):''), '[,*.^$~\')
exe 'syntax match DirvishColumnHead =\%'.i.'l^.\{-}\ze'.tail_esc.'$= conceal cchar='.icon
endif
endif
endfor
endf
Expand Down
6 changes: 4 additions & 2 deletions doc/dirvish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ dirvish#add_icon_fn(fn)
a given path, wins. Best practice: if you don't have anything meaningful
to show for a given path, return empty string (or whitespace).

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

*dirvish#remove_icon_fn()*
dirvish#remove_icon_fn(fn_id)
Expand Down