summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephan Seitz <stephan.seitz@fau.de>2020-07-12 16:11:31 +0200
committerThomas Vigouroux <39092278+vigoux@users.noreply.github.com>2020-07-13 22:26:17 +0200
commitee80e1ebc51f4e627e1d3c4a320e846baf343184 (patch)
tree30a1db3e880c6d6ab13c54ed81db591b616eac31
parentAdd ts_utils.highlight_node (diff)
downloadnvim-treesitter-ee80e1ebc51f4e627e1d3c4a320e846baf343184.tar
nvim-treesitter-ee80e1ebc51f4e627e1d3c4a320e846baf343184.tar.gz
nvim-treesitter-ee80e1ebc51f4e627e1d3c4a320e846baf343184.tar.bz2
nvim-treesitter-ee80e1ebc51f4e627e1d3c4a320e846baf343184.tar.lz
nvim-treesitter-ee80e1ebc51f4e627e1d3c4a320e846baf343184.tar.xz
nvim-treesitter-ee80e1ebc51f4e627e1d3c4a320e846baf343184.tar.zst
nvim-treesitter-ee80e1ebc51f4e627e1d3c4a320e846baf343184.zip
Add module refactor.highlight_current_scope
-rw-r--r--README.md3
-rw-r--r--lua/nvim-treesitter/configs.lua6
-rw-r--r--lua/nvim-treesitter/refactor/highlight_current_scope.lua46
-rw-r--r--plugin/nvim-treesitter.vim1
4 files changed, 56 insertions, 0 deletions
diff --git a/README.md b/README.md
index 04d72554d..8a9d3d512 100644
--- a/README.md
+++ b/README.md
@@ -116,6 +116,9 @@ require'nvim-treesitter.configs'.setup {
highlight_definitions = {
enable = true
},
+ highlight_current_scope = {
+ enable = true
+ },
smart_rename = {
enable = true,
keymaps = {
diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua
index abf739dd9..abed55a7d 100644
--- a/lua/nvim-treesitter/configs.lua
+++ b/lua/nvim-treesitter/configs.lua
@@ -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,
diff --git a/lua/nvim-treesitter/refactor/highlight_current_scope.lua b/lua/nvim-treesitter/refactor/highlight_current_scope.lua
new file mode 100644
index 000000000..b32ccaf3b
--- /dev/null
+++ b/lua/nvim-treesitter/refactor/highlight_current_scope.lua
@@ -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
diff --git a/plugin/nvim-treesitter.vim b/plugin/nvim-treesitter.vim
index 2bebbba99..90f997eb1 100644
--- a/plugin/nvim-treesitter.vim
+++ b/plugin/nvim-treesitter.vim
@@ -60,3 +60,4 @@ highlight default link TSInclude Include
highlight default link TSDefinitionUsage Visual
highlight default link TSDefinition Search
+highlight default link TSCurrentScope CursorLine