88 lines
2.3 KiB
Lua
88 lines
2.3 KiB
Lua
local M = {}
|
|
|
|
-- Function to determine fold level for each line
|
|
local function fold_completed_tasks(lnum)
|
|
local line = vim.fn.getline(lnum)
|
|
|
|
-- Check if the line contains a completed task
|
|
if line:match("%- %[X%]") then
|
|
return ">1" -- Start a fold at level 1
|
|
end
|
|
|
|
return "0" -- No fold
|
|
end
|
|
|
|
-- Function to hide completed tasks using foldexpr
|
|
function M.hide_completed()
|
|
local count = 0
|
|
local pattern = "%- %[X%]" -- Pattern to match completed tasks: "- [X]"
|
|
|
|
-- Get all lines in the current buffer
|
|
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
|
|
|
|
-- Count completed tasks
|
|
for _, line in ipairs(lines) do
|
|
if line:match(pattern) then
|
|
count = count + 1
|
|
end
|
|
end
|
|
|
|
-- If no completed tasks found, notify and return
|
|
if count == 0 then
|
|
vim.notify("No completed tasks found", vim.log.levels.INFO)
|
|
return
|
|
end
|
|
|
|
-- Save current folding settings
|
|
local old_foldmethod = vim.wo.foldmethod
|
|
local old_foldexpr = vim.wo.foldexpr
|
|
|
|
-- Set up folding based on expression
|
|
vim.wo.foldmethod = "expr"
|
|
vim.wo.foldexpr = "v:lua.require'markdown_organizer'.fold_expr(v:lnum)"
|
|
|
|
-- Close all folds to hide completed tasks
|
|
vim.cmd("normal! zM")
|
|
|
|
-- Notify user of how many tasks were hidden
|
|
vim.notify("Hidden " .. count .. " completed tasks", vim.log.levels.INFO)
|
|
|
|
-- Add autocmd to restore original folding settings when leaving buffer
|
|
vim.api.nvim_create_autocmd("BufLeave", {
|
|
buffer = 0,
|
|
callback = function()
|
|
vim.wo.foldmethod = old_foldmethod
|
|
vim.wo.foldexpr = old_foldexpr
|
|
end,
|
|
once = true
|
|
})
|
|
end
|
|
|
|
-- Expose the fold expression function to be used by foldexpr
|
|
function M.fold_expr(lnum)
|
|
return fold_completed_tasks(lnum)
|
|
end
|
|
|
|
-- Function to restore normal folding
|
|
function M.show_completed()
|
|
-- Reset folding settings to default
|
|
vim.wo.foldmethod = "manual"
|
|
vim.wo.foldexpr = "0"
|
|
|
|
-- Open all folds
|
|
vim.cmd("normal! zR")
|
|
|
|
vim.notify("Showing all tasks", vim.log.levels.INFO)
|
|
end
|
|
|
|
-- Register the commands
|
|
vim.api.nvim_create_user_command("HideCompleted", function()
|
|
M.hide_completed()
|
|
end, {})
|
|
|
|
vim.api.nvim_create_user_command("ShowCompleted", function()
|
|
M.show_completed()
|
|
end, {})
|
|
|
|
return M
|