-
Notifications
You must be signed in to change notification settings - Fork 36
/
indent.lua
71 lines (65 loc) · 1.88 KB
/
indent.lua
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
67
68
69
70
71
local foldingrange = require('ufo.model.foldingrange')
local bufmanager = require('ufo.bufmanager')
local Indent = {}
function Indent.getFolds(bufnr)
local buf = bufmanager:get(bufnr)
if not buf then
return
end
local lines = buf:lines(1, -1)
local sw = vim.bo[bufnr].shiftwidth
local ts = vim.bo[bufnr].ts
local levels = {}
for lnum = 1, #lines do
local line = lines[lnum]
local n = 0
local stop = false
for col = 1, #line do
-- compare byte is slightly faster than a char in the string
local b = line:byte(col, col)
if b == 0x20 then
-- ' '
n = n + 1
elseif b == 0x09 then
-- '\t'
n = n + (ts - (n % ts))
else
stop = true
break
end
end
local level = stop and math.ceil(n / sw) or -1
table.insert(levels, level)
end
local ranges = {}
local stack = {}
local function pop(curLevel, lastLnum)
while #stack > 0 do
local data = stack[#stack]
local level, lnum = data[1], data[2]
if level >= curLevel then
table.insert(ranges, foldingrange.new(lnum - 1, lastLnum - 1))
table.remove(stack)
else
break
end
end
end
local lastLnum = 1
local lastLevel = levels[1]
for i = 1, #lines do
local curLevel = levels[i]
if curLevel >= 0 then
if curLevel > 0 and curLevel > lastLevel then
table.insert(stack, {lastLevel, lastLnum})
elseif curLevel < lastLevel then
pop(curLevel, lastLnum)
end
lastLevel = curLevel
lastLnum = i
end
end
pop(0, lastLnum)
return ranges
end
return Indent