mirror of
https://github.com/neovim/neovim.git
synced 2024-12-19 18:55:14 -07:00
feat(treesitter): use 0-based indexing to show ranges in :InspectTree
Problem: - `:InspectTree` was showing node ranges in 1-based indexing, i.e., in vim cursor position (lnum, col). However, treesitter API adopts 0-based indexing to represent ranges (Range4). This can often be confusing for developers and plugin authors when debugging code written with treesiter APIs. Solution: - Change to 0-based indexing from 1-based indexing to show node ranges in `:InspectTree`. - Note: To make things not complicated, we do not provide an option or keymap to configure which indexing mode to use.
This commit is contained in:
parent
0e9a33572d
commit
a478bf936b
@ -224,6 +224,8 @@ The following new APIs and features were added.
|
|||||||
• |vim.treesitter.query.edit()| allows live editing of treesitter
|
• |vim.treesitter.query.edit()| allows live editing of treesitter
|
||||||
queries.
|
queries.
|
||||||
• Improved error messages for query parsing.
|
• Improved error messages for query parsing.
|
||||||
|
• `:InspectTree` (|vim.treesitter.inspect_tree()|) shows node ranges in
|
||||||
|
0-based indexing instead of 1-based indexing.
|
||||||
|
|
||||||
• |vim.ui.open()| opens URIs using the system default handler (macOS `open`,
|
• |vim.ui.open()| opens URIs using the system default handler (macOS `open`,
|
||||||
Windows `explorer`, Linux `xdg-open`, etc.)
|
Windows `explorer`, Linux `xdg-open`, etc.)
|
||||||
|
@ -3,16 +3,20 @@ local api = vim.api
|
|||||||
---@class TSDevModule
|
---@class TSDevModule
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
---@private
|
||||||
---@class TSTreeView
|
---@class TSTreeView
|
||||||
---@field ns integer API namespace
|
---@field ns integer API namespace
|
||||||
---@field opts table Options table with the following keys:
|
---@field opts TSTreeViewOpts
|
||||||
--- - anon (boolean): If true, display anonymous nodes
|
|
||||||
--- - lang (boolean): If true, display the language alongside each node
|
|
||||||
--- - indent (number): Number of spaces to indent nested lines. Default is 2.
|
|
||||||
---@field nodes TSP.Node[]
|
---@field nodes TSP.Node[]
|
||||||
---@field named TSP.Node[]
|
---@field named TSP.Node[]
|
||||||
local TSTreeView = {}
|
local TSTreeView = {}
|
||||||
|
|
||||||
|
---@private
|
||||||
|
---@class TSTreeViewOpts
|
||||||
|
---@field anon boolean If true, display anonymous nodes.
|
||||||
|
---@field lang boolean If true, display the language alongside each node.
|
||||||
|
---@field indent number Number of spaces to indent nested lines.
|
||||||
|
|
||||||
---@class TSP.Node
|
---@class TSP.Node
|
||||||
---@field node TSNode Treesitter node
|
---@field node TSNode Treesitter node
|
||||||
---@field field string? Node field
|
---@field field string? Node field
|
||||||
@ -115,6 +119,7 @@ function TSTreeView:new(bufnr, lang)
|
|||||||
ns = api.nvim_create_namespace('treesitter/dev-inspect'),
|
ns = api.nvim_create_namespace('treesitter/dev-inspect'),
|
||||||
nodes = nodes,
|
nodes = nodes,
|
||||||
named = named,
|
named = named,
|
||||||
|
---@type TSTreeViewOpts
|
||||||
opts = {
|
opts = {
|
||||||
anon = false,
|
anon = false,
|
||||||
lang = false,
|
lang = false,
|
||||||
@ -129,16 +134,12 @@ end
|
|||||||
|
|
||||||
local decor_ns = api.nvim_create_namespace('ts.dev')
|
local decor_ns = api.nvim_create_namespace('ts.dev')
|
||||||
|
|
||||||
---@param lnum integer
|
---@param range Range4
|
||||||
---@param col integer
|
|
||||||
---@param end_lnum integer
|
|
||||||
---@param end_col integer
|
|
||||||
---@return string
|
---@return string
|
||||||
local function get_range_str(lnum, col, end_lnum, end_col)
|
local function range_to_string(range)
|
||||||
if lnum == end_lnum then
|
---@type integer, integer, integer, integer
|
||||||
return string.format('[%d:%d - %d]', lnum + 1, col + 1, end_col)
|
local row, col, end_row, end_col = unpack(range)
|
||||||
end
|
return string.format('[%d, %d] - [%d, %d]', row, col, end_row, end_col)
|
||||||
return string.format('[%d:%d - %d:%d]', lnum + 1, col + 1, end_lnum + 1, end_col)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param w integer
|
---@param w integer
|
||||||
@ -212,7 +213,7 @@ function TSTreeView:draw(bufnr)
|
|||||||
local lang_hl_marks = {} ---@type table[]
|
local lang_hl_marks = {} ---@type table[]
|
||||||
|
|
||||||
for i, item in self:iter() do
|
for i, item in self:iter() do
|
||||||
local range_str = get_range_str(item.node:range())
|
local range_str = range_to_string({ item.node:range() })
|
||||||
local lang_str = self.opts.lang and string.format(' %s', item.lang) or ''
|
local lang_str = self.opts.lang and string.format(' %s', item.lang) or ''
|
||||||
|
|
||||||
local text ---@type string
|
local text ---@type string
|
||||||
|
Loading…
Reference in New Issue
Block a user