Skip to content

Commit

Permalink
fix: multiwindow support; fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo1321 authored and lewis6991 committed Oct 18, 2024
1 parent 9e36638 commit e7fdb4c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
Empty file added .root
Empty file.
13 changes: 9 additions & 4 deletions lua/treesitter-context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ end

local attached = {} --- @type table<integer,true>

local function close()
require('treesitter-context.render').close(api.nvim_get_current_win())
local function close(args)
local render = require('treesitter-context.render')
if args.event == "WinClosed" then
-- Closing current window instead of intended window may lead to context window flickering.
render.close(tonumber(args.match))
else
render.close(api.nvim_get_current_win())
end
end

local function close_all()
Expand All @@ -61,15 +67,14 @@ local function cannot_open(bufnr, winid)
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
local update_single_context = throttle_by_id(function(winid)
-- Since the update is performed asynchronously, the window may be closed at this moment.
-- Therefore, we need to check if it is still valid.
if not api.nvim_win_is_valid(winid) then
if not api.nvim_win_is_valid(winid) or vim.fn.getcmdtype() ~= '' then
return
end

Expand Down
24 changes: 21 additions & 3 deletions lua/treesitter-context/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ local config = require('treesitter-context.config')

local ns = api.nvim_create_namespace('nvim-treesitter-context')

--- List of buffers that are to be deleted.
---@type integer[]
local retired_buffers = {}

--- @class WindowContext
--- @field context_winid integer? The context window ID.
--- @field gutter_winid integer? The gutter window ID.
Expand Down Expand Up @@ -294,16 +298,30 @@ end
---@param context_winid? integer
local function close(context_winid)
vim.schedule(function()
if context_winid == nil or not api.nvim_win_is_valid(context_winid) then
if context_winid == nil or not api.nvim_win_is_valid(context_winid) then
return
end

local bufnr = api.nvim_win_get_buf(context_winid)
if bufnr ~= nil and api.nvim_buf_is_valid(bufnr) then
api.nvim_buf_delete(bufnr, { force = true })
if bufnr ~= nil then
table.insert(retired_buffers, bufnr)
end
if api.nvim_win_is_valid(context_winid) then
api.nvim_win_close(context_winid, true)
end

if fn.getcmdwintype() ~= '' then
-- Can't delete buffers when the command-line window is open.
return
end

-- Delete retired buffers.
for _, retired_bufnr in ipairs(retired_buffers) do
if api.nvim_buf_is_valid(retired_bufnr) then
api.nvim_buf_delete(retired_bufnr, { force = true })
end
end
retired_buffers = {}
end)
end

Expand Down

0 comments on commit e7fdb4c

Please sign in to comment.