mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
Add module refactor.highlight_current_scope
This commit is contained in:
parent
8dfe085c41
commit
ee80e1ebc5
4 changed files with 56 additions and 0 deletions
|
|
@ -116,6 +116,9 @@ require'nvim-treesitter.configs'.setup {
|
|||
highlight_definitions = {
|
||||
enable = true
|
||||
},
|
||||
highlight_current_scope = {
|
||||
enable = true
|
||||
},
|
||||
smart_rename = {
|
||||
enable = true,
|
||||
keymaps = {
|
||||
|
|
|
|||
|
|
@ -40,6 +40,12 @@ local builtin_modules = {
|
|||
disable = {},
|
||||
is_supported = queries.has_locals
|
||||
},
|
||||
highlight_current_scope = {
|
||||
module_path = 'nvim-treesitter.refactor.highlight_current_scope',
|
||||
enable = false,
|
||||
disable = {},
|
||||
is_supported = queries.has_locals,
|
||||
},
|
||||
smart_rename = {
|
||||
module_path = 'nvim-treesitter.refactor.smart_rename',
|
||||
enable = false,
|
||||
|
|
|
|||
46
lua/nvim-treesitter/refactor/highlight_current_scope.lua
Normal file
46
lua/nvim-treesitter/refactor/highlight_current_scope.lua
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
-- This module highlights the current scope of at the cursor position
|
||||
|
||||
local ts_utils = require'nvim-treesitter.ts_utils'
|
||||
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 = ts_utils.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
|
||||
|
|
@ -60,3 +60,4 @@ highlight default link TSInclude Include
|
|||
highlight default link TSDefinitionUsage Visual
|
||||
highlight default link TSDefinition Search
|
||||
|
||||
highlight default link TSCurrentScope CursorLine
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue