Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert multiwindow support #523

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ Note: calling `setup()` is optional.
```lua
require'treesitter-context'.setup{
enable = true, -- Enable this plugin (Can be enabled/disabled later via commands)
multiwindow = false, -- Enable multiwindow support.
max_lines = 0, -- How many lines the window should span. Values <= 0 mean no limit.
min_window_height = 0, -- Minimum editor window height to enable context. Values <= 0 mean no limit.
line_numbers = true,
Expand Down
3 changes: 0 additions & 3 deletions doc/nvim-treesitter-context.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ not required.
-- Enable this plugin (Can be enabled/disabled later via commands)
enable = true,

-- Enable multiwindow support.
multiwindow = false,

-- How many lines the window should span. Values <= 0 mean no limit.
max_lines = 0,

Expand Down
91 changes: 21 additions & 70 deletions lua/treesitter-context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,8 @@ end

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

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()
local render = require('treesitter-context.render')
if config.multiwindow then
for _, winid in pairs(api.nvim_list_wins()) do
render.close(winid)
end
else
render.close(api.nvim_get_current_win())
end
local function close()
require('treesitter-context.render').close()
end

---@param bufnr integer
Expand All @@ -67,29 +50,30 @@ 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) or vim.fn.getcmdtype() ~= '' then
if not api.nvim_win_is_valid(winid) then
return
end

local bufnr = api.nvim_win_get_buf(winid)

if cannot_open(bufnr, winid) or not config.multiwindow and winid ~= api.nvim_get_current_win() then
require('treesitter-context.render').close(winid)
if cannot_open(bufnr, winid) then
close()
return
end

local context_ranges, context_lines = require('treesitter-context.context').get(bufnr, winid)
all_contexts[bufnr] = context_ranges

if not context_ranges or #context_ranges == 0 then
require('treesitter-context.render').close(winid)
close()
return
end

Expand All @@ -98,23 +82,8 @@ local update_single_context = throttle_by_id(function(winid)
require('treesitter-context.render').open(bufnr, winid, context_ranges, context_lines)
end)

---@param args table
local function update(args)
if args.event == "OptionSet" and args.match ~= 'number' and args.match ~= 'relativenumber' then
return
end

local multiwindow_events = { "WinResized", "User" }

if config.multiwindow and vim.tbl_contains(multiwindow_events, args.event) then
-- Resizing a single window may cause many resizes in different windows,
-- so it is necessary to iterate over all windows when a WinResized event is received.
for _, winid in pairs(api.nvim_list_wins()) do
update_single_context(winid)
end
else
update_single_context(api.nvim_get_current_win())
end
local function update()
update_single_context(api.nvim_get_current_win())
end

local M = {
Expand All @@ -134,22 +103,7 @@ local function autocmd(event, callback, opts)
end

function M.enable()
local update_events = {
'WinScrolled',
'BufEnter',
'WinEnter',
'VimResized',
'DiagnosticChanged',
'CursorMoved',
'OptionSet',
}

if config.multiwindow then
table.insert(update_events, 'WinResized')
table.insert(update_events, 'WinLeave')
end

autocmd(update_events, update)
autocmd({ 'WinScrolled', 'BufEnter', 'WinEnter', 'VimResized', 'DiagnosticChanged' }, update)

autocmd('BufReadPost', function(args)
attached[args.buf] = nil
Expand All @@ -162,30 +116,27 @@ function M.enable()
attached[args.buf] = nil
end)

if config.multiwindow then
autocmd({ 'WinClosed' }, close)
else
autocmd({ 'BufLeave', 'WinLeave', 'WinClosed' }, close)
end
autocmd('CursorMoved', update)

autocmd('OptionSet', function(args)
if args.match == 'number' or args.match == 'relativenumber' then
update()
end
end)

autocmd({ 'BufLeave', 'WinLeave' }, close)

autocmd('User', close, { pattern = 'SessionSavePre' })
autocmd('User', update, { pattern = 'SessionSavePost' })

if config.multiwindow then
for _, winid in pairs(api.nvim_list_wins()) do
update_single_context(winid)
end
else
update_single_context(api.nvim_get_current_win())
end

update()
enabled = true
end

function M.disable()
augroup('treesitter_context_update', {})
attached = {}
close_all()
close()
enabled = false
end

Expand Down
5 changes: 0 additions & 5 deletions lua/treesitter-context/config.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

--- @class (exact) TSContext.Config
--- @field enable boolean
--- @field multiwindow boolean
--- @field max_lines integer
--- @field min_window_height integer
--- @field line_numbers boolean
Expand All @@ -17,9 +16,6 @@
--- Enable this plugin (Can be enabled/disabled later via commands)
--- @field enable? boolean
---
--- Enable multiwindow support.
--- @field multiwindow? boolean
---
--- How many lines the window should span. Values <= 0 mean no limit.
--- @field max_lines? integer
---
Expand Down Expand Up @@ -49,7 +45,6 @@
--- @type TSContext.Config
local default_config = {
enable = true,
multiwindow = false,
max_lines = 0, -- no limit
min_window_height = 0,
line_numbers = true,
Expand Down
19 changes: 8 additions & 11 deletions lua/treesitter-context/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,24 @@ local function calc_max_lines(winid)
end

---@param node TSNode
---@param bufnr integer
---@return string
local function hash_args(node, bufnr)
local function hash_node(node)
return table.concat({
node:id(),
node:symbol(),
node:child_count(),
node:type(),
node:range(),
bufnr,
}, ',')
end

--- Run the context query on a node and return the range if it is a valid
--- context node.
--- @param node TSNode
--- @param bufnr integer
--- @param query vim.treesitter.Query
--- @return Range4?
local context_range = cache.memoize(function(node, bufnr, query)
local context_range = cache.memoize(function(node, query)
local bufnr = api.nvim_get_current_buf()
local range = { node:range() } --- @type Range4
range[3] = range[1] + 1
range[4] = 0
Expand Down Expand Up @@ -121,7 +119,7 @@ local context_range = cache.memoize(function(node, bufnr, query)
return range
end
end
end, hash_args)
end, hash_node)

---@param lang string
---@return vim.treesitter.Query?
Expand Down Expand Up @@ -167,17 +165,16 @@ local function trim_contexts(context_ranges, context_lines, trim, top)
end

--- @param range Range4
--- @param bufnr integer
--- @return Range4, string[]
local function get_text_for_range(range, bufnr)
local function get_text_for_range(range)
local start_row, end_row, end_col = range[1], range[3], range[4]

if end_col == 0 then
end_row = end_row - 1
end_col = -1
end

local lines = api.nvim_buf_get_text(bufnr, start_row, 0, end_row, -1, {})
local lines = api.nvim_buf_get_text(0, start_row, 0, end_row, -1, {})

-- Strip any empty lines from the node
while #lines > 0 do
Expand Down Expand Up @@ -337,9 +334,9 @@ function M.get(bufnr, winid)

-- Only process the parent if it is not in view.
if parent_start_row < contexts_end_row then
local range0 = context_range(parent, bufnr, query)
local range0 = context_range(parent, query)
if range0 and range_is_valid(range0) then
local range, lines = get_text_for_range(range0, bufnr)
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
Expand Down
Loading
Loading