Explorar o código

feat: prompt when closing modified/term buffers (#2658)

kylo252 %!s(int64=3) %!d(string=hai) anos
pai
achega
a11c46c29a
Modificáronse 1 ficheiros con 24 adicións e 10 borrados
  1. 24 10
      lua/lvim/core/bufferline.lua

+ 24 - 10
lua/lvim/core/bufferline.lua

@@ -142,26 +142,42 @@ M.setup = function()
   end
 end
 
+--stylua: ignore
+
 -- Common kill function for bdelete and bwipeout
 -- credits: based on bbye and nvim-bufdel
----@param kill_command string defaults to "bd"
+---@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)
+  kill_command = kill_command or "bd"
+
   local bo = vim.bo
   local api = vim.api
+  local fmt = string.format
+  local fnamemodify = vim.fn.fnamemodify
 
   if bufnr == 0 or bufnr == nil then
     bufnr = api.nvim_get_current_buf()
   end
 
-  kill_command = kill_command or "bd"
+  local bufname = api.nvim_buf_get_name(bufnr)
 
-  -- 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)
-    )
+  if not force then
+    local warning
+    if bo[bufnr].modified then
+      warning = fmt([[No write since last change for (%s)]], fnamemodify(bufname, ":t"))
+    elseif api.nvim_buf_get_option(bufnr, "buftype") == "terminal" then
+      warning = fmt([[Terminal %s will be killed]], bufname)
+    end
+    if warning then
+      vim.ui.input({
+        prompt = string.format([[%s. Close it anyway? [y]es or [n]o (default: no): ]], warning),
+      }, function(choice)
+        if choice:match "ye?s?" then force = true end
+      end)
+      if not force then return end
+    end
   end
 
   -- Get list of windows IDs with the buffer to close
@@ -169,9 +185,7 @@ function M.buf_kill(kill_command, bufnr, force)
     return api.nvim_win_get_buf(win) == bufnr
   end, api.nvim_list_wins())
 
-  if #windows == 0 then
-    return
-  end
+  if #windows == 0 then return end
 
   if force then
     kill_command = kill_command .. "!"