Revert "rfc: use extmarks for highlighting and carets (#2099)" (#2138)

This reverts commit 8d13f4ca8a.
This commit is contained in:
Fabian David Schmidt
2022-08-19 10:08:55 +02:00
committed by GitHub
parent 8d13f4ca8a
commit 28dc08f614
3 changed files with 114 additions and 108 deletions

View File

@@ -262,6 +262,43 @@ function Picker:clear_extra_rows(results_bufnr)
log.trace("Clearing:", worst_line)
end
--- Highlight the entry corresponding to the given row
---@param results_bufnr number: the buffer number of the results buffer
---@param prompt table: table with information about the prompt buffer
---@param display string: the text corresponding to the given row
---@param row number: the number of the chosen row
function Picker:highlight_one_row(results_bufnr, prompt, display, row)
if not self.sorter.highlighter then
return
end
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
self:_increment "highlights"
vim.api.nvim_buf_add_highlight(results_bufnr, ns_telescope_matching, highlight, row, start - 1, finish)
end
end
local entry = self.manager:get_entry(self:get_index(row))
self.highlighter:hi_multiselect(row, self:is_multi_selected(entry))
end
--- Check if the given row number can be selected
---@param row number: the number of the chosen row in the results buffer
---@return boolean
@@ -762,6 +799,7 @@ function Picker:add_selection(row)
local entry = self.manager:get_entry(self:get_index(row))
self._multi:add(entry)
self:update_prefix(entry, row)
self:get_status_updater(self.prompt_win, self.prompt_bufnr)()
self.highlighter:hi_multiselect(row, true)
end
@@ -772,6 +810,7 @@ function Picker:remove_selection(row)
local entry = self.manager:get_entry(self:get_index(row))
self._multi:drop(entry)
self:update_prefix(entry, row)
self:get_status_updater(self.prompt_win, self.prompt_bufnr)()
self.highlighter:hi_multiselect(row, false)
end
@@ -800,6 +839,7 @@ function Picker:toggle_selection(row)
end
self._multi:toggle(entry)
self:update_prefix(entry, row)
self:get_status_updater(self.prompt_win, self.prompt_bufnr)()
self.highlighter:hi_multiselect(row, self._multi:is_selected(entry))
end
@@ -947,6 +987,7 @@ function Picker:set_selection(row)
self._selection_entry = entry
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
else
@@ -1005,7 +1046,25 @@ function Picker:update_prefix(entry, row)
return t
end
return prefix(entry == self._selection_entry, self:is_multi_selected(entry))
local line = vim.api.nvim_buf_get_lines(self.results_bufnr, row, row + 1, false)[1]
if not line then
log.trace(string.format("no line found at row %d in buffer %d", row, self.results_bufnr))
return
end
local old_caret = string.sub(line, 0, #prefix(true)) == prefix(true) and prefix(true)
or string.sub(line, 0, #prefix(true, true)) == prefix(true, true) and prefix(true, true)
or string.sub(line, 0, #prefix(false)) == prefix(false) and prefix(false)
or string.sub(line, 0, #prefix(false, true)) == prefix(false, true) and prefix(false, true)
if old_caret == false then
log.warn(string.format("can't identify old caret in line: %s", line))
return
end
local pre = prefix(entry == self._selection_entry, self:is_multi_selected(entry))
-- Only change the first couple characters, nvim_buf_set_text leaves the existing highlights
a.nvim_buf_set_text(self.results_bufnr, row, 0, row, #old_caret, { pre })
return pre
end
--- Refresh the previewer based on the current `status` of the picker
@@ -1105,7 +1164,8 @@ function Picker:entry_adder(index, entry, _, insert)
if display_highlights then
self.highlighter:hi_display(row, prefix, display_highlights)
end
self.highlighter:hi_sorter(row, self:_get_prompt(), display)
self:update_prefix(entry, row)
self:highlight_one_row(self.results_bufnr, self:_get_prompt(), display, row)
end
if not set_ok then
@@ -1311,6 +1371,7 @@ function Picker:_do_selection(prompt)
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