-
Notifications
You must be signed in to change notification settings - Fork 39
Feat(tree): add the ability to fold and unfold (recursively) tree nodes. #293
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ae76361
refactor: extract fold logic into tree-utils.setup_fold_keymaps()
NeOzay b47c202
refactor(tree): rename tree-utils to tree_utils and fix nil checks
NeOzay cc193e3
chore: untrack CLAUDE.md
NeOzay b278ba1
test(tree): add unit tests for is_foldable and recursive expand/collapse
NeOzay fbebb16
docs: add missing keymaps to README configuration
NeOzay 52db63e
fix(tree): warn when fold keymap key is undefined
NeOzay 3b62a73
Merge branch 'esmuellert:main' into main
NeOzay f2eb136
fix(tree): distinguish nil vs false for fold keymaps
NeOzay d109486
fix(tree_utils): silently skip disabled fold keymaps
esmuellert 4eb1c60
docs: add fold keymaps to vimdoc
esmuellert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| local M = {} | ||
|
|
||
| function M.find_foldable_node(node, tree) | ||
| if node:is_foldable() then | ||
| return node | ||
NeOzay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| if node._parent_id then | ||
| local parent = tree:get_node(node._parent_id) | ||
| if parent and parent:is_foldable() then | ||
| return parent | ||
| end | ||
| end | ||
| return nil | ||
| end | ||
|
|
||
| function M.find_foldable_at_cursor(tree) | ||
| local node = tree:get_node() | ||
| if not node then | ||
| return nil | ||
| end | ||
| return M.find_foldable_node(node, tree) | ||
| end | ||
|
|
||
| function M.get_root_node(tree) | ||
| local node = tree:get_node() | ||
| if not node then | ||
| return | ||
| end | ||
|
|
||
| local function find_root(n) | ||
| if not n._parent_id then | ||
| return n | ||
| end | ||
| local parent = tree:get_node(n._parent_id) | ||
| if parent then | ||
| return find_root(parent) | ||
| else | ||
| return n | ||
| end | ||
| end | ||
| return find_root(node) | ||
| end | ||
NeOzay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| -- Setup all fold-related keymaps on a tree buffer. | ||
| -- @param opts table { tree, keymaps, bufnr } | ||
| function M.setup_fold_keymaps(opts) | ||
| local tree = opts.tree | ||
| local keymaps = opts.keymaps | ||
| local bufnr = opts.bufnr | ||
|
|
||
| local function update_tree_view(node) | ||
| tree:render() | ||
| local winid = vim.fn.bufwinid(bufnr) | ||
| if node._line and winid ~= -1 then | ||
| vim.api.nvim_win_set_cursor(winid, { node._line, 0 }) | ||
NeOzay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
| end | ||
|
|
||
| local function fold_open() | ||
| local node = M.find_foldable_at_cursor(tree) | ||
| if not node then | ||
| return | ||
| end | ||
| node:expand() | ||
| update_tree_view(node) | ||
| end | ||
|
|
||
| local function fold_open_recursive() | ||
| local node = M.find_foldable_at_cursor(tree) | ||
| if not node then | ||
| return | ||
| end | ||
| node:expand_recursively() | ||
| update_tree_view(node) | ||
| end | ||
|
|
||
| local function fold_close() | ||
| local node = M.find_foldable_at_cursor(tree) | ||
| if not node then | ||
| return | ||
| end | ||
| node:collapse() | ||
| update_tree_view(node) | ||
| end | ||
|
|
||
| local function fold_close_recursive() | ||
| local node = M.find_foldable_at_cursor(tree) | ||
| if not node then | ||
| return | ||
| end | ||
| node:collapse_recursively() | ||
| update_tree_view(node) | ||
| end | ||
|
|
||
| local function fold_toggle() | ||
| local node = M.find_foldable_at_cursor(tree) | ||
| if not node then | ||
| return | ||
| end | ||
| if node:is_expanded() then | ||
| node:collapse() | ||
| else | ||
| node:expand() | ||
| end | ||
| update_tree_view(node) | ||
| end | ||
|
|
||
| local function fold_toggle_recursive() | ||
| local node = M.find_foldable_at_cursor(tree) | ||
| if not node then | ||
| return | ||
| end | ||
| if node:is_expanded() then | ||
| node:collapse_recursively() | ||
| else | ||
| node:expand_recursively() | ||
| end | ||
| update_tree_view(node) | ||
| end | ||
|
|
||
| local function fold_open_all() | ||
| local root = M.get_root_node(tree) | ||
| if not root then | ||
| return | ||
| end | ||
| root:expand_recursively() | ||
| update_tree_view(root) | ||
| end | ||
|
|
||
| local function fold_close_all() | ||
| local root = M.get_root_node(tree) | ||
| if not root then | ||
| return | ||
| end | ||
| root:collapse_recursively() | ||
| update_tree_view(root) | ||
| end | ||
|
|
||
| local fold_bindings = { | ||
| { key = "fold_open", fn = fold_open, desc = "Open fold" }, | ||
| { key = "fold_open_recursive", fn = fold_open_recursive, desc = "Open fold recursively" }, | ||
| { key = "fold_close", fn = fold_close, desc = "Close fold" }, | ||
| { key = "fold_close_recursive", fn = fold_close_recursive, desc = "Close fold recursively" }, | ||
| { key = "fold_toggle", fn = fold_toggle, desc = "Toggle fold" }, | ||
| { key = "fold_toggle_recursive", fn = fold_toggle_recursive, desc = "Toggle fold recursively" }, | ||
| { key = "fold_open_all", fn = fold_open_all, desc = "Open all folds" }, | ||
| { key = "fold_close_all", fn = fold_close_all, desc = "Close all folds" }, | ||
| } | ||
| local map_options = { noremap = true, silent = true, nowait = true } | ||
| for _, binding in ipairs(fold_bindings) do | ||
| local key = keymaps[binding.key] | ||
| if key then | ||
| vim.keymap.set("n", key, binding.fn, vim.tbl_extend("force", map_options, { buffer = bufnr, desc = binding.desc })) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return M | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.