refactor(treesitter): add Range type aliase for Range4|Range6

This commit is contained in:
Lewis Russell 2023-03-10 16:40:27 +00:00
parent 9d70fe062c
commit 58bbc2ea0b
7 changed files with 40 additions and 25 deletions

View File

@ -50,6 +50,11 @@ The following changes may require adaptations in user config or plugins.
• Unsaved changes are now preserved rather than discarded when |channel-stdio| • Unsaved changes are now preserved rather than discarded when |channel-stdio|
is closed. is closed.
• Changes to |vim.treesitter.get_node_text()|:
- It now returns `string`, as opposed to `string|string[]|nil`.
- The `concat` option has been removed as it was not consistently applied.
- Invalid ranges now cause an error instead of returning `nil`.
============================================================================== ==============================================================================
NEW FEATURES *news-features* NEW FEATURES *news-features*

View File

@ -898,7 +898,8 @@ Query:iter_captures({self}, {node}, {source}, {start}, {stop})
• {self} • {self}
Return: ~ Return: ~
(fun(): integer, TSNode, TSMetadata ): capture id, capture node, metadata (fun(): integer, TSNode, TSMetadata): capture id, capture node,
metadata
*Query:iter_matches()* *Query:iter_matches()*
Query:iter_matches({self}, {node}, {source}, {start}, {stop}) Query:iter_matches({self}, {node}, {source}, {start}, {stop})

View File

@ -16,6 +16,8 @@ local M = {}
---@field [5] integer end column ---@field [5] integer end column
---@field [6] integer end bytes ---@field [6] integer end bytes
---@alias Range Range4|Range6
---@private ---@private
---@param a_row integer ---@param a_row integer
---@param a_col integer ---@param a_col integer
@ -85,8 +87,8 @@ function M.validate(r)
end end
---@private ---@private
---@param r1 Range4|Range6 ---@param r1 Range
---@param r2 Range4|Range6 ---@param r2 Range
---@return boolean ---@return boolean
function M.intercepts(r1, r2) function M.intercepts(r1, r2)
local srow_1, scol_1, erow_1, ecol_1 = M.unpack4(r1) local srow_1, scol_1, erow_1, ecol_1 = M.unpack4(r1)
@ -106,7 +108,7 @@ function M.intercepts(r1, r2)
end end
---@private ---@private
---@param r Range4|Range6 ---@param r Range
---@return integer, integer, integer, integer ---@return integer, integer, integer, integer
function M.unpack4(r) function M.unpack4(r)
local off_1 = #r == 6 and 1 or 0 local off_1 = #r == 6 and 1 or 0
@ -121,8 +123,8 @@ function M.unpack6(r)
end end
---@private ---@private
---@param r1 Range4|Range6 ---@param r1 Range
---@param r2 Range4|Range6 ---@param r2 Range
---@return boolean whether r1 contains r2 ---@return boolean whether r1 contains r2
function M.contains(r1, r2) function M.contains(r1, r2)
local srow_1, scol_1, erow_1, ecol_1 = M.unpack4(r1) local srow_1, scol_1, erow_1, ecol_1 = M.unpack4(r1)
@ -143,7 +145,7 @@ end
---@private ---@private
---@param source integer|string ---@param source integer|string
---@param range Range4|Range6 ---@param range Range
---@return Range6 ---@return Range6
function M.add_bytes(source, range) function M.add_bytes(source, range)
if type(range) == 'table' and #range == 6 then if type(range) == 'table' and #range == 6 then

View File

@ -484,7 +484,6 @@ function LanguageTree:included_regions()
end end
---@private ---@private
--- TODO(lewis6991): cleanup of the node_range interface
---@param node TSNode ---@param node TSNode
---@param source string|integer ---@param source string|integer
---@param metadata TSMetadata ---@param metadata TSMetadata
@ -530,7 +529,7 @@ end
---@param pattern integer ---@param pattern integer
---@param lang string ---@param lang string
---@param combined boolean ---@param combined boolean
---@param ranges Range4[] ---@param ranges Range6[]
local function add_injection(t, tree_index, pattern, lang, combined, ranges) local function add_injection(t, tree_index, pattern, lang, combined, ranges)
assert(type(lang) == 'string') assert(type(lang) == 'string')
@ -558,13 +557,11 @@ end
--- https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection --- https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection
---@param match table<integer,TSNode> ---@param match table<integer,TSNode>
---@param metadata TSMetadata ---@param metadata TSMetadata
---@return string, boolean, Range4[] ---@return string?, boolean, Range6[]
function LanguageTree:_get_injection(match, metadata) function LanguageTree:_get_injection(match, metadata)
local ranges = {} ---@type Range4[] local ranges = {} ---@type Range6[]
local combined = metadata['injection.combined'] ~= nil local combined = metadata['injection.combined'] ~= nil
local lang = metadata['injection.language'] local lang = metadata['injection.language'] --[[@as string?]]
assert(type(lang) == 'string')
local include_children = metadata['injection.include-children'] ~= nil local include_children = metadata['injection.include-children'] ~= nil
for id, node in pairs(match) do for id, node in pairs(match) do
@ -584,7 +581,7 @@ end
---@private ---@private
---@param match table<integer,TSNode> ---@param match table<integer,TSNode>
---@param metadata TSMetadata ---@param metadata TSMetadata
---@return string, boolean, Range4[] ---@return string, boolean, Range6[]
function LanguageTree:_get_injection_deprecated(match, metadata) function LanguageTree:_get_injection_deprecated(match, metadata)
local lang = nil ---@type string local lang = nil ---@type string
local ranges = {} ---@type Range6[] local ranges = {} ---@type Range6[]
@ -910,7 +907,7 @@ end
---@private ---@private
---@param tree TSTree ---@param tree TSTree
---@param range Range4 ---@param range Range
---@return boolean ---@return boolean
local function tree_contains(tree, range) local function tree_contains(tree, range)
return Range.contains({ tree:root():range() }, range) return Range.contains({ tree:root():range() }, range)

View File

@ -59,10 +59,18 @@ end
---@private ---@private
---@param buf integer ---@param buf integer
---@param range Range6 ---@param range Range
---@returns string ---@returns string
local function buf_range_get_text(buf, range) local function buf_range_get_text(buf, range)
local start_row, start_col, end_row, end_col = Range.unpack4(range) local start_row, start_col, end_row, end_col = Range.unpack4(range)
if end_col == 0 then
if start_row == end_row then
start_col = -1
start_row = start_row - 1
end
end_col = -1
end_row = end_row - 1
end
local lines = a.nvim_buf_get_text(buf, start_row, start_col, end_row, end_col, {}) local lines = a.nvim_buf_get_text(buf, start_row, start_col, end_row, end_col, {})
return table.concat(lines, '\n') return table.concat(lines, '\n')
end end
@ -396,7 +404,7 @@ local predicate_handlers = {
predicate_handlers['vim-match?'] = predicate_handlers['match?'] predicate_handlers['vim-match?'] = predicate_handlers['match?']
---@class TSMetadata ---@class TSMetadata
---@field range Range4|Range6 ---@field range Range
---@field [integer] TSMetadata ---@field [integer] TSMetadata
---@field [string] integer|string ---@field [string] integer|string

View File

@ -302,7 +302,7 @@ local types = { 'integer', 'number', 'string', 'table', 'list', 'boolean', 'func
local tagged_types = { 'TSNode', 'LanguageTree' } local tagged_types = { 'TSNode', 'LanguageTree' }
-- Document these as 'table' -- Document these as 'table'
local alias_types = { 'Range4', 'Range6' } local alias_types = { 'Range', 'Range4', 'Range6', 'TSMetadata' }
-- Processes the file and writes filtered output to stdout. -- Processes the file and writes filtered output to stdout.
function TLua2DoX_filter.filter(this, AppStamp, Filename) function TLua2DoX_filter.filter(this, AppStamp, Filename)

View File

@ -196,7 +196,7 @@ void ui_refresh(void)
local manyruns = q(100) local manyruns = q(100)
-- First run should be at least 400x slower than an 100 subsequent runs. -- First run should be at least 400x slower than an 100 subsequent runs.
local factor = is_os('win') and 300 or 400 local factor = is_os('win') and 200 or 400
assert(factor * manyruns < firstrun, ('firstrun: %f ms, manyruns: %f ms'):format(firstrun / 1e6, manyruns / 1e6)) assert(factor * manyruns < firstrun, ('firstrun: %f ms, manyruns: %f ms'):format(firstrun / 1e6, manyruns / 1e6))
end) end)
@ -277,13 +277,13 @@ void ui_refresh(void)
eq('void', res2) eq('void', res2)
end) end)
it('support getting text where start of node is past EOF', function() it('support getting text where start of node is one past EOF', function()
local text = [[ local text = [[
def run def run
a = <<~E a = <<~E
end]] end]]
insert(text) insert(text)
local result = exec_lua([[ eq('', exec_lua[[
local fake_node = {} local fake_node = {}
function fake_node:start() function fake_node:start()
return 3, 0, 23 return 3, 0, 23
@ -291,12 +291,14 @@ end]]
function fake_node:end_() function fake_node:end_()
return 3, 0, 23 return 3, 0, 23
end end
function fake_node:range() function fake_node:range(bytes)
if bytes then
return 3, 0, 23, 3, 0, 23
end
return 3, 0, 3, 0 return 3, 0, 3, 0
end end
return vim.treesitter.get_node_text(fake_node, 0) == nil return vim.treesitter.get_node_text(fake_node, 0)
]]) ]])
eq(true, result)
end) end)
it('support getting empty text if node range is zero width', function() it('support getting empty text if node range is zero width', function()