diff options
| author | Thomas Vigouroux <tomvig38@gmail.com> | 2020-05-25 10:26:29 +0200 |
|---|---|---|
| committer | Thomas Vigouroux <tomvig38@gmail.com> | 2020-05-25 11:19:38 +0200 |
| commit | fbade728fed18a084bc5601a62628d3dec0c631c (patch) | |
| tree | f2728814efaacf1d746e2aa571c850f9d9faad77 /lua | |
| parent | Merge pull request #62 from theHamsta/c-locals (diff) | |
| download | nvim-treesitter-fbade728fed18a084bc5601a62628d3dec0c631c.tar nvim-treesitter-fbade728fed18a084bc5601a62628d3dec0c631c.tar.gz nvim-treesitter-fbade728fed18a084bc5601a62628d3dec0c631c.tar.bz2 nvim-treesitter-fbade728fed18a084bc5601a62628d3dec0c631c.tar.lz nvim-treesitter-fbade728fed18a084bc5601a62628d3dec0c631c.tar.xz nvim-treesitter-fbade728fed18a084bc5601a62628d3dec0c631c.tar.zst nvim-treesitter-fbade728fed18a084bc5601a62628d3dec0c631c.zip | |
feat: add syntax-based folding
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-treesitter/fold.lua | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/fold.lua b/lua/nvim-treesitter/fold.lua new file mode 100644 index 000000000..6a39461a7 --- /dev/null +++ b/lua/nvim-treesitter/fold.lua @@ -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 |
