diff --git a/lua/fzf-lua/actions.lua b/lua/fzf-lua/actions.lua index a1216cc..f7fd77c 100644 --- a/lua/fzf-lua/actions.lua +++ b/lua/fzf-lua/actions.lua @@ -173,6 +173,32 @@ M.file_edit_or_qf = function(selected, opts) end end +M.file_switch = function(selected, opts) + local bufnr = nil + local entry = path.entry_to_file(selected[1]) + local fullpath = entry.path + if not path.starts_with_separator(fullpath) then + fullpath = path.join({opts.cwd or vim.loop.cwd(), fullpath}) + end + for _, b in ipairs(vim.api.nvim_list_bufs()) do + local bname = vim.api.nvim_buf_get_name(b) + if bname and bname == fullpath then + bufnr = b + break + end + end + if not bufnr then return false end + local is_term = utils.is_term_buffer(0) + if not is_term then vim.cmd("normal! m`") end + local winid = utils.winid_from_tab_buf(0, bufnr) + if winid then vim.api.nvim_set_current_win(winid) end + if entry.line>1 or entry.col>1 then + vim.api.nvim_win_set_cursor(0, {tonumber(entry.line), tonumber(entry.col)-1}) + end + if not is_term then vim.cmd("norm! zvzz") end + return true +end + -- buffer actions M.vimcmd_buf = function(vimcmd, selected, _) for i = 1, #selected do diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index fd48b5f..ff504f5 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -725,6 +725,7 @@ M._action_to_helpstr = { [actions.file_vsplit] = "file-vsplit", [actions.file_tabedit] = "file-tabedit", [actions.file_sel_to_qf] = "file-selection-to-qf", + [actions.file_switch] = "file-switch", [actions.buf_edit] = "buffer-edit", [actions.buf_split] = "buffer-split", [actions.buf_vsplit] = "buffer-vsplit",