From ecdb755c1bd24278f5dab5dec3cbd4d7a85b8480 Mon Sep 17 00:00:00 2001 From: Foo-x Date: Wed, 30 Oct 2024 20:58:26 +0900 Subject: [PATCH] feat: highlight config Refs #402 --- README.md | 4 ++++ doc/oil.txt | 4 ++++ lua/oil/config.lua | 6 ++++++ lua/oil/view.lua | 18 +++++++++++++----- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 46063b2c..88803c3b 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,10 @@ require("oil").setup({ { "type", "asc" }, { "name", "asc" }, }, + -- Return a highlight group name for each entry + highlight = function(entry, is_link_target) + return nil + end, }, -- Extra arguments to pass to SCP when moving/copying files over SSH extra_scp_args = {}, diff --git a/doc/oil.txt b/doc/oil.txt index 70c589dd..6a76070e 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -116,6 +116,10 @@ CONFIG *oil-confi { "type", "asc" }, { "name", "asc" }, }, + -- Return a highlight group name for each entry + highlight = function(entry, is_link_target) + return nil + end, }, -- Extra arguments to pass to SCP when moving/copying files over SSH extra_scp_args = {}, diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 5ccde173..b0052160 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -101,6 +101,10 @@ local default_config = { { "type", "asc" }, { "name", "asc" }, }, + -- Return a highlight group name for each entry + highlight = function(entry, is_link_target) + return nil + end, }, -- Extra arguments to pass to SCP when moving/copying files over SSH extra_scp_args = {}, @@ -267,6 +271,7 @@ local M = {} ---@field natural_order boolean ---@field case_insensitive boolean ---@field sort oil.SortSpec[] +---@field highlight fun(entry: oil.Entry, is_link_target: boolean): string|nil ---@class (exact) oil.SetupViewOptions ---@field show_hidden? boolean Show files and directories that start with "." @@ -275,6 +280,7 @@ local M = {} ---@field natural_order? boolean Sort file names in a more intuitive order for humans. Is less performant, so you may want to set to false if you work with large directories. ---@field case_insensitive? boolean Sort file and directory names case insensitive ---@field sort? oil.SortSpec[] Sort order for the file list +---@field highlight fun(entry: oil.Entry, is_link_target: boolean): string|nil Return a highlight group name for each entry ---@class (exact) oil.SortSpec ---@field [1] string diff --git a/lua/oil/view.lua b/lua/oil/view.lua index 89e4b8fc..bda6bf63 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -700,10 +700,18 @@ M.format_entry_cols = function(entry, column_defs, col_width, adapter) end -- Always add the entry name at the end local entry_type = entry[FIELD_TYPE] + local external_entry = { + id = entry[FIELD_ID], + name = name, + parsed_name = name, + type = entry_type, + meta = meta, + } + local highlight = config.view_options.highlight if entry_type == "directory" then - table.insert(cols, { name .. "/", "OilDir" }) + table.insert(cols, { name .. "/", highlight(external_entry, false) or "OilDir" }) elseif entry_type == "socket" then - table.insert(cols, { name, "OilSocket" }) + table.insert(cols, { name, highlight(external_entry, false) or "OilSocket" }) elseif entry_type == "link" then local link_text if meta then @@ -719,12 +727,12 @@ M.format_entry_cols = function(entry, column_defs, col_width, adapter) end end - table.insert(cols, { name, "OilLink" }) + table.insert(cols, { name, highlight(external_entry, false) or "OilLink" }) if link_text then - table.insert(cols, { link_text, "OilLinkTarget" }) + table.insert(cols, { link_text, highlight(external_entry, true) or "OilLinkTarget" }) end else - table.insert(cols, { name, "OilFile" }) + table.insert(cols, { name, highlight(external_entry, false) or "OilFile" }) end return cols end