Introduction to Tree-sitter and Code Folding
Tree-sitter is a parsing library that provides fast, accurate syntax highlighting and code analysis by building a concrete syntax tree for your code. In LunarVim, Tree-sitter is pre-configured and powers advanced features like:
- Semantic syntax highlighting
- Smart code folding (collapsing/expanding code blocks)
- Refactoring-aware navigation
For JSON files, Tree-sitter understands nested objects, arrays, and key-value pairs, allowing you to fold sections of your JSON data logically. This is especially useful for large configuration files, API responses, or data dumps.
Why Tree-sitter Folding for JSON?
Traditional folding in Vim relies on indentation or regex patterns, which often fail with complex nested JSON. Tree-sitter folding, however:
- Folds based on the actual syntax structure (e.g., objects, arrays).
- Handles nested hierarchies perfectly.
- Works instantly, with no manual setup in LunarVim.
Setting Up Tree-sitter for JSON in LunarVim
LunarVim includes Tree-sitter by default, but ensure the JSON parser is installed:
- Open LunarVim.
- Run
:LvimSyncCorePlugins
to update parsers. - Verify JSON support with
:TSInstallInfo
(look forjson
in the list).
Folding/Unfolding JSON: Keybindings
LunarVim uses Neovim’s native folding commands, enhanced by Tree-sitter. Use these default keybindings:
Action | Keybinding |
---|---|
Toggle fold at cursor | za |
Close fold | zc |
Open fold | zo |
Close all folds | zM |
Open all folds | zR |
Fold level 1 | z1 |
Fold level 2 | z2 |
… | … |
Custom LunarVim Shortcuts (if using Space as leader):
Space + fc
: Close all folds (zM
)Space + fo
: Open all folds (zR
)
Example: Folding a JSON File
Consider this JSON data:
{
"users": [
{
"id": 1,
"name": "Alice",
"roles": ["admin", "user"]
},
{
"id": 2,
"name": "Bob",
"roles": ["user"]
}
]
}
Step-by-Step Workflow
- Open the JSON file in LunarVim.
- Navigate to a foldable block (e.g., the
users
array). - Press
zc
to collapse the array:{ "users": [ ... ], }
- Press
zo
to expand it again. - Use
zM
to collapse all nested objects/arrays at once.
Advanced Tips
1. Fold with Precision
- Fold specific levels: Use
z2
to fold up to level 2 (e.g., collapse outer objects but leave inner arrays open).
2. Customize Fold Appearance
Add this to your config.lua
to show fold indicators:
vim.opt.foldcolumn = '1' -- Shows a fold indicator gutter
vim.opt.fillchars = { fold = " " } -- Cleaner look
3. Use Fold Navigation
- Jump between folds with
zj
(next fold) andzk
(previous fold).
4. Persist Folds
Add this to retain folds when closing/reopening files:
vim.opt.viewoptions:append('folds') -- Save folds per file
Troubleshooting
- Folding not working? Run
:TSUpdate json
to update the JSON parser. - Folds look incorrect? Ensure
foldexpr
is set tonvim_treesitter#foldexpr()
:vim.opt.foldmethod = 'expr' vim.opt.foldexpr = 'nvim_treesitter#foldexpr()'
Conclusion
With Tree-sitter in LunarVim, managing JSON folding becomes intuitive and precise. Whether you’re working with massive configuration files or deeply nested API data, these keybindings and workflows will keep your workspace clutter-free.