From 0d059543978bb836ec27e855767b5cf16648eb04 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Wed, 4 Aug 2021 15:22:10 -0700 Subject: [PATCH] previewers migrated to new async_action --- README.md | 16 ++--- lua/fzf-lua/config.lua | 19 ++++-- lua/fzf-lua/core.lua | 3 +- lua/fzf-lua/previewer/init.lua | 85 ++++++++++++++++++-------- lua/fzf-lua/providers/buffers.lua | 2 +- lua/fzf-lua/providers/colorschemes.lua | 2 +- lua/fzf-lua/providers/module.lua | 2 +- 7 files changed, 86 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 6a7ad84..98b9509 100644 --- a/README.md +++ b/README.md @@ -76,17 +76,11 @@ Using [packer.nvim](https://github.com/wbthomason/packer.nvim) ```lua use { 'ibhagwan/fzf-lua', requires = { - 'vijaymarupudi/nvim-fzf', -- read note #1 below + 'vijaymarupudi/nvim-fzf', 'kyazdani42/nvim-web-devicons' } -- optional for icons } ``` -> **Note 1:** if you're having issues previewing grep or LSP matches that contain -> special characters (back-tick especially) you need to replace `vijaymarupudi/nvim-fzf` with -> my fork, `ibhagwan/nvim-fzf`. This is due to the issue described in [`nvim-fzf` -> PR#18](https://github.com/vijaymarupudi/nvim-fzf/pull/18/). - - -> **Note 2:** if you already have fzf installed you do not need to install `fzf` +> **Note:** if you already have fzf installed you do not need to install `fzf` > or `fzf.vim`, however if you do not have it installed, **you only need** fzf > which can be installed with (fzf.vim is not a requirement nor conflict): > ```vim @@ -202,6 +196,12 @@ require'fzf-lua'.setup { -- default_previewer = "bat", -- override the default previewer? -- by default auto-detect bat|cat previewers = { + cmd = { + -- custom previewer, will execute: + -- ` ` + cmd = "echo", + args = "", + }, cat = { cmd = "cat", args = "--number", diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index 745f9c8..d06cfdf 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -63,19 +63,30 @@ M.globals = { flip_columns = 120, default_previewer = utils._if(vim.fn.executable("bat")==1, "bat", "cat"), previewers = { - cat = { - cmd = "cat", - args = "--number", + cmd = { + -- custom previewer to be overidden by the user + cmd = "", + args = "", -- we use function here instead of the object due to -- vim.tbl_deep_extend not copying metatables and -- metamethods (__index and __call) - _new = function() return require 'fzf-lua.previewer'.cmd end, + _new = function() return require 'fzf-lua.previewer'.cmd_async end, + }, + cat = { + cmd = "cat", + args = "--number", + _new = function() return require 'fzf-lua.previewer'.cmd_async end, }, bat = { cmd = "bat", args = "--italic-text=always --style=numbers,changes --color always", theme = nil, config = nil, + _new = function() return require 'fzf-lua.previewer'.bat_async end, + }, + bat_native = { + cmd = "bat", + args = "--italic-text=always --style=numbers,changes --color always", _new = function() return require 'fzf-lua.previewer'.bat end, }, head = { diff --git a/lua/fzf-lua/core.lua b/lua/fzf-lua/core.lua index 0399408..62b9333 100644 --- a/lua/fzf-lua/core.lua +++ b/lua/fzf-lua/core.lua @@ -32,7 +32,7 @@ M.build_fzf_cli = function(opts) vim.fn.shellescape(opts.prompt), utils._if(opts.preview_window, opts.preview_window, config.preview_window()), utils._if(#opts.preview_offset>0, ":"..opts.preview_offset, ''), - utils._if(opts.preview, vim.fn.shellescape(opts.preview), "''"), + utils._if(opts.preview, opts.preview, "''"), -- HACK: support skim (rust version of fzf) utils._if(opts.fzf_bin and opts.fzf_bin:find('sk')~=nil, "--inline-info", "--info=inline"), utils._if(actions.expect(opts.actions), actions.expect(opts.actions), ''), @@ -171,7 +171,6 @@ M.fzf_files = function(opts) if preview_opts then local preview = preview_opts._new()(preview_opts, opts) opts.preview = preview:cmdline() - -- opts.preview = preview:action() end local selected = fzf.fzf(opts.fzf_fn, diff --git a/lua/fzf-lua/previewer/init.lua b/lua/fzf-lua/previewer/init.lua index 5a7db81..727ca31 100644 --- a/lua/fzf-lua/previewer/init.lua +++ b/lua/fzf-lua/previewer/init.lua @@ -1,5 +1,6 @@ local path = require "fzf-lua.path" local utils = require "fzf-lua.utils" +local helpers = require("fzf.helpers") local raw_action = require("fzf.actions").raw_action local Previewer = {} @@ -7,6 +8,8 @@ Previewer.base = {} Previewer.head = {} Previewer.cmd = {} Previewer.bat = {} +Previewer.cmd_async = {} +Previewer.bat_async = {} Previewer.buffer = {} -- Constructors call on Previewer.() @@ -29,12 +32,6 @@ function Previewer.base:new(o, opts) return self end -function Previewer.base:filespec(entry) - local file = path.entry_to_file(entry, nil, true) - print(file.file, file.line, file.col) - return file -end - -- Generic shell command previewer function Previewer.cmd:new(o, opts) self = setmetatable(Previewer.base(o, opts), { @@ -44,11 +41,11 @@ function Previewer.cmd:new(o, opts) return self end - function Previewer.cmd:cmdline(o) o = o or {} o.action = o.action or self:action(o) - return string.format("%s %s `%s`", self.cmd, self.args, o.action) + return vim.fn.shellescape(string.format('sh -c "%s %s `%s`"', + self.cmd, self.args, o.action)) end function Previewer.cmd:action(o) @@ -82,20 +79,8 @@ function Previewer.bat:cmdline(o) if self.opts._line_placeholder then highlight_line = string.format("--highlight-line={%d}", self.opts._line_placeholder) end - return string.format('sh -c "%s %s %s -- `%s`"', - self.cmd, self.args, highlight_line, self:action(o)) - --[[ return string.format("%s %s `%s` -- `%s`", - self.cmd, self.args, self:action_line(), o.action) ]] -end - --- not in use -function Previewer.bat:action_line(o) - o = o or {} - local act = raw_action(function (items, _, _) - local file = path.entry_to_file(items[1], self.opts.cwd) - return string.format("--highlight-line=%s", tostring(file.line)) - end) - return act + return vim.fn.shellescape(string.format('sh -c "%s %s %s `%s`"', + self.cmd, self.args, highlight_line, self:action(o))) end -- Specialized head previewer @@ -115,10 +100,58 @@ function Previewer.head:cmdline(o) if self.opts._line_placeholder then lines = string.format("--lines={%d}", self.opts._line_placeholder) end - return string.format('sh -c "%s %s %s -- `%s`"', - self.cmd, self.args, lines, self:action(o)) - --[[ return string.format("%s %s `%s` -- `%s`", - self.cmd, self.args, self:action_line(), o.action) ]] + return vim.fn.shellescape(string.format('sh -c "%s %s %s `%s`"', + self.cmd, self.args, lines, self:action(o))) +end + +-- new async_action from nvim-fzf +function Previewer.cmd_async:new(o, opts) + self = setmetatable(Previewer.base(o, opts), { + __index = vim.tbl_deep_extend("keep", + self, Previewer.base + )}) + return self +end + +function Previewer.cmd_async:cmdline(o) + o = o or {} + local act = helpers.choices_to_shell_cmd_previewer(function(items) + local file = path.entry_to_file(items[1], self.opts.cwd) + local cmd = string.format("%s %s %s", self.cmd, self.args, file.path) + -- uncomment to see the command in the preview window + -- cmd = vim.fn.shellescape(cmd) + return cmd + end) + return act +end + +function Previewer.bat_async:new(o, opts) + self = setmetatable(Previewer.cmd(o, opts), { + __index = vim.tbl_deep_extend("keep", + self, Previewer.cmd, Previewer.base + )}) + self.theme = o.theme + return self +end + +function Previewer.bat_async:cmdline(o) + o = o or {} + local highlight_line = "" + if self.opts._line_placeholder then + highlight_line = string.format("--highlight-line=", self.opts._line_placeholder) + end + local act = helpers.choices_to_shell_cmd_previewer(function(items) + local file = path.entry_to_file(items[1], self.opts.cwd) + local cmd = string.format("%s %s %s%s %s", + self.cmd, self.args, + highlight_line, + utils._if(#highlight_line>0, tostring(file.line), ""), + file.path) + -- uncomment to see the command in the preview window + -- cmd = vim.fn.shellescape(cmd) + return cmd + end) + return act end return Previewer diff --git a/lua/fzf-lua/providers/buffers.lua b/lua/fzf-lua/providers/buffers.lua index 88e01e7..df39b5b 100644 --- a/lua/fzf-lua/providers/buffers.lua +++ b/lua/fzf-lua/providers/buffers.lua @@ -2,7 +2,7 @@ if not pcall(require, "fzf") then return end -local action = require("fzf.actions").raw_action +local action = require("fzf.actions").action local core = require "fzf-lua.core" local path = require "fzf-lua.path" local utils = require "fzf-lua.utils" diff --git a/lua/fzf-lua/providers/colorschemes.lua b/lua/fzf-lua/providers/colorschemes.lua index aa400d7..8186563 100644 --- a/lua/fzf-lua/providers/colorschemes.lua +++ b/lua/fzf-lua/providers/colorschemes.lua @@ -3,7 +3,7 @@ if not pcall(require, "fzf") then end local fzf = require "fzf" -local action = require("fzf.actions").raw_action +local action = require("fzf.actions").action local core = require "fzf-lua.core" local utils = require "fzf-lua.utils" local config = require "fzf-lua.config" diff --git a/lua/fzf-lua/providers/module.lua b/lua/fzf-lua/providers/module.lua index b64bfdb..1eae04f 100644 --- a/lua/fzf-lua/providers/module.lua +++ b/lua/fzf-lua/providers/module.lua @@ -3,7 +3,7 @@ if not pcall(require, "fzf") then end local fzf = require "fzf" -local action = require("fzf.actions").raw_action +local action = require("fzf.actions").action local core = require "fzf-lua.core" local config = require "fzf-lua.config" local actions = require "fzf-lua.actions"