Skip to content

Commit b7aaca9

Browse files
phanenibhagwan
authored andcommitted
refactor: remove <auto> hack
1 parent aedcb6e commit b7aaca9

1 file changed

Lines changed: 70 additions & 71 deletions

File tree

lua/fzf-lua/actions.lua

Lines changed: 70 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,58 @@ M.resume = function(_, _)
139139
require("fzf-lua").resume()
140140
end
141141

142-
---@param _vimcmd string
142+
local edit_entry = function(entry, fullpath, will_replace_curbuf, opts)
143+
-- URI entries only execute new buffers (new|vnew|tabnew)
144+
-- and later use `utils.jump_to_location` to load the buffer
145+
local bufnr = (function()
146+
-- Is the requested buffer is already loaded by the (split) command?
147+
local curbuf = vim.api.nvim_win_get_buf(0)
148+
local curbname = vim.api.nvim_buf_get_name(curbuf)
149+
if entry.bufnr == curbuf or path.equals(curbname, fullpath) then return end
150+
-- Entry already contains bufnr
151+
if entry.bufnr then return entry.bufnr end
152+
-- Always open files relative to the current win/tab cwd (#1854)
153+
-- We normalize the path or Windows will fail with directories starting
154+
-- with special characters, for example "C:\app\(web)" will be translated
155+
-- by neovim to "c:\app(web)" (#1082)
156+
local relpath = path.normalize(path.relative_to(fullpath, uv.cwd()))
157+
local bufnr = vim.fn.bufadd(relpath)
158+
if bufnr == 0 and not opts.silent then
159+
utils.warn("Unable to add buffer %s", relpath)
160+
return
161+
else
162+
vim.bo[bufnr].buflisted = true
163+
return bufnr
164+
end
165+
end)()
166+
if tonumber(bufnr) then
167+
-- If current buffer is an unnamed empty buffer (e.g. "new"), wipe on switch
168+
if will_replace_curbuf
169+
and vim.bo.buftype == ""
170+
and vim.bo.filetype == ""
171+
and vim.api.nvim_buf_line_count(0) == 1
172+
and vim.api.nvim_buf_get_lines(0, 0, -1, false)[1] == ""
173+
and vim.api.nvim_buf_get_name(0) == ""
174+
then
175+
vim.bo.bufhidden = "wipe"
176+
end
177+
-- NOTE: nvim_set_current_buf will load the buffer if needed
178+
-- calling bufload will mess up `BufReadPost` autocmds
179+
-- vim.fn.bufload(bufnr)
180+
local ok, _ = pcall(vim.api.nvim_set_current_buf, bufnr)
181+
-- When `:set nohidden && set confirm`, neovim will invoke the save dialog
182+
-- and confirm with the user when trying to switch from a dirty buffer, if
183+
-- user cancelles the save dialog pcall will fail with:
184+
-- Vim:E37: No write since last change (add ! to override)
185+
if not ok then return end
186+
end
187+
end
188+
189+
---@param vimcmd string
143190
---@param selected string[]
144191
---@param opts fzf-lua.Config
145192
---@return string?
146-
M.vimcmd_entry = function(_vimcmd, selected, opts)
193+
M.vimcmd_entry = function(vimcmd, selected, opts)
147194
for i, sel in ipairs(selected) do
148195
(function()
149196
-- Lua 5.1 goto compatiblity hack (function wrap)
@@ -163,72 +210,25 @@ M.vimcmd_entry = function(_vimcmd, selected, opts)
163210
-- technically we should never get to the `uv.cwd()` fallback
164211
fullpath = path.join({ opts.cwd or opts._cwd or uv.cwd(), fullpath })
165212
end
166-
-- <auto> (without prefix, formerly `:b|e`) replace the current buffer
167-
local vimcmd, will_replace_curbuf = _vimcmd, _vimcmd == "<auto>"
168-
if will_replace_curbuf and utils.wo.winfixbuf then
169-
utils.warn("'winfixbuf' is set for current window, will open in a split.")
170-
vimcmd = "split | " .. vimcmd
171-
end
213+
local is_buf_edit = opts._is_buf_edit
172214
-- Can't be called from term window (for example, "reload" actions) due to
173215
-- nvim_exec2(): Vim(normal):Can't re-enter normal mode from terminal mode
174216
-- NOTE: we do not use `opts.__CTX.bufnr` as caller might be the fzf term
175217
if not utils.is_term_buffer(0) then
176218
vim.cmd("normal! m`")
177219
end
178-
if vimcmd then
179-
local cmd, is_buf_edit = vimcmd:gsub("|?%s-<auto>$", "")
180-
if is_buf_edit == 0 then
181-
local relpath = path.normalize(path.relative_to(fullpath, uv.cwd()))
182-
vim.cmd(("%s %s"):format(cmd, relpath))
183-
elseif not entry.uri and is_buf_edit > 0 then
184-
-- Command could have been "<auto>", in which case do nothing
185-
-- as we only have to load the buffer into the current window
186-
if #cmd > 0 then vim.cmd(cmd) end
187-
-- URI entries only execute new buffers (new|vnew|tabnew)
188-
-- and later use `utils.jump_to_location` to load the buffer
189-
local bufnr = (function()
190-
-- Is the requested buffer is already loaded by the (split) command?
191-
local curbuf = vim.api.nvim_win_get_buf(0)
192-
local curbname = vim.api.nvim_buf_get_name(curbuf)
193-
if entry.bufnr == curbuf or path.equals(curbname, fullpath) then return end
194-
-- Entry already contains bufnr
195-
if entry.bufnr then return entry.bufnr end
196-
-- Always open files relative to the current win/tab cwd (#1854)
197-
-- We normalize the path or Windows will fail with directories starting
198-
-- with special characters, for example "C:\app\(web)" will be translated
199-
-- by neovim to "c:\app(web)" (#1082)
200-
local relpath = path.normalize(path.relative_to(fullpath, uv.cwd()))
201-
local bufnr = vim.fn.bufadd(relpath)
202-
if bufnr == 0 and not opts.silent then
203-
utils.warn("Unable to add buffer %s", relpath)
204-
return
205-
else
206-
vim.bo[bufnr].buflisted = true
207-
return bufnr
208-
end
209-
end)()
210-
if tonumber(bufnr) then
211-
-- If current buffer is an unnamed empty buffer (e.g. "new"), wipe on switch
212-
if will_replace_curbuf
213-
and vim.bo.buftype == ""
214-
and vim.bo.filetype == ""
215-
and vim.api.nvim_buf_line_count(0) == 1
216-
and vim.api.nvim_buf_get_lines(0, 0, -1, false)[1] == ""
217-
and vim.api.nvim_buf_get_name(0) == ""
218-
then
219-
vim.bo.bufhidden = "wipe"
220-
end
221-
-- NOTE: nvim_set_current_buf will load the buffer if needed
222-
-- calling bufload will mess up `BufReadPost` autocmds
223-
-- vim.fn.bufload(bufnr)
224-
local ok, _ = pcall(vim.api.nvim_set_current_buf, bufnr)
225-
-- When `:set nohidden && set confirm`, neovim will invoke the save dialog
226-
-- and confirm with the user when trying to switch from a dirty buffer, if
227-
-- user cancelles the save dialog pcall will fail with:
228-
-- Vim:E37: No write since last change (add ! to override)
229-
if not ok then return end
230-
end
220+
if not is_buf_edit then
221+
local relpath = path.normalize(path.relative_to(fullpath, uv.cwd()))
222+
vim.cmd(("%s %s"):format(vimcmd, relpath))
223+
elseif not entry.uri then
224+
-- <auto> (without prefix, formerly `:b|e`) replace the current buffer
225+
local will_replace_curbuf = #vimcmd == 0
226+
if will_replace_curbuf and utils.wo.winfixbuf then
227+
utils.warn("'winfixbuf' is set for current window, will open in a split.")
228+
vimcmd, will_replace_curbuf = "split", false
231229
end
230+
if #vimcmd > 0 then vim.cmd(vimcmd) end
231+
edit_entry(entry, fullpath, will_replace_curbuf, opts)
232232
end
233233
-- Reload actions from fzf's (buf/arg del, etc) window end here
234234
if utils.is_term_buffer(0) and vim.bo.ft == "fzf" then
@@ -263,29 +263,28 @@ end
263263

264264
-- file actions
265265
M.file_edit = function(selected, opts)
266-
local vimcmd = "<auto>"
267-
M.vimcmd_entry(vimcmd, selected, opts)
266+
opts._is_buf_edit = true
267+
M.vimcmd_entry("", selected, opts)
268268
end
269269

270270
M.file_split = function(selected, opts)
271-
local vimcmd = "split | <auto>"
272-
M.vimcmd_entry(vimcmd, selected, opts)
271+
opts._is_buf_edit = true
272+
M.vimcmd_entry("split", selected, opts)
273273
end
274274

275275
M.file_vsplit = function(selected, opts)
276-
local vimcmd = "vsplit | <auto>"
277-
M.vimcmd_entry(vimcmd, selected, opts)
276+
M.vimcmd_entry("vsplit", selected, opts)
278277
end
279278

280279
M.file_tabedit = function(selected, opts)
281280
-- local vimcmd = "tab split | <auto>"
282-
local vimcmd = "tabnew | setlocal bufhidden=wipe | <auto>"
283-
M.vimcmd_entry(vimcmd, selected, opts)
281+
opts._is_buf_edit = true
282+
M.vimcmd_entry("tabnew | setlocal bufhidden=wipe", selected, opts)
284283
end
285284

286285
M.file_open_in_background = function(selected, opts)
287-
local vimcmd = "badd"
288-
M.vimcmd_entry(vimcmd, selected, opts)
286+
opts._is_buf_edit = true
287+
M.vimcmd_entry("badd", selected, opts)
289288
end
290289

291290
local sel_to_qf = function(selected, opts, is_loclist)

0 commit comments

Comments
 (0)