Skip to content

Commit b619e02

Browse files
committed
Replace <anything> with the set check
1 parent 1b214e5 commit b619e02

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

lua/obsidian/commands/set_checkbox.lua

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,5 @@ local set_checkbox = require("obsidian.util").set_checkbox
22

33
---@param client obsidian.Client
44
return function(client, data)
5-
-- If no check_char is provided, default to "x"
6-
if string.len(data.args) == 0 then
7-
data.args = "x"
8-
end
95
set_checkbox(data.args)
106
end

lua/obsidian/util.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,14 +541,17 @@ util.set_checkbox = function(check_char, line_num)
541541
line_num = line_num or unpack(vim.api.nvim_win_get_cursor(0))
542542
local line = vim.api.nvim_buf_get_lines(0, line_num - 1, line_num, false)[1]
543543

544-
local checkbox_pattern = "^%s*- %[.] "
544+
local checkbox_pattern = "^(%s*)- %[.%] "
545+
546+
local indent, rest = string.match(line, checkbox_pattern .. "(.*)")
547+
548+
if indent and rest then
549+
-- Rebuild the line with the new check_char
550+
line = indent .. "- [" .. check_char .. "] " .. rest
545551

546-
if string.match(line, checkbox_pattern) then
547-
-- Only set the check_char if the line contains the checkbox pattern.
548-
line = util.string_replace(line, "- [" .. check_char .. "]", "- [" .. check_char .. "]", 1)
552+
-- Update the line in the buffer (0-indexed)
553+
vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, true, { line })
549554
end
550-
-- 0-indexed
551-
vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, true, { line })
552555
end
553556

554557
---Determines if the given date is a working day (not weekend)

0 commit comments

Comments
 (0)