-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
397 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--@class avante.Range | ||
--@field start table Selection start point | ||
--@field start.line number Line number of the selection start | ||
--@field start.col number Column number of the selection start | ||
--@field finish table Selection end point | ||
--@field finish.line number Line number of the selection end | ||
--@field finish.col number Column number of the selection end | ||
local Range = {} | ||
Range.__index = Range | ||
-- Create a selection range | ||
-- @param start table Selection start point | ||
-- @param start.line number Line number of the selection start | ||
-- @param start.col number Column number of the selection start | ||
-- @param finish table Selection end point | ||
-- @param finish.line number Line number of the selection end | ||
-- @param finish.col number Column number of the selection end | ||
function Range.new(start, finish) | ||
local self = setmetatable({}, Range) | ||
self.start = start | ||
self.finish = finish | ||
return self | ||
end | ||
|
||
return Range |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
local Config = require("avante.config") | ||
|
||
local api = vim.api | ||
local fn = vim.fn | ||
|
||
local NAMESPACE = api.nvim_create_namespace("avante_selection") | ||
local PRIORITY = vim.highlight.priorities.user | ||
|
||
local Selection = {} | ||
|
||
function Selection:new() | ||
return setmetatable({ | ||
hints_popup_extmark_id = nil, | ||
edit_popup_renderer = nil, | ||
augroup = api.nvim_create_augroup("avante_selection", { clear = true }), | ||
}, { __index = self }) | ||
end | ||
|
||
function Selection:get_virt_text_line() | ||
local current_pos = fn.getpos(".") | ||
|
||
-- Get the current and start position line numbers | ||
local current_line = current_pos[2] - 1 -- 0-indexed | ||
|
||
-- Ensure line numbers are not negative and don't exceed buffer range | ||
local total_lines = api.nvim_buf_line_count(0) | ||
if current_line < 0 then | ||
current_line = 0 | ||
end | ||
if current_line >= total_lines then | ||
current_line = total_lines - 1 | ||
end | ||
|
||
-- Take the first line of the selection to ensure virt_text is always in the top right corner | ||
return current_line | ||
end | ||
|
||
function Selection:show_hints_popup() | ||
self:close_hints_popup() | ||
|
||
local hint_text = string.format(" [Ask %s] ", Config.mappings.ask) | ||
|
||
local virt_text_line = self:get_virt_text_line() | ||
|
||
self.hints_popup_extmark_id = vim.api.nvim_buf_set_extmark(0, NAMESPACE, virt_text_line, -1, { | ||
virt_text = { { hint_text, "Keyword" } }, | ||
virt_text_pos = "eol", | ||
priority = PRIORITY, | ||
}) | ||
end | ||
|
||
function Selection:close_hints_popup() | ||
if self.hints_popup_extmark_id then | ||
vim.api.nvim_buf_del_extmark(0, NAMESPACE, self.hints_popup_extmark_id) | ||
self.hints_popup_extmark_id = nil | ||
end | ||
end | ||
|
||
function Selection:setup() | ||
vim.api.nvim_create_autocmd({ "ModeChanged" }, { | ||
group = self.augroup, | ||
pattern = { "n:v", "n:V", "n:" }, -- Entering Visual mode from Normal mode | ||
callback = function() | ||
self:show_hints_popup() | ||
end, | ||
}) | ||
|
||
api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, { | ||
group = self.augroup, | ||
callback = function() | ||
if vim.fn.mode() == "v" or vim.fn.mode() == "V" or vim.fn.mode() == "" then | ||
self:show_hints_popup() | ||
else | ||
self:close_hints_popup() | ||
end | ||
end, | ||
}) | ||
|
||
api.nvim_create_autocmd({ "ModeChanged" }, { | ||
group = self.augroup, | ||
pattern = { "v:n", "v:i", "v:c" }, -- Switching from visual mode back to normal, insert, or other modes | ||
callback = function() | ||
self:close_hints_popup() | ||
end, | ||
}) | ||
end | ||
|
||
function Selection:delete_autocmds() | ||
if self.augroup then | ||
vim.api.nvim_del_augroup_by_id(self.augroup) | ||
end | ||
self.augroup = nil | ||
end | ||
|
||
return Selection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--@class avante.SelectionResult | ||
--@field content string Selected content | ||
--@field range avante.Range Selection range | ||
local SelectionResult = {} | ||
SelectionResult.__index = SelectionResult | ||
|
||
-- Create a selection content and range | ||
--@param content string Selected content | ||
--@param range avante.Range Selection range | ||
function SelectionResult.new(content, range) | ||
local self = setmetatable({}, SelectionResult) | ||
self.content = content | ||
self.range = range | ||
return self | ||
end | ||
|
||
return SelectionResult |
Oops, something went wrong.