Skip to content

Commit 99ce32f

Browse files
Foo-xstevearc
andauthored
feat: config option to customize filename highlight group (#508)
* feat: highlight config Refs #402 * perf: minimize perf impact when option not provided * doc: regenerate documentation * fix: symbolic link rendering * refactor: simplify conditional --------- Co-authored-by: Steven Arcangeli <[email protected]>
1 parent 60e6896 commit 99ce32f

File tree

4 files changed

+76
-16
lines changed

4 files changed

+76
-16
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ require("oil").setup({
232232
{ "type", "asc" },
233233
{ "name", "asc" },
234234
},
235+
-- Customize the highlight group for the file name
236+
highlight_filename = function(entry, is_hidden, is_link_target, is_link_orphan)
237+
return nil
238+
end,
235239
},
236240
-- Extra arguments to pass to SCP when moving/copying files over SSH
237241
extra_scp_args = {},

doc/oil.txt

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ CONFIG *oil-confi
117117
{ "type", "asc" },
118118
{ "name", "asc" },
119119
},
120+
-- Customize the highlight group for the file name
121+
highlight_filename = function(entry, is_hidden, is_link_target, is_link_orphan)
122+
return nil
123+
end,
120124
},
121125
-- Extra arguments to pass to SCP when moving/copying files over SSH
122126
extra_scp_args = {},

lua/oil/config.lua

+9
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ local default_config = {
102102
{ "type", "asc" },
103103
{ "name", "asc" },
104104
},
105+
-- Customize the highlight group for the file name
106+
highlight_filename = function(entry, is_hidden, is_link_target, is_link_orphan)
107+
return nil
108+
end,
105109
},
106110
-- Extra arguments to pass to SCP when moving/copying files over SSH
107111
extra_scp_args = {},
@@ -207,6 +211,9 @@ default_config.adapters = {
207211
["oil-trash://"] = "trash",
208212
}
209213
default_config.adapter_aliases = {}
214+
-- We want the function in the default config for documentation generation, but if we nil it out
215+
-- here we can get some performance wins
216+
default_config.view_options.highlight_filename = nil
210217

211218
---@class oil.Config
212219
---@field adapters table<string, string> Hidden from SetupOpts
@@ -281,6 +288,7 @@ local M = {}
281288
---@field natural_order boolean|"fast"
282289
---@field case_insensitive boolean
283290
---@field sort oil.SortSpec[]
291+
---@field highlight_filename? fun(entry: oil.Entry, is_hidden: boolean, is_link_target: boolean, is_link_orphan: boolean): string|nil
284292

285293
---@class (exact) oil.SetupViewOptions
286294
---@field show_hidden? boolean Show files and directories that start with "."
@@ -289,6 +297,7 @@ local M = {}
289297
---@field natural_order? boolean|"fast" Sort file names with numbers in a more intuitive order for humans. Can be slow for large directories.
290298
---@field case_insensitive? boolean Sort file and directory names case insensitive
291299
---@field sort? oil.SortSpec[] Sort order for the file list
300+
---@field highlight_filename? fun(entry: oil.Entry, is_hidden: boolean, is_link_target: boolean, is_link_orphan: boolean): string|nil Customize the highlight group for the file name
292301

293302
---@class (exact) oil.SortSpec
294303
---@field [1] string

lua/oil/view.lua

+59-16
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,28 @@ local function render_buffer(bufnr, opts)
691691
return seek_after_render_found
692692
end
693693

694+
---@param name string
695+
---@param meta? table
696+
---@return string filename
697+
---@return string|nil link_target
698+
local function get_link_text(name, meta)
699+
local link_text
700+
if meta then
701+
if meta.link_stat and meta.link_stat.type == "directory" then
702+
name = name .. "/"
703+
end
704+
705+
if meta.link then
706+
link_text = "-> " .. meta.link
707+
if meta.link_stat and meta.link_stat.type == "directory" then
708+
link_text = util.addslash(link_text)
709+
end
710+
end
711+
end
712+
713+
return name, link_text
714+
end
715+
694716
---@private
695717
---@param entry oil.InternalEntry
696718
---@param column_defs table[]
@@ -723,34 +745,55 @@ M.format_entry_cols = function(entry, column_defs, col_width, adapter, is_hidden
723745
end
724746
-- Always add the entry name at the end
725747
local entry_type = entry[FIELD_TYPE]
748+
749+
local get_custom_hl = config.view_options.highlight_filename
750+
local link_name, link_name_hl, link_target, link_target_hl
751+
if get_custom_hl then
752+
local external_entry = util.export_entry(entry)
753+
754+
if entry_type == "link" then
755+
link_name, link_target = get_link_text(name, meta)
756+
local is_orphan = not (meta and meta.link_stat)
757+
link_name_hl = get_custom_hl(external_entry, is_hidden, false, is_orphan)
758+
759+
if link_target then
760+
link_target_hl = get_custom_hl(external_entry, is_hidden, true, is_orphan)
761+
end
762+
763+
-- intentional fallthrough
764+
else
765+
local hl = get_custom_hl(external_entry, is_hidden, false, false)
766+
if hl then
767+
table.insert(cols, { name, hl })
768+
return cols
769+
end
770+
end
771+
end
772+
726773
if entry_type == "directory" then
727774
table.insert(cols, { name .. "/", "OilDir" .. hl_suffix })
728775
elseif entry_type == "socket" then
729776
table.insert(cols, { name, "OilSocket" .. hl_suffix })
730777
elseif entry_type == "link" then
731-
local link_text
732-
if meta then
733-
if meta.link_stat and meta.link_stat.type == "directory" then
734-
name = name .. "/"
735-
end
736-
737-
if meta.link then
738-
link_text = "-> " .. meta.link
739-
if meta.link_stat and meta.link_stat.type == "directory" then
740-
link_text = util.addslash(link_text)
741-
end
742-
end
778+
if not link_name then
779+
link_name, link_target = get_link_text(name, meta)
743780
end
744781
local is_orphan = not (meta and meta.link_stat)
782+
if not link_name_hl then
783+
link_name_hl = (is_orphan and "OilOrphanLink" or "OilLink") .. hl_suffix
784+
end
785+
table.insert(cols, { link_name, link_name_hl })
745786

746-
table.insert(cols, { name, (is_orphan and "OilOrphanLink" or "OilLink") .. hl_suffix })
747-
if link_text then
748-
local target_hl = (is_orphan and "OilOrphanLinkTarget" or "OilLinkTarget") .. hl_suffix
749-
table.insert(cols, { link_text, target_hl })
787+
if link_target then
788+
if not link_target_hl then
789+
link_target_hl = (is_orphan and "OilOrphanLinkTarget" or "OilLinkTarget") .. hl_suffix
790+
end
791+
table.insert(cols, { link_target, link_target_hl })
750792
end
751793
else
752794
table.insert(cols, { name, "OilFile" .. hl_suffix })
753795
end
796+
754797
return cols
755798
end
756799

0 commit comments

Comments
 (0)