From 1d6ea8d38100b9b4b4f1e49626697c9c41a6759c Mon Sep 17 00:00:00 2001 From: bhagwan Date: Thu, 2 Dec 2021 09:19:20 -0800 Subject: [PATCH] lines|blines|grep_curbuf|lgrep_curbuf: hide filename by default fix help buffer syntax highlighting in blines|buffer preview --- README.md | 25 +++++++++++++++++++++++++ doc/fzf-lua.txt | 25 +++++++++++++++++++++++++ lua/fzf-lua/config.lua | 12 ++++++++++++ lua/fzf-lua/path.lua | 2 +- lua/fzf-lua/previewer/builtin.lua | 19 ++++++++++++++----- lua/fzf-lua/providers/buffers.lua | 6 ------ lua/fzf-lua/providers/grep.lua | 6 ++++-- 7 files changed, 81 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 0aa97fd..3bae596 100644 --- a/README.md +++ b/README.md @@ -504,11 +504,36 @@ require'fzf-lua'.setup { ["ctrl-x"] = actions.buf_del, } }, + lines = { + previewer = "builtin", -- set to 'false' to disable + prompt = 'Lines❯ ', + show_unlisted = false, -- exclude 'help' buffers + no_term_buffers = true, -- exclude 'term' buffers + fzf_opts = { + -- do not include bufnr in fuzzy matching + -- tiebreak by line no. + ['--delimiter'] = vim.fn.shellescape(']'), + ["--nth"] = '2..', + ["--tiebreak"] = 'index', + }, + actions = { + ["default"] = actions.buf_edit, + ["ctrl-s"] = actions.buf_split, + ["ctrl-v"] = actions.buf_vsplit, + ["ctrl-t"] = actions.buf_tabedit, + } + }, blines = { previewer = "builtin", -- set to 'false' to disable prompt = 'BLines❯ ', show_unlisted = true, -- include 'help' buffers no_term_buffers = false, -- include 'term' buffers + fzf_opts = { + -- hide filename, tiebreak by line no. + ['--delimiter'] = vim.fn.shellescape('[:]'), + ["--with-nth"] = '2..', + ["--tiebreak"] = 'index', + }, actions = { ["default"] = actions.buf_edit, ["ctrl-s"] = actions.buf_split, diff --git a/doc/fzf-lua.txt b/doc/fzf-lua.txt index 8c65aa4..9652e04 100644 --- a/doc/fzf-lua.txt +++ b/doc/fzf-lua.txt @@ -538,11 +538,36 @@ Consult the list below for available settings: ["ctrl-x"] = actions.buf_del, } }, + lines = { + previewer = "builtin", -- set to 'false' to disable + prompt = 'Lines❯ ', + show_unlisted = false, -- exclude 'help' buffers + no_term_buffers = true, -- exclude 'term' buffers + fzf_opts = { + -- do not include bufnr in fuzzy matching + -- tiebreak by line no. + ['--delimiter'] = vim.fn.shellescape(']'), + ["--nth"] = '2..', + ["--tiebreak"] = 'index', + }, + actions = { + ["default"] = actions.buf_edit, + ["ctrl-s"] = actions.buf_split, + ["ctrl-v"] = actions.buf_vsplit, + ["ctrl-t"] = actions.buf_tabedit, + } + }, blines = { previewer = "builtin", -- set to 'false' to disable prompt = 'BLines❯ ', show_unlisted = true, -- include 'help' buffers no_term_buffers = false, -- include 'term' buffers + fzf_opts = { + -- hide filename, tiebreak by line no. + ['--delimiter'] = vim.fn.shellescape('[:]'), + ["--with-nth"] = '2..', + ["--tiebreak"] = 'index', + }, actions = { ["default"] = actions.buf_edit, ["ctrl-s"] = actions.buf_split, diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index 7592087..a0f4ace 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -324,6 +324,13 @@ M.globals.lines = { prompt = 'Lines> ', file_icons = true and M._has_devicons, color_icons = true, + show_unlisted = false, + no_term_buffers = true, + fzf_opts = { + ['--delimiter'] = vim.fn.shellescape(']'), + ["--nth"] = '2..', + ["--tiebreak"] = 'index', + }, actions = { ["default"] = actions.buf_edit, ["ctrl-s"] = actions.buf_split, @@ -338,6 +345,11 @@ M.globals.blines = { color_icons = true, show_unlisted = true, no_term_buffers = false, + fzf_opts = { + ['--delimiter'] = vim.fn.shellescape('[:]'), + ["--with-nth"] = '2..', + ["--tiebreak"] = 'index', + }, actions = { ["default"] = actions.buf_edit, ["ctrl-s"] = actions.buf_split, diff --git a/lua/fzf-lua/path.lua b/lua/fzf-lua/path.lua index 79c13a5..9ed5d98 100644 --- a/lua/fzf-lua/path.lua +++ b/lua/fzf-lua/path.lua @@ -170,7 +170,7 @@ function M.entry_to_file(entry, cwd) if bufnr then terminal = utils.is_term_buffer(bufnr) if terminal then - file, line = stripped:match("(.*):(%d+)$") + file, line = stripped:match("([^:]+):(%d+)") end end return { diff --git a/lua/fzf-lua/previewer/builtin.lua b/lua/fzf-lua/previewer/builtin.lua index 09c5ba2..8c13246 100644 --- a/lua/fzf-lua/previewer/builtin.lua +++ b/lua/fzf-lua/previewer/builtin.lua @@ -252,6 +252,7 @@ function Previewer.buffer_or_file:populate_preview_buf(entry_str) else --]] -- mark the buffer for unloading the next call self.preview_bufloaded = true + entry.filetype = vim.api.nvim_buf_get_option(entry.bufnr, 'filetype') local lines = vim.api.nvim_buf_get_lines(entry.bufnr, 0, -1, false) vim.api.nvim_buf_set_lines(self.preview_bufnr, 0, -1, false, lines) -- end @@ -335,11 +336,19 @@ function Previewer.buffer_or_file:do_syntax(entry) )) end if syntax_limit_reached == 0 then - -- prepend the buffer number to the path and - -- set as buffer name, this makes sure 'filetype detect' - -- gets the right filetype which enables the syntax - local tempname = path.join({tostring(bufnr), entry.path}) - pcall(api.nvim_buf_set_name, bufnr, tempname) + if entry.filetype and #entry.filetype>0 then + -- filetype was saved from a loaded buffer + -- this helps avoid losing highlights for help buffers + -- which are '.txt' files with 'ft=help' + -- api.nvim_buf_set_option(bufnr, 'filetype', entry.filetype) + pcall(api.nvim_buf_set_option, bufnr, 'filetype', entry.filetype) + else + -- prepend the buffer number to the path and + -- set as buffer name, this makes sure 'filetype detect' + -- gets the right filetype which enables the syntax + local tempname = path.join({tostring(bufnr), entry.path}) + pcall(api.nvim_buf_set_name, bufnr, tempname) + end -- nvim_buf_call has less side-effects than window switch api.nvim_buf_call(bufnr, function() vim.cmd('filetype detect') diff --git a/lua/fzf-lua/providers/buffers.lua b/lua/fzf-lua/providers/buffers.lua index 0f48636..253f499 100644 --- a/lua/fzf-lua/providers/buffers.lua +++ b/lua/fzf-lua/providers/buffers.lua @@ -205,9 +205,6 @@ end M.buffer_lines = function(opts) if not opts then return end - if opts.no_term_buffers == nil then - opts.no_term_buffers = true - end local buffers = filter_buffers(opts, opts.current_buffer_only and { vim.api.nvim_get_current_buf() } or vim.api.nvim_list_bufs()) @@ -249,9 +246,6 @@ M.buffer_lines = function(opts) -- disable multi-select opts.fzf_opts["--no-multi"] = '' opts.fzf_opts["--preview-window"] = 'hidden:right:0' - opts.fzf_opts["--delimiter"] = vim.fn.shellescape(']') - opts.fzf_opts["--nth"] = '2,-1' - opts.fzf_opts["--tiebreak"] = 'index' if opts.search and #opts.search>0 then opts.fzf_opts['--query'] = vim.fn.shellescape(opts.search) diff --git a/lua/fzf-lua/providers/grep.lua b/lua/fzf-lua/providers/grep.lua index fa673b0..5de648d 100644 --- a/lua/fzf-lua/providers/grep.lua +++ b/lua/fzf-lua/providers/grep.lua @@ -349,8 +349,10 @@ M.grep_curbuf = function(opts) if opts.exec_empty_query == nil then opts.exec_empty_query = true end + opts.fzf_opts = vim.tbl_extend("keep", + opts.fzf_opts or {}, config.globals.blines.fzf_opts) opts.filename = vim.api.nvim_buf_get_name(0) - if #opts.filename > 0 then + if #opts.filename > 0 and vim.loop.fs_stat(opts.filename) then opts.filename = path.relative(opts.filename, vim.loop.cwd()) if opts.lgrep then return M.live_grep(opts) @@ -359,7 +361,7 @@ M.grep_curbuf = function(opts) return M.grep(opts) end else - utils.info("Rg current buffer requires actual file on disk") + utils.info("Rg current buffer requires file on disk") return end end