Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,13 @@ actions.which_key = function(prompt_bufnr, opts)
local row_ = highlight_tbl.row
local col = highlight_tbl.col
for _, hl_block in ipairs(highlight) do
a.nvim_buf_add_highlight(km_buf, keymap_highlights, hl_block[2], row_, col + hl_block[1][1], col + hl_block[1][2])
utils.hl_range(
km_buf,
keymap_highlights,
hl_block[2],
{ row_, col + hl_block[1][1] },
{ row_, col + hl_block[1][2] }
)
end
end

Expand Down
9 changes: 4 additions & 5 deletions lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ function Picker:highlight_one_row(results_bufnr, prompt, display, row)

self:_increment "highlights"

vim.api.nvim_buf_add_highlight(results_bufnr, ns_telescope_matching, highlight, row, start - 1, finish)
utils.hl_range(results_bufnr, ns_telescope_matching, highlight, { row, start - 1 }, { row, finish })
end
end

Expand Down Expand Up @@ -975,13 +975,12 @@ function Picker:_reset_prefix_color(hl_group)
self._current_prefix_hl_group = hl_group or nil

if self.prompt_prefix ~= "" and a.nvim_buf_is_valid(self.prompt_bufnr) then
vim.api.nvim_buf_add_highlight(
utils.hl_range(
self.prompt_bufnr,
ns_telescope_prompt_prefix,
self._current_prefix_hl_group or "TelescopePromptPrefix",
0,
0,
#self.prompt_prefix
{ 0, 0 },
{ 0, #self.prompt_prefix }
)
end
end
Expand Down
21 changes: 11 additions & 10 deletions lua/telescope/pickers/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ local ns_telescope_selection = a.nvim_create_namespace "telescope_selection"
local ns_telescope_multiselection = a.nvim_create_namespace "telescope_multiselection"
local ns_telescope_entry = a.nvim_create_namespace "telescope_entry"

---TODO(clason): remove when dropping support for Nvim 0.10
local hl = vim.hl or vim.highlight

local Highlighter = {}
Highlighter.__index = Highlighter

Expand All @@ -30,13 +33,12 @@ function Highlighter:hi_display(row, prefix, display_highlights)
local len_prefix = #prefix

for _, hl_block in ipairs(display_highlights) do
a.nvim_buf_add_highlight(
hl.range(
results_bufnr,
ns_telescope_entry,
hl_block[2],
row,
len_prefix + hl_block[1][1],
len_prefix + hl_block[1][2]
{ row, len_prefix + hl_block[1][1] },
{ row, len_prefix + hl_block[1][2] }
)
end
end
Expand Down Expand Up @@ -69,7 +71,7 @@ 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)
hl.range(results_bufnr, ns_telescope_selection, "TelescopeSelectionCaret", { row, 0 }, { row, #caret })

a.nvim_buf_set_extmark(
results_bufnr,
Expand All @@ -84,18 +86,17 @@ function Highlighter:hi_multiselect(row, is_selected)
local results_bufnr = assert(self.picker.results_bufnr, "Must have a results bufnr")

if is_selected then
vim.api.nvim_buf_add_highlight(results_bufnr, ns_telescope_multiselection, "TelescopeMultiSelection", row, 0, -1)
hl.range(results_bufnr, ns_telescope_multiselection, "TelescopeMultiSelection", { row, 0 }, { row, -1 })
if self.picker.multi_icon then
local line = vim.api.nvim_buf_get_lines(results_bufnr, row, row + 1, false)[1]
local pos = line:find(self.picker.multi_icon)
if pos and pos <= math.max(#self.picker.selection_caret, #self.picker.entry_prefix) then
vim.api.nvim_buf_add_highlight(
hl.range(
results_bufnr,
ns_telescope_multiselection,
"TelescopeMultiIcon",
row,
pos - 1,
pos - 1 + #self.picker.multi_icon
{ row, pos - 1 },
{ row, pos - 1 + #self.picker.multi_icon }
)
end
end
Expand Down
89 changes: 40 additions & 49 deletions lua/telescope/previewers/buffer_previewer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,16 @@ local colorize_ls_long = function(bufnr, data, sections)
local section = sections[lnum]
for i = 1, section[1].end_index - 1 do -- Highlight permissions
local c = line:sub(i, i)
vim.api.nvim_buf_add_highlight(bufnr, ns_previewer, color_hash[c], lnum - 1, i - 1, i)
utils.hl_range(bufnr, ns_previewer, color_hash[c], { lnum - 1, i - 1 }, { lnum - 1, i })
end
for i = 2, #section do -- highlights size, (user, group), date and name
local hl_group = color_hash[i + (i ~= 2 and windows_add or 0)]
vim.api.nvim_buf_add_highlight(
utils.hl_range(
bufnr,
ns_previewer,
type(hl_group) == "function" and hl_group(line) or hl_group,
lnum - 1,
section[i].start_index - 1,
section[i].end_index - 1
{ lnum - 1, section[i].start_index - 1 },
{ lnum - 1, section[i].end_index - 1 }
)
end
end
Expand All @@ -133,7 +132,7 @@ local handle_directory_preview = function(filepath, bufnr, opts)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, paths)
for i, path in ipairs(paths) do
local hl = color_hash[6](data[i])
vim.api.nvim_buf_add_highlight(bufnr, ns_previewer, hl, i - 1, 0, #path)
utils.hl_range(bufnr, ns_previewer, hl, { i - 1, 0 }, { i - 1, #path })
end
end
else
Expand Down Expand Up @@ -531,13 +530,12 @@ previewers.vimgrep = defaulter(function(opts)

for i = lnum, lnend do
pcall(
vim.api.nvim_buf_add_highlight,
utils.hl_range,
bufnr,
ns_previewer,
"TelescopePreviewLine",
i,
i == lnum and col or 0,
i == lnend and colend or -1
{ i, i == lnum and col or 0 },
{ i, i == lnend and colend or -1 }
)
end

Expand Down Expand Up @@ -623,7 +621,14 @@ previewers.ctags = defaulter(function(opts)
if self.state.last_set_bufnr then
pcall(vim.api.nvim_buf_clear_namespace, self.state.last_set_bufnr, ns_previewer, 0, -1)
end
pcall(vim.api.nvim_buf_add_highlight, bufnr, ns_previewer, "TelescopePreviewMatch", entry.lnum - 1, 0, -1)
pcall(
utils.hl_range,
bufnr,
ns_previewer,
"TelescopePreviewMatch",
{ entry.lnum - 1, 0 },
{ entry.lnum - 1, -1 }
)
pcall(vim.api.nvim_win_set_cursor, self.state.winid, { entry.lnum, 0 })
self.state.last_set_bufnr = bufnr
end
Expand Down Expand Up @@ -751,42 +756,25 @@ previewers.git_branch_log = defaulter(function(opts)
if hstart then
if hend < #line then
pcall(
vim.api.nvim_buf_add_highlight,
utils.hl_range,
bufnr,
ns_previewer,
"TelescopeResultsIdentifier",
i - 1,
hstart - 1,
hend
{ i - 1, hstart - 1 },
{ i - 1, hend }
)
end
end
local _, cstart = line:find "- %("
if cstart then
local cend = string.find(line, "%) ")
if cend then
pcall(
vim.api.nvim_buf_add_highlight,
bufnr,
ns_previewer,
"TelescopeResultsConstant",
i - 1,
cstart - 1,
cend
)
pcall(utils.hl_range, bufnr, ns_previewer, "TelescopeResultsConstant", { i - 1, cstart - 1 }, { i - 1, cend })
end
end
local dstart, _ = line:find " %(%d"
if dstart then
pcall(
vim.api.nvim_buf_add_highlight,
bufnr,
ns_previewer,
"TelescopeResultsSpecialComment",
i - 1,
dstart,
#line
)
pcall(utils.hl_range, bufnr, ns_previewer, "TelescopeResultsSpecialComment", { i - 1, dstart }, { #line })
end
end
end
Expand Down Expand Up @@ -965,7 +953,7 @@ previewers.git_commit_message = defaulter(function(opts)
for k, v in ipairs(hl_map) do
local _, s = content[k]:find "%s"
if s then
vim.api.nvim_buf_add_highlight(bufnr, ns_previewer, v, k - 1, s, #content[k])
utils.hl_range(bufnr, ns_previewer, v, { k - 1, s }, { k - 1, #content[k] })
end
end
end,
Expand Down Expand Up @@ -1054,7 +1042,7 @@ previewers.autocommands = defaulter(function(_)

vim.api.nvim_buf_set_option(self.state.bufnr, "filetype", "vim")
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, display)
vim.api.nvim_buf_add_highlight(self.state.bufnr, 0, "TelescopeBorder", 1, 0, -1)
utils.hl_range(self.state.bufnr, 0, "TelescopeBorder", { 1, 0 }, { 1, -1 })
else
for idx, item in ipairs(results) do
if item == entry then
Expand All @@ -1064,7 +1052,13 @@ previewers.autocommands = defaulter(function(_)
end
end

vim.api.nvim_buf_add_highlight(self.state.bufnr, ns_previewer, "TelescopePreviewLine", selected_row + 1, 0, -1)
utils.hl_range(
self.state.bufnr,
ns_previewer,
"TelescopePreviewLine",
{ selected_row + 1, 0 },
{ selected_row + 1, -1 }
)
-- set the cursor position after self.state.bufnr is connected to the
-- preview window (which is scheduled in new_buffer_previewer)
vim.schedule(function()
Expand Down Expand Up @@ -1109,7 +1103,7 @@ previewers.highlights = defaulter(function(_)
local startPos = string.find(v, "xxx", 1, true) - 1
local endPos = startPos + 3
local hlgroup = string.match(v, "([^ ]*)%s+.*")
pcall(vim.api.nvim_buf_add_highlight, self.state.bufnr, 0, hlgroup, k - 1, startPos, endPos)
pcall(utils.hl_range, self.state.bufnr, 0, hlgroup, { k - 1, startPos }, { k - 1, endPos })
end
end

Expand All @@ -1120,13 +1114,12 @@ previewers.highlights = defaulter(function(_)
local lnum = vim.api.nvim_win_get_cursor(self.state.winid)[1]
-- That one is actually a match but its better to use it like that then matchadd
pcall(vim.api.nvim_buf_clear_namespace, self.state.bufnr, ns_previewer, 0, -1)
vim.api.nvim_buf_add_highlight(
utils.hl_range(
self.state.bufnr,
ns_previewer,
"TelescopePreviewMatch",
lnum - 1,
0,
#entry.value
{ lnum - 1, 0 },
{ lnum - 1, #entry.value }
)
-- we need to zz after the highlighting otherwise highlighting doesnt work
vim.cmd "norm! zz"
Expand Down Expand Up @@ -1194,24 +1187,22 @@ previewers.pickers = defaulter(function(_)

if display_highlight ~= nil then
for _, hl_block in ipairs(display_highlight) do
vim.api.nvim_buf_add_highlight(
utils.hl_range(
self.state.bufnr,
ns_telescope_entry,
hl_block[2],
row,
hl_block[1][1],
hl_block[1][2]
{ row, hl_block[1][1] },
{ row, hl_block[1][2] }
)
end
end
if picker._multi:is_selected(e) then
vim.api.nvim_buf_add_highlight(
utils.hl_range(
self.state.bufnr,
ns_telescope_multiselection,
"TelescopeMultiSelection",
row,
0,
-1
{ row, 0 },
{ row, -1 }
)
end
end
Expand Down
3 changes: 3 additions & 0 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ local utils = {}
utils.iswin = vim.uv.os_uname().sysname == "Windows_NT"
utils.nvim011 = vim.fn.has "nvim-0.11" == 1

---TODO(clason): remove when dropping support for Nvim 0.10
utils.hl_range = utils.nvim011 and vim.hl.range or vim.highlight.range

---TODO(clason): remove when dropping support for Nvim 0.10
utils.str_byteindex = utils.nvim011 and vim.str_byteindex or vim.lsp.util._str_byteindex_enc

Expand Down