Skip to content

Commit

Permalink
Hl Usages: don't calculate definitions again if on the same node (#22)
Browse files Browse the repository at this point in the history
Closes #21
  • Loading branch information
stsewd authored Jan 22, 2022
1 parent 7470880 commit 0dc8069
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ Highlights definition and usages of the current symbol under the cursor.
lua <<EOF
require'nvim-treesitter.configs'.setup {
refactor = {
highlight_definitions = { enable = true },
highlight_definitions = {
enable = true,
-- Set to false if you have an `updatetime` of ~100.
clear_on_cursor_move = true,
},
},
}
EOF
Expand Down
9 changes: 8 additions & 1 deletion doc/nvim-treesitter-refactor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ Query files: `locals.scm`.
Supported options:
- enable: `true` or `false`.
- disable: list of languages.
- clear_on_cursor_move: `true` or `false` if highlights should be cleared
when the cursor is moved. If your 'updatetime' is around `100`
you can set this to false to have a less laggy experience.

>
lua <<EOF
require'nvim-treesitter.configs'.setup {
refactor = {
highlight_definitions = { enable = true },
highlight_definitions = {
enable = true,
-- Set to false if you have an `updatetime` of ~100.
clear_on_cursor_move = true,
},
},
}
EOF
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-treesitter-refactor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function M.init()
enable = false,
disable = {},
is_supported = queries.has_locals,
clear_on_cursor_move = true,
},
highlight_current_scope = {
module_path = "nvim-treesitter-refactor.highlight_current_scope",
Expand Down
34 changes: 24 additions & 10 deletions lua/nvim-treesitter-refactor/highlight_definitions.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- This module highlights reference usages and the corresponding
-- definition on cursor hold.

local configs = require "nvim-treesitter.configs"
local ts_utils = require "nvim-treesitter.ts_utils"
local locals = require "nvim-treesitter.locals"
local api = vim.api
Expand All @@ -9,14 +10,19 @@ local cmd = api.nvim_command
local M = {}

local usage_namespace = api.nvim_create_namespace "nvim-treesitter-usages"
local last_nodes = {}

function M.highlight_usages(bufnr)
M.clear_usage_highlights(bufnr)

local node_at_point = ts_utils.get_node_at_cursor()
local references = locals.get_references(bufnr)
-- Don't calculate usages again if we are on the same node.
if node_at_point and node_at_point == last_nodes[bufnr] and M.has_highlights(bufnr) then
return
else
last_nodes[bufnr] = node_at_point
end

if not node_at_point or not vim.tbl_contains(references, node_at_point) then
M.clear_usage_highlights(bufnr)
if not node_at_point then
return
end

Expand All @@ -34,11 +40,16 @@ function M.highlight_usages(bufnr)
end
end

function M.has_highlights(bufnr)
return #api.nvim_buf_get_extmarks(bufnr, usage_namespace, 0, -1, {}) > 0
end

function M.clear_usage_highlights(bufnr)
api.nvim_buf_clear_namespace(bufnr, usage_namespace, 0, -1)
end

function M.attach(bufnr)
local config = configs.get_module "refactor.highlight_definitions"
cmd(string.format("augroup NvimTreesitterUsages_%d", bufnr))
cmd "au!"
-- luacheck: push ignore 631
Expand All @@ -49,13 +60,15 @@ function M.attach(bufnr)
bufnr
)
)
cmd(
string.format(
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-refactor.highlight_definitions'.clear_usage_highlights(%d)]],
bufnr,
bufnr
if config.clear_on_cursor_move then
cmd(
string.format(
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-refactor.highlight_definitions'.clear_usage_highlights(%d)]],
bufnr,
bufnr
)
)
)
end
cmd(
string.format(
[[autocmd InsertEnter <buffer=%d> lua require'nvim-treesitter-refactor.highlight_definitions'.clear_usage_highlights(%d)]],
Expand All @@ -72,6 +85,7 @@ function M.detach(bufnr)
cmd(string.format("autocmd! NvimTreesitterUsages_%d CursorHold", bufnr))
cmd(string.format("autocmd! NvimTreesitterUsages_%d CursorMoved", bufnr))
cmd(string.format("autocmd! NvimTreesitterUsages_%d InsertEnter", bufnr))
last_nodes[bufnr] = nil
end

return M

0 comments on commit 0dc8069

Please sign in to comment.