Skip to content

Commit 715cab3

Browse files
committed
refactor: replace deprecated nvim_buf_add_highlight with vim.hl.range
1 parent 47a8530 commit 715cab3

File tree

5 files changed

+67
-60
lines changed

5 files changed

+67
-60
lines changed

lua/telescope/actions/init.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,13 @@ actions.which_key = function(prompt_bufnr, opts)
14381438
local row_ = highlight_tbl.row
14391439
local col = highlight_tbl.col
14401440
for _, hl_block in ipairs(highlight) do
1441-
a.nvim_buf_add_highlight(km_buf, keymap_highlights, hl_block[2], row_, col + hl_block[1][1], col + hl_block[1][2])
1441+
utils.highlight(
1442+
km_buf,
1443+
keymap_highlights,
1444+
hl_block[2],
1445+
{ row_, col + hl_block[1][1] },
1446+
{ row_, col + hl_block[1][2] }
1447+
)
14421448
end
14431449
end
14441450

lua/telescope/pickers.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ function Picker:highlight_one_row(results_bufnr, prompt, display, row)
479479

480480
self:_increment "highlights"
481481

482-
vim.api.nvim_buf_add_highlight(results_bufnr, ns_telescope_matching, highlight, row, start - 1, finish)
482+
utils.highlight(results_bufnr, ns_telescope_matching, highlight, { row, start - 1 }, { row, finish })
483483
end
484484
end
485485

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

977977
if self.prompt_prefix ~= "" and a.nvim_buf_is_valid(self.prompt_bufnr) then
978-
vim.api.nvim_buf_add_highlight(
978+
utils.highlight(
979979
self.prompt_bufnr,
980980
ns_telescope_prompt_prefix,
981981
self._current_prefix_hl_group or "TelescopePromptPrefix",
982-
0,
983-
0,
984-
#self.prompt_prefix
982+
{ 0, 0 },
983+
{ 0, #self.prompt_prefix }
985984
)
986985
end
987986
end

lua/telescope/pickers/highlights.lua

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ local ns_telescope_selection = a.nvim_create_namespace "telescope_selection"
88
local ns_telescope_multiselection = a.nvim_create_namespace "telescope_multiselection"
99
local ns_telescope_entry = a.nvim_create_namespace "telescope_entry"
1010

11+
---TODO(clason): remove when dropping support for Nvim 0.10
12+
local highlight = vim.hl.range or vim.highlight.range
13+
1114
local Highlighter = {}
1215
Highlighter.__index = Highlighter
1316

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

3235
for _, hl_block in ipairs(display_highlights) do
33-
a.nvim_buf_add_highlight(
36+
highlight(
3437
results_bufnr,
3538
ns_telescope_entry,
3639
hl_block[2],
37-
row,
38-
len_prefix + hl_block[1][1],
39-
len_prefix + hl_block[1][2]
40+
{ row, len_prefix + hl_block[1][1] },
41+
{ row, len_prefix + hl_block[1][2] }
4042
)
4143
end
4244
end
@@ -69,7 +71,7 @@ function Highlighter:hi_selection(row, caret)
6971
local results_bufnr = assert(self.picker.results_bufnr, "Must have a results bufnr")
7072

7173
a.nvim_buf_clear_namespace(results_bufnr, ns_telescope_selection, 0, -1)
72-
a.nvim_buf_add_highlight(results_bufnr, ns_telescope_selection, "TelescopeSelectionCaret", row, 0, #caret)
74+
highlight(results_bufnr, ns_telescope_selection, "TelescopeSelectionCaret", { row, 0 }, { row, #caret })
7375

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

8688
if is_selected then
87-
vim.api.nvim_buf_add_highlight(results_bufnr, ns_telescope_multiselection, "TelescopeMultiSelection", row, 0, -1)
89+
highlight(results_bufnr, ns_telescope_multiselection, "TelescopeMultiSelection", { row, 0 }, { row, -1 })
8890
if self.picker.multi_icon then
8991
local line = vim.api.nvim_buf_get_lines(results_bufnr, row, row + 1, false)[1]
9092
local pos = line:find(self.picker.multi_icon)
9193
if pos and pos <= math.max(#self.picker.selection_caret, #self.picker.entry_prefix) then
92-
vim.api.nvim_buf_add_highlight(
94+
highlight(
9395
results_bufnr,
9496
ns_telescope_multiselection,
9597
"TelescopeMultiIcon",
96-
row,
97-
pos - 1,
98-
pos - 1 + #self.picker.multi_icon
98+
{ row, pos - 1 },
99+
{ row, pos - 1 + #self.picker.multi_icon }
99100
)
100101
end
101102
end

lua/telescope/previewers/buffer_previewer.lua

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,16 @@ local colorize_ls_long = function(bufnr, data, sections)
101101
local section = sections[lnum]
102102
for i = 1, section[1].end_index - 1 do -- Highlight permissions
103103
local c = line:sub(i, i)
104-
vim.api.nvim_buf_add_highlight(bufnr, ns_previewer, color_hash[c], lnum - 1, i - 1, i)
104+
utils.highlight(bufnr, ns_previewer, color_hash[c], { lnum - 1, i - 1 }, { lnum - 1, i })
105105
end
106106
for i = 2, #section do -- highlights size, (user, group), date and name
107107
local hl_group = color_hash[i + (i ~= 2 and windows_add or 0)]
108-
vim.api.nvim_buf_add_highlight(
108+
utils.highlight(
109109
bufnr,
110110
ns_previewer,
111111
type(hl_group) == "function" and hl_group(line) or hl_group,
112-
lnum - 1,
113-
section[i].start_index - 1,
114-
section[i].end_index - 1
112+
{ lnum - 1, section[i].start_index - 1 },
113+
{ lnum - 1, section[i].end_index - 1 }
115114
)
116115
end
117116
end
@@ -133,7 +132,7 @@ local handle_directory_preview = function(filepath, bufnr, opts)
133132
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, paths)
134133
for i, path in ipairs(paths) do
135134
local hl = color_hash[6](data[i])
136-
vim.api.nvim_buf_add_highlight(bufnr, ns_previewer, hl, i - 1, 0, #path)
135+
utils.highlight(bufnr, ns_previewer, hl, { i - 1, 0 }, { i - 1, #path })
137136
end
138137
end
139138
else
@@ -531,13 +530,12 @@ previewers.vimgrep = defaulter(function(opts)
531530

532531
for i = lnum, lnend do
533532
pcall(
534-
vim.api.nvim_buf_add_highlight,
533+
utils.highlight,
535534
bufnr,
536535
ns_previewer,
537536
"TelescopePreviewLine",
538-
i,
539-
i == lnum and col or 0,
540-
i == lnend and colend or -1
537+
{ i, i == lnum and col or 0 },
538+
{ i, i == lnend and colend or -1 }
541539
)
542540
end
543541

@@ -623,7 +621,14 @@ previewers.ctags = defaulter(function(opts)
623621
if self.state.last_set_bufnr then
624622
pcall(vim.api.nvim_buf_clear_namespace, self.state.last_set_bufnr, ns_previewer, 0, -1)
625623
end
626-
pcall(vim.api.nvim_buf_add_highlight, bufnr, ns_previewer, "TelescopePreviewMatch", entry.lnum - 1, 0, -1)
624+
pcall(
625+
utils.highlight,
626+
bufnr,
627+
ns_previewer,
628+
"TelescopePreviewMatch",
629+
{ entry.lnum - 1, 0 },
630+
{ entry.lnum - 1, -1 }
631+
)
627632
pcall(vim.api.nvim_win_set_cursor, self.state.winid, { entry.lnum, 0 })
628633
self.state.last_set_bufnr = bufnr
629634
end
@@ -751,13 +756,12 @@ previewers.git_branch_log = defaulter(function(opts)
751756
if hstart then
752757
if hend < #line then
753758
pcall(
754-
vim.api.nvim_buf_add_highlight,
759+
utils.highlight,
755760
bufnr,
756761
ns_previewer,
757762
"TelescopeResultsIdentifier",
758-
i - 1,
759-
hstart - 1,
760-
hend
763+
{ i - 1, hstart - 1 },
764+
{ i - 1, hend }
761765
)
762766
end
763767
end
@@ -766,27 +770,18 @@ previewers.git_branch_log = defaulter(function(opts)
766770
local cend = string.find(line, "%) ")
767771
if cend then
768772
pcall(
769-
vim.api.nvim_buf_add_highlight,
773+
utils.highlight,
770774
bufnr,
771775
ns_previewer,
772776
"TelescopeResultsConstant",
773-
i - 1,
774-
cstart - 1,
775-
cend
777+
{ i - 1, cstart - 1 },
778+
{ i - 1, cend }
776779
)
777780
end
778781
end
779782
local dstart, _ = line:find " %(%d"
780783
if dstart then
781-
pcall(
782-
vim.api.nvim_buf_add_highlight,
783-
bufnr,
784-
ns_previewer,
785-
"TelescopeResultsSpecialComment",
786-
i - 1,
787-
dstart,
788-
#line
789-
)
784+
pcall(utils.highlight, bufnr, ns_previewer, "TelescopeResultsSpecialComment", { i - 1, dstart }, { #line })
790785
end
791786
end
792787
end
@@ -965,7 +960,7 @@ previewers.git_commit_message = defaulter(function(opts)
965960
for k, v in ipairs(hl_map) do
966961
local _, s = content[k]:find "%s"
967962
if s then
968-
vim.api.nvim_buf_add_highlight(bufnr, ns_previewer, v, k - 1, s, #content[k])
963+
utils.highlight(bufnr, ns_previewer, v, { k - 1, s }, { k - 1, #content[k] })
969964
end
970965
end
971966
end,
@@ -1054,7 +1049,7 @@ previewers.autocommands = defaulter(function(_)
10541049

10551050
vim.api.nvim_buf_set_option(self.state.bufnr, "filetype", "vim")
10561051
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, display)
1057-
vim.api.nvim_buf_add_highlight(self.state.bufnr, 0, "TelescopeBorder", 1, 0, -1)
1052+
utils.highlight(self.state.bufnr, 0, "TelescopeBorder", { 1, 0 }, { 1, -1 })
10581053
else
10591054
for idx, item in ipairs(results) do
10601055
if item == entry then
@@ -1064,7 +1059,13 @@ previewers.autocommands = defaulter(function(_)
10641059
end
10651060
end
10661061

1067-
vim.api.nvim_buf_add_highlight(self.state.bufnr, ns_previewer, "TelescopePreviewLine", selected_row + 1, 0, -1)
1062+
utils.highlight(
1063+
self.state.bufnr,
1064+
ns_previewer,
1065+
"TelescopePreviewLine",
1066+
{ selected_row + 1, 0 },
1067+
{ selected_row + 1, -1 }
1068+
)
10681069
-- set the cursor position after self.state.bufnr is connected to the
10691070
-- preview window (which is scheduled in new_buffer_previewer)
10701071
vim.schedule(function()
@@ -1109,7 +1110,7 @@ previewers.highlights = defaulter(function(_)
11091110
local startPos = string.find(v, "xxx", 1, true) - 1
11101111
local endPos = startPos + 3
11111112
local hlgroup = string.match(v, "([^ ]*)%s+.*")
1112-
pcall(vim.api.nvim_buf_add_highlight, self.state.bufnr, 0, hlgroup, k - 1, startPos, endPos)
1113+
pcall(utils.highlight, self.state.bufnr, 0, hlgroup, { k - 1, startPos }, { k - 1, endPos })
11131114
end
11141115
end
11151116

@@ -1120,13 +1121,12 @@ previewers.highlights = defaulter(function(_)
11201121
local lnum = vim.api.nvim_win_get_cursor(self.state.winid)[1]
11211122
-- That one is actually a match but its better to use it like that then matchadd
11221123
pcall(vim.api.nvim_buf_clear_namespace, self.state.bufnr, ns_previewer, 0, -1)
1123-
vim.api.nvim_buf_add_highlight(
1124+
utils.highlight(
11241125
self.state.bufnr,
11251126
ns_previewer,
11261127
"TelescopePreviewMatch",
1127-
lnum - 1,
1128-
0,
1129-
#entry.value
1128+
{ lnum - 1, 0 },
1129+
{ lnum - 1, #entry.value }
11301130
)
11311131
-- we need to zz after the highlighting otherwise highlighting doesnt work
11321132
vim.cmd "norm! zz"
@@ -1194,24 +1194,22 @@ previewers.pickers = defaulter(function(_)
11941194

11951195
if display_highlight ~= nil then
11961196
for _, hl_block in ipairs(display_highlight) do
1197-
vim.api.nvim_buf_add_highlight(
1197+
utils.highlight(
11981198
self.state.bufnr,
11991199
ns_telescope_entry,
12001200
hl_block[2],
1201-
row,
1202-
hl_block[1][1],
1203-
hl_block[1][2]
1201+
{ row, hl_block[1][1] },
1202+
{ row, hl_block[1][2] }
12041203
)
12051204
end
12061205
end
12071206
if picker._multi:is_selected(e) then
1208-
vim.api.nvim_buf_add_highlight(
1207+
utils.highlight(
12091208
self.state.bufnr,
12101209
ns_telescope_multiselection,
12111210
"TelescopeMultiSelection",
1212-
row,
1213-
0,
1214-
-1
1211+
{ row, 0 },
1212+
{ row, -1 }
12151213
)
12161214
end
12171215
end

lua/telescope/utils.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ local utils = {}
1818
utils.iswin = vim.uv.os_uname().sysname == "Windows_NT"
1919
utils.nvim011 = vim.fn.has "nvim-0.11" == 1
2020

21+
---TODO(clason): remove when dropping support for Nvim 0.10
22+
utils.highlight = utils.nvim011 and vim.hl.range or vim.highlight.range
23+
2124
---TODO(clason): remove when dropping support for Nvim 0.10
2225
utils.str_byteindex = utils.nvim011 and vim.str_byteindex or vim.lsp.util._str_byteindex_enc
2326

0 commit comments

Comments
 (0)