Skip to content

chore: resolve deprecated in 0.11 #3053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 24, 2025
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
9 changes: 8 additions & 1 deletion lua/nvim-tree/appearance/hi-test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ local Class = require("nvim-tree.classic")
-- others with name and links less than this arbitrary value are short
local SHORT_LEN = 50

local namespace_hi_test_id = vim.api.nvim_create_namespace("NvimTreeHiTest")

---@class (exact) HighlightDisplay: Class for :NvimTreeHiTest
---@field group string nvim-tree highlight group name
---@field links string link chain to a concretely defined group
Expand Down Expand Up @@ -52,7 +54,12 @@ function HighlightDisplay:render(bufnr, fmt, l)
local text = string.format(fmt, self.group, self.links, self.def)

vim.api.nvim_buf_set_lines(bufnr, l, -1, true, { text })
vim.api.nvim_buf_add_highlight(bufnr, -1, self.group, l, 0, #self.group)

if vim.fn.has("nvim-0.11") == 1 then
vim.hl.range(bufnr, namespace_hi_test_id, self.group, { l, 0 }, { l, #self.group, }, {})
else
vim.api.nvim_buf_add_highlight(bufnr, -1, self.group, l, 0, #self.group) ---@diagnostic disable-line: deprecated
end

return l + 1
end
Expand Down
28 changes: 17 additions & 11 deletions lua/nvim-tree/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ local WIN_HL = table.concat({
"CursorLine:NvimTreeCursorLine",
}, ",")

local namespace_help_id = vim.api.nvim_create_namespace("NvimTreeHelp")

local M = {
config = {},

Expand Down Expand Up @@ -82,8 +84,8 @@ end

--- Compute all lines for the buffer
---@param map table keymap.get_keymap
---@return table strings of text
---@return table arrays of arguments 3-6 for nvim_buf_add_highlight()
---@return string[] lines of text
---@return HighlightRangeArgs[] hl_range_args for lines
---@return number maximum length of text
local function compute(map)
local head_lhs = "nvim-tree mappings"
Expand Down Expand Up @@ -130,10 +132,10 @@ local function compute(map)
local width = #lines[1]

-- header highlight, assume one character keys
local hl = {
{ "NvimTreeFolderName", 0, 0, #head_lhs },
{ "NvimTreeFolderName", 0, width - 1, width },
{ "NvimTreeFolderName", 1, width - 1, width },
local hl_range_args = {
{ higroup = "NvimTreeFolderName", start = { 0, 0, }, finish = { 0, #head_lhs, }, },
{ higroup = "NvimTreeFolderName", start = { 0, width - 1, }, finish = { 0, width, }, },
{ higroup = "NvimTreeFolderName", start = { 1, width - 1, }, finish = { 1, width, }, },
}

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

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

return lines, hl, width
return lines, hl_range_args, width
end

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

-- text and highlight
local lines, hl, width = compute(map)
local lines, hl_range_args, width = compute(map)

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

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

-- open a very restricted window
Expand Down
14 changes: 5 additions & 9 deletions lua/nvim-tree/renderer/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,9 @@ local BUILTIN_DECORATORS = {
Cut = CutDecorator,
}

---@class (exact) AddHighlightArgs
---@field group string[]
---@field line number
---@field col_start number
---@field col_end number

---@class (exact) Builder
---@field lines string[] includes icons etc.
---@field hl_args AddHighlightArgs[] line highlights
---@field hl_range_args HighlightRangeArgs[] highlights for lines
---@field signs string[] line signs
---@field extmarks table[] extra marks for right icon placement
---@field virtual_lines table[] virtual lines for hidden count display
Expand All @@ -67,7 +61,7 @@ function Builder:new(args)
self.explorer = args.explorer
self.index = 0
self.depth = 0
self.hl_args = {}
self.hl_range_args = {}
self.combined_groups = {}
self.lines = {}
self.markers = {}
Expand Down Expand Up @@ -106,7 +100,9 @@ end
---@param start number
---@param end_ number|nil
function Builder:insert_highlight(groups, start, end_)
table.insert(self.hl_args, { groups, self.index, start, end_ or -1 })
for _, higroup in ipairs(groups) do
table.insert(self.hl_range_args, { higroup = higroup, start = { self.index, start, }, finish = { self.index, end_ or -1, } })
end
end

---@private
Expand Down
8 changes: 7 additions & 1 deletion lua/nvim-tree/renderer/components/full-name.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ local function show()
---@type vim.api.keyset.extmark_details
local details = extmark[4]

vim.api.nvim_buf_add_highlight(0, ns_id, details.hl_group, 0, col, details.end_col)
if type(details) == "table" then
if vim.fn.has("nvim-0.12") == 1 then
vim.hl.range(0, ns_id, details.hl_group, { 0, col }, { 0, details.end_col, }, {})
else
vim.api.nvim_buf_add_highlight(0, ns_id, details.hl_group, 0, col, details.end_col) ---@diagnostic disable-line: deprecated
end
end
end
vim.cmd([[ setlocal nowrap cursorline noswapfile nobuflisted buftype=nofile bufhidden=wipe ]])
end)
Expand Down
24 changes: 14 additions & 10 deletions lua/nvim-tree/renderer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ local namespace_highlights_id = vim.api.nvim_create_namespace("NvimTreeHighlight
local namespace_extmarks_id = vim.api.nvim_create_namespace("NvimTreeExtmarks")
local namespace_virtual_lines_id = vim.api.nvim_create_namespace("NvimTreeVirtualLines")

---@alias HighlightRangeArgs { higroup:string, start:integer[], finish:integer[] } named arguments for vim.hl.range

---@class (exact) Renderer: Class
---@field explorer Explorer
local Renderer = Class:extend()
Expand All @@ -30,19 +32,19 @@ end
---@private
---@param bufnr number
---@param lines string[]
---@param hl_args AddHighlightArgs[]
---@param hl_range_args HighlightRangeArgs[]
---@param signs string[]
---@param extmarks table[] extra marks for right icon placement
---@param virtual_lines table[] virtual lines for hidden count display
function Renderer:_draw(bufnr, lines, hl_args, signs, extmarks, virtual_lines)
function Renderer:_draw(bufnr, lines, hl_range_args, signs, extmarks, virtual_lines)
if vim.fn.has("nvim-0.10") == 1 then
vim.api.nvim_set_option_value("modifiable", true, { buf = bufnr })
else
vim.api.nvim_buf_set_option(bufnr, "modifiable", true) ---@diagnostic disable-line: deprecated
end

vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
self:render_hl(bufnr, hl_args)
self:render_hl(bufnr, hl_range_args)

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

---@private
function Renderer:render_hl(bufnr, hl)
---@param bufnr integer
---@param hl_range_args HighlightRangeArgs[]
function Renderer:render_hl(bufnr, hl_range_args)
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
return
end
vim.api.nvim_buf_clear_namespace(bufnr, namespace_highlights_id, 0, -1)
for _, data in ipairs(hl) do
if type(data[1]) == "table" then
for _, group in ipairs(data[1]) do
vim.api.nvim_buf_add_highlight(bufnr, namespace_highlights_id, group, data[2], data[3], data[4])
end
for _, args in ipairs(hl_range_args) do
if vim.fn.has("nvim-0.11") == 1 then
vim.hl.range(bufnr, namespace_highlights_id, args.higroup, args.start, args.finish, {})
else
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
end
end
end
Expand All @@ -103,7 +107,7 @@ function Renderer:draw()

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

self:_draw(bufnr, builder.lines, builder.hl_args, builder.signs, builder.extmarks, builder.virtual_lines)
self:_draw(bufnr, builder.lines, builder.hl_range_args, builder.signs, builder.extmarks, builder.virtual_lines)

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