Skip to content

Commit

Permalink
version: 2.4.0
Browse files Browse the repository at this point in the history
* refactor: add ability to disable diagnostic indicator
* fix(buffers): use full file path in buffer object
* style(stylua): reformat project
  • Loading branch information
akinsho authored Jul 10, 2022
2 parents b5a2b1f + 8a3107c commit bf9eb69
Show file tree
Hide file tree
Showing 22 changed files with 269 additions and 651 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ require('bufferline').setup {
tab_size = 18,
diagnostics = false | "nvim_lsp" | "coc",
diagnostics_update_in_insert = false,
-- The diagnostics indicator can be set to nil to keep the buffer name highlight but delete the highlighting
diagnostics_indicator = function(count, level, diagnostics_dict, context)
return "("..count..")"
end,
Expand Down
3 changes: 3 additions & 0 deletions doc/bufferline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ The highlighting for the filename if there is an error can be changed by
replacing the highlights for `error`, `error_visible`, `error_selected`,
`warning`, `warning_visible`, `warning_selected`.

The diagnostics indicator can be set to `false` to remove the indicators
completely whilst still keeping the highlight of the buffer name.

==============================================================================
GROUPS *bufferline-groups*

Expand Down
140 changes: 44 additions & 96 deletions lua/bufferline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,18 @@ local M = {
-----------------------------------------------------------------------------//
local function restore_positions()
local str = vim.g[positions_key]
if not str then
return str
end
if not str then return str end
-- these are converted to strings when stored
-- so have to be converted back before usage
local ids = vim.split(str, ",")
if ids and #ids > 0 then
state.custom_sort = vim.tbl_map(tonumber, ids)
end
if ids and #ids > 0 then state.custom_sort = vim.tbl_map(tonumber, ids) end
end

---@param list Component[]
---@return Component[]
local function filter_invisible(list)
return utils.fold({}, function(accum, item)
if item.focusable ~= false and not item.hidden then
table.insert(accum, item)
end
if item.focusable ~= false and not item.hidden then table.insert(accum, item) end
return accum
end, list)
end
Expand All @@ -76,9 +70,7 @@ end
---@return Component[]
local function sorter(list)
-- if the user has reshuffled the buffers manually don't try and sort them
if state.custom_sort then
return list
end
if state.custom_sort then return list end
return sorters.sort(list, nil, state)
end

Expand All @@ -87,9 +79,7 @@ end
---@return number
local function get_current_index(current_state)
for index, component in ipairs(current_state.components) do
if component:current() then
return index
end
if component:current() then return index end
end
end

Expand Down Expand Up @@ -119,9 +109,7 @@ end
local function toggle_bufferline()
local item_count = config:is_tabline() and utils.get_tab_count() or utils.get_buf_count()
local status = (config.options.always_show_bufferline or item_count > 1) and 2 or 0
if vim.o.showtabline ~= status then
vim.o.showtabline = status
end
if vim.o.showtabline ~= status then vim.o.showtabline = status end
end

local function apply_colors()
Expand All @@ -136,9 +124,7 @@ end
function M.group_action(name, action)
assert(name, "A name must be passed to execute a group action")
if action == "close" then
groups.command(name, function(b)
api.nvim_buf_delete(b.id, { force = true })
end)
groups.command(name, function(b) api.nvim_buf_delete(b.id, { force = true }) end)
elseif action == "toggle" then
groups.toggle_hidden(nil, name)
ui.refresh()
Expand All @@ -160,14 +146,10 @@ end
local function handle_group_enter()
local options = config.options
local _, element = commands.get_current_element_index(state, { include_hidden = true })
if not element or not element.group then
return
end
if not element or not element.group then return end
local current_group = groups.get_by_id(element.group)
if options.groups.options.toggle_hidden_on_enter then
if current_group.hidden then
groups.set_hidden(current_group.id, false)
end
if current_group.hidden then groups.set_hidden(current_group.id, false) end
end
utils.for_each(state.components, function(tab)
local group = groups.get_by_id(tab.group)
Expand All @@ -184,46 +166,34 @@ local function setup_autocommands(conf)
api.nvim_create_autocmd("ColorScheme", {
pattern = "*",
group = BUFFERLINE_GROUP,
callback = function()
apply_colors()
end,
callback = function() apply_colors() end,
})
if not options or vim.tbl_isempty(options) then
return
end
if not options or vim.tbl_isempty(options) then return end
if options.persist_buffer_sort then
api.nvim_create_autocmd("SessionLoadPost", {
pattern = "*",
group = BUFFERLINE_GROUP,
callback = function()
restore_positions()
end,
callback = function() restore_positions() end,
})
end
if not options.always_show_bufferline then
-- toggle tabline
api.nvim_create_autocmd({ "BufAdd", "TabEnter" }, {
pattern = "*",
group = BUFFERLINE_GROUP,
callback = function()
toggle_bufferline()
end,
callback = function() toggle_bufferline() end,
})
end

api.nvim_create_autocmd("BufRead", {
pattern = "*",
once = true,
callback = function()
vim.schedule(handle_group_enter)
end,
callback = function() vim.schedule(handle_group_enter) end,
})

api.nvim_create_autocmd("BufEnter", {
pattern = "*",
callback = function()
handle_group_enter()
end,
callback = function() handle_group_enter() end,
})
end

Expand All @@ -232,76 +202,54 @@ end
---@param cursor_pos number
---@return string[]
---@diagnostic disable-next-line: unused-local
local function complete_groups(arg_lead, cmd_line, cursor_pos)
return groups.names()
end
local function complete_groups(arg_lead, cmd_line, cursor_pos) return groups.names() end

local function setup_commands()
local cmd = api.nvim_create_user_command

cmd("BufferLinePick", function()
M.pick_buffer()
end, {})
cmd("BufferLinePick", function() M.pick_buffer() end, {})

cmd("BufferLinePickClose", function()
M.close_buffer_with_pick()
end, {})
cmd("BufferLinePickClose", function() M.close_buffer_with_pick() end, {})

cmd("BufferLineCycleNext", function()
M.cycle(1)
end, {})
cmd("BufferLineCycleNext", function() M.cycle(1) end, {})

cmd("BufferLineCyclePrev", function()
M.cycle(-1)
end, {})
cmd("BufferLineCyclePrev", function() M.cycle(-1) end, {})

cmd("BufferLineCloseRight", function()
M.close_in_direction("right")
end, {})
cmd("BufferLineCloseRight", function() M.close_in_direction("right") end, {})

cmd("BufferLineCloseLeft", function()
M.close_in_direction("left")
end, {})
cmd("BufferLineCloseLeft", function() M.close_in_direction("left") end, {})

cmd("BufferLineMoveNext", function()
M.move(1)
end, {})
cmd("BufferLineMoveNext", function() M.move(1) end, {})

cmd("BufferLineMovePrev", function()
M.move(-1)
end, {})
cmd("BufferLineMovePrev", function() M.move(-1) end, {})

cmd("BufferLineSortByExtension", function()
M.sort_buffers_by("extension")
end, {})
cmd("BufferLineSortByExtension", function() M.sort_buffers_by("extension") end, {})

cmd("BufferLineSortByDirectory", function()
M.sort_buffers_by("directory")
end, {})
cmd("BufferLineSortByDirectory", function() M.sort_buffers_by("directory") end, {})

cmd("BufferLineSortByRelativeDirectory", function()
M.sort_buffers_by("relative_directory")
end, {})
cmd(
"BufferLineSortByRelativeDirectory",
function() M.sort_buffers_by("relative_directory") end,
{}
)

cmd("BufferLineSortByTabs", function()
M.sort_buffers_by("tabs")
end, {})
cmd("BufferLineSortByTabs", function() M.sort_buffers_by("tabs") end, {})

cmd("BufferLineGoToBuffer", function(opts)
M.go_to_buffer(opts.args)
end, { nargs = 1 })
cmd("BufferLineGoToBuffer", function(opts) M.go_to_buffer(opts.args) end, { nargs = 1 })

cmd("BufferLineGroupClose", function(opts)
M.group_action(opts.args, "close")
end, { nargs = 1, complete = complete_groups })
cmd(
"BufferLineGroupClose",
function(opts) M.group_action(opts.args, "close") end,
{ nargs = 1, complete = complete_groups }
)

cmd("BufferLineGroupToggle", function(opts)
M.group_action(opts.args, "toggle")
end, { nargs = 1, complete = complete_groups })
cmd(
"BufferLineGroupToggle",
function(opts) M.group_action(opts.args, "toggle") end,
{ nargs = 1, complete = complete_groups }
)

cmd("BufferLineTogglePin", function()
M.toggle_pin()
end, { nargs = 0 })
cmd("BufferLineTogglePin", function() M.toggle_pin() end, { nargs = 0 })
end

---@private
Expand Down
28 changes: 9 additions & 19 deletions lua/bufferline/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,23 @@ local diagnostics = require("bufferline.diagnostics")

local M = {}

local api = vim.api

--- sorts buf_names in place, but doesn't add/remove any values
--- @param buf_nums number[]
--- @param sorted number[]
--- @return number[]
local function get_updated_buffers(buf_nums, sorted)
if not sorted then
return buf_nums
end
if not sorted then return buf_nums end
local nums = { unpack(buf_nums) }
local reverse_lookup_sorted = utils.tbl_reverse_lookup(sorted)

--- a comparator that sorts buffers by their position in sorted
local sort_by_sorted = function(buf_id_1, buf_id_2)
local buf_1_rank = reverse_lookup_sorted[buf_id_1]
local buf_2_rank = reverse_lookup_sorted[buf_id_2]
if not buf_1_rank then
return false
end
if not buf_2_rank then
return true
end
if not buf_1_rank then return false end
if not buf_2_rank then return true end
return buf_1_rank < buf_2_rank
end
table.sort(nums, sort_by_sorted)
Expand All @@ -48,14 +44,10 @@ end
---@param callback fun(buf: integer, bufs: integer[]): boolean
---@return integer[]
local function apply_buffer_filter(buf_nums, callback)
if type(callback) ~= "function" then
return buf_nums
end
if type(callback) ~= "function" then return buf_nums end
local filtered = {}
for _, buf in ipairs(buf_nums) do
if callback(buf, buf_nums) then
table.insert(filtered, buf)
end
if callback(buf, buf_nums) then table.insert(filtered, buf) end
end
return filtered
end
Expand All @@ -78,7 +70,7 @@ function M.get_components(state)
local Buffer = require("bufferline.models").Buffer
for i, buf_id in ipairs(buf_nums) do
local buf = Buffer:new({
path = vim.fn.bufname(buf_id),
path = api.nvim_buf_get_name(buf_id),
id = buf_id,
ordinal = i,
diagnostics = all_diagnostics[buf_id],
Expand All @@ -89,9 +81,7 @@ function M.get_components(state)
components[i] = buf
end

return vim.tbl_map(function(buf)
return ui.element(state, buf)
end, duplicates.mark(components))
return vim.tbl_map(function(buf) return ui.element(state, buf) end, duplicates.mark(components))
end

return M
Loading

0 comments on commit bf9eb69

Please sign in to comment.