Skip to content

Commit 296fc73

Browse files
committed
Revert multiwindow support
Too buggy, needs more development. This reverts commit f1b7285. This reverts commit e7fdb4c. This reverts commit 9e36638. This reverts commit 6cba006. This reverts commit bf6386d. This reverts commit 3de5708. This reverts commit 63374d1.
1 parent 158377d commit 296fc73

File tree

6 files changed

+88
-181
lines changed

6 files changed

+88
-181
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ Note: calling `setup()` is optional.
211211
```lua
212212
require'treesitter-context'.setup{
213213
enable = true, -- Enable this plugin (Can be enabled/disabled later via commands)
214-
multiwindow = false, -- Enable multiwindow support.
215214
max_lines = 0, -- How many lines the window should span. Values <= 0 mean no limit.
216215
min_window_height = 0, -- Minimum editor window height to enable context. Values <= 0 mean no limit.
217216
line_numbers = true,

doc/nvim-treesitter-context.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ not required.
2323
-- Enable this plugin (Can be enabled/disabled later via commands)
2424
enable = true,
2525

26-
-- Enable multiwindow support.
27-
multiwindow = false,
28-
2926
-- How many lines the window should span. Values <= 0 mean no limit.
3027
max_lines = 0,
3128

lua/treesitter-context.lua

Lines changed: 21 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,8 @@ end
3939

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

42-
local function close(args)
43-
local render = require('treesitter-context.render')
44-
if args.event == "WinClosed" then
45-
-- Closing current window instead of intended window may lead to context window flickering.
46-
render.close(tonumber(args.match))
47-
else
48-
render.close(api.nvim_get_current_win())
49-
end
50-
end
51-
52-
local function close_all()
53-
local render = require('treesitter-context.render')
54-
if config.multiwindow then
55-
for _, winid in pairs(api.nvim_list_wins()) do
56-
render.close(winid)
57-
end
58-
else
59-
render.close(api.nvim_get_current_win())
60-
end
42+
local function close()
43+
require('treesitter-context.render').close()
6144
end
6245

6346
---@param bufnr integer
@@ -67,29 +50,30 @@ local function cannot_open(bufnr, winid)
6750
or vim.bo[bufnr].filetype == ''
6851
or vim.bo[bufnr].buftype ~= ''
6952
or vim.wo[winid].previewwindow
53+
or vim.fn.getcmdtype() ~= ''
7054
or api.nvim_win_get_height(winid) < config.min_window_height
7155
end
7256

7357
---@param winid integer
7458
local update_single_context = throttle_by_id(function(winid)
7559
-- Since the update is performed asynchronously, the window may be closed at this moment.
7660
-- Therefore, we need to check if it is still valid.
77-
if not api.nvim_win_is_valid(winid) or vim.fn.getcmdtype() ~= '' then
61+
if not api.nvim_win_is_valid(winid) then
7862
return
7963
end
8064

8165
local bufnr = api.nvim_win_get_buf(winid)
8266

83-
if cannot_open(bufnr, winid) or not config.multiwindow and winid ~= api.nvim_get_current_win() then
84-
require('treesitter-context.render').close(winid)
67+
if cannot_open(bufnr, winid) then
68+
close()
8569
return
8670
end
8771

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

9175
if not context_ranges or #context_ranges == 0 then
92-
require('treesitter-context.render').close(winid)
76+
close()
9377
return
9478
end
9579

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

101-
---@param args table
102-
local function update(args)
103-
if args.event == "OptionSet" and args.match ~= 'number' and args.match ~= 'relativenumber' then
104-
return
105-
end
106-
107-
local multiwindow_events = { "WinResized", "User" }
108-
109-
if config.multiwindow and vim.tbl_contains(multiwindow_events, args.event) then
110-
-- Resizing a single window may cause many resizes in different windows,
111-
-- so it is necessary to iterate over all windows when a WinResized event is received.
112-
for _, winid in pairs(api.nvim_list_wins()) do
113-
update_single_context(winid)
114-
end
115-
else
116-
update_single_context(api.nvim_get_current_win())
117-
end
85+
local function update()
86+
update_single_context(api.nvim_get_current_win())
11887
end
11988

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

136105
function M.enable()
137-
local update_events = {
138-
'WinScrolled',
139-
'BufEnter',
140-
'WinEnter',
141-
'VimResized',
142-
'DiagnosticChanged',
143-
'CursorMoved',
144-
'OptionSet',
145-
}
146-
147-
if config.multiwindow then
148-
table.insert(update_events, 'WinResized')
149-
table.insert(update_events, 'WinLeave')
150-
end
151-
152-
autocmd(update_events, update)
106+
autocmd({ 'WinScrolled', 'BufEnter', 'WinEnter', 'VimResized', 'DiagnosticChanged' }, update)
153107

154108
autocmd('BufReadPost', function(args)
155109
attached[args.buf] = nil
@@ -162,30 +116,27 @@ function M.enable()
162116
attached[args.buf] = nil
163117
end)
164118

165-
if config.multiwindow then
166-
autocmd({ 'WinClosed' }, close)
167-
else
168-
autocmd({ 'BufLeave', 'WinLeave', 'WinClosed' }, close)
169-
end
119+
autocmd('CursorMoved', update)
120+
121+
autocmd('OptionSet', function(args)
122+
if args.match == 'number' or args.match == 'relativenumber' then
123+
update()
124+
end
125+
end)
126+
127+
autocmd({ 'BufLeave', 'WinLeave' }, close)
170128

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

174-
if config.multiwindow then
175-
for _, winid in pairs(api.nvim_list_wins()) do
176-
update_single_context(winid)
177-
end
178-
else
179-
update_single_context(api.nvim_get_current_win())
180-
end
181-
132+
update()
182133
enabled = true
183134
end
184135

185136
function M.disable()
186137
augroup('treesitter_context_update', {})
187138
attached = {}
188-
close_all()
139+
close()
189140
enabled = false
190141
end
191142

lua/treesitter-context/config.lua

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
--- @class (exact) TSContext.Config
33
--- @field enable boolean
4-
--- @field multiwindow boolean
54
--- @field max_lines integer
65
--- @field min_window_height integer
76
--- @field line_numbers boolean
@@ -17,9 +16,6 @@
1716
--- Enable this plugin (Can be enabled/disabled later via commands)
1817
--- @field enable? boolean
1918
---
20-
--- Enable multiwindow support.
21-
--- @field multiwindow? boolean
22-
---
2319
--- How many lines the window should span. Values <= 0 mean no limit.
2420
--- @field max_lines? integer
2521
---
@@ -49,7 +45,6 @@
4945
--- @type TSContext.Config
5046
local default_config = {
5147
enable = true,
52-
multiwindow = false,
5348
max_lines = 0, -- no limit
5449
min_window_height = 0,
5550
line_numbers = true,

lua/treesitter-context/context.lua

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,24 @@ local function calc_max_lines(winid)
6666
end
6767

6868
---@param node TSNode
69-
---@param bufnr integer
7069
---@return string
71-
local function hash_args(node, bufnr)
70+
local function hash_node(node)
7271
return table.concat({
7372
node:id(),
7473
node:symbol(),
7574
node:child_count(),
7675
node:type(),
7776
node:range(),
78-
bufnr,
7977
}, ',')
8078
end
8179

8280
--- Run the context query on a node and return the range if it is a valid
8381
--- context node.
8482
--- @param node TSNode
85-
--- @param bufnr integer
8683
--- @param query vim.treesitter.Query
8784
--- @return Range4?
88-
local context_range = cache.memoize(function(node, bufnr, query)
85+
local context_range = cache.memoize(function(node, query)
86+
local bufnr = api.nvim_get_current_buf()
8987
local range = { node:range() } --- @type Range4
9088
range[3] = range[1] + 1
9189
range[4] = 0
@@ -121,7 +119,7 @@ local context_range = cache.memoize(function(node, bufnr, query)
121119
return range
122120
end
123121
end
124-
end, hash_args)
122+
end, hash_node)
125123

126124
---@param lang string
127125
---@return vim.treesitter.Query?
@@ -167,17 +165,16 @@ local function trim_contexts(context_ranges, context_lines, trim, top)
167165
end
168166

169167
--- @param range Range4
170-
--- @param bufnr integer
171168
--- @return Range4, string[]
172-
local function get_text_for_range(range, bufnr)
169+
local function get_text_for_range(range)
173170
local start_row, end_row, end_col = range[1], range[3], range[4]
174171

175172
if end_col == 0 then
176173
end_row = end_row - 1
177174
end_col = -1
178175
end
179176

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

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

338335
-- Only process the parent if it is not in view.
339336
if parent_start_row < contexts_end_row then
340-
local range0 = context_range(parent, bufnr, query)
337+
local range0 = context_range(parent, query)
341338
if range0 and range_is_valid(range0) then
342-
local range, lines = get_text_for_range(range0, bufnr)
339+
local range, lines = get_text_for_range(range0)
343340
if range_is_valid(range) then
344341
local last_context = context_ranges[#context_ranges]
345342
if last_context and parent_start_row == last_context[1] then

0 commit comments

Comments
 (0)