From 802bd6bdf07dec84525463c904f034bb76a61eec Mon Sep 17 00:00:00 2001 From: Yi Ming Date: Thu, 18 Jul 2024 19:21:01 +0800 Subject: [PATCH] feat(ui): pass formatter opts to icon fetcher as well --- doc/bufferline.txt | 2 +- lua/bufferline/models.lua | 47 ++++++++++++++++++++++----------------- lua/bufferline/types.lua | 4 +++- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/doc/bufferline.txt b/doc/bufferline.txt index f8132a2e..01a0eb81 100644 --- a/doc/bufferline.txt +++ b/doc/bufferline.txt @@ -136,7 +136,7 @@ The available configuration are: }, color_icons = true | false, -- whether or not to add the filetype icon highlights get_element_icon = function(element) - -- element consists of {filetype: string, path: string, extension: string, directory: string} + -- element consists of {filetype: string, extension: string, directory: string, type:string?}, and all the properties same as the `name_formatter` function -- This can be used to change how bufferline fetches the icon -- for an element e.g. a buffer or a tab. -- e.g. diff --git a/lua/bufferline/models.lua b/lua/bufferline/models.lua index 36284076..7183f961 100644 --- a/lua/bufferline/models.lua +++ b/lua/bufferline/models.lua @@ -110,22 +110,23 @@ function Tabpage:new(tab) tab.modified = get_modified_state(tab.buffers) tab.buftype = vim.bo[tab.buf].buftype tab.extension = fn.fnamemodify(tab.path, ":e") - tab.icon, tab.icon_highlight = utils.get_icon({ + ---@type bufferline.TabFormatterOpts + local formatter_opts = { + name = tab.name, + path = tab.path, + bufnr = tab.buf, + tabnr = tab.id, + buffers = tab.buffers, + } + if tab.name_formatter and type(tab.name_formatter) == "function" then + tab.name = tab.name_formatter(formatter_opts) or tab.name + end + tab.icon, tab.icon_highlight = utils.get_icon(vim.tbl_extend("keep", { filetype = vim.bo[tab.buf].filetype, directory = fn.isdirectory(tab.path) > 0, - path = tab.path, extension = tab.extension, type = tab.buftype, - }) - if tab.name_formatter and type(tab.name_formatter) == "function" then - tab.name = tab.name_formatter({ - name = tab.name, - path = tab.path, - bufnr = tab.buf, - tabnr = tab.id, - buffers = tab.buffers, - }) or tab.name - end + }, formatter_opts)) setmetatable(tab, self) self.__index = self return tab @@ -166,23 +167,29 @@ function Buffer:new(buf) buf.buftype = vim.bo[buf.id].buftype buf.extension = fn.fnamemodify(buf.path, ":e") local is_directory = fn.isdirectory(buf.path) > 0 - buf.icon, buf.icon_highlight = utils.get_icon({ - filetype = vim.bo[buf.id].filetype, - directory = is_directory, - path = buf.path, - extension = buf.extension, - type = buf.buftype, - }) local name = "[No Name]" if buf.path and #buf.path > 0 then name = fn.fnamemodify(buf.path, ":t") name = is_directory and name .. "/" or name end + ---@type bufferline.BufFormatterOpts + local formatter_opts = { + name = name, + path = buf.path, + bufnr = buf.id, + } if buf.name_formatter and type(buf.name_formatter) == "function" then - name = buf.name_formatter({ name = name, path = buf.path, bufnr = buf.id }) or name + name = buf.name_formatter(formatter_opts) or name end + buf.icon, buf.icon_highlight = utils.get_icon(vim.tbl_extend("keep", { + filetype = vim.bo[buf.id].filetype, + directory = is_directory, + extension = buf.extension, + type = buf.buftype, + }, formatter_opts)) + buf.name = name setmetatable(buf, self) diff --git a/lua/bufferline/types.lua b/lua/bufferline/types.lua index 2ad54149..5a464785 100644 --- a/lua/bufferline/types.lua +++ b/lua/bufferline/types.lua @@ -19,6 +19,8 @@ ---@alias bufferline.DiagnosticIndicator fun(count: number, level: string, errors: table, ctx: table): string ---@alias bufferline.HoverOptions {reveal: string[], delay: integer, enabled: boolean} +---@alias bufferline.BufFormatterOpts {name: string, path: string, bufnr: number} +---@alias bufferline.TabFormatterOpts {buffers: number[], tabnr: number} | bufferline.BufFormatterOpts ---@alias bufferline.IconFetcherOpts {directory: boolean, path: string, extension: string, filetype: string?} ---@class bufferline.Options @@ -136,7 +138,7 @@ ---@class bufferline.Buffer ---@field public extension string the file extension ---@field public path string the full path to the file ----@field public name_formatter function? dictates how the name should be shown +---@field public name_formatter fun(opts: bufferline.BufFormatterOpts): string? ---@field public id integer the buffer number ---@field public name string the visible name for the file ---@field public filename string