fix: Better highlights (#344)
* scratch: easy reproduce for buffers * fix: Many highlight fixes
This commit is contained in:
@@ -14,6 +14,7 @@ local utils = require('telescope.utils')
|
|||||||
|
|
||||||
local layout_strategies = require('telescope.pickers.layout_strategies')
|
local layout_strategies = require('telescope.pickers.layout_strategies')
|
||||||
local entry_display = require('telescope.pickers.entry_display')
|
local entry_display = require('telescope.pickers.entry_display')
|
||||||
|
local p_highlights = require('telescope.pickers.highlights')
|
||||||
local p_scroller = require('telescope.pickers.scroller')
|
local p_scroller = require('telescope.pickers.scroller')
|
||||||
|
|
||||||
local EntryManager = require('telescope.entry_manager')
|
local EntryManager = require('telescope.entry_manager')
|
||||||
@@ -39,8 +40,6 @@ local extend = function(opts, defaults)
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
local ns_telescope_selection = a.nvim_create_namespace('telescope_selection')
|
|
||||||
local ns_telescope_entry = a.nvim_create_namespace('telescope_entry')
|
|
||||||
local ns_telescope_matching = a.nvim_create_namespace('telescope_matching')
|
local ns_telescope_matching = a.nvim_create_namespace('telescope_matching')
|
||||||
local ns_telescope_prompt = a.nvim_create_namespace('telescope_prompt')
|
local ns_telescope_prompt = a.nvim_create_namespace('telescope_prompt')
|
||||||
local ns_telescope_prompt_prefix = a.nvim_create_namespace('telescope_prompt_prefix')
|
local ns_telescope_prompt_prefix = a.nvim_create_namespace('telescope_prompt_prefix')
|
||||||
@@ -130,6 +129,8 @@ function Picker:new(opts)
|
|||||||
obj.sorting_strategy
|
obj.sorting_strategy
|
||||||
)
|
)
|
||||||
|
|
||||||
|
obj.highlighter = p_highlights.new(obj)
|
||||||
|
|
||||||
return obj
|
return obj
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -252,7 +253,7 @@ function Picker:highlight_displayed_rows(results_bufnr, prompt)
|
|||||||
vim.api.nvim_buf_clear_namespace(results_bufnr, ns_telescope_matching, 0, -1)
|
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)
|
local displayed_rows = vim.api.nvim_buf_get_lines(results_bufnr, 0, -1, false)
|
||||||
for row_index = 1, #displayed_rows do
|
for row_index = 1, math.min(#displayed_rows, self.max_results) do
|
||||||
local display = displayed_rows[row_index]
|
local display = displayed_rows[row_index]
|
||||||
|
|
||||||
self:highlight_one_row(results_bufnr, prompt, display, row_index - 1)
|
self:highlight_one_row(results_bufnr, prompt, display, row_index - 1)
|
||||||
@@ -374,6 +375,7 @@ function Picker:find()
|
|||||||
|
|
||||||
a.nvim_buf_add_highlight(prompt_bufnr, ns_telescope_prompt_prefix, 'TelescopePromptPrefix', 0, 0, #prompt_prefix)
|
a.nvim_buf_add_highlight(prompt_bufnr, ns_telescope_prompt_prefix, 'TelescopePromptPrefix', 0, 0, #prompt_prefix)
|
||||||
end
|
end
|
||||||
|
self.prompt_prefix = prompt_prefix
|
||||||
|
|
||||||
-- Temporarily disabled: Draw the screen ASAP. This makes things feel speedier.
|
-- Temporarily disabled: Draw the screen ASAP. This makes things feel speedier.
|
||||||
-- vim.cmd [[redraw]]
|
-- vim.cmd [[redraw]]
|
||||||
@@ -421,10 +423,7 @@ function Picker:find()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local prompt = vim.trim(
|
local prompt = self:_get_prompt()
|
||||||
vim.api.nvim_buf_get_lines(prompt_bufnr, first_line, last_line, false)[1]:sub(#prompt_prefix)
|
|
||||||
)
|
|
||||||
|
|
||||||
if self.sorter then
|
if self.sorter then
|
||||||
self.sorter:_start(prompt)
|
self.sorter:_start(prompt)
|
||||||
end
|
end
|
||||||
@@ -555,6 +554,8 @@ function Picker:find()
|
|||||||
vim.api.nvim_buf_attach(prompt_bufnr, false, {
|
vim.api.nvim_buf_attach(prompt_bufnr, false, {
|
||||||
on_lines = on_lines,
|
on_lines = on_lines,
|
||||||
on_detach = vim.schedule_wrap(function()
|
on_detach = vim.schedule_wrap(function()
|
||||||
|
self:_reset_highlights()
|
||||||
|
|
||||||
on_lines = nil
|
on_lines = nil
|
||||||
|
|
||||||
-- TODO: Can we add a "cleanup" / "teardown" function that completely removes these.
|
-- TODO: Can we add a "cleanup" / "teardown" function that completely removes these.
|
||||||
@@ -683,19 +684,21 @@ function Picker:add_selection(row)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Picker:display_multi_select(results_bufnr)
|
function Picker:display_multi_select(results_bufnr)
|
||||||
for entry, _ in pairs(self.multi_select) do
|
if true then return end
|
||||||
local index = self.manager:find_entry(entry)
|
|
||||||
if index then
|
-- for entry, _ in pairs(self.multi_select) do
|
||||||
vim.api.nvim_buf_add_highlight(
|
-- local index = self.manager:find_entry(entry)
|
||||||
results_bufnr,
|
-- if index then
|
||||||
ns_telescope_selection,
|
-- vim.api.nvim_buf_add_highlight(
|
||||||
"TelescopeMultiSelection",
|
-- results_bufnr,
|
||||||
self:get_row(index),
|
-- ns_telescope_selection,
|
||||||
0,
|
-- "TelescopeMultiSelection",
|
||||||
-1
|
-- self:get_row(index),
|
||||||
)
|
-- 0,
|
||||||
end
|
-- -1
|
||||||
end
|
-- )
|
||||||
|
-- end
|
||||||
|
-- end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Picker:reset_selection()
|
function Picker:reset_selection()
|
||||||
@@ -740,26 +743,27 @@ function Picker:set_selection(row)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local prompt = vim.api.nvim_buf_get_lines(self.prompt_bufnr, 0, 1, false)[1]
|
local prompt = self:_get_prompt()
|
||||||
|
|
||||||
-- Handle adding '> ' to beginning of selections
|
-- Handle adding '> ' to beginning of selections
|
||||||
if self._selection_row then
|
if self._selection_row then
|
||||||
local old_selection = a.nvim_buf_get_lines(results_bufnr, self._selection_row, self._selection_row + 1, false)[1]
|
local display, display_highlights = entry_display.resolve(self, self._selection_entry)
|
||||||
|
|
||||||
if old_selection then
|
if display then
|
||||||
local old_display = ' ' .. old_selection:sub(3)
|
display = ' ' .. display
|
||||||
a.nvim_buf_set_lines(results_bufnr, self._selection_row, self._selection_row + 1, false, {old_display})
|
a.nvim_buf_set_lines(results_bufnr, self._selection_row, self._selection_row + 1, false, {display})
|
||||||
|
|
||||||
if prompt and self.sorter and self.sorter.highlighter then
|
self.highlighter:hi_display(self._selection_row, ' ', display_highlights)
|
||||||
self:highlight_one_row(results_bufnr, prompt, old_display, self._selection_row)
|
self.highlighter:hi_sorter(self._selection_row, prompt, display)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local caret = '>'
|
local caret = '>'
|
||||||
local display = string.format('%s %s', caret,
|
-- local display = string.format('%s %s', caret,
|
||||||
(a.nvim_buf_get_lines(results_bufnr, row, row + 1, false)[1] or ''):sub(3)
|
-- (a.nvim_buf_get_lines(results_bufnr, row, row + 1, false)[1] or ''):sub(3)
|
||||||
)
|
-- )
|
||||||
|
local display, display_highlights = entry_display.resolve(self, entry)
|
||||||
|
display = caret .. ' ' .. display
|
||||||
|
|
||||||
-- TODO: You should go back and redraw the highlights for this line from the sorter.
|
-- TODO: You should go back and redraw the highlights for this line from the sorter.
|
||||||
-- That's the only smart thing to do.
|
-- That's the only smart thing to do.
|
||||||
@@ -769,29 +773,12 @@ function Picker:set_selection(row)
|
|||||||
end
|
end
|
||||||
a.nvim_buf_set_lines(results_bufnr, row, row + 1, false, {display})
|
a.nvim_buf_set_lines(results_bufnr, row, row + 1, false, {display})
|
||||||
|
|
||||||
a.nvim_buf_clear_namespace(results_bufnr, ns_telescope_selection, 0, -1)
|
self.highlighter:hi_selection(row, caret)
|
||||||
a.nvim_buf_add_highlight(
|
self.highlighter:hi_display(row, ' ', display_highlights)
|
||||||
results_bufnr,
|
self.highlighter:hi_sorter(row, prompt, display)
|
||||||
ns_telescope_selection,
|
|
||||||
'TelescopeSelectionCaret',
|
|
||||||
row,
|
|
||||||
0,
|
|
||||||
#caret
|
|
||||||
)
|
|
||||||
a.nvim_buf_add_highlight(
|
|
||||||
results_bufnr,
|
|
||||||
ns_telescope_selection,
|
|
||||||
'TelescopeSelection',
|
|
||||||
row,
|
|
||||||
#caret,
|
|
||||||
-1
|
|
||||||
)
|
|
||||||
|
|
||||||
self:display_multi_select(results_bufnr)
|
-- TODO: Actually implement this for real TJ, don't leave around half implemented code plz :)
|
||||||
|
-- self:display_multi_select(results_bufnr)
|
||||||
if prompt and self.sorter and self.sorter.highlighter then
|
|
||||||
self:highlight_one_row(results_bufnr, prompt, display, row)
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
if not set_ok then
|
if not set_ok then
|
||||||
@@ -850,15 +837,7 @@ function Picker:entry_adder(index, entry, score)
|
|||||||
|
|
||||||
local set_ok = pcall(vim.api.nvim_buf_set_lines, self.results_bufnr, row, row + 1, false, {display})
|
local set_ok = pcall(vim.api.nvim_buf_set_lines, self.results_bufnr, row, row + 1, false, {display})
|
||||||
if set_ok and display_highlights then
|
if set_ok and display_highlights then
|
||||||
-- TODO: This should actually be done during the cursor moving stuff annoyingly.... didn't see this bug yesterday.
|
self.highlighter:hi_display(row, prefix, display_highlights)
|
||||||
for _, hl_block in ipairs(display_highlights) do
|
|
||||||
a.nvim_buf_add_highlight(self.results_bufnr,
|
|
||||||
ns_telescope_entry,
|
|
||||||
hl_block[2],
|
|
||||||
row,
|
|
||||||
#prefix + hl_block[1][1],
|
|
||||||
#prefix + hl_block[1][2])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- This pretty much only fails when people leave newlines in their results.
|
-- This pretty much only fails when people leave newlines in their results.
|
||||||
@@ -946,6 +925,16 @@ function pickers.on_close_prompt(prompt_bufnr)
|
|||||||
picker.close_windows(status)
|
picker.close_windows(status)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Picker:_get_prompt()
|
||||||
|
return vim.trim(
|
||||||
|
vim.api.nvim_buf_get_lines(self.prompt_bufnr, 0, 1, false)[1]:sub(#self.prompt_prefix)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Picker:_reset_highlights()
|
||||||
|
self.highlighter:clear_display()
|
||||||
|
end
|
||||||
|
|
||||||
pickers._Picker = Picker
|
pickers._Picker = Picker
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
76
lua/telescope/pickers/highlights.lua
Normal file
76
lua/telescope/pickers/highlights.lua
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
local a = vim.api
|
||||||
|
|
||||||
|
local highlights = {}
|
||||||
|
|
||||||
|
local ns_telescope_selection = a.nvim_create_namespace('telescope_selection')
|
||||||
|
local ns_telescope_entry = a.nvim_create_namespace('telescope_entry')
|
||||||
|
|
||||||
|
local Highlighter = {}
|
||||||
|
Highlighter.__index = Highlighter
|
||||||
|
|
||||||
|
function Highlighter:new(picker)
|
||||||
|
return setmetatable({
|
||||||
|
picker = picker,
|
||||||
|
}, self)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Highlighter:hi_display(row, prefix, display_highlights)
|
||||||
|
local results_bufnr = assert(self.picker.results_bufnr, "Must have a results bufnr")
|
||||||
|
|
||||||
|
a.nvim_buf_clear_namespace(results_bufnr, ns_telescope_entry, row, row + 1)
|
||||||
|
local len_prefix = #prefix
|
||||||
|
|
||||||
|
for _, hl_block in ipairs(display_highlights) do
|
||||||
|
a.nvim_buf_add_highlight(
|
||||||
|
results_bufnr,
|
||||||
|
ns_telescope_entry,
|
||||||
|
hl_block[2],
|
||||||
|
row,
|
||||||
|
len_prefix + hl_block[1][1],
|
||||||
|
len_prefix + hl_block[1][2]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Highlighter:clear_display()
|
||||||
|
a.nvim_buf_clear_namespace(self.picker.results_bufnr, ns_telescope_entry, 0, -1)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Highlighter:hi_sorter(row, prompt, display)
|
||||||
|
local picker = self.picker
|
||||||
|
if not picker.sorter or not picker.sorter.highlighter then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local results_bufnr = assert(self.picker.results_bufnr, "Must have a results bufnr")
|
||||||
|
picker:highlight_one_row(results_bufnr, prompt, display, row)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Highlighter:hi_selection(row, caret)
|
||||||
|
local results_bufnr = assert(self.picker.results_bufnr, "Must have a results bufnr")
|
||||||
|
|
||||||
|
a.nvim_buf_clear_namespace(results_bufnr, ns_telescope_selection, 0, -1)
|
||||||
|
a.nvim_buf_add_highlight(
|
||||||
|
results_bufnr,
|
||||||
|
ns_telescope_selection,
|
||||||
|
'TelescopeSelectionCaret',
|
||||||
|
row,
|
||||||
|
0,
|
||||||
|
#caret
|
||||||
|
)
|
||||||
|
|
||||||
|
a.nvim_buf_add_highlight(
|
||||||
|
results_bufnr,
|
||||||
|
ns_telescope_selection,
|
||||||
|
'TelescopeSelection',
|
||||||
|
row,
|
||||||
|
#caret,
|
||||||
|
-1
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
highlights.new = function(...)
|
||||||
|
return Highlighter:new(...)
|
||||||
|
end
|
||||||
|
|
||||||
|
return highlights
|
||||||
16
scratch/digia_init.vim
Normal file
16
scratch/digia_init.vim
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
set rtp+=.
|
||||||
|
set rtp+=../plenary.nvim/
|
||||||
|
set rtp+=../popup.nvim/
|
||||||
|
|
||||||
|
|
||||||
|
set statusline=""
|
||||||
|
set statusline+=%<%f:%l:%v " filename:col:line/total lines
|
||||||
|
set statusline+=\ "
|
||||||
|
set statusline+=%h%m%r " help/modified/readonly
|
||||||
|
set statusline+=\ "
|
||||||
|
set statusline+=[%{&ft}] " filetype
|
||||||
|
set statusline+=%= " alignment group
|
||||||
|
set statusline+=\ "
|
||||||
|
|
||||||
|
" nnoremap <silent> <c-p> :lua require('telescope.builtin').git_files()<CR>
|
||||||
|
nnoremap <silent> <c-p> :lua require("telescope.builtin").find_files{ find_command = { "rg", "--smart-case", "--files", "--hidden", "--follow", "-g", "!.git/*" } }<CR>
|
||||||
Reference in New Issue
Block a user