aboutsummaryrefslogtreecommitdiffhomepage
path: root/.config/nvim/lua/leaf/fold.lua
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2021-11-19 15:57:09 +0900
committernsfisis <nsfisis@gmail.com>2022-01-29 17:37:40 +0900
commitf5986deda1ef0edf984b91792e44c72efbeececc (patch)
treeca43da3bd58d1349122a6e55ea9eb4b438d0c344 /.config/nvim/lua/leaf/fold.lua
parent52062894a34a57d5c76b86f842befe5fd98a7773 (diff)
downloaddotfiles-f5986deda1ef0edf984b91792e44c72efbeececc.tar.gz
dotfiles-f5986deda1ef0edf984b91792e44c72efbeececc.tar.zst
dotfiles-f5986deda1ef0edf984b91792e44c72efbeececc.zip
neovim:leaf: add leaf plugin
Diffstat (limited to '.config/nvim/lua/leaf/fold.lua')
-rw-r--r--.config/nvim/lua/leaf/fold.lua60
1 files changed, 60 insertions, 0 deletions
diff --git a/.config/nvim/lua/leaf/fold.lua b/.config/nvim/lua/leaf/fold.lua
new file mode 100644
index 0000000..6f8a0b5
--- /dev/null
+++ b/.config/nvim/lua/leaf/fold.lua
@@ -0,0 +1,60 @@
+local M = {}
+
+
+local F = vim.fn
+local V = vim.v
+
+
+local function find_plain_string(haystack, needle, start)
+ return haystack:find(needle, start or 1, true)
+end
+
+
+function M.foldexpr()
+ local current_line_indent = F.indent(V.lnum)
+ local task_level = math.floor(current_line_indent / F.shiftwidth())
+ if V.lnum == F.line('$') then
+ return task_level
+ end
+ local next_line_indent = F.indent(V.lnum + 1)
+ if current_line_indent < next_line_indent then
+ return ('>%d'):format(task_level + 1)
+ else
+ return task_level
+ end
+end
+
+
+function M.foldtext()
+ local foldstart = V.foldstart
+ local foldend = V.foldend
+ local shiftwidth = F.shiftwidth()
+ local current_line_indent = F.indent(foldstart)
+
+ local todo = 0
+ local done = 0
+ for i = foldstart + 1, foldend do
+ local line = F.getline(i)
+ local line_indent = F.indent(i)
+ if line_indent == current_line_indent + shiftwidth then
+ if find_plain_string(line, '[x] ') then
+ done = done + 1
+ elseif find_plain_string(line, '[ ] ') then
+ todo = todo + 1
+ elseif find_plain_string(line, '[-] ') then
+ done = done + 1
+ end
+ end
+ end
+
+ local progress
+ if done + todo == 0 then
+ progress = '...'
+ else
+ progress = (' [%d/%d]'):format(done, done + todo)
+ end
+ return F.getline(foldstart) .. progress
+end
+
+
+return M