Skip to content

Commit

Permalink
refactor: reverse how the parents table is constructed/iterated
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed Oct 4, 2024
1 parent 63374d1 commit 03d07bd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
35 changes: 8 additions & 27 deletions lua/treesitter-context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,13 @@ end

---@param bufnr integer
---@param winid integer
local function can_open(bufnr, winid)
if not attached[bufnr] then
return false
end

if vim.bo[bufnr].filetype == '' then
return false
end

if vim.bo[bufnr].buftype ~= '' then
return false
end

if vim.wo[winid].previewwindow then
return false
end

if vim.fn.getcmdtype() ~= '' then
return false
end

if api.nvim_win_get_height(winid) < config.min_window_height then
return false
end

return true
local function cannot_open(bufnr, winid)
return not attached[bufnr]
or vim.bo[bufnr].filetype == ''
or vim.bo[bufnr].buftype ~= ''
or vim.wo[winid].previewwindow
or vim.fn.getcmdtype() ~= ''
or api.nvim_win_get_height(winid) < config.min_window_height
end

---@param winid integer
Expand All @@ -90,7 +71,7 @@ local update_single_context = throttle_by_id(function(winid)

local bufnr = api.nvim_win_get_buf(winid)

if not can_open(bufnr, winid) then
if cannot_open(bufnr, winid) then
close()
return
end
Expand Down
10 changes: 4 additions & 6 deletions lua/treesitter-context/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ local function get_parent_nodes(langtree, range)
if root.child_containing_descendant ~= nil then
local p = root --- @type TSNode?
while p do
table.insert(ret, 1, p)
ret[#ret + 1] = p
p = p:child_containing_descendant(n)
end
table.insert(ret, 1, n)
ret[#ret + 1] = n
else
while n do
ret[#ret + 1] = n
table.insert(ret, 1, n)
n = n:parent()
end
end
Expand Down Expand Up @@ -328,9 +328,7 @@ function M.get(bufnr, winid)
contexts_height = 0

for parents, query in iter_context_parents(bufnr, line_range) do
for j = #parents, 1, -1 do
local parent = parents[j]

for _, parent in ipairs(parents) do
local parent_start_row = parent:range()
local contexts_end_row = top_row + math.min(max_lines, contexts_height)

Expand Down

0 comments on commit 03d07bd

Please sign in to comment.