@@ -397,6 +397,21 @@ local function copy_extmarks(bufnr, ctx_bufnr, contexts)
397397 { details = true }
398398 )
399399
400+ local namespaces = {} --- @type table<integer , true>
401+ for nm , id in pairs (api .nvim_get_namespaces ()) do
402+ -- Only copy extmarks from core as they are the only ones we can update
403+ -- reliably.
404+ if vim .startswith (nm , ' nvim.' ) then
405+ namespaces [id ] = true
406+ end
407+ end
408+
409+ --- @param e vim.api.keyset.get_extmark_item
410+ extmarks = vim .tbl_filter (function (e )
411+ local opts = e [4 ] --[[ @as vim.api.keyset.extmark_details]]
412+ return namespaces [opts .ns_id ]
413+ end , extmarks )
414+
400415 for _ , m in ipairs (extmarks ) do
401416 local id , row , col = m [1 ], m [2 ], m [3 ]
402417 local opts = m [4 ] --[[ @as vim.api.keyset.extmark_details]]
@@ -452,7 +467,8 @@ local M = {}
452467--- @param winid integer
453468--- @param ctx_ranges Range4[]
454469--- @param ctx_lines string[]
455- function M .open (winid , ctx_ranges , ctx_lines )
470+ --- @param force_hl_update ? boolean
471+ function M .open (winid , ctx_ranges , ctx_lines , force_hl_update )
456472 local bufnr = api .nvim_win_get_buf (winid )
457473 local gutter_width = get_gutter_width (winid )
458474 local win_width = math.max (1 , api .nvim_win_get_width (winid ) - gutter_width )
@@ -499,16 +515,16 @@ function M.open(winid, ctx_ranges, ctx_lines)
499515
500516 local ctx_bufnr = api .nvim_win_get_buf (window_context .context_winid )
501517
502- if not set_lines (ctx_bufnr , ctx_lines ) then
503- -- Context didn't change, can return here
504- return
505- end
518+ local changed = set_lines (ctx_bufnr , ctx_lines )
506519
507- api .nvim_buf_clear_namespace (ctx_bufnr , - 1 , 0 , - 1 )
508- highlight_contexts (bufnr , ctx_bufnr , ctx_ranges )
509- copy_extmarks (bufnr , ctx_bufnr , ctx_ranges )
510- highlight_bottom (ctx_bufnr , win_height - 1 , ' TreesitterContextBottom' )
511- horizontal_scroll_contexts (winid , window_context .context_winid )
520+ if changed or force_hl_update then
521+ -- Update highlights
522+ api .nvim_buf_clear_namespace (ctx_bufnr , - 1 , 0 , - 1 )
523+ highlight_contexts (bufnr , ctx_bufnr , ctx_ranges )
524+ copy_extmarks (bufnr , ctx_bufnr , ctx_ranges )
525+ highlight_bottom (ctx_bufnr , win_height - 1 , ' TreesitterContextBottom' )
526+ horizontal_scroll_contexts (winid , window_context .context_winid )
527+ end
512528end
513529
514530--- @param exclude_winids integer[] The only window for which the context should be displayed.
0 commit comments