Skip to content

Commit 6d29ea9

Browse files
author
Tray Dennis
committed
Return additional node data rather than recalculate
1 parent e5053a9 commit 6d29ea9

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

lua/orgmode/org/mappings.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ function OrgMappings:set_priority(direction)
312312
return string.match(vim.treesitter.query.get_node_text(node, 0), '%[#(%w+)%]')
313313
end
314314
local headline = tree_utils.closest_headline()
315-
local priority_node = tree_utils.get_priority(headline)
316-
local current_priority = priority_node and extract_priority(priority_node) or ''
315+
local priority_node, current_priority = tree_utils.get_priority(headline)
317316
local priority_state = PriorityState:new(current_priority)
318317

319318
local new_priority
@@ -758,8 +757,7 @@ end
758757
---@return string
759758
function OrgMappings:_change_todo_state(direction, use_fast_access)
760759
local headline = tree_utils.closest_headline()
761-
local todo = tree_utils.get_todo(headline)
762-
local current_keyword = todo and vim.treesitter.query.get_node_text(todo, 0) or ''
760+
local todo, current_keyword = tree_utils.get_todo(headline)
763761
local todo_state = TodoState:new({ current_state = current_keyword })
764762
local next_state = nil
765763
if use_fast_access and todo_state:has_fast_access() then

lua/orgmode/utils/treesitter.lua

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ local M = {}
44

55
-- Searches headline item nodes for a match
66
local function parse_item(headline, pattern)
7+
local match = ''
78
local matching_nodes = vim.tbl_filter(function(node)
89
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
1015
end, ts_utils.get_named_children(headline:field('item')[1]))
11-
return matching_nodes[1]
16+
return matching_nodes[1], match
1217
end
1318

1419
-- walks the tree to find a headline
@@ -32,19 +37,28 @@ function M.closest_headline()
3237
end
3338

3439
function M.get_priority(headline)
35-
return parse_item(headline, '%[#%w+%]')
40+
return parse_item(headline, '%[#(%w+)%]')
3641
end
3742

43+
-- Returns the headlines todo node, it's keyword,
44+
-- and if it's in done state
45+
-- @return Node, string, boolean
3846
function M.get_todo(headline)
3947
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
4152
for _, word in ipairs(keywords) do
4253
local todo = parse_item(headline, string.gsub(word, '-', '%%-'))
4354
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
4559
end
4660
end
47-
return todos[1]
61+
return todo_node, keyword, is_done
4862
end
4963

5064
function M.get_stars(headline)
@@ -88,7 +102,7 @@ function M.set_todo(headline, keyword)
88102

89103
local stars = M.get_stars(headline)
90104
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))
92106
end
93107

94108
return M

0 commit comments

Comments
 (0)