Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8174cf1

Browse files
committedFeb 7, 2025
feat(icons): Add support for highlight groups in icons in Vim
1 parent 0e820f1 commit 8174cf1

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed
 

‎.editorconfig

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[*]
2+
indent_style = space
3+
indent_size = 2
4+
tab_width = 2

‎.gitignore

-1
This file was deleted.

‎autoload/dirvish.vim

+16-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ let s:noswapfile = (2 == exists(':noswapfile')) ? 'noswapfile' : ''
44
let s:noau = 'silent noautocmd keepjumps'
55
let s:cb_map = {} " callback map
66
let s:rel = get(g:, 'dirvish_relative_paths', 0)
7+
let s:virttext_feat = has('nvim-0.8') ? 'extmark' : ((v:version >= 901 && has('textprop'))? 'textprop': 'conceal')
78

89
" Debug:
910
" echo '' > dirvish.log ; tail -F dirvish.log
@@ -426,9 +427,6 @@ func! s:apply_icons() abort
426427
if 0 == len(s:cb_map)
427428
return
428429
endif
429-
if !exists('s:virttext_feat')
430-
let s:virttext_feat = has('nvim-0.8') ? 'extmark' : ((v:version >= 901 && has('textprop'))? 'textprop': 'conceal')
431-
endif
432430
if s:virttext_feat ==# 'extmark'
433431
if !exists('s:ns_id')
434432
let s:ns_id = nvim_create_namespace('dirvish.icons')
@@ -446,29 +444,36 @@ func! s:apply_icons() abort
446444
for f in getline(1, '$')
447445
let i += 1
448446
for id in sort(keys(s:cb_map))
449-
let l:icon = s:cb_map[id](f)
450-
if type(l:icon) == v:t_string
451-
if -1 != match(l:icon, '\S')
447+
let icon = s:cb_map[id](f)
448+
if type(icon) == type('')
449+
if -1 != match(icon, '\S')
450+
unlet icon
452451
break
453452
endif
454453
endif
455454
endfor
456455
if exists('l:icon')
457456
if s:virttext_feat ==# 'extmark'
458-
let l:virt_text = type(l:icon) == v:t_dict ? [[l:icon.icon, l:icon.hl]] : [[l:icon, 'DirvishColumnHead']]
457+
let l:virt_text = type(icon) == type({}) ? [[icon.icon, icon.hl]] : [[icon, 'DirvishColumnHead']]
459458
call nvim_buf_set_extmark(0, s:ns_id, i-1, 0, #{virt_text: l:virt_text, virt_text_pos: 'inline'})
460459
elseif s:virttext_feat ==# 'textprop'
461-
if type(l:icon) == v:t_string
462-
call prop_add(i, 1, #{type: s:prop_type, text: l:icon})
460+
if type(icon) == type('')
461+
call prop_add(i, 1, #{type: s:prop_type, text: icon})
462+
else
463+
let l:prop_type = prop_type_get('dirvish.'.icon.hl, {})
464+
if l:prop_type == {}
465+
call prop_type_add('dirvish.'.icon.hl, #{highlight: icon.hl})
466+
endif
467+
call prop_add(i, 1, #{type: 'dirvish.'.icon.hl, text: icon.icon})
463468
endif
464469
else
465470
let isdir = (f[-1:] == s:sep)
466471
let f = substitute(s:f(f), escape(s:sep,'\').'$', '', 'g') " Full path, trim slash.
467472
let tail_esc = escape(fnamemodify(f,':t').(isdir?(s:sep):''), '[,*.^$~\')
468-
exe 'syntax match DirvishColumnHead =\%'.i.'l^.\{-}\ze'.tail_esc.'$= conceal cchar='.l:icon
473+
exe 'syntax match DirvishColumnHead =\%'.i.'l^.\{-}\ze'.tail_esc.'$= conceal cchar='.icon
469474
endif
475+
unlet icon
470476
endif
471-
unlet l:icon
472477
endfor
473478
endf
474479

‎doc/dirvish.txt

+19-4
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,27 @@ 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 string
118-
(the "icon"). Example: >vim
117+
{fn} is any |Funcref| that takes a path (string) and returns an `icon-item` as
118+
a |string| or a |dict| with the following keys:
119+
- "icon" (|string|): the string representing the icon.
120+
- "hl" (|string|): the highlight group to use for the icon.
121+
122+
Note: If your Vim isn't Vim 9.1+ with |+textprop| or Nvim 0.8+, `icon-item`
123+
can only be a character.
124+
125+
Example that returns a string: >vim
119126
call dirvish#add_icon_fn({p -> p[-1:]=='/'?'📂':'📄'})
120127
<
121-
Note: multi-character icons are only supported on Nvim 0.8+ or Vim 9.1+
122-
with |+textprop|.
128+
Example that uses |mini.icons| <https://github.com/echasnovski/mini.icons>
129+
0.15.0 as icon provider >lua
130+
--- NOTE: mini.icons must be loaded before vim-dirvish
131+
vim.fn['dirvish#add_icon_fn'](function(p)
132+
local get = require('mini.icons').get
133+
local icon, hl = get(p:sub(-1) == '/' and 'directory' or 'file', p)
134+
icon = icon .. ' '
135+
return { icon = icon, hl = hl }
136+
end)
137+
<
123138

124139
*dirvish#remove_icon_fn()*
125140
dirvish#remove_icon_fn(fn_id)

0 commit comments

Comments
 (0)
Please sign in to comment.