Skip to content

Commit

Permalink
♻ Move bufdelete to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
Nguyen-Hoang-Nam committed Jul 9, 2022
1 parent c830e4c commit d4a86d5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lua/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ map("v", "<S-Tab>", "<gV", options)

map("n", "<Leader>q", [[<Cmd>let @/=""<CR>]], cmd_options)
map("n", "<Leader>s", [[:silent write<CR>]], cmd_options)
map("n", "<Leader>w", "", cmd_option(utils_core.bufdelete))
map("n", "<Leader>w", "", cmd_option(require("utils.bufdelete").bufdelete))

map("n", "<Leader>m", "", cmd_option(format.format))
map("n", "<Leader>c", "", cmd_option(format.range_format))
Expand Down
37 changes: 37 additions & 0 deletions lua/utils/bufdelete.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local bo = vim.bo
local cmd = vim.cmd
local api = vim.api

local M = {}

function M.bufdelete()
if bo.modified then
cmd("write")
end

local bufnr = api.nvim_get_current_buf()

-- TODO: Get list of valid buffers
local buffers = vim.tbl_filter(function(buf)
return bo[buf].buflisted and api.nvim_buf_is_valid(buf)
end, api.nvim_list_bufs())

if #buffers == 1 then
-- TODO: Hide scrollview before close buffer
cmd("silent! ScrollViewDisable | bd " .. bufnr .. " | silent! ScrollViewEnable")

-- TODO: Show Dashboard when closing the last buffer
require("dashboard").instance(true)
else
if bufnr ~= buffers[#buffers] then
cmd("bnext")
else
cmd("bprevious")
end

-- TODO: Hide scrollview before close buffer
cmd("silent! ScrollViewDisable | bd " .. bufnr .. " | silent! ScrollViewEnable")
end
end

return M
27 changes: 0 additions & 27 deletions lua/utils/core.lua
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
local bo = vim.bo
local cmd = vim.cmd
local api = vim.api

local M = {
is_rest = false,
}

function M.bufdelete()
if bo.modified then
cmd("write")
end

local bufnr = api.nvim_get_current_buf()

local buffers = vim.tbl_filter(function(buf)
return bo[buf].buflisted and api.nvim_buf_is_valid(buf)
end, api.nvim_list_bufs())

if #buffers == 1 then
cmd("silent! ScrollViewDisable | bd " .. bufnr .. " | silent! ScrollViewEnable")

require("dashboard").instance(true)
else
if bufnr ~= buffers[#buffers] then
cmd("bnext")
else
cmd("bprevious")
end

cmd("silent! ScrollViewDisable | bd " .. bufnr .. " | silent! ScrollViewEnable")
end
end

-- Count number of properties in table
-- Because of lua only count consecutive properties
-- @param T Table
Expand Down

0 comments on commit d4a86d5

Please sign in to comment.