Skip to content

Commit 2c128fa

Browse files
refactor: Use custom predicate for todo keyword highlights
1 parent 6e40eec commit 2c128fa

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

lua/orgmode/colors/todo_highlighter.lua

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ local function add_todo_keyword_highlights()
88
if not query_files or #query_files == 0 then
99
return
1010
end
11-
local todo_keywords = config:get_todo_keywords()
1211
local faces = highlights.parse_todo_keyword_faces()
12+
if not faces or vim.tbl_isempty(faces) then
13+
return
14+
end
15+
1316
local all_lines = {}
1417
for i, _ in pairs(query_files) do
1518
if i ~= #query_files then
@@ -31,20 +34,6 @@ local function add_todo_keyword_highlights()
3134
if err then
3235
return
3336
end
34-
local todo_type = table.concat(
35-
vim.tbl_map(function(word)
36-
return string.format('"%s"', word)
37-
end, todo_keywords.TODO),
38-
' '
39-
)
40-
local done_type = table.concat(
41-
vim.tbl_map(function(word)
42-
return string.format('"%s"', word)
43-
end, todo_keywords.DONE),
44-
' '
45-
)
46-
table.insert(lines, string.format([[(item . (expr) @OrgTODO @nospell (#any-of? @OrgTODO %s))]], todo_type))
47-
table.insert(lines, string.format([[(item . (expr) @OrgDONE @nospell (#any-of? @OrgDONE %s))]], done_type))
4837
for face_name, face_hl in pairs(faces) do
4938
table.insert(
5039
lines,

lua/orgmode/config/init.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,20 @@ function Config:get_inheritable_tags(headline)
343343
end, headline.tags)
344344
end
345345

346+
function Config:setup_ts_predicates()
347+
local todo_keywords = self:get_todo_keywords().KEYS
348+
349+
vim.treesitter.query.add_predicate('org-is-todo-keyword?', function(match, _, source, predicate)
350+
local node = match[predicate[2]]
351+
if node then
352+
local text = vim.treesitter.get_node_text(node, source)
353+
return todo_keywords[text] and todo_keywords[text].type == predicate[3] or false
354+
end
355+
356+
return false
357+
end, true)
358+
end
359+
346360
function Config:ts_highlights_enabled()
347361
if self.ts_hl_enabled ~= nil then
348362
return self.ts_hl_enabled

lua/orgmode/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ local function setup(opts)
108108
instance = Org:new()
109109
check_ts_grammar()
110110
local config = require('orgmode.config'):extend(opts)
111+
config:setup_ts_predicates()
111112
vim.defer_fn(function()
112113
if config.notifications.enabled and #vim.api.nvim_list_uis() > 0 then
113114
require('orgmode.parser.files').load(vim.schedule_wrap(function()

queries/org/highlights.scm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
(headline (stars) @stars (#eq? @stars "*******")) @OrgTSHeadlineLevel7
1010
(headline (stars) @stars (#eq? @stars "********")) @OrgTSHeadlineLevel8
1111
(headline (item) @spell)
12+
(item . (expr) @OrgTODO @nospell (#org-is-todo-keyword? @OrgTODO "TODO"))
13+
(item . (expr) @OrgDONE @nospell (#org-is-todo-keyword? @OrgDONE "DONE"))
1214
(list (listitem (paragraph) @spell))
1315
(body (paragraph) @spell)
1416
(bullet) @OrgTSBullet

0 commit comments

Comments
 (0)