Skip to content

Commit e05b32d

Browse files
committed
feat(icons): Add support for highlight groups in icons in Vim
1 parent 0e820f1 commit e05b32d

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
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

+8-1
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ func! s:apply_icons() abort
449449
let l:icon = s:cb_map[id](f)
450450
if type(l:icon) == v:t_string
451451
if -1 != match(l:icon, '\S')
452+
unlet l:icon
452453
break
453454
endif
454455
endif
@@ -460,15 +461,21 @@ func! s:apply_icons() abort
460461
elseif s:virttext_feat ==# 'textprop'
461462
if type(l:icon) == v:t_string
462463
call prop_add(i, 1, #{type: s:prop_type, text: l:icon})
464+
else
465+
let l:prop_type = prop_type_get('dirvish.'.l:icon.hl, {})
466+
if l:prop_type == {}
467+
call prop_type_add('dirvish.'.l:icon.hl, #{highlight: l:icon.hl})
468+
endif
469+
call prop_add(i, 1, #{type: 'dirvish.'.l:icon.hl, text: l:icon.icon})
463470
endif
464471
else
465472
let isdir = (f[-1:] == s:sep)
466473
let f = substitute(s:f(f), escape(s:sep,'\').'$', '', 'g') " Full path, trim slash.
467474
let tail_esc = escape(fnamemodify(f,':t').(isdir?(s:sep):''), '[,*.^$~\')
468475
exe 'syntax match DirvishColumnHead =\%'.i.'l^.\{-}\ze'.tail_esc.'$= conceal cchar='.l:icon
469476
endif
477+
unlet l:icon
470478
endif
471-
unlet l:icon
472479
endfor
473480
endf
474481

doc/dirvish.txt

+24-4
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,32 @@ 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
134+
if p:sub(-1) == '/' then
135+
icon, hl = get('directory', p)
136+
else
137+
icon, hl = get('file', p)
138+
end
139+
icon = icon .. ' '
140+
return { icon = icon, hl = hl }
141+
end)
142+
<
123143

124144
*dirvish#remove_icon_fn()*
125145
dirvish#remove_icon_fn(fn_id)

0 commit comments

Comments
 (0)