@@ -9,13 +9,19 @@ local cmd = api.nvim_command
9
9
local M = {}
10
10
11
11
local usage_namespace = api .nvim_create_namespace (' nvim-treesitter-usages' )
12
+ local last_node = nil
12
13
13
14
function M .highlight_usages (bufnr )
14
- M .clear_usage_highlights (bufnr )
15
-
16
15
local node_at_point = ts_utils .get_node_at_cursor ()
17
- local references = locals .get_references (bufnr )
16
+ -- Don't calculate usages again if we are on the same node.
17
+ if node_at_point == last_node and M .has_highlights (bufnr ) then
18
+ return
19
+ else
20
+ last_node = node_at_point
21
+ end
18
22
23
+ M .clear_usage_highlights (bufnr )
24
+ local references = locals .get_references (bufnr )
19
25
if not node_at_point or not vim .tbl_contains (references , node_at_point ) then
20
26
return
21
27
end
@@ -34,6 +40,10 @@ function M.highlight_usages(bufnr)
34
40
end
35
41
end
36
42
43
+ function M .has_highlights (bufnr )
44
+ return # api .nvim_buf_get_extmarks (bufnr , usage_namespace , 0 , - 1 , {}) > 0
45
+ end
46
+
37
47
function M .clear_usage_highlights (bufnr )
38
48
api .nvim_buf_clear_namespace (bufnr , usage_namespace , 0 , - 1 )
39
49
end
@@ -43,7 +53,6 @@ function M.attach(bufnr)
43
53
cmd ' au!'
44
54
-- luacheck: push ignore 631
45
55
cmd (string.format ([[ autocmd CursorHold <buffer=%d> lua require'nvim-treesitter-refactor.highlight_definitions'.highlight_usages(%d)]] , bufnr , bufnr ))
46
- cmd (string.format ([[ autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-refactor.highlight_definitions'.clear_usage_highlights(%d)]] , bufnr , bufnr ))
47
56
cmd (string.format ([[ autocmd InsertEnter <buffer=%d> lua require'nvim-treesitter-refactor.highlight_definitions'.clear_usage_highlights(%d)]] , bufnr , bufnr ))
48
57
-- luacheck: pop
49
58
cmd ' augroup END'
52
61
function M .detach (bufnr )
53
62
M .clear_usage_highlights (bufnr )
54
63
cmd (string.format (' autocmd! NvimTreesitterUsages_%d CursorHold' , bufnr ))
55
- cmd (string.format (' autocmd! NvimTreesitterUsages_%d CursorMoved' , bufnr ))
56
64
cmd (string.format (' autocmd! NvimTreesitterUsages_%d InsertEnter' , bufnr ))
57
65
end
58
66
0 commit comments