1
1
local config = require (' orgmode.config' )
2
- local utils = require (' orgmode.utils' )
3
2
local ts_utils = require (' nvim-treesitter.ts_utils' )
4
3
local query = nil
5
4
@@ -64,6 +63,11 @@ local markers = {
64
63
},
65
64
}
66
65
66
+ --- @param node userdata
67
+ --- @param source number
68
+ --- @param offset_col_start ? number
69
+ --- @param offset_col_end ? number
70
+ --- @return string
67
71
local function get_node_text (node , source , offset_col_start , offset_col_end )
68
72
local start_row , start_col = node :start ()
69
73
local end_row , end_col = node :end_ ()
@@ -73,7 +77,7 @@ local function get_node_text(node, source, offset_col_start, offset_col_end)
73
77
local lines
74
78
local eof_row = vim .api .nvim_buf_line_count (source )
75
79
if start_row >= eof_row then
76
- return nil
80
+ return ' '
77
81
end
78
82
79
83
if end_col == 0 then
@@ -107,7 +111,7 @@ local function get_predicate_nodes(match, n)
107
111
local total = n or 2
108
112
local counter = 1
109
113
local nodes = {}
110
- for i , node in pairs (match ) do
114
+ for _ , node in pairs (match ) do
111
115
nodes [counter ] = node
112
116
counter = counter + 1
113
117
if counter > total then
@@ -228,21 +232,14 @@ local function load_deps()
228
232
vim .treesitter .query .add_predicate (' org-is-valid-latex-range?' , is_valid_latex_range )
229
233
end
230
234
231
- --- @param bufnr ? number
232
- --- @param first_line ? number
233
- --- @param last_line ? number
234
- --- @return table[]
235
- local function get_matches (bufnr , first_line , last_line )
236
- bufnr = bufnr or 0
237
- local root = get_tree (bufnr )
238
- if not root then
239
- return
240
- end
241
-
235
+ --- @param bufnr number
236
+ --- @param line_index number
237
+ --- @return table
238
+ local get_matches = ts_utils .memoize_by_buf_tick (function (bufnr , line_index , root )
242
239
local ranges = {}
243
240
local taken_locations = {}
244
241
245
- for _ , match , _ in query :iter_matches (root , bufnr , first_line , last_line ) do
242
+ for _ , match , _ in query :iter_matches (root , bufnr , line_index , line_index + 1 ) do
246
243
for _ , node in pairs (match ) do
247
244
local char = node :type ()
248
245
-- saves unnecessary parsing, since \\ is not used below
@@ -357,44 +354,28 @@ local function get_matches(bufnr, first_line, last_line)
357
354
end
358
355
end
359
356
360
- return result , link_result , latex_result
361
- end
362
-
363
- local function apply (namespace , bufnr , _ , first_line , last_line , _ )
364
- -- Add some offset to make sure everything is covered
365
- local line_ranges = { {} }
366
- local current_line_range = 1
367
- local last_valid_line = - 1
368
- for i = first_line , last_line do
369
- if vim .fn .foldclosed (i + 1 ) == - 1 then
370
- -- Generate list of valid ranges
371
- if last_valid_line < 0 or (i - 1 ) == last_valid_line then
372
- table.insert (line_ranges [current_line_range ], i )
373
- else
374
- current_line_range = current_line_range + 1
375
- line_ranges [current_line_range ] = { i }
376
- end
377
- last_valid_line = i
378
- end
379
- end
357
+ return {
358
+ ranges = result ,
359
+ link_ranges = link_result ,
360
+ latex_ranges = latex_result ,
361
+ }
362
+ end , {
363
+ key = function (bufnr , line_index )
364
+ return bufnr .. ' __' .. line_index
365
+ end ,
366
+ })
380
367
381
- -- None of the lines are valid
382
- if last_valid_line < 0 then
368
+ local function apply (namespace , bufnr , line_index )
369
+ bufnr = bufnr or 0
370
+ local root = get_tree (bufnr )
371
+ if not root then
383
372
return
384
373
end
385
- local ranges = {}
386
- local link_ranges = {}
387
- local latex_ranges = {}
388
-
389
- for _ , range in ipairs (line_ranges ) do
390
- local r , link_r , latex_r = get_matches (bufnr , math.max (1 , range [1 ] - 5 ), range [# range ] + 5 )
391
- utils .concat (ranges , r or {})
392
- utils .concat (link_ranges , link_r or {})
393
- utils .concat (latex_ranges , latex_r or {})
394
- end
374
+
375
+ local result = get_matches (bufnr , line_index , root )
395
376
local hide_markers = config .org_hide_emphasis_markers
396
377
397
- for _ , range in ipairs (ranges ) do
378
+ for _ , range in ipairs (result . ranges ) do
398
379
vim .api .nvim_buf_set_extmark (bufnr , namespace , range .from .start .line , range .from .start .character , {
399
380
ephemeral = true ,
400
381
end_col = range .to [' end' ].character ,
@@ -416,7 +397,7 @@ local function apply(namespace, bufnr, _, first_line, last_line, _)
416
397
end
417
398
end
418
399
419
- for _ , link_range in ipairs (link_ranges ) do
400
+ for _ , link_range in ipairs (result . link_ranges ) do
420
401
local line = vim .api .nvim_buf_get_lines (bufnr , link_range .from .start .line , link_range .from .start .line + 1 , false )[1 ]
421
402
local link = line :sub (link_range .from .start .character + 1 , link_range .to [' end' ].character )
422
403
local alias = link :find (' %]%[' ) or 1
@@ -441,7 +422,7 @@ local function apply(namespace, bufnr, _, first_line, last_line, _)
441
422
})
442
423
end
443
424
444
- for _ , latex_range in ipairs (latex_ranges ) do
425
+ for _ , latex_range in ipairs (result . latex_ranges ) do
445
426
vim .api .nvim_buf_set_extmark (bufnr , namespace , latex_range .from .start .line , latex_range .from .start .character , {
446
427
ephemeral = true ,
447
428
end_col = latex_range .to [' end' ].character ,
0 commit comments