Skip to content

Commit

Permalink
refactor: drop number item support
Browse files Browse the repository at this point in the history
  • Loading branch information
phanen committed Mar 29, 2024
1 parent 4e7a0aa commit 92dd391
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
1 change: 0 additions & 1 deletion lua/mder/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ local default_opts = {

M.check_box = "%[x%]"
M.empty_box = "%[ %]"
M.list_items = { "%-", "%+", "%*", "%=", "%d+%." }

M.setup = function(opts)
opts = vim.tbl_deep_extend("force", default_opts, opts or {})
Expand Down
32 changes: 6 additions & 26 deletions lua/mder/line.lua
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
local config = require "mder.config"
local checkbox = config.check_box
local emptybox = config.empty_box
local items = config.list_items

local contain_item = function(line)
return vim.iter(items):any(function(i) return line:find("^%s*" .. i) end)
end
local contain_item = function(line) return line:find "^%s*[%-%+%*%=]" end

local contain_box = function(line, box)
return vim.iter(items):any(function(i) return line:find("^%s*" .. i .. " " .. box) end)
end
local contain_box = function(line, box) return line:find("^%s*[%-%+%*%=]%s" .. box) end

local make_box = function(line)
for _, i in ipairs(items) do
local new_line, ok = line:gsub("^(%s*" .. i .. "%s)(.*)", "%1[ ] %2", 1)
if ok == 1 then return new_line end
end
return line
end
local make_box = function(line) return (line:gsub("^(%s*[%-%+%*%=]%s)(.*)", "%1[ ] %2", 1)) end

local make_item = function(line)
local new_line, _ = line:gsub("^(%s*)(%S*.*)$", "%1* %2")
return new_line
end
local make_item = function(line) return (line:gsub("^(%s*)(%S*.*)$", "%1* %2")) end

local toggle_line = function(line)
if not contain_item(line) then return make_item(line) end
if contain_box(line, checkbox) then
local new_line, _ = line:gsub(checkbox, emptybox, 1)
return new_line
end
if contain_box(line, emptybox) then
local new_line, _ = line:gsub(emptybox, checkbox, 1)
return new_line
end
if contain_box(line, checkbox) then return (line:gsub(checkbox, emptybox, 1)) end
if contain_box(line, emptybox) then return (line:gsub(emptybox, checkbox, 1)) end
return make_box(line)
end

Expand Down

0 comments on commit 92dd391

Please sign in to comment.