summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/refactor/highlight_definitions.lua
blob: 8a75980d667745d9d2c25d4534cc1217d8512e60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- This module highlights reference usages and the corresponding
-- definition on cursor hold.

local ts_utils = require'nvim-treesitter.ts_utils'
local locals = require'nvim-treesitter.locals'
local api = vim.api
local cmd = api.nvim_command

local M = {}

local usage_namespace = api.nvim_create_namespace('nvim-treesitter-usages')

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)

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

  local def_node, scope = locals.find_definition(node_at_point, bufnr)
  local usages = locals.find_usages(def_node, scope, bufnr)

  for _, usage_node in ipairs(usages) do
    if usage_node ~= node_at_point then
      ts_utils.highlight_node(usage_node, bufnr, usage_namespace, 'TSDefinitionUsage')
    end
  end

  if def_node ~= node_at_point then
    ts_utils.highlight_node(def_node, bufnr, usage_namespace, 'TSDefinition')
  end
end

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

function M.attach(bufnr)
  local bufnr = bufnr or api.nvim_get_current_buf()

  cmd(string.format('augroup NvimTreesitterUsages_%d', bufnr))
  cmd 'au!'
  -- luacheck: push ignore 631
  cmd(string.format([[autocmd CursorHold <buffer=%d> lua require'nvim-treesitter.refactor.highlight_definitions'.highlight_usages(%d)]], bufnr, bufnr))
  cmd(string.format([[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter.refactor.highlight_definitions'.clear_usage_highlights(%d)]], bufnr, bufnr))
  cmd(string.format([[autocmd InsertEnter <buffer=%d> lua require'nvim-treesitter.refactor.highlight_definitions'.clear_usage_highlights(%d)]], bufnr, bufnr))
  -- luacheck: pop
  cmd 'augroup END'
end

function M.detach(bufnr)
  M.clear_usage_highlights(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))
end

return M