Skip to content

Commit 33b3f5a

Browse files
committed
refactor: make go_to_context() get contexts directly
- Removes the need for the internal `all_contexts` variable. - Remove `bufnr` argument from `Context.get()` and make `winid` optional.
1 parent 3adfad5 commit 33b3f5a

File tree

4 files changed

+14
-24
lines changed

4 files changed

+14
-24
lines changed

lua/treesitter-context.lua

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ end
2323
--- @module 'treesitter-context.render'
2424
local Render = defer_require('treesitter-context.render')
2525

26-
--- @type table<integer, Range4[]>
27-
local all_contexts = {}
28-
2926
--- Schedule a function to run on the next event loop iteration.
3027
--- If the function is called again within 150ms, it will be scheduled
3128
--- again to run on the next event loop iteration. This means that
@@ -110,17 +107,14 @@ local update_win = throttle_by_id(function(winid)
110107
return
111108
end
112109

113-
local bufnr = api.nvim_win_get_buf(winid)
114-
115-
local context_ranges, context_lines = require('treesitter-context.context').get(bufnr, winid)
116-
all_contexts[bufnr] = context_ranges
110+
local context_ranges, context_lines = require('treesitter-context.context').get(winid)
117111

118112
if not context_ranges or #context_ranges == 0 then
119113
Render.close(winid)
120114
return
121115
end
122116

123-
Render.open(bufnr, winid, context_ranges, assert(context_lines))
117+
Render.open(winid, context_ranges, assert(context_lines))
124118
end)
125119

126120
local multiwindow_events = {
@@ -312,8 +306,7 @@ function M.go_to_context(depth)
312306
depth = depth or 1
313307
local line = api.nvim_win_get_cursor(0)[1]
314308
local context = nil
315-
local bufnr = api.nvim_get_current_buf()
316-
local contexts = all_contexts[bufnr] or {}
309+
local contexts = require('treesitter-context.context').get() or {}
317310

318311
for idx = #contexts, 1, -1 do
319312
local c = contexts[idx]
@@ -326,7 +319,7 @@ function M.go_to_context(depth)
326319
end
327320
end
328321

329-
if context == nil then
322+
if not context then
330323
return
331324
end
332325

lua/treesitter-context/context.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,12 @@ local function range_is_valid(range)
312312
return true
313313
end
314314

315-
--- @param bufnr integer
316-
--- @param winid integer
315+
--- @param winid? integer
317316
--- @return Range4[]?, string[]?
318-
function M.get(bufnr, winid)
317+
function M.get(winid)
318+
winid = winid or api.nvim_get_current_win()
319+
local bufnr = api.nvim_win_get_buf(winid)
320+
319321
-- vim.treesitter.get_parser() calls bufload(), but we don't actually want to load the buffer:
320322
-- this method is called during plugin init, before other plugins or the user's config
321323
-- have a chance to initialize.

lua/treesitter-context/render.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,11 @@ end
449449

450450
local M = {}
451451

452-
--- @param bufnr integer
453452
--- @param winid integer
454453
--- @param ctx_ranges Range4[]
455454
--- @param ctx_lines string[]
456-
function M.open(bufnr, winid, ctx_ranges, ctx_lines)
455+
function M.open(winid, ctx_ranges, ctx_lines)
456+
local bufnr = api.nvim_win_get_buf(winid)
457457
local gutter_width = get_gutter_width(winid)
458458
local win_width = math.max(1, api.nvim_win_get_width(winid) - gutter_width)
459459

test/contexts_spec.lua

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,15 @@ for _, lang in ipairs(langs_with_queries) do
176176
for cursor_row, context_rows in pairs(contexts) do
177177
it(('line %s in %s'):format(cursor_row, test_file), function()
178178
cmd('edit ' .. test_file)
179-
local bufnr = api.nvim_get_current_buf()
180179
local winid = api.nvim_get_current_win()
181180
api.nvim_win_set_cursor(winid, { cursor_row + 1, 0 })
182181
assert(fn.getline('.'):match('{{CURSOR}}'))
183182
feed(string.format('zt%d<C-y>', #context_rows + 2))
184183

185184
--- @type [integer,integer,integer,integer][]
186-
local ranges = exec_lua(
187-
[[
188-
return require('treesitter-context.context').get(...)
189-
]],
190-
bufnr,
191-
winid
192-
)
185+
local ranges = exec_lua(function(...)
186+
return assert(require('treesitter-context.context').get(...))
187+
end, winid)
193188

194189
local act_context_rows = {} --- @type integer[]
195190
for _, r in ipairs(ranges) do

0 commit comments

Comments
 (0)