From f47f1dc03712bf49bf278e103e5196a09c5a6210 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Fri, 11 Sep 2020 15:28:32 -0400 Subject: [PATCH] feat: make buffer work much better --- lua/telescope/actions.lua | 8 +++++--- lua/telescope/builtin.lua | 8 ++++---- lua/telescope/make_entry.lua | 15 +++++++++++++++ lua/telescope/previewers.lua | 3 +++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lua/telescope/actions.lua b/lua/telescope/actions.lua index 337ed8c..f4a29e5 100644 --- a/lua/telescope/actions.lua +++ b/lua/telescope/actions.lua @@ -75,11 +75,11 @@ local function goto_file_selection(prompt_bufnr, command) a.nvim_win_set_config(preview_win, {style = ''}) end - actions.close(prompt_bufnr) - local original_win_id = picker.original_win_id or 0 local entry_bufnr = entry.bufnr + actions.close(prompt_bufnr) + -- TODO: Sometimes we open something with missing line numbers and stuff... if entry_bufnr then a.nvim_win_set_buf(original_win_id, entry_bufnr) @@ -115,8 +115,10 @@ end function actions.close(prompt_bufnr) local picker = actions.get_current_picker(prompt_bufnr) + local prompt_win = state.get_status(prompt_bufnr).prompt_win - vim.cmd(string.format([[bwipeout! %s]], prompt_bufnr)) + vim.api.nvim_win_close(prompt_win, true) + vim.cmd(string.format([[bdelete! %s]], prompt_bufnr)) local original_win_id = picker.original_win_id or 0 a.nvim_set_current_win(original_win_id) diff --git a/lua/telescope/builtin.lua b/lua/telescope/builtin.lua index 794f5b0..58f1bbc 100644 --- a/lua/telescope/builtin.lua +++ b/lua/telescope/builtin.lua @@ -350,10 +350,9 @@ end -- Leave this alias around for people. builtin.fd = builtin.find_files --- TODO: Sometimes some window options (for me, I've experience number & relativenumber) --- don't work when we open this up. +-- TODO: I'd like to use the `vim_buffer` previewer, but it doesn't seem to work due to some styling problems. -- I think it has something to do with nvim_open_win and style='minimal', --- but I can't figure that part out at the moment... +-- Status, currently operational. builtin.buffers = function(opts) opts = opts or {} @@ -370,7 +369,8 @@ builtin.buffers = function(opts) results = buffers, entry_maker = make_entry.gen_from_buffer(opts) }, - previewer = previewers.vim_buffer.new(opts), + -- previewer = previewers.vim_buffer.new(opts), + previewer = previewers.vimgrep.new(opts), sorter = sorters.get_generic_fuzzy_sorter(), }):find() end diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index b595ffc..35fef98 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -180,10 +180,23 @@ function make_entry.gen_from_quickfix(opts) end function make_entry.gen_from_buffer(opts) + local get_position = function(entry) + local tabpage_wins = vim.api.nvim_tabpage_list_wins(0) + for k, v in ipairs(tabpage_wins) do + if entry == vim.api.nvim_win_get_buf(v) then + return vim.api.nvim_win_get_cursor(v) + end + end + + return {} + end + return function(entry) local bufnr_str = tostring(entry) local bufname = vim.api.nvim_buf_get_name(entry) + local position = get_position(entry) + if '' == bufname then return nil end @@ -197,6 +210,8 @@ function make_entry.gen_from_buffer(opts) bufnr = entry, filename = bufname, + + lnum = position[1] or 1, } end end diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua index 3f0713d..57e51b2 100644 --- a/lua/telescope/previewers.lua +++ b/lua/telescope/previewers.lua @@ -241,6 +241,9 @@ previewers.vimgrep = defaulter(function(_) local _, _, filename, lnum, col, text = string.find(line, [[([^:]+):(%d+):(%d+):(.*)]]) + filename = filename or entry.filename + lnum = lnum or entry.lnum or 0 + local context = math.floor(height / 2) local start = math.max(0, lnum - context) local finish = lnum + context