From 247b9d8604d919af562cc0580d9e96c76d408c88 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Thu, 8 Aug 2024 12:05:55 -0700 Subject: [PATCH] WIP --- lua/fzf-lua/actions.lua | 49 ++++++++++++++++++++++++++++++- lua/fzf-lua/path.lua | 1 + lua/fzf-lua/previewer/builtin.lua | 10 ------- lua/fzf-lua/previewer/fzf.lua | 1 + lua/fzf-lua/profiles/fzf-vim.lua | 1 + lua/fzf-lua/utils.lua | 4 +++ 6 files changed, 55 insertions(+), 11 deletions(-) diff --git a/lua/fzf-lua/actions.lua b/lua/fzf-lua/actions.lua index 16500de5..e2464bbf 100644 --- a/lua/fzf-lua/actions.lua +++ b/lua/fzf-lua/actions.lua @@ -114,6 +114,54 @@ M.vimcmd = function(vimcmd, selected, noesc) end end +M.vimcmd_entry = function(vimcmd, selected, opts, pcall_vimcmd) + for _, sel in ipairs(selected) do + (function() + -- Lua 5.1 goto compatiblity hack (function wrap) + local entry = path.entry_to_file(sel, opts, opts.force_uri) + -- "" could be set by `autocmds` + if entry.path == "" then return end + -- Is valid and loaded buffer? + local fullpath = entry.bufname or entry.path or entry.uri and entry.uri:match("^%a+://(.*)") + if not path.is_absolute(fullpath) then + fullpath = path.join({ opts.cwd or opts._cwd or uv.cwd(), fullpath }) + end + -- Are we replacing the origin buffer? + local will_replace_curbuf = vimcmd == "e" and path.equals(fullpath, utils.CTX().bname) + or vimcmd == "b" and entry.bufnr and entry.bufnr == utils.CTX().bufnr + if will_replace_curbuf + and not vim.o.hidden + and not vim.o.autowriteall + and utils.buffer_is_dirty(nil, false, true) then + -- when `:set nohidden`, confirm with the user when trying to switch + -- from a dirty buffer, abort if declined, save buffer if requested + if utils.save_dialog(nil) then + vimcmd = vimcmd .. "!" + else + return + end + end + if will_replace_curbuf + and vim.fn.exists("&winfixbuf") == 1 + and vim.wo.winfixbuf + then + utils.warn("'winfixbuf' is set for current window, will open in a split.") + vimcmd = "split | " .. vimcmd + end + -- Add current location to jumplist + if not entry.term then vim.cmd("normal! m`") end + -- Killing term buffers requires "!" (#1078) + if entry.term and vimcmd == "bd" then + vimcmd = vimcmd .. "!" + end + -- Only change from current buffer if target is different + if (vimcmd == "b" or vimcmd == "e") then + else + end + end)() + end +end + M.vimcmd_file = function(vimcmd, selected, opts, pcall_vimcmd) local curbuf = vim.api.nvim_buf_get_name(0) local is_term = utils.is_term_buffer(0) @@ -122,7 +170,6 @@ M.vimcmd_file = function(vimcmd, selected, opts, pcall_vimcmd) -- Lua 5.1 goto compatiblity hack (function wrap) local entry = path.entry_to_file(selected[i], opts, opts.force_uri) if entry.path == "" then return end - entry.ctag = opts._ctag and path.entry_to_ctag(selected[i]) local fullpath = entry.path or entry.uri and entry.uri:match("^%a+://(.*)") if not path.is_absolute(fullpath) then fullpath = path.join({ opts.cwd or opts._cwd or uv.cwd(), fullpath }) diff --git a/lua/fzf-lua/path.lua b/lua/fzf-lua/path.lua index 9111c226..86d97bd8 100644 --- a/lua/fzf-lua/path.lua +++ b/lua/fzf-lua/path.lua @@ -494,6 +494,7 @@ function M.entry_to_file(entry, opts, force_uri) path = file, line = tonumber(line) or 0, col = tonumber(col) or 0, + ctag = opts._ctag and M.entry_to_ctag(stripped) or nil, } end diff --git a/lua/fzf-lua/previewer/builtin.lua b/lua/fzf-lua/previewer/builtin.lua index 8971971a..e261a997 100644 --- a/lua/fzf-lua/previewer/builtin.lua +++ b/lua/fzf-lua/previewer/builtin.lua @@ -1118,16 +1118,6 @@ function Previewer.tags:new(o, opts, fzf_win) return self end -function Previewer.tags:parse_entry(entry_str) - -- first parse as normal entry - -- must use 'super.' and send self as 1st arg - -- or the ':' syntactic sugar will send super's - -- self which doesn't have self.opts - local entry = self.super.parse_entry(self, entry_str) - entry.ctag = path.entry_to_ctag(entry_str) - return entry -end - function Previewer.tags:set_cursor_hl(entry) -- pcall(vim.fn.clearmatches, self.win.preview_winid) pcall(api.nvim_win_call, self.win.preview_winid, function() diff --git a/lua/fzf-lua/previewer/fzf.lua b/lua/fzf-lua/previewer/fzf.lua index 284d6490..589a3939 100644 --- a/lua/fzf-lua/previewer/fzf.lua +++ b/lua/fzf-lua/previewer/fzf.lua @@ -181,6 +181,7 @@ function Previewer.cmd_async:parse_entry_and_verify(entrystr) -- make relative for bat's header display local filepath = path.relative_to(entry.bufname or entry.path or "", uv.cwd()) if self.opts._ctag then + -- NOTE: override `entry.ctag` with the unescaped version entry.ctag = path.entry_to_ctag(entry.stripped, true) if not tonumber(entry.line) or tonumber(entry.line) < 1 then -- default tags are without line numbers diff --git a/lua/fzf-lua/profiles/fzf-vim.lua b/lua/fzf-lua/profiles/fzf-vim.lua index 882e7339..3c958b48 100644 --- a/lua/fzf-lua/profiles/fzf-vim.lua +++ b/lua/fzf-lua/profiles/fzf-vim.lua @@ -24,6 +24,7 @@ local function setup_commands(no_override, prefix) ["Changes"] = utils.create_user_command_callback("changes"), ["Marks"] = utils.create_user_command_callback("marks"), ["Jumps"] = utils.create_user_command_callback("jumps"), + ["Commands"] = utils.create_user_command_callback("commands"), ["History"] = utils.create_user_command_callback("oldfiles", "query", { [":"] = "command_history", ["/"] = "search_history", diff --git a/lua/fzf-lua/utils.lua b/lua/fzf-lua/utils.lua index 0979c2bd..8b8170e5 100644 --- a/lua/fzf-lua/utils.lua +++ b/lua/fzf-lua/utils.lua @@ -808,6 +808,10 @@ function M.fzf_winobj() return loadstring("return require'fzf-lua'.win.__SELF()")() end +function M.CTX() + return loadstring("return require'fzf-lua'.core.CTX()")() +end + function M.resume_get(what, opts) local f = loadstring("return require'fzf-lua'.config.resume_get")() return f(what, opts)