diff options
| author | Simon Hauser <Simon-Hauser@outlook.de> | 2022-07-01 23:29:24 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-01 23:29:24 +0200 |
| commit | 7df95f9b208ba7228a25e7f75fb4cc02d6604cce (patch) | |
| tree | e4933fb547bc886f27f06011a6c4780facfd7642 /lua/telescope/pickers.lua | |
| parent | 1aa74b231c6f93152c4ac51549a0563dca9b4453 (diff) | |
| parent | e778abfdb457cc47ca47ce9b76905e043e87e598 (diff) | |
Merge pull request #1945 from nvim-telescope/dev
full changelog `:help telescope.changelog-1945`
Diffstat (limited to 'lua/telescope/pickers.lua')
| -rw-r--r-- | lua/telescope/pickers.lua | 91 |
1 files changed, 54 insertions, 37 deletions
diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 12d6209..82ccda9 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -24,8 +24,6 @@ local p_window = require "telescope.pickers.window" local EntryManager = require "telescope.entry_manager" local MultiSelect = require "telescope.pickers.multi" -local get_default = utils.get_default - local truncate = require("plenary.strings").truncate local strdisplaywidth = require("plenary.strings").strdisplaywidth @@ -65,26 +63,27 @@ function Picker:new(opts) -- pcall(v.clear) -- end - local layout_strategy = get_default(opts.layout_strategy, config.values.layout_strategy) + local layout_strategy = vim.F.if_nil(opts.layout_strategy, config.values.layout_strategy) local obj = setmetatable({ - prompt_title = get_default(opts.prompt_title, config.values.prompt_title), - results_title = get_default(opts.results_title, config.values.results_title), + prompt_title = vim.F.if_nil(opts.prompt_title, config.values.prompt_title), + results_title = vim.F.if_nil(opts.results_title, config.values.results_title), -- either whats passed in by the user or whats defined by the previewer preview_title = opts.preview_title, - prompt_prefix = get_default(opts.prompt_prefix, config.values.prompt_prefix), - wrap_results = get_default(opts.wrap_results, config.values.wrap_results), - selection_caret = get_default(opts.selection_caret, config.values.selection_caret), - entry_prefix = get_default(opts.entry_prefix, config.values.entry_prefix), - multi_icon = get_default(opts.multi_icon, config.values.multi_icon), + prompt_prefix = vim.F.if_nil(opts.prompt_prefix, config.values.prompt_prefix), + wrap_results = vim.F.if_nil(opts.wrap_results, config.values.wrap_results), + selection_caret = vim.F.if_nil(opts.selection_caret, config.values.selection_caret), + entry_prefix = vim.F.if_nil(opts.entry_prefix, config.values.entry_prefix), + multi_icon = vim.F.if_nil(opts.multi_icon, config.values.multi_icon), - initial_mode = get_default(opts.initial_mode, config.values.initial_mode), + initial_mode = vim.F.if_nil(opts.initial_mode, config.values.initial_mode), _original_mode = vim.api.nvim_get_mode().mode, - debounce = get_default(tonumber(opts.debounce), nil), + debounce = vim.F.if_nil(tonumber(opts.debounce), nil), + _finder_attached = true, default_text = opts.default_text, - get_status_text = get_default(opts.get_status_text, config.values.get_status_text), + get_status_text = vim.F.if_nil(opts.get_status_text, config.values.get_status_text), _on_input_filter_cb = opts.on_input_filter_cb or function() end, finder = assert(opts.finder, "Finder is required."), @@ -95,7 +94,7 @@ function Picker:new(opts) default_selection_index = opts.default_selection_index, - get_selection_window = get_default(opts.get_selection_window, config.values.get_selection_window), + get_selection_window = vim.F.if_nil(opts.get_selection_window, config.values.get_selection_window), cwd = opts.cwd, @@ -106,32 +105,32 @@ function Picker:new(opts) and opts._multi or MultiSelect:new(), - track = get_default(opts.track, false), + track = vim.F.if_nil(opts.track, false), stats = {}, attach_mappings = opts.attach_mappings, - file_ignore_patterns = get_default(opts.file_ignore_patterns, config.values.file_ignore_patterns), + file_ignore_patterns = vim.F.if_nil(opts.file_ignore_patterns, config.values.file_ignore_patterns), - scroll_strategy = get_default(opts.scroll_strategy, config.values.scroll_strategy), - sorting_strategy = get_default(opts.sorting_strategy, config.values.sorting_strategy), - tiebreak = get_default(opts.tiebreak, config.values.tiebreak), - selection_strategy = get_default(opts.selection_strategy, config.values.selection_strategy), + scroll_strategy = vim.F.if_nil(opts.scroll_strategy, config.values.scroll_strategy), + sorting_strategy = vim.F.if_nil(opts.sorting_strategy, config.values.sorting_strategy), + tiebreak = vim.F.if_nil(opts.tiebreak, config.values.tiebreak), + selection_strategy = vim.F.if_nil(opts.selection_strategy, config.values.selection_strategy), - push_cursor_on_edit = get_default(opts.push_cursor_on_edit, false), - push_tagstack_on_edit = get_default(opts.push_tagstack_on_edit, false), + push_cursor_on_edit = vim.F.if_nil(opts.push_cursor_on_edit, false), + push_tagstack_on_edit = vim.F.if_nil(opts.push_tagstack_on_edit, false), layout_strategy = layout_strategy, layout_config = config.smarter_depth_2_extend(opts.layout_config or {}, config.values.layout_config or {}), - __cycle_layout_list = get_default(opts.cycle_layout_list, config.values.cycle_layout_list), + __cycle_layout_list = vim.F.if_nil(opts.cycle_layout_list, config.values.cycle_layout_list), window = { - winblend = get_default( + winblend = vim.F.if_nil( opts.winblend, type(opts.window) == "table" and opts.window.winblend or config.values.winblend ), - border = get_default(opts.border, type(opts.window) == "table" and opts.window.border or config.values.border), - borderchars = get_default( + border = vim.F.if_nil(opts.border, type(opts.window) == "table" and opts.window.border or config.values.border), + borderchars = vim.F.if_nil( opts.borderchars, type(opts.window) == "table" and opts.window.borderchars or config.values.borderchars ), @@ -505,10 +504,12 @@ function Picker:find() -- Register attach vim.api.nvim_buf_attach(prompt_bufnr, false, { on_lines = function(...) - find_id = self:_next_find_id() + if self._finder_attached then + find_id = self:_next_find_id() - status_updater { completed = false } - self._on_lines(...) + status_updater { completed = false } + self._on_lines(...) + end end, on_detach = function() @@ -692,7 +693,7 @@ end --- --- Example usage in telescope: --- - `actions.delete_buffer()` ----@param delete_cb function: called with each deleted selection +---@param delete_cb function: called for each selection fn(s) -> bool|nil (true|nil removes the entry from the results) function Picker:delete_selection(delete_cb) vim.validate { delete_cb = { delete_cb, "f" } } local original_selection_strategy = self.selection_strategy @@ -718,8 +719,10 @@ function Picker:delete_selection(delete_cb) return x > y end) for _, index in ipairs(selection_index) do - local selection = table.remove(self.finder.results, index) - delete_cb(selection) + local delete_cb_return = delete_cb(self.finder.results[index]) + if delete_cb_return == nil or delete_cb_return == true then + table.remove(self.finder.results, index) + end end if used_multi_select then @@ -908,7 +911,7 @@ function Picker:refresh(finder, opts) local handle = type(opts.new_prefix) == "table" and unpack or function(x) return x end - self:change_prompt_prefix(handle(opts.new_prefix)) + self:change_prompt_prefix(handle(opts.new_prefix), opts.prefix_hl_group) end if finder then @@ -962,6 +965,9 @@ function Picker:set_selection(row) state.set_global_key("selected_entry", entry) if not entry then + -- also refresh previewer when there is no entry selected, so the preview window is cleared + self._selection_entry = entry + self:refresh_previewer() return end @@ -1065,10 +1071,6 @@ end --- Refresh the previewer based on the current `status` of the picker function Picker:refresh_previewer() local status = state.get_status(self.prompt_bufnr) - if not self._selection_entry then - -- if selection_entry is nil there is nothing to be previewed - return - end if self.previewer and status.preview_win and a.nvim_win_is_valid(status.preview_win) then self:_increment "previewed" @@ -1365,6 +1367,16 @@ function Picker:_do_selection(prompt) else self:set_selection(self:get_reset_row()) end + elseif selection_strategy == "none" then + if self._selection_entry then + local old_entry, old_row = self._selection_entry, self._selection_row + self:reset_selection() -- required to reset selection before updating prefix + if old_row >= 0 then + self:update_prefix(old_entry, old_row) + self.highlighter:hi_multiselect(old_row, self:is_multi_selected(old_entry)) + end + end + return else error("Unknown selection strategy: " .. selection_strategy) end @@ -1495,6 +1507,11 @@ function Picker:_reset_highlights() vim.api.nvim_buf_clear_namespace(self.results_bufnr, ns_telescope_matching, 0, -1) end +-- Toggles whether finder is attached to prompt buffer input +function Picker:_toggle_finder_attach() + self._finder_attached = not self._finder_attached +end + function Picker:_detach() self.finder:close() |
