summaryrefslogtreecommitdiff
path: root/lua/telescope/pickers.lua
diff options
context:
space:
mode:
authorTJ DeVries <devries.timothyj@gmail.com>2020-09-26 12:36:58 -0400
committerTJ DeVries <devries.timothyj@gmail.com>2020-09-26 12:36:58 -0400
commit079773ebee5f18f55c72856049c1a332dabe28e2 (patch)
treea18bc36fd5608577641fc501e94ee08e9d326bdd /lua/telescope/pickers.lua
parent33b6db19abc3354e1c766c5cf15172427996b2a7 (diff)
fix: No longer lose highlights when moving through results
Diffstat (limited to 'lua/telescope/pickers.lua')
-rw-r--r--lua/telescope/pickers.lua93
1 files changed, 51 insertions, 42 deletions
diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua
index ebcb8ea..b6fba4d 100644
--- a/lua/telescope/pickers.lua
+++ b/lua/telescope/pickers.lua
@@ -227,7 +227,6 @@ function Picker:clear_extra_rows(results_bufnr)
if self.sorting_strategy == 'ascending' then
local num_results = self.manager:num_results()
local worst_line = self.max_results - num_results
- log.info(self.max_results, num_results, worst_line)
if worst_line <= 0 then
return
@@ -253,34 +252,38 @@ function Picker:highlight_displayed_rows(results_bufnr, prompt)
vim.api.nvim_buf_clear_namespace(results_bufnr, ns_telescope_matching, 0, -1)
local displayed_rows = vim.api.nvim_buf_get_lines(results_bufnr, 0, -1, false)
- for row = 1, #displayed_rows do
- local display = displayed_rows[row]
-
- local highlights = self.sorter:highlighter(prompt, display)
- if highlights then
- for _, hl in ipairs(highlights) do
- local highlight, start, finish
- if type(hl) == 'table' then
- highlight = hl.highlight or 'TelescopeMatching'
- start = hl.start
- finish = hl.finish or hl.start
- elseif type(hl) == 'number' then
- highlight = 'TelescopeMatching'
- start = hl
- finish = hl
- else
- error('Invalid higlighter fn')
- end
+ for row_index = 1, #displayed_rows do
+ local display = displayed_rows[row_index]
+
+ self:highlight_one_row(results_bufnr, prompt, display, row_index - 1)
+ end
+end
- vim.api.nvim_buf_add_highlight(
- results_bufnr,
- ns_telescope_matching,
- highlight,
- row - 1,
- start - 1,
- finish
- )
+function Picker:highlight_one_row(results_bufnr, prompt, display, row)
+ local highlights = self.sorter:highlighter(prompt, display)
+ if highlights then
+ for _, hl in ipairs(highlights) do
+ local highlight, start, finish
+ if type(hl) == 'table' then
+ highlight = hl.highlight or 'TelescopeMatching'
+ start = hl.start
+ finish = hl.finish or hl.start
+ elseif type(hl) == 'number' then
+ highlight = 'TelescopeMatching'
+ start = hl
+ finish = hl
+ else
+ error('Invalid higlighter fn')
end
+
+ vim.api.nvim_buf_add_highlight(
+ results_bufnr,
+ ns_telescope_matching,
+ highlight,
+ row,
+ start - 1,
+ finish
+ )
end
end
end
@@ -597,7 +600,7 @@ function Picker:close_windows(status)
end
function Picker:get_selection()
- return self._selection
+ return self._selection_entry
end
function Picker:get_selection_row()
@@ -609,7 +612,7 @@ function Picker:move_selection(change)
end
function Picker:reset_selection()
- self._selection = nil
+ self._selection_entry = nil
self._selection_row = nil
end
@@ -640,16 +643,27 @@ function Picker:set_selection(row)
-- Probably something with setting a row that's too high for this?
-- Not sure.
local set_ok, set_errmsg = pcall(function()
+ local prompt = vim.api.nvim_buf_get_lines(self.prompt_bufnr, 0, 1, false)[1]
+
-- Handle adding '> ' to beginning of selections
if self._selection_row then
local old_selection = a.nvim_buf_get_lines(results_bufnr, self._selection_row, self._selection_row + 1, false)[1]
if old_selection then
- a.nvim_buf_set_lines(results_bufnr, self._selection_row, self._selection_row + 1, false, {' ' .. old_selection:sub(3)})
+ local old_display = ' ' .. old_selection:sub(3)
+ a.nvim_buf_set_lines(results_bufnr, self._selection_row, self._selection_row + 1, false, {old_display})
+
+ if prompt and self.sorter.highlighter then
+ self:highlight_one_row(results_bufnr, prompt, old_display, self._selection_row)
+ end
end
end
- a.nvim_buf_set_lines(results_bufnr, row, row + 1, false, {'> ' .. (a.nvim_buf_get_lines(results_bufnr, row, row + 1, false)[1] or ''):sub(3)})
+ local display = '> ' .. (a.nvim_buf_get_lines(results_bufnr, row, row + 1, false)[1] or ''):sub(3)
+
+ -- TODO: You should go back and redraw the highlights for this line from the sorter.
+ -- That's the only smart thing to do.
+ a.nvim_buf_set_lines(results_bufnr, row, row + 1, false, {display})
a.nvim_buf_clear_namespace(results_bufnr, ns_telescope_selection, 0, -1)
a.nvim_buf_add_highlight(
@@ -660,6 +674,10 @@ function Picker:set_selection(row)
0,
-1
)
+
+ if prompt and self.sorter.highlighter then
+ self:highlight_one_row(results_bufnr, prompt, display, row)
+ end
end)
if not set_ok then
@@ -667,21 +685,12 @@ function Picker:set_selection(row)
return
end
- -- if self._match_id then
- -- -- vim.fn.matchdelete(self._match_id)
- -- vim.fn.clearmatches(results_win)
- -- end
-
- -- self._match_id = vim.fn.matchaddpos("Conceal", { {row + 1, 1, 2} }, 0, -1, { window = results_win, conceal = ">" })
- if self._selection == entry and self._selection_row == row then
+ if self._selection_entry == entry and self._selection_row == row then
return
end
- -- TODO: Don't let you go over / under the buffer limits
- -- TODO: Make sure you start exactly at the bottom selected
-
-- TODO: Get row & text in the same obj
- self._selection = entry
+ self._selection_entry = entry
self._selection_row = row
if status.preview_win and self.previewer then