From 5d1d1ef45c6161af14c7d3d3097057e8fb5ac257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=E1=BA=A1m=20B=C3=ACnh=20An?= <111893501+brianhuster@users.noreply.github.com> Date: Thu, 6 Feb 2025 23:43:43 +0700 Subject: [PATCH] feat(icons): use virtual text if possible #255 Closes #145 Closes #253 --- autoload/dirvish.vim | 29 ++++++++++++++++++++++++----- doc/dirvish.txt | 6 ++++-- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/autoload/dirvish.vim b/autoload/dirvish.vim index fe310ea..10d0149 100644 --- a/autoload/dirvish.vim +++ b/autoload/dirvish.vim @@ -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') + 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 @@ -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 diff --git a/doc/dirvish.txt b/doc/dirvish.txt index 6253f70..56fed1b 100644 --- a/doc/dirvish.txt +++ b/doc/dirvish.txt @@ -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)