Skip to content

Commit 802bd6b

Browse files
committed
feat(ui): pass formatter opts to icon fetcher as well
1 parent 0b2fd86 commit 802bd6b

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

doc/bufferline.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ The available configuration are:
136136
},
137137
color_icons = true | false, -- whether or not to add the filetype icon highlights
138138
get_element_icon = function(element)
139-
-- element consists of {filetype: string, path: string, extension: string, directory: string}
139+
-- element consists of {filetype: string, extension: string, directory: string, type:string?}, and all the properties same as the `name_formatter` function
140140
-- This can be used to change how bufferline fetches the icon
141141
-- for an element e.g. a buffer or a tab.
142142
-- e.g.

lua/bufferline/models.lua

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,23 @@ function Tabpage:new(tab)
110110
tab.modified = get_modified_state(tab.buffers)
111111
tab.buftype = vim.bo[tab.buf].buftype
112112
tab.extension = fn.fnamemodify(tab.path, ":e")
113-
tab.icon, tab.icon_highlight = utils.get_icon({
113+
---@type bufferline.TabFormatterOpts
114+
local formatter_opts = {
115+
name = tab.name,
116+
path = tab.path,
117+
bufnr = tab.buf,
118+
tabnr = tab.id,
119+
buffers = tab.buffers,
120+
}
121+
if tab.name_formatter and type(tab.name_formatter) == "function" then
122+
tab.name = tab.name_formatter(formatter_opts) or tab.name
123+
end
124+
tab.icon, tab.icon_highlight = utils.get_icon(vim.tbl_extend("keep", {
114125
filetype = vim.bo[tab.buf].filetype,
115126
directory = fn.isdirectory(tab.path) > 0,
116-
path = tab.path,
117127
extension = tab.extension,
118128
type = tab.buftype,
119-
})
120-
if tab.name_formatter and type(tab.name_formatter) == "function" then
121-
tab.name = tab.name_formatter({
122-
name = tab.name,
123-
path = tab.path,
124-
bufnr = tab.buf,
125-
tabnr = tab.id,
126-
buffers = tab.buffers,
127-
}) or tab.name
128-
end
129+
}, formatter_opts))
129130
setmetatable(tab, self)
130131
self.__index = self
131132
return tab
@@ -166,23 +167,29 @@ function Buffer:new(buf)
166167
buf.buftype = vim.bo[buf.id].buftype
167168
buf.extension = fn.fnamemodify(buf.path, ":e")
168169
local is_directory = fn.isdirectory(buf.path) > 0
169-
buf.icon, buf.icon_highlight = utils.get_icon({
170-
filetype = vim.bo[buf.id].filetype,
171-
directory = is_directory,
172-
path = buf.path,
173-
extension = buf.extension,
174-
type = buf.buftype,
175-
})
176170
local name = "[No Name]"
177171
if buf.path and #buf.path > 0 then
178172
name = fn.fnamemodify(buf.path, ":t")
179173
name = is_directory and name .. "/" or name
180174
end
181175

176+
---@type bufferline.BufFormatterOpts
177+
local formatter_opts = {
178+
name = name,
179+
path = buf.path,
180+
bufnr = buf.id,
181+
}
182182
if buf.name_formatter and type(buf.name_formatter) == "function" then
183-
name = buf.name_formatter({ name = name, path = buf.path, bufnr = buf.id }) or name
183+
name = buf.name_formatter(formatter_opts) or name
184184
end
185185

186+
buf.icon, buf.icon_highlight = utils.get_icon(vim.tbl_extend("keep", {
187+
filetype = vim.bo[buf.id].filetype,
188+
directory = is_directory,
189+
extension = buf.extension,
190+
type = buf.buftype,
191+
}, formatter_opts))
192+
186193
buf.name = name
187194

188195
setmetatable(buf, self)

lua/bufferline/types.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
---@alias bufferline.DiagnosticIndicator fun(count: number, level: string, errors: table<string, any>, ctx: table<string, any>): string
2020

2121
---@alias bufferline.HoverOptions {reveal: string[], delay: integer, enabled: boolean}
22+
---@alias bufferline.BufFormatterOpts {name: string, path: string, bufnr: number}
23+
---@alias bufferline.TabFormatterOpts {buffers: number[], tabnr: number} | bufferline.BufFormatterOpts
2224
---@alias bufferline.IconFetcherOpts {directory: boolean, path: string, extension: string, filetype: string?}
2325

2426
---@class bufferline.Options
@@ -136,7 +138,7 @@
136138
---@class bufferline.Buffer
137139
---@field public extension string the file extension
138140
---@field public path string the full path to the file
139-
---@field public name_formatter function? dictates how the name should be shown
141+
---@field public name_formatter fun(opts: bufferline.BufFormatterOpts): string?
140142
---@field public id integer the buffer number
141143
---@field public name string the visible name for the file
142144
---@field public filename string

0 commit comments

Comments
 (0)