Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to apply the current scope highlight to the whole line #31

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lua/nvim-treesitter-refactor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function M.init()
enable = false,
disable = {},
is_supported = queries.has_locals,
highlight_eol = false,
highlight_cursor = false,
},
smart_rename = {
module_path = "nvim-treesitter-refactor.smart_rename",
Expand Down
20 changes: 16 additions & 4 deletions lua/nvim-treesitter-refactor/highlight_current_scope.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,10 +17,21 @@ 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 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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't use functions that assume the current buffer for a function that explicitly takes a bufnr as an argument

end_col = 0,
hl_group = "TSCurrentScope",
hl_eol = config.highlight_eol,
})
end
local start_line, _, end_line, _ = current_scope:range()

if start_line ~= 0 then
ts_utils.highlight_node(current_scope, bufnr, current_scope_namespace, "TSCurrentScope")
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
Expand All @@ -34,7 +46,7 @@ function M.attach(bufnr)
-- luacheck: push ignore 631
cmd(
string.format(
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-refactor.highlight_current_scope'.highlight_current_scope(%d)]],
[[autocmd CursorMoved,WinScrolled <buffer=%d> lua require'nvim-treesitter-refactor.highlight_current_scope'.highlight_current_scope(%d)]],
bufnr,
bufnr
)
Expand Down