Skip to content

Commit

Permalink
feat(duplicates): only treat as duplicate if in the same group (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyelopez authored Mar 7, 2024
1 parent 1064399 commit 66949d0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/bufferline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ The available configuration are:
show_close_icon = true | false,
show_tab_indicators = true | false,
show_duplicate_prefix = true | false, -- whether to show duplicate buffer prefix
duplicates_across_groups = true, -- whether to consider duplicate paths in different groups as duplicates
persist_buffer_sort = true, -- whether or not custom sorted buffers should persist
move_wraps_at_ends = false, -- whether or not the move command "wraps" at the first or last position
-- can also be a table containing 2 custom separators
Expand Down
1 change: 1 addition & 0 deletions lua/bufferline/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ local function get_defaults()
show_close_icon = true,
show_tab_indicators = true,
show_duplicate_prefix = true,
duplicates_across_groups = true,
enforce_regular_tabs = false,
always_show_bufferline = true,
persist_buffer_sort = true,
Expand Down
5 changes: 5 additions & 0 deletions lua/bufferline/duplicates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ end
---@param elements bufferline.TabElement[]
---@return bufferline.TabElement[]
function M.mark(elements)
local options = config.options
local duplicates_across_groups = options.duplicates_across_groups
return utils.map(function(current)
if current.path == "" then return current end
local duplicate = duplicates[current.name]
Expand All @@ -30,6 +32,9 @@ function M.mark(elements)
else
local depth, limit, is_same_buffer = 1, 10, false
for _, element in ipairs(duplicate) do
if (duplicates_across_groups == false) and (current.group ~= element.group) then
return current
end
local element_depth = 1
is_same_buffer = current.path == element.path
while is_same_path(current.path, element.path, element_depth) and not is_same_buffer do
Expand Down
1 change: 1 addition & 0 deletions lua/bufferline/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
---@field public show_close_icon? boolean
---@field public show_tab_indicators? boolean
---@field public show_duplicate_prefix? boolean
---@field public duplicates_across_groups? boolean
---@field public enforce_regular_tabs? boolean
---@field public always_show_bufferline? boolean
---@field public persist_buffer_sort? boolean
Expand Down
58 changes: 58 additions & 0 deletions tests/duplicates_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe("Duplicate Tests - ", function()
end)

it("should mark duplicate files", function()
config.setup({})
config.apply()
local result = duplicates.mark({
{
path = "/test/dir_a/dir_b/file.txt",
Expand All @@ -34,6 +36,62 @@ describe("Duplicate Tests - ", function()
assert.is_equal(#result, 3)
end)

it("should show duplicates across groups", function()
config.setup({ options = { duplicates_across_groups = true } })
config.apply()
local result = duplicates.mark({
{
path = "/test/dir_a/dir_b/file.txt",
name = "file.txt",
ordinal = 1,
group = "A",
},
{
path = "/test/dir_a/dir_c/file.txt",
name = "file.txt",
ordinal = 2,
group = "A",
},
{
path = "/test/dir_a/dir_d/file.txt",
name = "file.txt",
ordinal = 1,
group = "B",
},
})
assert.is_equal(result[1].duplicated, "path")
assert.is_equal(result[2].duplicated, "path")
assert.is_equal(result[3].duplicated, "path")
end)

it("should not show duplicates across groups", function()
config.setup({ options = { duplicates_across_groups = false } })
config.apply()
local result = duplicates.mark({
{
path = "/test/dir_a/dir_b/file.txt",
name = "file.txt",
ordinal = 1,
group = "A",
},
{
path = "/test/dir_a/dir_c/file.txt",
name = "file.txt",
ordinal = 2,
group = "A",
},
{
path = "/test/dir_a/dir_d/file.txt",
name = "file.txt",
ordinal = 1,
group = "B",
},
})
assert.is_equal(result[1].duplicated, "path")
assert.is_equal(result[2].duplicated, "path")
assert.falsy(result[3].duplicated)
end)

it("should return the correct prefix count", function()
local result = duplicates.mark({
{
Expand Down

0 comments on commit 66949d0

Please sign in to comment.