Skip to content

Commit 46d8e5c

Browse files
phanenibhagwan
authored andcommitted
refactor(actions): simplify vimcmd_entry
1 parent 94e2ae0 commit 46d8e5c

2 files changed

Lines changed: 36 additions & 36 deletions

File tree

lua/fzf-lua/actions.lua

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -150,29 +150,28 @@ M.resume = function(_, _)
150150
require("fzf-lua").resume()
151151
end
152152

153-
local edit_entry = function(entry, fullpath, will_replace_curbuf, opts)
153+
---requested buffer already loaded in the current window (split?)
154+
---@param buf? integer
155+
---@param fullpath string
156+
---@return boolean
157+
local buf_edited = function(buf, fullpath)
154158
local curbuf = vim.api.nvim_win_get_buf(0)
155-
local curbname = vim.api.nvim_buf_get_name(curbuf)
156-
if entry.bufnr == curbuf or path.equals(curbname, fullpath) then
157-
-- requested buffer already loaded in the current window (split?)
158-
return true
159-
end
160-
local bufnr = entry.bufnr or (function()
161-
-- Always open files relative to the current win/tab cwd (#1854)
162-
-- We normalize the path or Windows will fail with directories starting
163-
-- with special characters, for example "C:\app\(web)" will be translated
164-
-- by neovim to "c:\app(web)" (#1082)
165-
local relpath = path.normalize(path.relative_to(fullpath, utils.cwd()))
166-
local bufnr0 = vim.fn.bufadd(relpath)
167-
if bufnr0 == 0 and not opts.silent then
168-
utils.warn("Unable to add buffer %s", relpath)
169-
return
170-
end
171-
vim.bo[bufnr0].buflisted = true
172-
return bufnr0
173-
end)()
174-
-- abort if we're unable to load the buffer
175-
if not tonumber(bufnr) then return end
159+
return buf == curbuf or path.equals(fullpath, vim.api.nvim_buf_get_name(curbuf))
160+
end
161+
162+
---@param relpath string
163+
---@return integer?
164+
local load_buf = function(relpath)
165+
local bufnr = vim.fn.bufadd(relpath)
166+
if bufnr == 0 then return end
167+
vim.bo[bufnr].buflisted = true
168+
return bufnr
169+
end
170+
171+
---@param bufnr integer
172+
---@param will_replace_curbuf boolean
173+
---@return boolean? success
174+
local set_buf = function(bufnr, will_replace_curbuf)
176175
-- wipe unnamed empty buffers (e.g. "new") on switch
177176
if will_replace_curbuf
178177
and vim.bo.buftype == ""
@@ -223,17 +222,13 @@ M.vimcmd_entry = function(vimcmd, selected, opts, bufedit)
223222
if not utils.is_term_buffer(0) then
224223
vim.cmd("normal! m`")
225224
end
225+
-- Always open files relative to the current win/tab cwd (#1854)
226+
-- We normalize the path or Windows will fail with directories starting
227+
-- with special characters, for example "C:\app\(web)" will be translated
228+
-- by neovim to "c:\app(web)" (#1082)
229+
local relpath = path.normalize(path.relative_to(fullpath, utils.cwd()))
226230
if bufedit then
227-
local will_replace_curbuf = (function()
228-
if #vimcmd > 0 then return false end
229-
local curbuf = vim.api.nvim_win_get_buf(0)
230-
local curbname = vim.api.nvim_buf_get_name(curbuf)
231-
if entry.bufnr == curbuf or path.equals(curbname, fullpath) then
232-
-- requested buffer already loaded in the current window (split?)
233-
return false
234-
end
235-
return true
236-
end)()
231+
local will_replace_curbuf = #vimcmd == 0 and not buf_edited(entry.bufnr, fullpath)
237232
if will_replace_curbuf then
238233
if utils.wo.winfixbuf then
239234
utils.warn("'winfixbuf' is set for current window, will open in a split.")
@@ -242,18 +237,23 @@ M.vimcmd_entry = function(vimcmd, selected, opts, bufedit)
242237
and not vim.o.confirm
243238
and not vim.o.autowriteall
244239
and utils.buffer_is_dirty(vim.api.nvim_get_current_buf(), true, true) then
240+
if not opts.silent then utils.warn("cannot replace modified buffer") end
245241
return
246242
end
247243
end
248244
if #vimcmd > 0 then vim.cmd(vimcmd) end
249245
-- NOTE: URI entries only execute new buffers (new|vnew|tabnew)
250246
-- and later use `utils.jump_to_location` to load the buffer
251-
if not entry.uri and not edit_entry(entry, fullpath, will_replace_curbuf, opts) then
247+
if not entry.uri and not buf_edited(entry.bufnr, fullpath) then
252248
-- error loading buffer or save dialog cancelled
253-
return
249+
local bufnr = entry.bufnr or load_buf(relpath)
250+
if not bufnr then
251+
if not opts.silent then utils.warn("Unable to add buffer %s", fullpath) end
252+
return
253+
end
254+
if not set_buf(bufnr, will_replace_curbuf) then return end
254255
end
255256
else
256-
local relpath = path.normalize(path.relative_to(fullpath, utils.cwd()))
257257
vim.cmd(("%s %s"):format(vimcmd, relpath))
258258
end
259259
-- Reload actions from fzf's (buf/arg del, etc) window end here

lua/fzf-lua/utils.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ function M.is_term_buffer(bufnr)
10251025
return vim.api.nvim_buf_is_valid(bufnr) and vim.bo[bufnr].buftype == "terminal"
10261026
end
10271027

1028-
---@param bufnr integer
1028+
---@param bufnr? integer
10291029
---@param warn? boolean
10301030
---@param only_if_last_buffer? boolean
10311031
---@return boolean

0 commit comments

Comments
 (0)