summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/refactor/highlight_definitions.lua
blob: bdbec15887b02385823d9ee7640b0892ba9e83e8 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
-- This module highlights reference usages and the corresponding
-- definition on cursor hold.

local parsers = require'nvim-treesitter.parsers'
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 = ts_utils.find_definition(node_at_point, bufnr)
  local usages = ts_utils.find_usages(node_at_point, scope)

  for _, usage_node in ipairs(usages) do
    local start_row, start_col, _, end_col = usage_node:range()

    if usage_node ~= node_at_point then
      api.nvim_buf_add_highlight(
        bufnr, 
        usage_namespace, 
        'TSDefinitionUsage', 
        start_row,
        start_col,
        end_col)
    end
  end

  if def_node ~= node_at_point then
    local start_row, start_col, _, end_col = def_node:range()

    api.nvim_buf_add_highlight(
      bufnr, 
      usage_namespace, 
      'TSDefinition', 
      start_row,
      start_col,
      end_col)
  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!'
  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))
  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