|
46 | 46 | function M.get_todo(headline)
|
47 | 47 | local keywords = config.todo_keywords.ALL
|
48 | 48 | local done_keywords = config.todo_keywords.DONE
|
49 |
| - local todo_node = nil |
50 |
| - local keyword = nil |
51 |
| - local is_done = nil |
52 | 49 | for _, word in ipairs(keywords) do
|
53 | 50 | local todo = parse_item(headline, string.gsub(word, '-', '%%-'))
|
54 | 51 | if todo then
|
55 |
| - todo_node = todo |
56 |
| - keyword = word |
57 |
| - is_done = vim.tbl_contains(done_keywords, word) |
58 |
| - break |
| 52 | + return todo, word, vim.tbl_contains(done_keywords, word) |
59 | 53 | end
|
60 | 54 | end
|
61 |
| - return todo_node, keyword, is_done |
62 | 55 | end
|
63 | 56 |
|
64 | 57 | function M.get_stars(headline)
|
65 | 58 | return headline:field('stars')[1]
|
66 | 59 | end
|
67 | 60 |
|
68 |
| -function M.set_node_text(node, text) |
| 61 | +-- @param front_trim boolean |
| 62 | +function M.set_node_text(node, text, front_trim) |
69 | 63 | local sr, sc, er, ec = node:range()
|
70 | 64 | if string.len(text) == 0 then
|
71 |
| - ec = ec + 1 |
| 65 | + if front_trim then |
| 66 | + sc = sc - 1 |
| 67 | + else |
| 68 | + ec = ec + 1 |
| 69 | + end |
72 | 70 | end
|
73 | 71 | vim.api.nvim_buf_set_text(0, sr, sc, er, ec, { text })
|
74 | 72 | end
|
@@ -105,4 +103,53 @@ function M.set_todo(headline, keyword)
|
105 | 103 | M.set_node_text(stars, string.format('%s %s', text, keyword))
|
106 | 104 | end
|
107 | 105 |
|
| 106 | +function M.get_plan(headline) |
| 107 | + local section = headline:parent() |
| 108 | + for _, node in ipairs(ts_utils.get_named_children(section)) do |
| 109 | + if node:type() == 'plan' then |
| 110 | + return node |
| 111 | + end |
| 112 | + end |
| 113 | +end |
| 114 | + |
| 115 | +function M.get_dates(headline) |
| 116 | + local plan = M.get_plan(headline) |
| 117 | + local dates = {} |
| 118 | + for _, node in ipairs(ts_utils.get_named_children(plan)) do |
| 119 | + local name = vim.treesitter.query.get_node_text(node:named_child(0), 0) |
| 120 | + dates[name] = node |
| 121 | + end |
| 122 | + return dates |
| 123 | +end |
| 124 | + |
| 125 | +function M.repeater_dates(headline) |
| 126 | + return vim.tbl_filter(function(entry) |
| 127 | + local timestamp = entry:field('timestamp')[1] |
| 128 | + for _, node in ipairs(ts_utils.get_named_children(timestamp)) do |
| 129 | + if node:type() == 'repeat' then |
| 130 | + return true |
| 131 | + end |
| 132 | + end |
| 133 | + end, M.get_dates(headline)) |
| 134 | +end |
| 135 | + |
| 136 | +function M.add_closed_date(headline) |
| 137 | + local dates = M.get_dates(headline) |
| 138 | + if vim.tbl_count(dates) == 0 or dates['CLOSED'] then |
| 139 | + return |
| 140 | + end |
| 141 | + local last_child = dates['DEADLINE'] or dates['SCHEDULED'] |
| 142 | + local ptext = vim.treesitter.query.get_node_text(last_child, 0) |
| 143 | + local text = ptext .. ' CLOSED: [' .. vim.fn.strftime('%Y-%m-%d %a %H:%M') .. ']' |
| 144 | + M.set_node_text(last_child, text) |
| 145 | +end |
| 146 | + |
| 147 | +function M.remove_closed_date(headline) |
| 148 | + local dates = M.get_dates(headline) |
| 149 | + if vim.tbl_count(dates) == 0 or not dates['CLOSED'] then |
| 150 | + return |
| 151 | + end |
| 152 | + M.set_node_text(dates['CLOSED'], '', true) |
| 153 | +end |
| 154 | + |
108 | 155 | return M
|
0 commit comments