Skip to content

Commit

Permalink
feat: highlight selected code (yetone#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone authored Aug 27, 2024
1 parent d7be4a5 commit 37f0cf1
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lua/avante/selection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local api = vim.api
local fn = vim.fn

local NAMESPACE = api.nvim_create_namespace("avante_selection")
local SELECTED_CODE_NAMESPACE = api.nvim_create_namespace("avante_selected_code")
local PRIORITY = vim.highlight.priorities.user

local EIDTING_INPUT_START_SPINNER_PATTERN = "AvanteEditingInputStartSpinner"
Expand All @@ -16,6 +17,7 @@ local EIDTING_INPUT_STOP_SPINNER_PATTERN = "AvanteEditingInputStopSpinner"
---@field selection avante.SelectionResult | nil
---@field cursor_pos table | nil
---@field shortcuts_extmark_id integer | nil
---@field selected_code_extmark_id integer | nil
---@field augroup integer | nil
---@field editing_input_bufnr integer | nil
---@field editing_input_winid integer | nil
Expand All @@ -29,6 +31,7 @@ Selection.did_setup = false
function Selection:new(id)
return setmetatable({
shortcuts_extmark_id = nil,
selected_code_extmark_id = nil,
augroup = api.nvim_create_augroup("avante_selection_" .. id, { clear = true }),
selection = nil,
cursor_pos = nil,
Expand Down Expand Up @@ -89,6 +92,14 @@ function Selection:close_editing_input()
api.nvim_win_close(self.editing_input_winid, true)
self.editing_input_winid = nil
end
if self.code_winid and api.nvim_win_is_valid(self.code_winid) then
local code_bufnr = api.nvim_win_get_buf(self.code_winid)
api.nvim_buf_clear_namespace(code_bufnr, SELECTED_CODE_NAMESPACE, 0, -1)
if self.selected_code_extmark_id then
api.nvim_buf_del_extmark(code_bufnr, SELECTED_CODE_NAMESPACE, self.selected_code_extmark_id)
self.selected_code_extmark_id = nil
end
end
if self.cursor_pos and self.code_winid then
vim.schedule(function()
api.nvim_win_set_cursor(self.code_winid, { self.cursor_pos[1], self.cursor_pos[2] })
Expand Down Expand Up @@ -257,6 +268,8 @@ function Selection:show_editing_input_shortcuts_hints()
end

function Selection:create_editing_input()
self:close_editing_input()

local code_bufnr = api.nvim_get_current_buf()
local code_wind = api.nvim_get_current_win()
self.cursor_pos = api.nvim_win_get_cursor(code_wind)
Expand All @@ -267,7 +280,19 @@ function Selection:create_editing_input()

self.selection = Utils.get_visual_selection_and_range()

self:close_editing_input()
self.selected_code_extmark_id = api.nvim_buf_set_extmark(
code_bufnr,
SELECTED_CODE_NAMESPACE,
self.selection.range.start.line - 1,
self.selection.range.start.col - 1,
{
hl_group = "Visual",
hl_mode = "combine",
end_row = self.selection.range.finish.line - 1,
end_col = self.selection.range.finish.col,
priority = PRIORITY,
}
)

local bufnr = api.nvim_create_buf(false, true)

Expand Down

0 comments on commit 37f0cf1

Please sign in to comment.