Skip to content

Commit 0dc8069

Browse files
authored
Hl Usages: don't calculate definitions again if on the same node (#22)
Closes #21
1 parent 7470880 commit 0dc8069

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

Diff for: README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ Highlights definition and usages of the current symbol under the cursor.
2525
lua <<EOF
2626
require'nvim-treesitter.configs'.setup {
2727
refactor = {
28-
highlight_definitions = { enable = true },
28+
highlight_definitions = {
29+
enable = true,
30+
-- Set to false if you have an `updatetime` of ~100.
31+
clear_on_cursor_move = true,
32+
},
2933
},
3034
}
3135
EOF

Diff for: doc/nvim-treesitter-refactor.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@ Query files: `locals.scm`.
2626
Supported options:
2727
- enable: `true` or `false`.
2828
- disable: list of languages.
29+
- clear_on_cursor_move: `true` or `false` if highlights should be cleared
30+
when the cursor is moved. If your 'updatetime' is around `100`
31+
you can set this to false to have a less laggy experience.
2932

3033
>
3134
lua <<EOF
3235
require'nvim-treesitter.configs'.setup {
3336
refactor = {
34-
highlight_definitions = { enable = true },
37+
highlight_definitions = {
38+
enable = true,
39+
-- Set to false if you have an `updatetime` of ~100.
40+
clear_on_cursor_move = true,
41+
},
3542
},
3643
}
3744
EOF

Diff for: lua/nvim-treesitter-refactor.lua

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function M.init()
1010
enable = false,
1111
disable = {},
1212
is_supported = queries.has_locals,
13+
clear_on_cursor_move = true,
1314
},
1415
highlight_current_scope = {
1516
module_path = "nvim-treesitter-refactor.highlight_current_scope",

Diff for: lua/nvim-treesitter-refactor/highlight_definitions.lua

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- This module highlights reference usages and the corresponding
22
-- definition on cursor hold.
33

4+
local configs = require "nvim-treesitter.configs"
45
local ts_utils = require "nvim-treesitter.ts_utils"
56
local locals = require "nvim-treesitter.locals"
67
local api = vim.api
@@ -9,14 +10,19 @@ local cmd = api.nvim_command
910
local M = {}
1011

1112
local usage_namespace = api.nvim_create_namespace "nvim-treesitter-usages"
13+
local last_nodes = {}
1214

1315
function M.highlight_usages(bufnr)
14-
M.clear_usage_highlights(bufnr)
15-
1616
local node_at_point = ts_utils.get_node_at_cursor()
17-
local references = locals.get_references(bufnr)
17+
-- Don't calculate usages again if we are on the same node.
18+
if node_at_point and node_at_point == last_nodes[bufnr] and M.has_highlights(bufnr) then
19+
return
20+
else
21+
last_nodes[bufnr] = node_at_point
22+
end
1823

19-
if not node_at_point or not vim.tbl_contains(references, node_at_point) then
24+
M.clear_usage_highlights(bufnr)
25+
if not node_at_point then
2026
return
2127
end
2228

@@ -34,11 +40,16 @@ function M.highlight_usages(bufnr)
3440
end
3541
end
3642

43+
function M.has_highlights(bufnr)
44+
return #api.nvim_buf_get_extmarks(bufnr, usage_namespace, 0, -1, {}) > 0
45+
end
46+
3747
function M.clear_usage_highlights(bufnr)
3848
api.nvim_buf_clear_namespace(bufnr, usage_namespace, 0, -1)
3949
end
4050

4151
function M.attach(bufnr)
52+
local config = configs.get_module "refactor.highlight_definitions"
4253
cmd(string.format("augroup NvimTreesitterUsages_%d", bufnr))
4354
cmd "au!"
4455
-- luacheck: push ignore 631
@@ -49,13 +60,15 @@ function M.attach(bufnr)
4960
bufnr
5061
)
5162
)
52-
cmd(
53-
string.format(
54-
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-refactor.highlight_definitions'.clear_usage_highlights(%d)]],
55-
bufnr,
56-
bufnr
63+
if config.clear_on_cursor_move then
64+
cmd(
65+
string.format(
66+
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-refactor.highlight_definitions'.clear_usage_highlights(%d)]],
67+
bufnr,
68+
bufnr
69+
)
5770
)
58-
)
71+
end
5972
cmd(
6073
string.format(
6174
[[autocmd InsertEnter <buffer=%d> lua require'nvim-treesitter-refactor.highlight_definitions'.clear_usage_highlights(%d)]],
@@ -72,6 +85,7 @@ function M.detach(bufnr)
7285
cmd(string.format("autocmd! NvimTreesitterUsages_%d CursorHold", bufnr))
7386
cmd(string.format("autocmd! NvimTreesitterUsages_%d CursorMoved", bufnr))
7487
cmd(string.format("autocmd! NvimTreesitterUsages_%d InsertEnter", bufnr))
88+
last_nodes[bufnr] = nil
7589
end
7690

7791
return M

0 commit comments

Comments
 (0)