@@ -10,13 +10,15 @@ local PriorityState = require('orgmode.objects.priority_state')
1010--- @class OrgConfig : OrgConfigOpts
1111--- @field opts table
1212--- @field todo_keywords OrgTodoKeywords
13+ --- @field priorities table<string , { type : string , hl_group : string } >
1314local Config = {}
1415
1516--- @param opts ? table
1617function Config :new (opts )
1718 local data = {
1819 opts = vim .tbl_deep_extend (' force' , defaults , opts or {}),
1920 todo_keywords = nil ,
21+ priorities = nil ,
2022 }
2123 setmetatable (data , self )
2224 return data
4143--- @return OrgConfig
4244function Config :extend (opts )
4345 self .todo_keywords = nil
46+ self .priorities = nil
4447 opts = opts or {}
4548 self :_deprecation_notify (opts )
4649 if not self :_are_priorities_valid (opts ) then
@@ -326,6 +329,10 @@ function Config:get_priority_range()
326329end
327330
328331function Config :get_priorities ()
332+ if self .priorities then
333+ return self .priorities
334+ end
335+
329336 local priorities = {
330337 [self .opts .org_priority_highest ] = { type = ' highest' , hl_group = ' @org.priority.highest' },
331338 }
@@ -351,6 +358,9 @@ function Config:get_priorities()
351358 -- we need to overwrite the lowest value set by the second loop
352359 priorities [self .opts .org_priority_lowest ] = { type = ' lowest' , hl_group = ' @org.priority.lowest' }
353360
361+ -- Cache priorities to avoid unnecessary recalculations
362+ self .priorities = priorities
363+
354364 return priorities
355365end
356366
@@ -360,23 +370,24 @@ function Config:setup_ts_predicates()
360370
361371 vim .treesitter .query .add_predicate (' org-is-todo-keyword?' , function (match , _ , source , predicate )
362372 local node = match [predicate [2 ]]
373+ node = node and node [# node ]
363374 if node then
364375 local text = vim .treesitter .get_node_text (node , source )
365376 return todo_keywords [text ] and todo_keywords [text ].type == predicate [3 ] or false
366377 end
367378
368379 return false
369- end , { force = true , all = false })
380+ end , { force = true , all = true })
370381
371382 local org_cycle_separator_lines = math.max (self .opts .org_cycle_separator_lines , 0 )
372383
373384 vim .treesitter .query .add_directive (' org-set-fold-offset!' , function (match , _ , bufnr , pred , metadata )
374385 if org_cycle_separator_lines == 0 then
375386 return
376387 end
377- --- @type TSNode | nil
378388 local capture_id = pred [2 ]
379389 local section_node = match [capture_id ]
390+ section_node = section_node and section_node [# section_node ]
380391 if not capture_id or not section_node or section_node :type () ~= ' section' then
381392 return
382393 end
@@ -402,65 +413,63 @@ function Config:setup_ts_predicates()
402413 end
403414 range [3 ] = range [3 ] - 1
404415 metadata [capture_id ].range = range
405- end , { force = true , all = false })
416+ end , { force = true , all = true })
406417
407418 vim .treesitter .query .add_predicate (' org-is-valid-priority?' , function (match , _ , source , predicate )
419+ --- @type TSNode | nil
408420 local node = match [predicate [2 ]]
409- local type = predicate [ 3 ]
421+ node = node and node [ # node ]
410422 if not node then
411423 return false
412424 end
413425
426+ local type = predicate [3 ]
414427 local text = vim .treesitter .get_node_text (node , source )
415- local is_valid = valid_priorities [text ] and valid_priorities [text ].type == type
416- if not is_valid then
417- return false
418- end
419- local priority_text = ' [#' .. text .. ' ]'
420- local full_node_text = vim .treesitter .get_node_text (node :parent (), source )
421- if priority_text ~= full_node_text then
422- return false
423- end
428+ -- Leave only priority cookie: [#A] -> A
429+ text = text :sub (3 , - 2 )
430+ return valid_priorities [text ] and valid_priorities [text ].type == type
431+ end , { force = true , all = true })
424432
425- local prev_sibling = node :parent ():prev_sibling ()
426- -- If first child, consider it valid
427- if not prev_sibling then
428- return true
433+ vim .treesitter .query .add_directive (' org-set-block-language!' , function (match , _ , bufnr , pred , metadata )
434+ local lang_node = match [pred [2 ]]
435+ lang_node = lang_node and lang_node [# lang_node ]
436+ if not lang_node then
437+ return
429438 end
430-
431- -- If prev sibling has more prev siblings, it means that the prev_sibling is not a todo keyword
432- -- so this priority is not valid
433- if prev_sibling :prev_sibling () then
434- return false
439+ local text = vim .treesitter .get_node_text (lang_node , bufnr )
440+ if not text or vim .trim (text ) == ' ' then
441+ return
435442 end
443+ metadata [' injection.language' ] = utils .detect_filetype (text , true )
444+ end , { force = true , all = true })
436445
437- local todo_text = vim .treesitter .get_node_text (prev_sibling , source )
438- local is_prev_sibling_todo_keyword = todo_keywords [todo_text ] and true or false
439- return is_prev_sibling_todo_keyword
440- end , { force = true , all = false })
441-
442- vim .treesitter .query .add_directive (' org-set-block-language!' , function (match , _ , bufnr , pred , metadata )
446+ vim .treesitter .query .add_directive (' org-set-inline-block-language!' , function (match , _ , bufnr , pred , metadata )
443447 local lang_node = match [pred [2 ]]
448+ lang_node = lang_node and lang_node [# lang_node ]
444449 if not lang_node then
445450 return
446451 end
447452 local text = vim .treesitter .get_node_text (lang_node , bufnr )
448453 if not text or vim .trim (text ) == ' ' then
449454 return
450455 end
456+ -- Remove `src_` part: src_lua -> lua
457+ text = text :sub (5 )
458+ -- Remove opening brackend and parameters: lua[params]{ -> lua
459+ text = text :gsub (' [%{%[].*' , ' ' )
451460 metadata [' injection.language' ] = utils .detect_filetype (text , true )
452- end , { force = true , all = false })
461+ end , { force = true , all = true })
453462
454463 vim .treesitter .query .add_predicate (' org-is-headline-level?' , function (match , _ , _ , predicate )
455- --- @type TSNode
456464 local node = match [predicate [2 ]]
457- local level = tonumber ( predicate [ 3 ])
465+ node = node and node [ # node ]
458466 if not node then
459467 return false
460468 end
469+ local level = tonumber (predicate [3 ])
461470 local _ , _ , _ , node_end_col = node :range ()
462471 return ((node_end_col - 1 ) % 8 ) + 1 == level
463- end , { force = true , all = false })
472+ end , { force = true , all = true })
464473end
465474
466475--- @param content table
0 commit comments