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

Added option node_context #21

Open
wants to merge 4 commits into
base: main
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/twilight.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*twilight.txt* For NVIM v0.5.0 Last change: 2022 September 05
*twilight.txt* For NVIM v0.5.0 Last change: 2022 November 23

==============================================================================
Table of Contents *twilight-table-of-contents*
Expand Down
1 change: 1 addition & 0 deletions lua/twilight/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local defaults = {
inactive = false, -- when true, other windows will be fully dimmed (unless they contain the same buffer)
},
context = 10, -- amount of lines we will try to show arounc the current line
node_context = 0, -- amount of extra nodes to show. Only node types mention in expand are considered
treesitter = true, -- use treesitter when available for the filetype
-- treesitter is used to automatically expand the visible text,
-- but you can further control the types of nodes that should always be fully expanded
Expand Down
13 changes: 13 additions & 0 deletions lua/twilight/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,24 @@ end

function M.get_expand_root(node, opts)
opts = opts or {}
local left_context = config.options.node_context
local root
while node do
if config.expand[node:type()] then
if opts.first then
return node
end
if left_context <= 0 then
return node
end
left_context = left_context - 1
root = node
end
node = node:parent()
end
if left_context > 0 then
return
end
return root
end

Expand All @@ -173,6 +181,11 @@ function M.get_context(buf, line)
end
local from, to = M.expand(buf, line, line, line)

if config.options.node_context > 0 then
local lcount = vim.api.nvim_buf_line_count(buf)
return 2, lcount + 2
end

while to - from < config.options.context do
local pf, pt, pnode = M.expand(buf, from, to, from - 1)
local nf, nt, nnode = M.expand(buf, from, to, to + 1)
Expand Down