Skip to content

Commit 87c7d49

Browse files
committed
feat(files): add content.highlight to customize entry highlighting
Resolve #1790
1 parent cf32454 commit 87c7d49

File tree

7 files changed

+152
-8
lines changed

7 files changed

+152
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ There are following change types:
105105

106106
- Ensure preview window is never hidden, even if cursor is on the line for a not (yet) existing file system entry. This reduces flickering of preview window when creating new files in Insert mode.
107107

108+
### Expand
109+
110+
- Add `config.content.highlight` to customize how file system entry is highlighted. Defaults to a new `default_highlight()`.
111+
108112
## mini.hues
109113

110114
### Evolve

doc/mini-files.txt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,11 @@ Defaults ~
567567
content = {
568568
-- Predicate for which file system entries to show
569569
filter = nil,
570-
-- What prefix to show to the left of file system entry
570+
-- Highlight group to use for a file system entry
571+
highlight = nil,
572+
-- Prefix text and highlight to show to the left of file system entry
571573
prefix = nil,
572-
-- In which order to show file system entries
574+
-- Order in which to show file system entries
573575
sort = nil,
574576
},
575577

@@ -625,6 +627,10 @@ A file system entry data is a table with the following fields:
625627
- <name> `(string)` - basename of an entry (including extension).
626628
- <path> `(string)` - full path of an entry.
627629

630+
`content.highlight` describes how file system entry name should be highlighted.
631+
Takes file system entry data as input and returns a highlight group name.
632+
Uses |MiniFiles.default_highlight()| by default.
633+
628634
`content.prefix` describes what text (prefix) to show to the left of file
629635
system entry name (if any) and how to highlight it. It also takes file
630636
system entry data as input and returns tuple of text and highlight group
@@ -968,5 +974,21 @@ Parameters ~
968974
Return ~
969975
`(table)` Sorted array of file system entries.
970976

977+
------------------------------------------------------------------------------
978+
*MiniFiles.default_highlight()*
979+
`MiniFiles.default_highlight`({fs_entry})
980+
Default file system entry highlight
981+
982+
Returns `'MiniFilesDirectory'` for directory and `'MiniFilesFile'` otherwise.
983+
984+
Parameters ~
985+
{fs_entry} `(table)` Table with the following fields:
986+
- <fs_type> `(string)` - one of "file" or "directory".
987+
- <name> `(string)` - basename of an entry (including extension).
988+
- <path> `(string)` - full path of an entry.
989+
990+
Return ~
991+
`(string)` Highlight group name.
992+
971993

972994
vim:tw=78:ts=8:noet:ft=help:norl:

lua/mini/files.lua

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,10 @@ end
591591
--- A file system entry data is a table with the following fields:
592592
--- __minifiles_fs_entry_data_fields
593593
---
594+
--- `content.highlight` describes how file system entry name should be highlighted.
595+
--- Takes file system entry data as input and returns a highlight group name.
596+
--- Uses |MiniFiles.default_highlight()| by default.
597+
---
594598
--- `content.prefix` describes what text (prefix) to show to the left of file
595599
--- system entry name (if any) and how to highlight it. It also takes file
596600
--- system entry data as input and returns tuple of text and highlight group
@@ -661,9 +665,11 @@ MiniFiles.config = {
661665
content = {
662666
-- Predicate for which file system entries to show
663667
filter = nil,
664-
-- What prefix to show to the left of file system entry
668+
-- Highlight group to use for a file system entry
669+
highlight = nil,
670+
-- Prefix text and highlight to show to the left of file system entry
665671
prefix = nil,
666-
-- In which order to show file system entries
672+
-- Order in which to show file system entries
667673
sort = nil,
668674
},
669675

@@ -1223,6 +1229,18 @@ MiniFiles.default_sort = function(fs_entries)
12231229
return vim.tbl_map(function(x) return { name = x.name, fs_type = x.fs_type, path = x.path } end, res)
12241230
end
12251231

1232+
--- Default file system entry highlight
1233+
---
1234+
--- Returns `'MiniFilesDirectory'` for directory and `'MiniFilesFile'` otherwise.
1235+
---
1236+
---@param fs_entry table Table with the following fields:
1237+
--- __minifiles_fs_entry_data_fields
1238+
---
1239+
---@return string Highlight group name.
1240+
MiniFiles.default_highlight = function(fs_entry)
1241+
return fs_entry.fs_type == 'directory' and 'MiniFilesDirectory' or 'MiniFilesFile'
1242+
end
1243+
12261244
-- Helper data ================================================================
12271245
-- Module default config
12281246
H.default_config = vim.deepcopy(MiniFiles.config)
@@ -1270,6 +1288,7 @@ H.setup_config = function(config)
12701288

12711289
H.check_type('content', config.content, 'table')
12721290
H.check_type('content.filter', config.content.filter, 'function', true)
1291+
H.check_type('content.highlight', config.content.highlight, 'function', true)
12731292
H.check_type('content.prefix', config.content.prefix, 'function', true)
12741293
H.check_type('content.sort', config.content.sort, 'function', true)
12751294

@@ -1348,6 +1367,7 @@ end
13481367
H.normalize_opts = function(explorer_opts, opts)
13491368
opts = vim.tbl_deep_extend('force', H.get_config(), explorer_opts or {}, opts or {})
13501369
opts.content.filter = opts.content.filter or MiniFiles.default_filter
1370+
opts.content.highlight = opts.content.highlight or MiniFiles.default_highlight
13511371
opts.content.prefix = opts.content.prefix or MiniFiles.default_prefix
13521372
opts.content.sort = opts.content.sort or MiniFiles.default_sort
13531373

@@ -2241,7 +2261,8 @@ H.buffer_update_directory = function(buf_id, path, opts, is_preview)
22412261

22422262
-- Compute lines
22432263
local lines, icon_hl, name_hl = {}, {}, {}
2244-
local prefix_fun, n_computed_prefixes = opts.content.prefix, is_preview and vim.o.lines or math.huge
2264+
local prefix_fun, hl_fun = opts.content.prefix, opts.content.highlight
2265+
local n_computed_prefixes = is_preview and vim.o.lines or math.huge
22452266
for i, entry in ipairs(fs_entries) do
22462267
local prefix, hl, name
22472268
-- Compute prefix only in visible preview (for performance).
@@ -2253,7 +2274,7 @@ H.buffer_update_directory = function(buf_id, path, opts, is_preview)
22532274
prefix, hl, name = prefix or '', hl or '', H.sanitize_string(entry.name)
22542275
table.insert(lines, string.format(line_format, H.path_index[entry.path], prefix, name))
22552276
table.insert(icon_hl, hl)
2256-
table.insert(name_hl, entry.fs_type == 'directory' and 'MiniFilesDirectory' or 'MiniFilesFile')
2277+
table.insert(name_hl, hl_fun(entry) or 'MiniFilesNormal')
22572278
end
22582279

22592280
-- Set lines

readmes/mini-files.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,11 @@ Here are code snippets for some common installation methods (use only one):
172172
content = {
173173
-- Predicate for which file system entries to show
174174
filter = nil,
175-
-- What prefix to show to the left of file system entry
175+
-- Highlight group to use for a file system entry
176+
highlight = nil,
177+
-- Prefix text and highlight to show to the left of file system entry
176178
prefix = nil,
177-
-- In which order to show file system entries
179+
-- Order in which to show file system entries
178180
sort = nil,
179181
},
180182

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--|---------|---------|---------|---------|---------|---------|---------|---------|
2+
01|┌MOCK_ROOT/tests/dir-files/common ─────────────────┐
3+
02|│ .a-dir │
4+
03|│ a-dir │
5+
04|│ b-dir │
6+
05|│ .a-file │
7+
06|│ a-file │
8+
07|│ A-file-2 │
9+
08|│ b-file │
10+
09|└──────────────────────────────────────────────────┘
11+
10|~
12+
11|~
13+
12|~
14+
13|~
15+
14|~
16+
15| 1,9-7 All
17+
18+
--|---------|---------|---------|---------|---------|---------|---------|---------|
19+
01|01111111111111111111111111111111110000000000000000002222222222222222222222222222
20+
02|03344444455555555555555555555555555555555555555555506666666666666666666666666666
21+
03|07744444444444444444444444444444444444444444444444406666666666666666666666666666
22+
04|07744444444444444444444444444444444444444444444444406666666666666666666666666666
23+
05|04444444444444444444444444444444444444444444444444406666666666666666666666666666
24+
06|04444444444444444444444444444444444444444444444444406666666666666666666666666666
25+
07|04444444444444444444444444444444444444444444444444406666666666666666666666666666
26+
08|04444444444444444444444444444444444444444444444444406666666666666666666666666666
27+
09|00000000000000000000000000000000000000000000000000006666666666666666666666666666
28+
10|66666666666666666666666666666666666666666666666666666666666666666666666666666666
29+
11|66666666666666666666666666666666666666666666666666666666666666666666666666666666
30+
12|66666666666666666666666666666666666666666666666666666666666666666666666666666666
31+
13|66666666666666666666666666666666666666666666666666666666666666666666666666666666
32+
14|66666666666666666666666666666666666666666666666666666666666666666666666666666666
33+
15|22222222222222222222222222222222222222222222222222222222222222222222222222222222
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--|---------|---------|---------|---------|---------|---------|---------|---------|
2+
01|┌MOCK_ROOT/tests/dir-files/common ─────────────────┐
3+
02|│ .a-dir │
4+
03|│ a-dir │
5+
04|│ b-dir │
6+
05|│ .a-file │
7+
06|│ a-file │
8+
07|│ A-file-2 │
9+
08|│ b-file │
10+
09|└──────────────────────────────────────────────────┘
11+
10|~
12+
11|~
13+
12|~
14+
13|~
15+
14|~
16+
15| 1,9-7 All
17+
18+
--|---------|---------|---------|---------|---------|---------|---------|---------|
19+
01|01111111111111111111111111111111110000000000000000002222222222222222222222222222
20+
02|03344444455555555555555555555555555555555555555555506666666666666666666666666666
21+
03|07788888999999999999999999999999999999999999999999906666666666666666666666666666
22+
04|077:::::999999999999999999999999999999999999999999906666666666666666666666666666
23+
05|099:::::::9999999999999999999999999999999999999999906666666666666666666666666666
24+
06|09988888899999999999999999999999999999999999999999906666666666666666666666666666
25+
07|099::::::::999999999999999999999999999999999999999906666666666666666666666666666
26+
08|099::::::99999999999999999999999999999999999999999906666666666666666666666666666
27+
09|00000000000000000000000000000000000000000000000000006666666666666666666666666666
28+
10|66666666666666666666666666666666666666666666666666666666666666666666666666666666
29+
11|66666666666666666666666666666666666666666666666666666666666666666666666666666666
30+
12|66666666666666666666666666666666666666666666666666666666666666666666666666666666
31+
13|66666666666666666666666666666666666666666666666666666666666666666666666666666666
32+
14|66666666666666666666666666666666666666666666666666666666666666666666666666666666
33+
15|22222222222222222222222222222222222222222222222222222222222222222222222222222222

tests/test_files.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ T['setup()']['creates `config` field'] = function()
238238
local expect_config = function(field, value) eq(child.lua_get('MiniFiles.config.' .. field), value) end
239239

240240
expect_config('content.filter', vim.NIL)
241+
expect_config('content.highlight', vim.NIL)
241242
expect_config('content.prefix', vim.NIL)
242243
expect_config('content.sort', vim.NIL)
243244

@@ -279,6 +280,7 @@ T['setup()']['validates `config` argument'] = function()
279280
expect_config_error('a', 'config', 'table')
280281
expect_config_error({ content = 'a' }, 'content', 'table')
281282
expect_config_error({ content = { filter = 1 } }, 'content.filter', 'function')
283+
expect_config_error({ content = { highlight = 1 } }, 'content.highlight', 'function')
282284
expect_config_error({ content = { prefix = 1 } }, 'content.prefix', 'function')
283285
expect_config_error({ content = { sort = 1 } }, 'content.sort', 'function')
284286

@@ -646,6 +648,33 @@ T['open()']['respects `content.filter`'] = function()
646648
child.expect_screenshot()
647649
end
648650

651+
T['open()']['respects `content.highlight`'] = function()
652+
child.lua([[
653+
_G.highlight_arg = {}
654+
MiniFiles.config.content.highlight = function(fs_entry)
655+
_G.highlight_arg = fs_entry
656+
-- Should use 'MiniFilesNormal' as a fallback
657+
return nil
658+
end
659+
]])
660+
661+
open(test_dir_path)
662+
child.expect_screenshot()
663+
validate_fs_entry(child.lua_get('_G.highlight_arg'))
664+
665+
-- Local value from argument should take precedence
666+
child.lua([[_G.highlight_starts_from_a = function(fs_entry)
667+
return vim.startswith(fs_entry.name, 'a') and 'String' or 'Comment'
668+
end]])
669+
670+
local lua_cmd = string.format(
671+
'MiniFiles.open(%s, false, { content = { highlight = _G.highlight_starts_from_a } })',
672+
vim.inspect(test_dir_path)
673+
)
674+
child.lua(lua_cmd)
675+
child.expect_screenshot()
676+
end
677+
649678
T['open()']['respects `content.prefix`'] = function()
650679
child.set_size(15, 60)
651680

0 commit comments

Comments
 (0)