Skip to content
Closed
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
26 changes: 13 additions & 13 deletions lua/barbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ local command = vim.api.nvim_command --- @type function
local create_user_command = vim.api.nvim_create_user_command --- @type function
local set_option = vim.api.nvim_set_option --- @type function

local api = require'barbar.api'
local bbye = require'barbar.bbye'
local events = require'barbar.events'
local notify = require'barbar.utils'.notify
local render = require'barbar.ui.render'
local state = require'barbar.state'
local utils = require'barbar.utils'
local api = require('barbar.api')
local bbye = require('barbar.bbye')
local events = require('barbar.events')
local markdown_inline_code = require('barbar.utils').markdown_inline_code
local notify = require('barbar.utils').notify
local scroll = require('barbar.ui.render').scroll
local state = require('barbar.state')

-------------------------------
-- Section: `barbar` module
-------------------------------

--- @class barbar
--- @class Barbar
local barbar = {}

--- Setup this plugin.
Expand Down Expand Up @@ -47,7 +47,7 @@ function barbar.setup(options)
local index = tonumber(tbl.args)
if not index then
return notify(
'Invalid argument to ' .. utils.markdown_inline_code':BufferGoto',
'Invalid argument to ' .. markdown_inline_code':BufferGoto',
vim.log.levels.ERROR
)
end
Expand All @@ -59,7 +59,7 @@ function barbar.setup(options)
local buffers = state.buffers
local buffer_indices = {}

for i = 1, #buffers do
for i in ipairs(buffers) do
table_insert(buffer_indices, tostring(i))
end

Expand All @@ -82,7 +82,7 @@ function barbar.setup(options)
vim.api.nvim_cmd and
function(tbl) vim.cmd.BufferMovePrevious {count = tbl.count} end or
function(tbl) command('BufferMovePrevious ' .. tbl.count) end,
{count = true, desc = 'Synonym for ' .. utils.markdown_inline_code':BufferMovePrevious'}
{count = true, desc = 'Synonym for ' .. markdown_inline_code':BufferMovePrevious'}
)

create_user_command(
Expand Down Expand Up @@ -188,13 +188,13 @@ function barbar.setup(options)

create_user_command(
'BufferScrollLeft',
function(tbl) render.scroll(-max(1, tbl.count)) end,
function(tbl) scroll(-max(1, tbl.count)) end,
{count = true, desc = 'Scroll the bufferline left'}
)

create_user_command(
'BufferScrollRight',
function(tbl) render.scroll(max(1, tbl.count)) end,
function(tbl) scroll(max(1, tbl.count)) end,
{count = true, desc = 'Scroll the bufferline right'}
)

Expand Down
2 changes: 1 addition & 1 deletion lua/barbar/animate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local schedule_wrap = vim.schedule_wrap
--- @field timer userdata
--- @field type unknown

--- @class barbar.animate
--- @class barbar.Animate
local animate = {}

--- The amount of time between rendering the next part of the animation.
Expand Down
94 changes: 48 additions & 46 deletions lua/barbar/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@ local set_current_buf = vim.api.nvim_set_current_buf --- @type function
-- TODO: remove `vim.fs and` after 0.8 release
local normalize = vim.fs and vim.fs.normalize

local animate = require'barbar.animate'
local bbye = require'barbar.bbye'
local Buffer = require'barbar.buffer'
local config = require'barbar.config'
local JumpMode = require'barbar.jump_mode'
local Layout = require'barbar.ui.layout'
local render = require'barbar.ui.render'
local state = require'barbar.state'
local utils = require'barbar.utils'
local animate = require('barbar.animate')
local bdelete = require('barbar.bbye').bdelete
local buffer = require('barbar.buffer')
local config = require('barbar.config')
local index_of = require('barbar.utils.list').index_of
local is_relative_path = require('barbar.fs').is_relative_path
local jump_mode = require('barbar.jump_mode')
local layout = require('barbar.ui.layout')
local notify = require('barbar.utils').notify
local render = require('barbar.ui.render')
local state = require('barbar.state')

local ESC = vim.api.nvim_replace_termcodes('<Esc>', true, false, true)

--- Initialize the buffer pick mode.
--- @param fn fun()
--- @return nil
local function pick_buffer_wrap(fn)
if JumpMode.reinitialize then
JumpMode.initialize_indexes()
if jump_mode.reinitialize then
jump_mode.initialize_indexes()
end

state.is_picking_buffer = true
Expand All @@ -49,7 +51,7 @@ end
--- @param buffer_number integer
--- @return nil
local function notify_buffer_not_found(buffer_number)
utils.notify(
notify(
'Current buffer (' .. buffer_number .. ") not found in barbar.nvim's list of buffers: " .. vim.inspect(state.buffers),
vim.log.levels.ERROR
)
Expand All @@ -73,7 +75,7 @@ local function with_pin_order(order_func)
end
end

--- @class barbar.api
--- @class barbar.Api
local api = {}

--- Close all open buffers, except the current one.
Expand All @@ -83,7 +85,7 @@ function api.close_all_but_current()

for _, buffer_number in ipairs(state.buffers) do
if buffer_number ~= current_bufnr then
bbye.bdelete(false, buffer_number)
bdelete(false, buffer_number)
end
end

Expand All @@ -93,10 +95,10 @@ end
--- Close all open buffers, except those in visible windows.
--- @return nil
function api.close_all_but_visible()
local visible = Buffer.activities.Visible
local visible = buffer.activities.Visible
for _, buffer_number in ipairs(state.buffers) do
if Buffer.get_activity(buffer_number) < visible then
bbye.bdelete(false, buffer_number)
if buffer.get_activity(buffer_number) < visible then
bdelete(false, buffer_number)
end
end

Expand All @@ -108,7 +110,7 @@ end
function api.close_all_but_pinned()
for _, buffer_number in ipairs(state.buffers) do
if not state.is_pinned(buffer_number) then
bbye.bdelete(false, buffer_number)
bdelete(false, buffer_number)
end
end

Expand All @@ -122,7 +124,7 @@ function api.close_all_but_current_or_pinned()

for _, buffer_number in ipairs(state.buffers) do
if not state.is_pinned(buffer_number) and buffer_number ~= current_bufnr then
bbye.bdelete(false, buffer_number)
bdelete(false, buffer_number)
end
end

Expand All @@ -132,13 +134,13 @@ end
--- Close all buffers which are visually left of the current buffer.
--- @return nil
function api.close_buffers_left()
local idx = utils.index_of(state.buffers, get_current_buf())
local idx = index_of(state.buffers, get_current_buf())
if idx == nil or idx == 1 then
return
end

for i = idx - 1, 1, -1 do
bbye.bdelete(false, state.buffers[i])
bdelete(false, state.buffers[i])
end

render.update()
Expand All @@ -147,13 +149,13 @@ end
--- Close all buffers which are visually right of the current buffer.
--- @return nil
function api.close_buffers_right()
local idx = utils.index_of(state.buffers, get_current_buf())
local idx = index_of(state.buffers, get_current_buf())
if idx == nil then
return
end

for i = #state.buffers, idx + 1, -1 do
bbye.bdelete(false, state.buffers[i])
bdelete(false, state.buffers[i])
end

render.update()
Expand All @@ -180,7 +182,7 @@ function api.goto_buffer(index)
if buffer_number then
set_current_buf(buffer_number)
else
utils.notify(
notify(
'E86: buffer at index ' .. index .. ' in list ' .. vim.inspect(state.buffers) .. ' does not exist.',
vim.log.levels.ERROR
)
Expand All @@ -195,15 +197,15 @@ function api.goto_buffer_relative(steps)
render.get_updated_buffers()

if #state.buffers < 1 then
return utils.notify('E85: There is no listed buffer', vim.log.levels.ERROR)
return notify('E85: There is no listed buffer', vim.log.levels.ERROR)
end

local current_bufnr = render.set_current_win_listed_buffer()
local idx = utils.index_of(state.buffers, current_bufnr)
local idx = index_of(state.buffers, current_bufnr)

if not idx then -- fall back to: 1. the alternate buffer, 2. the first buffer
idx = utils.index_of(state.buffers, bufnr'#') or 1
utils.notify(
idx = index_of(state.buffers, bufnr'#') or 1
notify(
"Couldn't find buffer #" .. current_bufnr .. ' in the list: ' .. vim.inspect(state.buffers) ..
'. Falling back to buffer #' .. state.buffers[idx],
vim.log.levels.INFO
Expand All @@ -222,7 +224,7 @@ local move_animation_data = {
--- An incremental animation for `move_buffer_animated`.
--- @return nil
local function move_buffer_animated_tick(ratio, current_animation)
for _, current_number in ipairs(Layout.buffers) do
for _, current_number in ipairs(layout.buffers) do
local current_data = state.get_buffer_data(current_number)

if current_animation.running == true then
Expand Down Expand Up @@ -263,15 +265,15 @@ local function move_buffer(from_idx, to_idx)

local previous_positions
if animation == true then
previous_positions = Layout.calculate_buffers_position_by_buffer_number()
previous_positions = layout.calculate_buffers_position_by_buffer_number()
end

table_remove(state.buffers, from_idx)
table_insert(state.buffers, to_idx, buffer_number)
state.sort_pins_to_left()

if animation == true then
local current_index = utils.index_of(Layout.buffers, buffer_number)
local current_index = index_of(layout.buffers, buffer_number)
local start_index = min(from_idx, current_index)
local end_index = max(from_idx, current_index)

Expand All @@ -281,9 +283,9 @@ local function move_buffer(from_idx, to_idx)
animate.stop(move_animation)
end

local next_positions = Layout.calculate_buffers_position_by_buffer_number()
local next_positions = layout.calculate_buffers_position_by_buffer_number()

for _, layout_bufnr in ipairs(Layout.buffers) do
for _, layout_bufnr in ipairs(layout.buffers) do
local current_data = state.get_buffer_data(layout_bufnr)

local previous_position = previous_positions[layout_bufnr]
Expand Down Expand Up @@ -319,7 +321,7 @@ function api.move_current_buffer_to(idx)
end

local current_bufnr = get_current_buf()
local from_idx = utils.index_of(state.buffers, current_bufnr)
local from_idx = index_of(state.buffers, current_bufnr)

if from_idx == nil then
return notify_buffer_not_found(current_bufnr)
Expand All @@ -335,7 +337,7 @@ function api.move_current_buffer(steps)
render.update()

local current_bufnr = get_current_buf()
local idx = utils.index_of(state.buffers, current_bufnr)
local idx = index_of(state.buffers, current_bufnr)

if idx == nil then
return notify_buffer_not_found(current_bufnr)
Expand All @@ -361,8 +363,8 @@ function api.order_by_directory()

-- TODO: remove this block after 0.8 releases
if not normalize then
local a_is_relative = utils.is_relative_path(name_of_a)
if a_is_relative and utils.is_relative_path(name_of_b) then
local a_is_relative = is_relative_path(name_of_a)
if a_is_relative and is_relative_path(name_of_b) then
return a_less_than_b
end

Expand Down Expand Up @@ -408,13 +410,13 @@ function api.pick_buffer()
pick_buffer_wrap(function()
local ok, letter = pcall(function() return char(getchar()) end)
if ok and letter ~= '' then
if JumpMode.buffer_by_letter[letter] ~= nil then
set_current_buf(JumpMode.buffer_by_letter[letter])
if jump_mode.buffer_by_letter[letter] ~= nil then
set_current_buf(jump_mode.buffer_by_letter[letter])
else
utils.notify("Couldn't find buffer", vim.log.levels.WARN)
notify("Couldn't find buffer", vim.log.levels.WARN)
end
else
utils.notify('Invalid input', vim.log.levels.WARN)
notify('Invalid input', vim.log.levels.WARN)
end
end)
end
Expand All @@ -426,15 +428,15 @@ function api.pick_buffer_delete()
while true do
local ok, letter = pcall(function() return char(getchar()) end)
if ok and letter ~= '' then
if JumpMode.buffer_by_letter[letter] ~= nil then
bbye.bdelete(false, JumpMode.buffer_by_letter[letter])
if jump_mode.buffer_by_letter[letter] ~= nil then
bdelete(false, jump_mode.buffer_by_letter[letter])
elseif letter == ESC then
break
else
utils.notify("Couldn't find buffer with letter '" .. letter .. "'", vim.log.levels.WARN)
notify("Couldn't find buffer with letter '" .. letter .. "'", vim.log.levels.WARN)
end
else
utils.notify('Invalid input', vim.log.levels.WARN)
notify('Invalid input', vim.log.levels.WARN)
end

render.update()
Expand All @@ -446,7 +448,7 @@ end
--- @param width integer the amount to offset
--- @param text? string text to put in the offset
--- @param hl? string
--- @param side? 'left'|'right'
--- @param side? side
--- @return nil
function api.set_offset(width, text, hl, side)
if side == nil then
Expand Down
15 changes: 8 additions & 7 deletions lua/barbar/bbye.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ local set_current_win = vim.api.nvim_set_current_win --- @type function
local win_get_buf = vim.api.nvim_win_get_buf --- @type function
local win_is_valid = vim.api.nvim_win_is_valid --- @type function

local config = require'barbar.config'
local state = require'barbar.state'
local utils = require'barbar.utils'
local config = require('barbar.config')
local list = require('barbar.utils.list')
local markdown_inline_code = require('barbar.utils').markdown_inline_code
local state = require('barbar.state')

-------------------
-- Section: helpers
Expand Down Expand Up @@ -100,10 +101,10 @@ local function get_focus_on_close(closing_number)
end

if focus_on_close == 'right' then
state_bufnrs = utils.list_reverse(state.buffers)
state_bufnrs = list.reverse(state.buffers)
end

local index = utils.index_of(state_bufnrs, closing_number)
local index = list.index_of(state_bufnrs, closing_number)
if index then
index = index - 1

Expand Down Expand Up @@ -158,7 +159,7 @@ end
-- Section: module
------------------

--- @class bbye
--- @class barbar.Bbye
local bbye = {}

--- Delete a buffer
Expand Down Expand Up @@ -249,7 +250,7 @@ function bbye.delete(action, force, buffer, mods)
return err(msg)
end
else
return err('Could not delete buffer ' .. buffer_number .. ' with ' .. utils.markdown_inline_code(action))
return err('Could not delete buffer ' .. buffer_number .. ' with ' .. markdown_inline_code(action))
end
end
end
Expand Down
Loading