Explorar o código

Merge branch 'rolling'

kylo252 %!s(int64=3) %!d(string=hai) anos
pai
achega
4afda11297
Modificáronse 3 ficheiros con 64 adicións e 1 borrados
  1. 62 0
      lua/lvim/core/bufferline.lua
  2. 1 0
      lua/lvim/core/commands.lua
  3. 1 1
      lua/lvim/core/which-key.lua

+ 62 - 0
lua/lvim/core/bufferline.lua

@@ -139,4 +139,66 @@ M.setup = function()
   end
 end
 
+-- Common kill function for bdelete and bwipeout
+-- credits: based on bbye and nvim-bufdel
+---@param kill_command String defaults to "bd"
+---@param bufnr Number defaults to the current buffer
+---@param force Boolean defaults to false
+function M.buf_kill(kill_command, bufnr, force)
+  local bo = vim.bo
+  local api = vim.api
+
+  if bufnr == 0 or bufnr == nil then
+    bufnr = api.nvim_get_current_buf()
+  end
+
+  kill_command = kill_command or "bd"
+
+  -- If buffer is modified and force isn't true, print error and abort
+  if not force and bo[bufnr].modified then
+    return api.nvim_err_writeln(
+      string.format("No write since last change for buffer %d (set force to true to override)", bufnr)
+    )
+  end
+
+  -- Get list of windows IDs with the buffer to close
+  local windows = vim.tbl_filter(function(win)
+    return api.nvim_win_get_buf(win) == bufnr
+  end, api.nvim_list_wins())
+
+  if #windows == 0 then
+    return
+  end
+
+  if force then
+    kill_command = kill_command .. "!"
+  end
+
+  -- Get list of active buffers
+  local buffers = vim.tbl_filter(function(buf)
+    return api.nvim_buf_is_valid(buf) and bo[buf].buflisted
+  end, api.nvim_list_bufs())
+
+  -- If there is only one buffer (which has to be the current one), vim will
+  -- create a new buffer on :bd.
+  -- For more than one buffer, pick the previous buffer (wrapping around if necessary)
+  if #buffers > 1 then
+    for i, v in ipairs(buffers) do
+      if v == bufnr then
+        local prev_buf_idx = i == 1 and (#buffers - 1) or (i - 1)
+        local prev_buffer = buffers[prev_buf_idx]
+        for _, win in ipairs(windows) do
+          api.nvim_win_set_buf(win, prev_buffer)
+        end
+      end
+    end
+  end
+
+  -- Check if buffer still exists, to ensure the target buffer wasn't killed
+  -- due to options like bufhidden=wipe.
+  if api.nvim_buf_is_valid(bufnr) and bo[bufnr].buflisted then
+    vim.cmd(string.format("%s %d", kill_command, bufnr))
+  end
+end
+
 return M

+ 1 - 0
lua/lvim/core/commands.lua

@@ -10,6 +10,7 @@ M.defaults = {
     endif
   endfunction
   ]],
+  [[ command! BufferKill lua require('user.bufferline').buf_kill('bd') ]],
   -- :LvimInfo
   [[ command! LvimInfo lua require('lvim.core.info').toggle_popup(vim.bo.filetype) ]],
   [[ command! LvimCacheReset lua require('lvim.utils.hooks').reset_cache() ]],

+ 1 - 1
lua/lvim/core/which-key.lua

@@ -67,7 +67,7 @@ M.config = function()
       ["w"] = { "<cmd>w!<CR>", "Save" },
       ["q"] = { "<cmd>q!<CR>", "Quit" },
       ["/"] = { "<cmd>lua require('Comment.api').toggle_current_linewise()<CR>", "Comment" },
-      ["c"] = { "<cmd>bdelete!<CR>", "Close Buffer" },
+      ["c"] = { "<cmd>BufferKill<CR>", "Close Buffer" },
       ["f"] = { require("lvim.core.telescope.custom-finders").find_project_files, "Find File" },
       ["h"] = { "<cmd>nohlsearch<CR>", "No Highlight" },
       b = {