fix: do not create contexts with invalid ranges

Fixes #442
This commit is contained in:
Lewis Russell 2024-07-05 14:41:57 +01:00
parent 813170c8ef
commit d1767935a6
2 changed files with 12 additions and 18 deletions

View file

@ -50,13 +50,6 @@ local function open(bufnr, winid, ctx_ranges, ctx_lines)
require('treesitter-context.render').open(bufnr, winid, ctx_ranges, ctx_lines)
end
---@param bufnr integer
---@param winid integer
---@return Range4[]?, string[]?
local function get_context(bufnr, winid)
return require('treesitter-context.context').get(bufnr, winid)
end
local attached = {} --- @type table<integer,true>
---@param bufnr integer
@ -98,7 +91,7 @@ local update = throttle(function()
return
end
local context, context_lines = get_context(bufnr, winid)
local context, context_lines = require('treesitter-context.context').get(bufnr, winid)
all_contexts[bufnr] = context
if not context or #context == 0 then

View file

@ -311,18 +311,19 @@ function M.get(bufnr, winid)
local range0 = context_range(parent, query)
if range0 and range_is_valid(range0) then
local range, lines = get_text_for_range(range0)
if range_is_valid(range) then
local last_context = context_ranges[#context_ranges]
if last_context and parent_start_row == last_context[1] then
-- If there are multiple contexts on the same row, then prefer the inner
contexts_height = contexts_height - util.get_range_height(last_context)
context_ranges[#context_ranges] = nil
context_lines[#context_lines] = nil
end
local last_context = context_ranges[#context_ranges]
if last_context and parent_start_row == last_context[1] then
-- If there are multiple contexts on the same row, then prefer the inner
contexts_height = contexts_height - util.get_range_height(last_context)
context_ranges[#context_ranges] = nil
context_lines[#context_lines] = nil
contexts_height = contexts_height + util.get_range_height(range)
context_ranges[#context_ranges + 1] = range
context_lines[#context_lines + 1] = lines
end
contexts_height = contexts_height + util.get_range_height(range)
context_ranges[#context_ranges + 1] = range
context_lines[#context_lines + 1] = lines
end
end
end