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
|
-- This module highlights the current scope of at the cursor position
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 current_scope_namespace = api.nvim_create_namespace('nvim-treesitter-current-scope')
function M.highlight_current_scope(bufnr)
M.clear_highlights(bufnr)
local node_at_point = ts_utils.get_node_at_cursor()
local current_scope = locals.containing_scope(node_at_point, bufnr)
local start_line = current_scope:start()
if current_scope and start_line ~= 0 then
ts_utils.highlight_node(current_scope, bufnr, current_scope_namespace, 'TSCurrentScope')
end
end
function M.clear_highlights(bufnr)
api.nvim_buf_clear_namespace(bufnr, current_scope_namespace, 0, -1)
end
function M.attach(bufnr)
local bufnr = bufnr or api.nvim_get_current_buf()
cmd(string.format('augroup NvimTreesitterCurrentScope_%d', bufnr))
cmd 'au!'
-- luacheck: push ignore 631
cmd(string.format([[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter.refactor.highlight_current_scope'.highlight_current_scope(%d)]], bufnr, bufnr))
cmd(string.format([[autocmd BufLeave <buffer=%d> lua require'nvim-treesitter.refactor.highlight_current_scope'.clear_highlights(%d)]], bufnr, bufnr))
-- luacheck: pop
cmd 'augroup END'
end
function M.detach(bufnr)
M.clear_usage_highlights(bufnr)
cmd(string.format('autocmd! NvimTreesitterCurrentScope_%d CursorHold', bufnr))
cmd(string.format('autocmd! NvimTreesitterCurrentScope_%d BufLeave', bufnr))
end
return M
|