From 329863d754c98037896fe324c72f02b106041e8c Mon Sep 17 00:00:00 2001 From: bhagwan Date: Thu, 9 Sep 2021 18:26:34 -0700 Subject: [PATCH] added marks builtin previewer --- README.md | 7 ++++-- lua/fzf-lua/config.lua | 5 ++++ lua/fzf-lua/previewer/builtin.lua | 39 ++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8756d8e..8112e42 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,8 @@ require'fzf-lua'.setup { 'ctrl-u:unix-line-discard', 'ctrl-f:half-page-down', 'ctrl-b:half-page-up', + 'ctrl-a:beginning-of-line', + 'ctrl-e:end-of-line', 'alt-a:toggle-all', }, --[[ fzf_colors = { -- fzf '--color=' options @@ -447,6 +449,7 @@ require'fzf-lua'.setup { }, }, -- uncomment to disable the previewer + -- nvim = { marks = { previewer = { _ctor = false } } }, -- helptags = { previewer = { _ctor = false } }, -- manpages = { previewer = { _ctor = false } }, -- uncomment to set dummy win location (help|man bar) @@ -481,7 +484,7 @@ EOF ## TODO -- [x] Add more providers +- [x] ~~Add more providers~~ + [x] ~~LSP (refs, symbols, etc)~~ (2021-07-20) + [x] ~~git commits~~ (2021-08-05) + [x] ~~git branches~~ (2021-08-05) @@ -496,7 +499,7 @@ EOF + [x] ~~tags~~ (2021-08-15) - [x] ~~Built-in previewer with treesitter support~~ (2021-08-29) - [x] ~~Improve previewer for `buffers`~~ (2021-08-29) -- [ ] Add support for `marks` in the builtin previewer +- [x] ~~Add builtin previews for `marks|help_tags|man_pages`~~ (2021-09-09) - [ ] Add built-in plugin documentation - [ ] Complete the Wiki diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index 9f794ac..7833fe2 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -53,6 +53,8 @@ M.globals = { 'ctrl-u:unix-line-discard', 'ctrl-f:half-page-down', 'ctrl-b:half-page-up', + 'ctrl-a:beginning-of-line', + 'ctrl-e:end-of-line', 'alt-a:toggle-all', }, preview_border = 'border', @@ -371,6 +373,9 @@ M.globals.nvim = { actions = { ["default"] = actions.goto_mark, }, + previewer = { + _ctor = previewers.builtin.marks, + }, }, commands = { prompt = 'Commands> ', diff --git a/lua/fzf-lua/previewer/builtin.lua b/lua/fzf-lua/previewer/builtin.lua index 9b504b4..9466ea8 100644 --- a/lua/fzf-lua/previewer/builtin.lua +++ b/lua/fzf-lua/previewer/builtin.lua @@ -11,6 +11,7 @@ Previewer.base = {} Previewer.buffer_or_file = {} Previewer.help_tags = {} Previewer.man_pages = {} +Previewer.marks = {} -- signgleton instance used for our keymappings local _self = nil @@ -295,8 +296,12 @@ function Previewer.buffer_or_file:new(o, opts, fzf_win) return self end +function Previewer.buffer_or_file:parse_entry(entry_str) + return path.entry_to_file(entry_str, self.opts.cwd) +end + function Previewer.buffer_or_file:populate_preview_buf(entry_str) - local entry = path.entry_to_file(entry_str, self.opts.cwd) + local entry = self:parse_entry(entry_str) if entry.bufnr and api.nvim_buf_is_loaded(entry.bufnr) then -- must convert to number or our backup will have conflicting keys local bufnr = tonumber(entry.bufnr) @@ -547,4 +552,36 @@ function Previewer.man_pages:parse_entry(entry_str) -- return require'fzf-lua.providers.manpages'.getmanpage(entry_str) end +function Previewer.marks:new(o, opts, fzf_win) + self = setmetatable(Previewer.buffer_or_file(o, opts, fzf_win), { + __index = vim.tbl_deep_extend("keep", + self, Previewer.buffer_or_file, Previewer.base + )}) + return self +end + +function Previewer.marks:parse_entry(entry_str) + local bufnr = nil + local mark, lnum, col, filepath = entry_str:match("(.)%s+(%d+)%s+(%d+)%s+(.*)") + -- try to acquire position from sending buffer + -- if this succeeds (line>0) the mark is inside + local pos = vim.api.nvim_buf_get_mark(self.win.src_bufnr, mark) + if pos and pos[1] > 0 and pos[1] == tonumber(lnum) then + bufnr = self.win.src_bufnr + filepath = api.nvim_buf_get_name(bufnr) + end + if filepath and #filepath>0 then + local ok, res = pcall(vim.fn.expand, filepath) + if not ok then filepath = '' + else filepath = res end + filepath = path.relative(filepath, vim.loop.cwd()) + end + return { + bufnr = bufnr, + path = filepath, + line = tonumber(lnum) or 1, + col = tonumber(col) or 1, + } +end + return Previewer