Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: close by step and by index #104

Merged
merged 1 commit into from
Mar 27, 2023
Merged
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
14 changes: 11 additions & 3 deletions lua/cokeline/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local is_picking = {
close = false,
}

---@param goal '"switch"' | '"focus"'
---@param goal '"switch"' | '"focus"' | '"close"'
---@param index index
local by_index = function(goal, index)
local target_buffer = filter(function(buffer)
Expand All @@ -35,10 +35,13 @@ local by_index = function(goal, index)
buffers.move_buffer(focused_buffer, target_buffer._valid_index)
elseif goal == "focus" then
cmd("b" .. target_buffer.number)
elseif goal == "close" then
utils.buf_delete(target_buffer.number)
vim.cmd.redrawtabline()
end
end

---@param goal '"switch"' | '"focus"'
---@param goal '"switch"' | '"focus"' |'"close"'
---@param step '-1' | '1'
local by_step = function(goal, step)
local focused_buffer = filter(function(buffer)
Expand All @@ -57,10 +60,15 @@ local by_step = function(goal, step)
target_index = (target_index - 1) % #_G.cokeline.valid_buffers + 1
end

local target_buf = _G.cokeline.valid_buffers[target_index].number

if goal == "switch" then
buffers.move_buffer(focused_buffer, target_index)
elseif goal == "focus" then
cmd("b" .. _G.cokeline.valid_buffers[target_index].number)
cmd("b" .. target_buf)
elseif goal == "close" then
utils.buf_delete(target_buf)
vim.cmd.redrawtabline()
end
end

Expand Down
56 changes: 29 additions & 27 deletions lua/cokeline/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,40 @@ local function buf_delete(bufnr, focus, wipeout)

local win = vim.fn.bufwinid(bufnr)

-- Get a list of buffers that are valid switch targets
local switchable = vim.tbl_filter(function(buf)
return vim.api.nvim_buf_is_valid(buf)
and vim.bo[buf].buflisted
and buf ~= bufnr
end, vim.api.nvim_list_bufs())
if win ~= -1 then
-- Get a list of buffers that are valid switch targets
local switchable = vim.tbl_filter(function(buf)
return vim.api.nvim_buf_is_valid(buf)
and vim.bo[buf].buflisted
and buf ~= bufnr
end, vim.api.nvim_list_bufs())

local switch_target
if #switchable > 0 then
for _, switch_nr in ipairs(switchable) do
-- If we're looking for a buffer after the current one, break here
if switch_nr < bufnr then
-- Keep looping to find the previous buffer
-- This also serves as a fallback if there's no
-- next buffer, and `focus_next` is true
switch_target = switch_nr
local switch_target
if #switchable > 0 then
for _, switch_nr in ipairs(switchable) do
-- If we're looking for a buffer after the current one, break here
if switch_nr < bufnr then
-- Keep looping to find the previous buffer
-- This also serves as a fallback if there's no
-- next buffer, and `focus_next` is true
switch_target = switch_nr
end
if switch_nr > bufnr and (focus_next or switch_target == nil) then
-- We found the next buffer, break
switch_target = switch_nr
break
end
end
if switch_nr > bufnr and (focus_next or switch_target == nil) then
-- We found the next buffer, break
switch_target = switch_nr
break
else
-- If there's no possible switch target, create a new buffer and switch to it
switch_target = vim.api.nvim_create_buf(true, false)
if switch_target == 0 then
vim.api.nvim_err_writeln("Failed to create new buffer")
end
end
else
-- If there's no possible switch target, create a new buffer and switch to it
switch_target = vim.api.nvim_create_buf(true, false)
if switch_target == 0 then
vim.api.nvim_err_writeln("Failed to create new buffer")
end
end

vim.api.nvim_win_set_buf(win, switch_target)
vim.api.nvim_win_set_buf(win, switch_target)
end

if wipeout then
if vim.api.nvim_buf_is_valid(bufnr) then
Expand Down