11local M = {}
2+ local vim = vim
3+ local api = vim .api
4+ local inspect_pos = vim .inspect_pos
5+ local context_update = vim .fn [" context#update" ]
26
37function M :fill_hl_cache (row ,col )
48if self .eol_col [row ]== nil then
5- local line = vim . api .nvim_buf_get_lines (self .buf ,row - 1 ,row ,false )[1 ]
9+ local line = api .nvim_buf_get_lines (self .buf ,row - 1 ,row ,false )[1 ]
610self .eol_col [row ]= line and # line or 0
711end
812
@@ -12,15 +16,15 @@ function M:fill_hl_cache(row, col)
1216end
1317
1418-- needs nvim >0.9.0
15- local pos = vim . inspect_pos (self .buf ,row - 1 ,col , {extmarks = false })
19+ local pos = inspect_pos (self .buf ,row - 1 ,col , {extmarks = false })
1620
1721-- will fallback to treesitter if hl_group is empty
1822if # pos .semantic_tokens > 0 then
1923local hl_group = nil
2024for _ ,token in ipairs (pos .semantic_tokens )do
2125-- TODO using nvim_get_hl() didn't work
2226-- returns { [true] = 6 } if hl_group is empty
23- if vim . api .nvim_get_hl_by_name (token .opts .hl_group ,true )[true ]~= 6 then
27+ if api .nvim_get_hl_by_name (token .opts .hl_group ,true )[true ]~= 6 then
2428hl_group = token .opts .hl_group
2529end
2630end
@@ -69,14 +73,14 @@ function M:get_highlight(row, col)
6973self .scheduled = self .scheduled + 1
7074vim .schedule (function ()
7175self .scheduled = self .scheduled - 1
72- if self .cached [row ]== nil then
76+ if self .cached [row ]== nil or not api . nvim_buf_is_valid ( self . buf ) then
7377return
7478end
7579self :fill_hl_cache (row ,col )
7680if self .scheduled < 1 then
7781-- last to finish triggers update
7882self .scheduled = 0
79- vim . fn [ " context#update " ] (" OptionSet" )-- need to use OptionSet to force update
83+ context_update (" OptionSet" )-- need to use OptionSet to force update
8084end
8185end )
8286
9599
96100local _list = {}
97101local function nvim_hlgroup (winid ,row ,col )
98- local buf = vim . api .nvim_win_get_buf (winid )
102+ local buf = api .nvim_win_get_buf (winid )
99103local m = _list [buf ]
100104if m == nil then
101105m = M .new (buf )
102- vim . api .nvim_buf_attach (buf ,false , {
106+ api .nvim_buf_attach (buf ,false , {
103107on_detach = function ()
104108_list [buf ]= nil
105109end ,