mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-14 17:30:08 -04:00
feat: add syntax-based folding
This commit is contained in:
parent
d1da10ce1c
commit
fbade728fe
2 changed files with 33 additions and 0 deletions
|
|
@ -1,3 +1,7 @@
|
||||||
function! nvim_treesitter#statusline(len)
|
function! nvim_treesitter#statusline(len)
|
||||||
return luaeval("require'nvim-treesitter'.statusline(_A)", a:len)
|
return luaeval("require'nvim-treesitter'.statusline(_A)", a:len)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! nvim_treesitter#foldexpr()
|
||||||
|
return luaeval(printf('require"nvim-treesitter.fold".get_fold_indic(%d)', v:lnum))
|
||||||
|
endfunction
|
||||||
|
|
|
||||||
29
lua/nvim-treesitter/fold.lua
Normal file
29
lua/nvim-treesitter/fold.lua
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
local api = vim.api
|
||||||
|
local parsers = require'nvim-treesitter.parsers'
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
function M.get_fold_indic(lnum)
|
||||||
|
if not parsers.has_parser() or not lnum then return '0' end
|
||||||
|
|
||||||
|
local function smallest_multiline_containing(node, level)
|
||||||
|
for index = 0,(node:named_child_count() -1) do
|
||||||
|
local child = node:named_child(index)
|
||||||
|
local start, _, stop, _ = child:range()
|
||||||
|
|
||||||
|
if start ~= stop and start <= (lnum -1) and stop >= (lnum -1) then
|
||||||
|
return smallest_multiline_containing(child, level + 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return node, level
|
||||||
|
end
|
||||||
|
|
||||||
|
local parser = parsers.get_parser()
|
||||||
|
|
||||||
|
local multiline_here, level = smallest_multiline_containing(parser:parse():root(), 0)
|
||||||
|
|
||||||
|
return tostring(level)
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
Loading…
Add table
Add a link
Reference in a new issue