From 5faadd95425e9ed43ebba2283ad39c8265528d41 Mon Sep 17 00:00:00 2001 From: JafarAbdi Date: Sat, 26 Mar 2022 06:15:41 +0300 Subject: [PATCH 1/5] Fix a bug causing to not highlight a scope starting in the first line --- lua/nvim-treesitter-refactor/highlight_current_scope.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-treesitter-refactor/highlight_current_scope.lua b/lua/nvim-treesitter-refactor/highlight_current_scope.lua index fc26187..aa3b4cb 100644 --- a/lua/nvim-treesitter-refactor/highlight_current_scope.lua +++ b/lua/nvim-treesitter-refactor/highlight_current_scope.lua @@ -16,9 +16,9 @@ function M.highlight_current_scope(bufnr) local current_scope = locals.containing_scope(node_at_point, bufnr) if current_scope then - local start_line = current_scope:start() + local start_line, _, end_line, _ = current_scope:range() - if start_line ~= 0 then + if start_line ~= 0 or end_line ~= vim.fn.line("$") then ts_utils.highlight_node(current_scope, bufnr, current_scope_namespace, "TSCurrentScope") end end From 92813bc4bc0bd9a760989bfefa2796831036e752 Mon Sep 17 00:00:00 2001 From: JafarAbdi Date: Sat, 26 Mar 2022 07:16:03 +0300 Subject: [PATCH 2/5] Add option to highlight the whole line --- lua/nvim-treesitter-refactor.lua | 1 + .../highlight_current_scope.lua | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lua/nvim-treesitter-refactor.lua b/lua/nvim-treesitter-refactor.lua index e51f320..38cb36f 100644 --- a/lua/nvim-treesitter-refactor.lua +++ b/lua/nvim-treesitter-refactor.lua @@ -17,6 +17,7 @@ function M.init() enable = false, disable = {}, is_supported = queries.has_locals, + highlight_eol = false, }, smart_rename = { module_path = "nvim-treesitter-refactor.smart_rename", diff --git a/lua/nvim-treesitter-refactor/highlight_current_scope.lua b/lua/nvim-treesitter-refactor/highlight_current_scope.lua index aa3b4cb..f2103a6 100644 --- a/lua/nvim-treesitter-refactor/highlight_current_scope.lua +++ b/lua/nvim-treesitter-refactor/highlight_current_scope.lua @@ -1,5 +1,6 @@ -- This module highlights the current scope of at the cursor position +local configs = require "nvim-treesitter.configs" local ts_utils = require "nvim-treesitter.ts_utils" local locals = require "nvim-treesitter.locals" local api = vim.api @@ -16,10 +17,21 @@ function M.highlight_current_scope(bufnr) local current_scope = locals.containing_scope(node_at_point, bufnr) if current_scope then + -- Highlight range [start_line, end_line) 0-based + local highlighter = function(start_line, end_line) + local config = configs.get_module "refactor.highlight_current_scope" + vim.api.nvim_buf_set_extmark(bufnr, current_scope_namespace, math.max(vim.fn.line("w0") - 1, start_line), 0, { + end_row = math.min(vim.fn.line("w$"), end_line), + end_col = 0, + hl_group = "TSCurrentScope", + hl_eol = config.highlight_eol, + }) + end local start_line, _, end_line, _ = current_scope:range() if start_line ~= 0 or end_line ~= vim.fn.line("$") then - ts_utils.highlight_node(current_scope, bufnr, current_scope_namespace, "TSCurrentScope") + highlighter(start_line, vim.fn.line(".") - 1) + highlighter(vim.fn.line("."), end_line + 1) end end end From b5b8c80b16ebf309d4485e169b0cfcdfee5b9002 Mon Sep 17 00:00:00 2001 From: JafarAbdi Date: Sat, 26 Mar 2022 07:16:24 +0300 Subject: [PATCH 3/5] Highlight on win scroll --- lua/nvim-treesitter-refactor/highlight_current_scope.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-treesitter-refactor/highlight_current_scope.lua b/lua/nvim-treesitter-refactor/highlight_current_scope.lua index f2103a6..9ad7302 100644 --- a/lua/nvim-treesitter-refactor/highlight_current_scope.lua +++ b/lua/nvim-treesitter-refactor/highlight_current_scope.lua @@ -46,7 +46,7 @@ function M.attach(bufnr) -- luacheck: push ignore 631 cmd( string.format( - [[autocmd CursorMoved lua require'nvim-treesitter-refactor.highlight_current_scope'.highlight_current_scope(%d)]], + [[autocmd CursorMoved,WinScrolled lua require'nvim-treesitter-refactor.highlight_current_scope'.highlight_current_scope(%d)]], bufnr, bufnr ) From 1f4f0ee1c821190d1f1a6098ffefa382294c16af Mon Sep 17 00:00:00 2001 From: JafarAbdi Date: Sat, 26 Mar 2022 07:30:24 +0300 Subject: [PATCH 4/5] Add option to highlight cursor in case cursorlineopt is set to only number --- lua/nvim-treesitter-refactor.lua | 1 + lua/nvim-treesitter-refactor/highlight_current_scope.lua | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lua/nvim-treesitter-refactor.lua b/lua/nvim-treesitter-refactor.lua index 38cb36f..697ebfa 100644 --- a/lua/nvim-treesitter-refactor.lua +++ b/lua/nvim-treesitter-refactor.lua @@ -18,6 +18,7 @@ function M.init() disable = {}, is_supported = queries.has_locals, highlight_eol = false, + highlight_cursor = false, }, smart_rename = { module_path = "nvim-treesitter-refactor.smart_rename", diff --git a/lua/nvim-treesitter-refactor/highlight_current_scope.lua b/lua/nvim-treesitter-refactor/highlight_current_scope.lua index 9ad7302..53bd531 100644 --- a/lua/nvim-treesitter-refactor/highlight_current_scope.lua +++ b/lua/nvim-treesitter-refactor/highlight_current_scope.lua @@ -17,9 +17,9 @@ function M.highlight_current_scope(bufnr) local current_scope = locals.containing_scope(node_at_point, bufnr) if current_scope then + local config = configs.get_module "refactor.highlight_current_scope" -- Highlight range [start_line, end_line) 0-based local highlighter = function(start_line, end_line) - local config = configs.get_module "refactor.highlight_current_scope" vim.api.nvim_buf_set_extmark(bufnr, current_scope_namespace, math.max(vim.fn.line("w0") - 1, start_line), 0, { end_row = math.min(vim.fn.line("w$"), end_line), end_col = 0, @@ -30,7 +30,7 @@ function M.highlight_current_scope(bufnr) local start_line, _, end_line, _ = current_scope:range() if start_line ~= 0 or end_line ~= vim.fn.line("$") then - highlighter(start_line, vim.fn.line(".") - 1) + highlighter(start_line, vim.fn.line(".") + (config.highlight_cursor and 0 or -1)) highlighter(vim.fn.line("."), end_line + 1) end end From 938bdff1d35df62577b4b0da102fc99f50524cde Mon Sep 17 00:00:00 2001 From: JafarAbdi Date: Sat, 14 May 2022 19:19:18 +0300 Subject: [PATCH 5/5] Fix stylua --- .../highlight_current_scope.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lua/nvim-treesitter-refactor/highlight_current_scope.lua b/lua/nvim-treesitter-refactor/highlight_current_scope.lua index 53bd531..8904eba 100644 --- a/lua/nvim-treesitter-refactor/highlight_current_scope.lua +++ b/lua/nvim-treesitter-refactor/highlight_current_scope.lua @@ -20,8 +20,8 @@ function M.highlight_current_scope(bufnr) local config = configs.get_module "refactor.highlight_current_scope" -- Highlight range [start_line, end_line) 0-based local highlighter = function(start_line, end_line) - vim.api.nvim_buf_set_extmark(bufnr, current_scope_namespace, math.max(vim.fn.line("w0") - 1, start_line), 0, { - end_row = math.min(vim.fn.line("w$"), end_line), + vim.api.nvim_buf_set_extmark(bufnr, current_scope_namespace, math.max(vim.fn.line "w0" - 1, start_line), 0, { + end_row = math.min(vim.fn.line "w$", end_line), end_col = 0, hl_group = "TSCurrentScope", hl_eol = config.highlight_eol, @@ -29,9 +29,9 @@ function M.highlight_current_scope(bufnr) end local start_line, _, end_line, _ = current_scope:range() - if start_line ~= 0 or end_line ~= vim.fn.line("$") then - highlighter(start_line, vim.fn.line(".") + (config.highlight_cursor and 0 or -1)) - highlighter(vim.fn.line("."), end_line + 1) + if start_line ~= 0 or end_line ~= vim.fn.line "$" then + highlighter(start_line, vim.fn.line "." + (config.highlight_cursor and 0 or -1)) + highlighter(vim.fn.line ".", end_line + 1) end end end