Skip to content

Commit 3d2aae1

Browse files
committed
refactor: replace deprecated nvim_buf_add_highlight with vim.hl.range
1 parent 1d7729c commit 3d2aae1

File tree

5 files changed

+65
-65
lines changed

5 files changed

+65
-65
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.hl_range(
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.hl_range(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.hl_range(
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 hl = vim.hl or vim.highlight
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+
hl.range(
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+
hl.range(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+
hl.range(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+
hl.range(
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: 40 additions & 49 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.hl_range(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.hl_range(
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.hl_range(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.hl_range,
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.hl_range,
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,42 +756,25 @@ 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.hl_range,
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
764768
local _, cstart = line:find "- %("
765769
if cstart then
766770
local cend = string.find(line, "%) ")
767771
if cend then
768-
pcall(
769-
vim.api.nvim_buf_add_highlight,
770-
bufnr,
771-
ns_previewer,
772-
"TelescopeResultsConstant",
773-
i - 1,
774-
cstart - 1,
775-
cend
776-
)
772+
pcall(utils.hl_range, bufnr, ns_previewer, "TelescopeResultsConstant", { i - 1, cstart - 1 }, { i - 1, cend })
777773
end
778774
end
779775
local dstart, _ = line:find " %(%d"
780776
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-
)
777+
pcall(utils.hl_range, bufnr, ns_previewer, "TelescopeResultsSpecialComment", { i - 1, dstart }, { #line })
790778
end
791779
end
792780
end
@@ -965,7 +953,7 @@ previewers.git_commit_message = defaulter(function(opts)
965953
for k, v in ipairs(hl_map) do
966954
local _, s = content[k]:find "%s"
967955
if s then
968-
vim.api.nvim_buf_add_highlight(bufnr, ns_previewer, v, k - 1, s, #content[k])
956+
utils.hl_range(bufnr, ns_previewer, v, { k - 1, s }, { k - 1, #content[k] })
969957
end
970958
end
971959
end,
@@ -1054,7 +1042,7 @@ previewers.autocommands = defaulter(function(_)
10541042

10551043
vim.api.nvim_buf_set_option(self.state.bufnr, "filetype", "vim")
10561044
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)
1045+
utils.hl_range(self.state.bufnr, 0, "TelescopeBorder", { 1, 0 }, { 1, -1 })
10581046
else
10591047
for idx, item in ipairs(results) do
10601048
if item == entry then
@@ -1064,7 +1052,13 @@ previewers.autocommands = defaulter(function(_)
10641052
end
10651053
end
10661054

1067-
vim.api.nvim_buf_add_highlight(self.state.bufnr, ns_previewer, "TelescopePreviewLine", selected_row + 1, 0, -1)
1055+
utils.hl_range(
1056+
self.state.bufnr,
1057+
ns_previewer,
1058+
"TelescopePreviewLine",
1059+
{ selected_row + 1, 0 },
1060+
{ selected_row + 1, -1 }
1061+
)
10681062
-- set the cursor position after self.state.bufnr is connected to the
10691063
-- preview window (which is scheduled in new_buffer_previewer)
10701064
vim.schedule(function()
@@ -1109,7 +1103,7 @@ previewers.highlights = defaulter(function(_)
11091103
local startPos = string.find(v, "xxx", 1, true) - 1
11101104
local endPos = startPos + 3
11111105
local hlgroup = string.match(v, "([^ ]*)%s+.*")
1112-
pcall(vim.api.nvim_buf_add_highlight, self.state.bufnr, 0, hlgroup, k - 1, startPos, endPos)
1106+
pcall(utils.hl_range, self.state.bufnr, 0, hlgroup, { k - 1, startPos }, { k - 1, endPos })
11131107
end
11141108
end
11151109

@@ -1120,13 +1114,12 @@ previewers.highlights = defaulter(function(_)
11201114
local lnum = vim.api.nvim_win_get_cursor(self.state.winid)[1]
11211115
-- That one is actually a match but its better to use it like that then matchadd
11221116
pcall(vim.api.nvim_buf_clear_namespace, self.state.bufnr, ns_previewer, 0, -1)
1123-
vim.api.nvim_buf_add_highlight(
1117+
utils.hl_range(
11241118
self.state.bufnr,
11251119
ns_previewer,
11261120
"TelescopePreviewMatch",
1127-
lnum - 1,
1128-
0,
1129-
#entry.value
1121+
{ lnum - 1, 0 },
1122+
{ lnum - 1, #entry.value }
11301123
)
11311124
-- we need to zz after the highlighting otherwise highlighting doesnt work
11321125
vim.cmd "norm! zz"
@@ -1194,24 +1187,22 @@ previewers.pickers = defaulter(function(_)
11941187

11951188
if display_highlight ~= nil then
11961189
for _, hl_block in ipairs(display_highlight) do
1197-
vim.api.nvim_buf_add_highlight(
1190+
utils.hl_range(
11981191
self.state.bufnr,
11991192
ns_telescope_entry,
12001193
hl_block[2],
1201-
row,
1202-
hl_block[1][1],
1203-
hl_block[1][2]
1194+
{ row, hl_block[1][1] },
1195+
{ row, hl_block[1][2] }
12041196
)
12051197
end
12061198
end
12071199
if picker._multi:is_selected(e) then
1208-
vim.api.nvim_buf_add_highlight(
1200+
utils.hl_range(
12091201
self.state.bufnr,
12101202
ns_telescope_multiselection,
12111203
"TelescopeMultiSelection",
1212-
row,
1213-
0,
1214-
-1
1204+
{ row, 0 },
1205+
{ row, -1 }
12151206
)
12161207
end
12171208
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.hl_range = 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)