Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/bufferline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes path which is a breaking change and adds type which it doesn't explain here what that is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path is included in name_formatter arguments, which I explained will be fully included. So it simplifies the expression, not changing the behavior.

-- This can be used to change how bufferline fetches the icon
-- for an element e.g. a buffer or a tab.
-- e.g.
Expand Down
47 changes: 27 additions & 20 deletions lua/bufferline/models.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we passing in the buffer type at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept this because we have done this already, you can see it there:

type = tab.buftype,

Actually, I found it might be a mistake, it may be intended to be used here
if type == "terminal" then return webdev_icons.get_icon(type) end

But the type here will be the global variable included in Lua stdlib, the type function, so it's a false condition. type was not documented, but I kept it to avoid underlying breaking change.

}, formatter_opts))

buf.name = name

setmetatable(buf, self)
Expand Down
4 changes: 3 additions & 1 deletion lua/bufferline/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
---@alias bufferline.DiagnosticIndicator fun(count: number, level: string, errors: table<string, any>, ctx: table<string, any>): 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
Expand Down Expand Up @@ -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
Expand Down