fix: check if caret is present before removal (#1390)

This commit is contained in:
Luke Kershaw
2021-10-31 15:52:09 +00:00
committed by GitHub
parent d7f09f5895
commit c08f95823d

View File

@@ -837,20 +837,25 @@ function Picker:set_selection(row)
local set_ok, set_errmsg = pcall(function() local set_ok, set_errmsg = pcall(function()
local prompt = self:_get_prompt() local prompt = self:_get_prompt()
-- Handle removing '> ' from beginning of previous selection (if still visible) -- This block handles removing the caret from beginning of previous selection (if still visible)
-- Check if previous selection is still visible
if self._selection_entry and self.manager:find_entry(self._selection_entry) then if self._selection_entry and self.manager:find_entry(self._selection_entry) then
-- Find the (possibly new) row of the old selection -- Find the (possibly new) row of the old selection
local row_old_selection = self:get_row(self.manager:find_entry(self._selection_entry)) local row_old_selection = self:get_row(self.manager:find_entry(self._selection_entry))
-- Only change the first couple characters, nvim_buf_set_text leaves the existing highlights local line = a.nvim_buf_get_lines(results_bufnr, row_old_selection, row_old_selection + 1, false)[1]
a.nvim_buf_set_text( --Check if that row still has the caret
results_bufnr, if string.sub(line, 0, #self.selection_caret) == self.selection_caret then
row_old_selection, -- Only change the first couple characters, nvim_buf_set_text leaves the existing highlights
0, a.nvim_buf_set_text(
row_old_selection, results_bufnr,
#self.selection_caret, row_old_selection,
{ self.entry_prefix } 0,
) row_old_selection,
self.highlighter:hi_multiselect(row_old_selection, self:is_multi_selected(self._selection_entry)) #self.selection_caret,
{ self.entry_prefix }
)
self.highlighter:hi_multiselect(row_old_selection, self:is_multi_selected(self._selection_entry))
end
end end
local caret = self.selection_caret local caret = self.selection_caret