@@ -4,11 +4,16 @@ local M = {}
4
4
5
5
-- Searches headline item nodes for a match
6
6
local function parse_item (headline , pattern )
7
+ local match = ' '
7
8
local matching_nodes = vim .tbl_filter (function (node )
8
9
local text = vim .treesitter .query .get_node_text (node , 0 ) or ' '
9
- return string.match (text , pattern )
10
+ local m = string.match (text , pattern )
11
+ if m then
12
+ match = string.match (text , pattern )
13
+ return true
14
+ end
10
15
end , ts_utils .get_named_children (headline :field (' item' )[1 ]))
11
- return matching_nodes [1 ]
16
+ return matching_nodes [1 ], match
12
17
end
13
18
14
19
-- walks the tree to find a headline
@@ -32,19 +37,28 @@ function M.closest_headline()
32
37
end
33
38
34
39
function M .get_priority (headline )
35
- return parse_item (headline , ' %[#%w+%]' )
40
+ return parse_item (headline , ' %[#( %w+) %]' )
36
41
end
37
42
43
+ -- Returns the headlines todo node, it's keyword,
44
+ -- and if it's in done state
45
+ -- @return Node, string, boolean
38
46
function M .get_todo (headline )
39
47
local keywords = config .todo_keywords .ALL
40
- local todos = {}
48
+ local done_keywords = config .todo_keywords .DONE
49
+ local todo_node = nil
50
+ local keyword = nil
51
+ local is_done = nil
41
52
for _ , word in ipairs (keywords ) do
42
53
local todo = parse_item (headline , string.gsub (word , ' -' , ' %%-' ))
43
54
if todo then
44
- table.insert (todos , todo )
55
+ todo_node = todo
56
+ keyword = word
57
+ is_done = vim .tbl_contains (done_keywords , word )
58
+ break
45
59
end
46
60
end
47
- return todos [ 1 ]
61
+ return todo_node , keyword , is_done
48
62
end
49
63
50
64
function M .get_stars (headline )
@@ -88,7 +102,7 @@ function M.set_todo(headline, keyword)
88
102
89
103
local stars = M .get_stars (headline )
90
104
local text = vim .treesitter .query .get_node_text (stars , 0 )
91
- M .set_node_text (stars , string.format (" %s %s" , text , keyword ))
105
+ M .set_node_text (stars , string.format (' %s %s' , text , keyword ))
92
106
end
93
107
94
108
return M
0 commit comments