Skip to content
Closed
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ require'treesitter-context'.setup{
multiline_threshold = 20, -- Maximum number of lines to show for a single context
trim_scope = 'outer', -- Which context lines to discard if `max_lines` is exceeded. Choices: 'inner', 'outer'
mode = 'cursor', -- Line used to calculate context. Choices: 'cursor', 'topline'
-- Summarizer can be 'set_lines' or 'fold'. The latter recommends nvim >= 0.10.
summarizer = 'set_lines',
-- The 'foldtext' option for the context window when `summarizer = 'fold'`.
-- If `nil` (default), tries `"v:lua.vim.treesitter.foldtext()"` and fallbacks to
-- "getline(v:foldstart)"
foldtext = nil,
-- Separator between context and content. Should be a single character string, like '-'.
-- When separator is set, the context will only show up when there are at least 2 lines above cursorline.
separator = nil,
Expand Down
3 changes: 3 additions & 0 deletions lua/treesitter-context/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
--- @field trim_scope 'outer'|'inner'
--- @field zindex integer
--- @field mode 'cursor'|'topline'
--- @field summarizer 'set_lines' | 'fold'
--- @field foldtext? string
--- @field separator? string
--- @field on_attach? fun(buf: integer): boolean

Expand Down Expand Up @@ -52,6 +54,7 @@ local default_config = {
trim_scope = 'outer',
zindex = 20,
mode = 'cursor',
summarizer = 'set_lines',
}

local config = vim.deepcopy(default_config)
Expand Down
37 changes: 35 additions & 2 deletions lua/treesitter-context/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,23 @@ local function display_window(bufnr, winid, width, height, col, ty, hl)
})
vim.w[winid][ty] = true
vim.wo[winid].wrap = false
vim.wo[winid].foldenable = false
vim.wo[winid].signcolumn = 'no'
vim.wo[winid].winhl = 'NormalFloat:' .. hl

local fold = config.summarizer == 'fold'
vim.wo[winid].foldenable = fold
if fold then
api.nvim_set_hl(ns, "Folded", { link = "TreesitterContext" })
api.nvim_win_set_hl_ns(winid, ns)
vim.wo[winid].foldmethod = "manual"
if config.foldtext then
vim.wo[winid].foldtext = config.foldtext
elseif vim.treesitter.foldtext then
vim.wo[winid].foldtext = "v:lua.vim.treesitter.foldtext()"
else
vim.wo[winid].foldtext = "getline(v:foldstart)"
end
end
else
api.nvim_win_set_config(winid, {
win = api.nvim_get_current_win(),
Expand Down Expand Up @@ -348,8 +363,10 @@ function M.open(bufnr, winid, ctx_ranges, ctx_lines)
win_close(gutter_winid)
end

local fold = config.summarizer == "fold"

context_winid = display_window(
ctx_bufnr,
fold and bufnr or ctx_bufnr,
context_winid,
win_width,
win_height,
Expand All @@ -358,6 +375,22 @@ function M.open(bufnr, winid, ctx_ranges, ctx_lines)
'TreesitterContext'
)

if fold then
vim.api.nvim_win_call(context_winid, function()
vim.api.nvim_win_set_cursor(context_winid, {ctx_ranges[1][1] + 1, 1})
vim.cmd([[normal! ztzE]])

for i, range in ipairs(ctx_ranges) do
if ctx_ranges[i + 1] then
local srow = range[1] + 1
local erow = ctx_ranges[i + 1][1]
vim.cmd(srow .. "," .. erow .. "fold")
end
end
end)
return
end

if not set_lines(ctx_bufnr, ctx_lines) then
-- Context didn't change, can return here
return
Expand Down