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
3 changes: 2 additions & 1 deletion doc/bufferline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ The available configuration are:
sort_by = 'insert_after_current' |'insert_at_end' | 'id' | 'extension' | 'relative_directory' | 'directory' | 'tabs' | function(buffer_a, buffer_b)
-- add custom logic
return buffer_a.modified > buffer_b.modified
end
end,
show_modified = true | false | function(context): boolean
}
}
<
Expand Down
1 change: 1 addition & 0 deletions lua/bufferline/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ local function get_defaults()
groups = { items = {}, options = { toggle_hidden_on_enter = true } },
hover = { enabled = false, reveal = {}, delay = 200 },
debug = { logging = false },
show_modified = true,
}
return { options = opts, highlights = derive_colors(opts.style_preset) }
end
Expand Down
15 changes: 14 additions & 1 deletion lua/bufferline/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,19 @@ local function add_icon(context)
end
end

--- Determine if the modified icon should be shown for the current element.
--- @param context bufferline.RenderContext
--- @return boolean
local function show_modified(context)
local show = config.options.show_modified
if type(show) == "boolean" then
return show
elseif type(show) == "function" then
return show(context)
end
return false
end

--- The suffix can be either the modified icon, space to replace the icon if
--- a user has turned them off or the close icon if the element is not currently modified
--- @param context bufferline.RenderContext
Expand All @@ -329,7 +342,7 @@ local function add_suffix(context)
highlight = element.modified and hl.modified or nil,
}
local close = get_close_icon(element.id, context)
return not element.modified and close or modified
return not (show_modified(context) and element.modified) and close or modified
end

--- TODO: We increment the buffer length by the separator although the final
Expand Down
27 changes: 27 additions & 0 deletions tests/ui_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,33 @@ describe("UI Tests", function()
})
assert.is_equal(segment.text, "a_very_very_very_…")
end)

it("should render an icon if show_modified is true", function()
config.setup({ options = { show_modified = true } })
config.apply()
local buf = MockBuffer:new({ id = 1, name = "file.txt", length = 10, modified = true })
local el = ui.element({}, buf)
local segment = ui.to_tabline_str(el:component(1))
assert.is_truthy(segment:match(config.options.modified_icon))
end)

it("should not render an icon if show_modified is false", function()
config.setup({ options = { show_modified = false } })
config.apply()
local buf = MockBuffer:new({ id = 1, name = "file.txt", length = 10, modified = true })
local el = ui.element({}, buf)
local segment = ui.to_tabline_str(el:component(1))
assert.is_falsy(segment:match(config.options.modified_icon))
end)

it("should render an icon if show_modified is a function", function()
config.setup({ options = { show_modified = function(context) return true end } })
config.apply()
local buf = MockBuffer:new({ id = 1, name = "file.txt", length = 10, modified = true })
local el = ui.element({}, buf)
local segment = ui.to_tabline_str(el:component(1))
assert.is_truthy(segment:match(config.options.modified_icon))
end)
end)

describe("Hover events - ", function()
Expand Down