Skip to content

Commit 31c0389

Browse files
committed
feat(enhancement): support nearest parent
Fixes #453
1 parent 404502e commit 31c0389

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,11 @@ hi TreesitterContextLineNumberBottom gui=underline guisp=Grey
407407
vim.keymap.set("n", "[c", function()
408408
require("treesitter-context").go_to_context(vim.v.count1)
409409
end, { silent = true })
410+
411+
412+
vim.keymap.set("n", "[C", function()
413+
require("treesitter-context").go_to_context(vim.v.count1, "entire window")
414+
end, { silent = true })
410415
```
411416

412417
## Adding support for other languages

lua/treesitter-context.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ local Render = defer_require('treesitter-context.render')
2626
--- @type table<integer, Range4[]>
2727
local all_contexts = {}
2828

29+
--- @type table<integer, Range4[]>
30+
local all_visible_contexts = {}
31+
2932
--- Schedule a function to run on the next event loop iteration.
3033
--- If the function is called again within 150ms, it will be scheduled
3134
--- again to run on the next event loop iteration. This means that
@@ -115,8 +118,10 @@ local update_single_context = throttle_by_id(function(winid)
115118
return
116119
end
117120

118-
local context_ranges, context_lines = require('treesitter-context.context').get(bufnr, winid)
121+
local context_ranges, context_lines, visible_context_ranges =
122+
require('treesitter-context.context').get(bufnr, winid)
119123
all_contexts[bufnr] = context_ranges
124+
all_visible_contexts[bufnr] = visible_context_ranges
120125

121126
if not context_ranges or #context_ranges == 0 then
122127
Render.close(winid)
@@ -310,14 +315,23 @@ function M.setup(options)
310315
end
311316
end
312317

318+
--- jump to the nearest context parent
319+
---
313320
--- @param depth integer? default 1
314-
function M.go_to_context(depth)
321+
--- @param mode? 'context window' | 'entire window' default 'context window', the parents to pick from when jumping
322+
function M.go_to_context(depth, mode)
315323
depth = depth or 1
324+
mode = mode or 'context window'
316325
local line = api.nvim_win_get_cursor(0)[1]
317326
local context = nil
318327
local bufnr = api.nvim_get_current_buf()
319328
local contexts = all_contexts[bufnr] or {}
320329

330+
-- add all visible if that is wanted
331+
if mode == 'entire window' then
332+
vim.list_extend(contexts, all_visible_contexts[bufnr])
333+
end
334+
321335
for idx = #contexts, 1, -1 do
322336
local c = contexts[idx]
323337
if depth == 0 then

lua/treesitter-context/context.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ end
297297

298298
--- @param bufnr integer
299299
--- @param winid integer
300-
--- @return Range4[]?, string[]?
300+
--- @return Range4[]?, string[]?, Range4[]?
301301
function M.get(bufnr, winid)
302302
-- vim.treesitter.get_parser() calls bufload(), but we don't actually want to load the buffer:
303303
-- this method is called during plugin init, before other plugins or the user's config
@@ -326,6 +326,7 @@ function M.get(bufnr, winid)
326326
end
327327

328328
local context_ranges = {} --- @type Range4[]
329+
local visible_context_ranges = {} --- @type Range4[]
329330
local context_lines = {} --- @type string[][]
330331
local contexts_height = 0
331332

@@ -335,6 +336,7 @@ function M.get(bufnr, winid)
335336
local line_range = { node_row, col0, node_row, col0 + 1 }
336337

337338
context_ranges = {}
339+
visible_context_ranges = {}
338340
context_lines = {}
339341
contexts_height = 0
340342

@@ -366,6 +368,15 @@ function M.get(bufnr, winid)
366368
context_lines[#context_lines + 1] = lines
367369
end
368370
end
371+
else
372+
local range0 = context_range(parent, bufnr, query)
373+
if range0 and range_is_valid(range0) then
374+
local range, _lines = get_text_for_range(range0, bufnr)
375+
-- TODO: consider printing lines here for debug purposes
376+
if range_is_valid(range) then
377+
visible_context_ranges[#visible_context_ranges + 1] = range
378+
end
379+
end
369380
end
370381
end
371382
end
@@ -382,7 +393,7 @@ function M.get(bufnr, winid)
382393
trim_contexts(context_ranges, context_lines, trim, config.trim_scope == 'outer')
383394
end
384395

385-
return context_ranges, tbl_flatten(context_lines)
396+
return context_ranges, tbl_flatten(context_lines), visible_context_ranges
386397
end
387398

388399
return M

0 commit comments

Comments
 (0)