Skip to content

Commit

Permalink
fix: do not create contexts with invalid ranges
Browse files Browse the repository at this point in the history
Fixes #442
  • Loading branch information
lewis6991 committed Jul 5, 2024
1 parent 813170c commit d176793
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
9 changes: 1 addition & 8 deletions lua/treesitter-context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions lua/treesitter-context/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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
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

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
Expand Down

0 comments on commit d176793

Please sign in to comment.