From d0cf646f65746415294f570ec643ffd0101ca3ab Mon Sep 17 00:00:00 2001 From: August Masquelier <31262046+levouh@users.noreply.github.com> Date: Sun, 4 Apr 2021 05:04:06 -0600 Subject: [PATCH] feat: current buffer fuzzy find improvements (#694) If you don't want to have a previewer disable it with `:Telescope current_buffer_fuzzy_find previewer=false` To ignore empty lines do: `:Telescope current_buffer_fuzzy_find skip_empty_lines=true` --- lua/telescope/builtin/files.lua | 29 +++++++++++++++-------------- lua/telescope/make_entry.lua | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/lua/telescope/builtin/files.lua b/lua/telescope/builtin/files.lua index ae125aa..f091ad0 100644 --- a/lua/telescope/builtin/files.lua +++ b/lua/telescope/builtin/files.lua @@ -329,34 +329,35 @@ files.treesitter = function(opts) end files.current_buffer_fuzzy_find = function(opts) + -- All actions are on the current buffer + local bufnr = vim.api.nvim_get_current_buf() + local filename = vim.fn.expand(vim.api.nvim_buf_get_name(bufnr)) + local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) local lines_with_numbers = {} - for k, v in ipairs(lines) do - table.insert(lines_with_numbers, {k, v}) - end - local bufnr = vim.api.nvim_get_current_buf() + for lnum, line in ipairs(lines) do + table.insert(lines_with_numbers, { + lnum = lnum, + line = line, + bufnr = bufnr, + filename = filename, + }) + end pickers.new(opts, { prompt_title = 'Current Buffer Fuzzy', finder = finders.new_table { results = lines_with_numbers, - entry_maker = function(enumerated_line) - return { - bufnr = bufnr, - display = enumerated_line[2], - ordinal = enumerated_line[2], - - lnum = enumerated_line[1], - } - end + entry_maker = opts.entry_maker or make_entry.gen_from_buffer_lines(opts), }, sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), attach_mappings = function() action_set.select:enhance { post = function() local selection = action_state.get_selected_entry() - vim.api.nvim_win_set_cursor(0, {selection.lnum, 0}) + vim.api.nvim_win_set_cursor(0, { selection.lnum, 0 }) end, } diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index dd31193..e4b3d4e 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -680,6 +680,38 @@ function make_entry.gen_from_highlights() end end +function make_entry.gen_from_buffer_lines(opts) + local displayer = entry_display.create { + separator = ' │ ', + items = { + { width = 5 }, + { remaining = true }, + }, + } + + local make_display = function(entry) + return displayer { + { entry.lnum, opts.lnum_highlight_group or 'TelescopeResultsSpecialComment' }, + entry.line + } + end + + return function(entry) + if opts.skip_empty_lines and string.match(entry.line, '^$') then + return + end + + return { + valid = true, + ordinal = entry.line, + display = make_display, + filename = entry.filename, + lnum = entry.lnum, + line = entry.line, + } + end +end + function make_entry.gen_from_vimoptions() local process_one_opt = function(o) local ok, value_origin