blob: 0bfe0f7cc44cf5f7aecf948d7502d97225937b2f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
local api = vim.api
local utils = require'nvim-treesitter.ts_utils'
local query = require'nvim-treesitter.query'
local parsers = require'nvim-treesitter.parsers'
local M = {}
local folds_levels = utils.memoize_by_buf_tick(function(bufnr)
local lang = parsers.get_buf_lang(bufnr)
local matches
if query.has_fold(lang) then
matches = query.get_capture_matches(bufnr, "@fold", "fold")
elseif query.has_locals(lang) then
matches = query.get_capture_matches(bufnr, "@scope", "locals")
else
return {}
end
local levels_tmp = {}
for _, node in ipairs(matches) do
local start, _, stop, stop_col = node.node:range()
if stop_col > 0 then
stop = stop + 1
end
-- This can be folded
-- Fold only multiline nodes that are not exactly the same as prevsiously met folds
if start ~= stop and not (levels_tmp[start] and levels_tmp[stop]) then
levels_tmp[start] = (levels_tmp[start] or 0) + 1
levels_tmp[stop] = (levels_tmp[stop] or 0) - 1
end
end
local levels = {}
local current_level = 0
for lnum=0,api.nvim_buf_line_count(bufnr) do
local prefix= ''
local shift = levels_tmp[lnum] or 0
if levels_tmp[lnum] and shift >= 0 then
prefix = '>'
end
current_level = current_level + shift
levels[lnum + 1] = prefix .. tostring(current_level)
end
return levels
end)
function M.get_fold_indic(lnum)
if not parsers.has_parser() or not lnum then return '0' end
local buf = api.nvim_get_current_buf()
local levels = folds_levels(buf) or {}
return levels[lnum] or '0'
end
return M
|