Skip to content

Commit db74032

Browse files
authored
chore: resolve deprecated in 0.11 (#3053)
* chore: resolve deprecated in 0.11 * chore: resolve deprecated in 0.11 * chore: resolve deprecated in 0.11 * chore: resolve deprecated in 0.11 * chore: resolve deprecated in 0.11 * chore: resolve deprecated in 0.11
1 parent fca0b67 commit db74032

File tree

5 files changed

+51
-32
lines changed

5 files changed

+51
-32
lines changed

lua/nvim-tree/appearance/hi-test.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ local Class = require("nvim-tree.classic")
55
-- others with name and links less than this arbitrary value are short
66
local SHORT_LEN = 50
77

8+
local namespace_hi_test_id = vim.api.nvim_create_namespace("NvimTreeHiTest")
9+
810
---@class (exact) HighlightDisplay: Class for :NvimTreeHiTest
911
---@field group string nvim-tree highlight group name
1012
---@field links string link chain to a concretely defined group
@@ -52,7 +54,12 @@ function HighlightDisplay:render(bufnr, fmt, l)
5254
local text = string.format(fmt, self.group, self.links, self.def)
5355

5456
vim.api.nvim_buf_set_lines(bufnr, l, -1, true, { text })
55-
vim.api.nvim_buf_add_highlight(bufnr, -1, self.group, l, 0, #self.group)
57+
58+
if vim.fn.has("nvim-0.11") == 1 then
59+
vim.hl.range(bufnr, namespace_hi_test_id, self.group, { l, 0 }, { l, #self.group, }, {})
60+
else
61+
vim.api.nvim_buf_add_highlight(bufnr, -1, self.group, l, 0, #self.group) ---@diagnostic disable-line: deprecated
62+
end
5663

5764
return l + 1
5865
end

lua/nvim-tree/help.lua

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ local WIN_HL = table.concat({
1111
"CursorLine:NvimTreeCursorLine",
1212
}, ",")
1313

14+
local namespace_help_id = vim.api.nvim_create_namespace("NvimTreeHelp")
15+
1416
local M = {
1517
config = {},
1618

@@ -82,8 +84,8 @@ end
8284

8385
--- Compute all lines for the buffer
8486
---@param map table keymap.get_keymap
85-
---@return table strings of text
86-
---@return table arrays of arguments 3-6 for nvim_buf_add_highlight()
87+
---@return string[] lines of text
88+
---@return HighlightRangeArgs[] hl_range_args for lines
8789
---@return number maximum length of text
8890
local function compute(map)
8991
local head_lhs = "nvim-tree mappings"
@@ -130,10 +132,10 @@ local function compute(map)
130132
local width = #lines[1]
131133

132134
-- header highlight, assume one character keys
133-
local hl = {
134-
{ "NvimTreeFolderName", 0, 0, #head_lhs },
135-
{ "NvimTreeFolderName", 0, width - 1, width },
136-
{ "NvimTreeFolderName", 1, width - 1, width },
135+
local hl_range_args = {
136+
{ higroup = "NvimTreeFolderName", start = { 0, 0, }, finish = { 0, #head_lhs, }, },
137+
{ higroup = "NvimTreeFolderName", start = { 0, width - 1, }, finish = { 0, width, }, },
138+
{ higroup = "NvimTreeFolderName", start = { 1, width - 1, }, finish = { 1, width, }, },
137139
}
138140

139141
-- mappings, left padded 1
@@ -145,10 +147,10 @@ local function compute(map)
145147
width = math.max(#line, width)
146148

147149
-- highlight lhs
148-
table.insert(hl, { "NvimTreeFolderName", i + 1, 1, #l.lhs + 1 })
150+
table.insert(hl_range_args, { higroup = "NvimTreeFolderName", start = { i + 1, 1, }, finish = { i + 1, #l.lhs + 1, }, })
149151
end
150152

151-
return lines, hl, width
153+
return lines, hl_range_args, width
152154
end
153155

154156
--- close the window and delete the buffer, if they exist
@@ -172,7 +174,7 @@ local function open()
172174
local map = keymap.get_keymap()
173175

174176
-- text and highlight
175-
local lines, hl, width = compute(map)
177+
local lines, hl_range_args, width = compute(map)
176178

177179
-- create the buffer
178180
M.bufnr = vim.api.nvim_create_buf(false, true)
@@ -187,8 +189,12 @@ local function open()
187189
end
188190

189191
-- highlight it
190-
for _, h in ipairs(hl) do
191-
vim.api.nvim_buf_add_highlight(M.bufnr, -1, h[1], h[2], h[3], h[4])
192+
for _, args in ipairs(hl_range_args) do
193+
if vim.fn.has("nvim-0.11") == 1 then
194+
vim.hl.range(M.bufnr, namespace_help_id, args.higroup, args.start, args.finish, {})
195+
else
196+
vim.api.nvim_buf_add_highlight(M.bufnr, -1, args.higroup, args.start[1], args.start[2], args.finish[2]) ---@diagnostic disable-line: deprecated
197+
end
192198
end
193199

194200
-- open a very restricted window

lua/nvim-tree/renderer/builder.lua

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,9 @@ local BUILTIN_DECORATORS = {
3333
Cut = CutDecorator,
3434
}
3535

36-
---@class (exact) AddHighlightArgs
37-
---@field group string[]
38-
---@field line number
39-
---@field col_start number
40-
---@field col_end number
41-
4236
---@class (exact) Builder
4337
---@field lines string[] includes icons etc.
44-
---@field hl_args AddHighlightArgs[] line highlights
38+
---@field hl_range_args HighlightRangeArgs[] highlights for lines
4539
---@field signs string[] line signs
4640
---@field extmarks table[] extra marks for right icon placement
4741
---@field virtual_lines table[] virtual lines for hidden count display
@@ -67,7 +61,7 @@ function Builder:new(args)
6761
self.explorer = args.explorer
6862
self.index = 0
6963
self.depth = 0
70-
self.hl_args = {}
64+
self.hl_range_args = {}
7165
self.combined_groups = {}
7266
self.lines = {}
7367
self.markers = {}
@@ -106,7 +100,9 @@ end
106100
---@param start number
107101
---@param end_ number|nil
108102
function Builder:insert_highlight(groups, start, end_)
109-
table.insert(self.hl_args, { groups, self.index, start, end_ or -1 })
103+
for _, higroup in ipairs(groups) do
104+
table.insert(self.hl_range_args, { higroup = higroup, start = { self.index, start, }, finish = { self.index, end_ or -1, } })
105+
end
110106
end
111107

112108
---@private

lua/nvim-tree/renderer/components/full-name.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ local function show()
7979
---@type vim.api.keyset.extmark_details
8080
local details = extmark[4]
8181

82-
vim.api.nvim_buf_add_highlight(0, ns_id, details.hl_group, 0, col, details.end_col)
82+
if type(details) == "table" then
83+
if vim.fn.has("nvim-0.12") == 1 then
84+
vim.hl.range(0, ns_id, details.hl_group, { 0, col }, { 0, details.end_col, }, {})
85+
else
86+
vim.api.nvim_buf_add_highlight(0, ns_id, details.hl_group, 0, col, details.end_col) ---@diagnostic disable-line: deprecated
87+
end
88+
end
8389
end
8490
vim.cmd([[ setlocal nowrap cursorline noswapfile nobuflisted buftype=nofile bufhidden=wipe ]])
8591
end)

lua/nvim-tree/renderer/init.lua

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ local namespace_highlights_id = vim.api.nvim_create_namespace("NvimTreeHighlight
1111
local namespace_extmarks_id = vim.api.nvim_create_namespace("NvimTreeExtmarks")
1212
local namespace_virtual_lines_id = vim.api.nvim_create_namespace("NvimTreeVirtualLines")
1313

14+
---@alias HighlightRangeArgs { higroup:string, start:integer[], finish:integer[] } named arguments for vim.hl.range
15+
1416
---@class (exact) Renderer: Class
1517
---@field explorer Explorer
1618
local Renderer = Class:extend()
@@ -30,19 +32,19 @@ end
3032
---@private
3133
---@param bufnr number
3234
---@param lines string[]
33-
---@param hl_args AddHighlightArgs[]
35+
---@param hl_range_args HighlightRangeArgs[]
3436
---@param signs string[]
3537
---@param extmarks table[] extra marks for right icon placement
3638
---@param virtual_lines table[] virtual lines for hidden count display
37-
function Renderer:_draw(bufnr, lines, hl_args, signs, extmarks, virtual_lines)
39+
function Renderer:_draw(bufnr, lines, hl_range_args, signs, extmarks, virtual_lines)
3840
if vim.fn.has("nvim-0.10") == 1 then
3941
vim.api.nvim_set_option_value("modifiable", true, { buf = bufnr })
4042
else
4143
vim.api.nvim_buf_set_option(bufnr, "modifiable", true) ---@diagnostic disable-line: deprecated
4244
end
4345

4446
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
45-
self:render_hl(bufnr, hl_args)
47+
self:render_hl(bufnr, hl_range_args)
4648

4749
if vim.fn.has("nvim-0.10") == 1 then
4850
vim.api.nvim_set_option_value("modifiable", false, { buf = bufnr })
@@ -77,16 +79,18 @@ function Renderer:_draw(bufnr, lines, hl_args, signs, extmarks, virtual_lines)
7779
end
7880

7981
---@private
80-
function Renderer:render_hl(bufnr, hl)
82+
---@param bufnr integer
83+
---@param hl_range_args HighlightRangeArgs[]
84+
function Renderer:render_hl(bufnr, hl_range_args)
8185
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
8286
return
8387
end
8488
vim.api.nvim_buf_clear_namespace(bufnr, namespace_highlights_id, 0, -1)
85-
for _, data in ipairs(hl) do
86-
if type(data[1]) == "table" then
87-
for _, group in ipairs(data[1]) do
88-
vim.api.nvim_buf_add_highlight(bufnr, namespace_highlights_id, group, data[2], data[3], data[4])
89-
end
89+
for _, args in ipairs(hl_range_args) do
90+
if vim.fn.has("nvim-0.11") == 1 then
91+
vim.hl.range(bufnr, namespace_highlights_id, args.higroup, args.start, args.finish, {})
92+
else
93+
vim.api.nvim_buf_add_highlight(bufnr, namespace_highlights_id, args.higroup, args.start[1], args.start[2], args.finish[2]) ---@diagnostic disable-line: deprecated
9094
end
9195
end
9296
end
@@ -103,7 +107,7 @@ function Renderer:draw()
103107

104108
local builder = Builder(self.explorer):build()
105109

106-
self:_draw(bufnr, builder.lines, builder.hl_args, builder.signs, builder.extmarks, builder.virtual_lines)
110+
self:_draw(bufnr, builder.lines, builder.hl_range_args, builder.signs, builder.extmarks, builder.virtual_lines)
107111

108112
if cursor and #builder.lines >= cursor[1] then
109113
vim.api.nvim_win_set_cursor(view.get_winnr() or 0, cursor)

0 commit comments

Comments
 (0)