From 9ae7c2aff3e8716ac78d579327a24ef2d2f8a2d0 Mon Sep 17 00:00:00 2001 From: siduck Date: Sun, 1 May 2022 08:01:47 +0530 Subject: [PATCH] fix error while closing file (#1002) --- lua/core/options.lua | 1 + lua/core/utils.lua | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lua/core/options.lua b/lua/core/options.lua index 80863da..342a1cd 100644 --- a/lua/core/options.lua +++ b/lua/core/options.lua @@ -5,6 +5,7 @@ local g = vim.g g.did_load_filetypes = 0 g.do_filetype_lua = 1 +opt.confirm = true opt.laststatus = 3 -- global statusline opt.title = true opt.clipboard = "unnamedplus" diff --git a/lua/core/utils.lua b/lua/core/utils.lua index 04de6b0..33b095f 100644 --- a/lua/core/utils.lua +++ b/lua/core/utils.lua @@ -3,11 +3,25 @@ local M = {} local cmd = vim.cmd M.close_buffer = function(force) - if vim.bo.buftype == "terminal" then vim.api.nvim_win_hide(0) return end - force = force or not vim.bo.buflisted or vim.bo.buftype == "nofile" - -- if not force, change to prev buf and then close current - local close_cmd = force and ":bd!" or ":bp | bd" .. vim.fn.bufnr() - vim.cmd(close_cmd) + if vim.bo.buftype == "terminal" then + vim.api.nvim_win_hide(0) + return + end + + local fileExists = vim.fn.filereadable(vim.fn.expand "%p") + local modified = vim.api.nvim_buf_get_option(vim.fn.bufnr(), "modified") + + -- if file doesnt exist & its modified + if fileExists == 0 and modified then + print "no file name? add it now!" + return + end + + force = force or not vim.bo.buflisted or vim.bo.buftype == "nofile" + + -- if not force, change to prev buf and then close current + local close_cmd = force and ":bd!" or ":bp | bd" .. vim.fn.bufnr() + vim.cmd(close_cmd) end M.load_config = function() @@ -96,7 +110,9 @@ end M.remove_default_plugins = function(plugins) local removals = require("core.utils").load_config().plugins.remove or {} if not vim.tbl_isempty(removals) then - for _, plugin in pairs(removals) do plugins[plugin] = nil end + for _, plugin in pairs(removals) do + plugins[plugin] = nil + end end return plugins end