Skip to content

Commit

Permalink
chore(cmd): add manual refresh and update types (#44)
Browse files Browse the repository at this point in the history
* chore(cmd): add manual refresh and update types

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>

* chore: add refresh mapping

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>

---------

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
  • Loading branch information
aarnphm authored Aug 17, 2024
1 parent d0d4f8a commit 95b42e8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 38 deletions.
1 change: 1 addition & 0 deletions lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ M.defaults = {
mappings = {
ask = "<leader>aa",
edit = "<leader>ae",
refresh = "<leader>ar",
diff = {
ours = "co",
theirs = "ct",
Expand Down
30 changes: 30 additions & 0 deletions lua/avante/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ H.commands = function()
end
sidebar:close()
end, { desc = "avante: close chat window" })
cmd("Refresh", function()
M.refresh()
end, { desc = "avante: refresh windows" })
end

H.keymaps = function()
vim.keymap.set({ "n", "v" }, Config.mappings.ask, M.toggle, { noremap = true })
vim.keymap.set("n", Config.mappings.refresh, M.refresh, { noremap = true })
end

H.autocmds = function()
Expand Down Expand Up @@ -123,6 +127,32 @@ M.toggle = function()
return sidebar:toggle()
end

M.refresh = function()
local sidebar = M._get()
if not sidebar then
return
end
local curbuf = vim.api.nvim_get_current_buf()

local focused = sidebar.view.buf == curbuf or sidebar.bufnr.result == curbuf or sidebar.bufnr.input == curbuf
if focused or not sidebar.view:is_open() then
return
end

local ft = vim.api.nvim_get_option_value("filetype", { buf = curbuf })
local listed = vim.api.nvim_get_option_value("buflisted", { buf = curbuf })

if ft == "Avante" or not listed then
return
end

local curwin = vim.api.nvim_get_current_win()

sidebar.code.win = curwin
sidebar.code.buf = curbuf
sidebar:render()
end

---@param opts? avante.Config
function M.setup(opts)
---PERF: we can still allow running require("avante").setup() multiple times to override config if users wish to
Expand Down
25 changes: 11 additions & 14 deletions lua/avante/range.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
--@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
---@class avante.Range
---@field start avante.RangeSelection start point
---@field finish avante.RangeSelection Selection end point
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

---@class avante.RangeSelection: table<string, integer>
---@field line number
---@field col number

---Create a selection range
---@param start avante.RangeSelection Selection start point
---@param finish avante.RangeSelection Selection end point
function Range.new(start, finish)
local self = setmetatable({}, Range)
self.start = start
Expand Down
10 changes: 5 additions & 5 deletions lua/avante/selection_result.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
--@class avante.SelectionResult
--@field content string Selected content
--@field range avante.Range Selection range
---@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
---@param content string Selected content
---@param range avante.Range Selection range
function SelectionResult.new(content, range)
local self = setmetatable({}, SelectionResult)
self.content = content
Expand Down
20 changes: 1 addition & 19 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function Sidebar:intialize()

local filetype = api.nvim_get_option_value("filetype", { buf = self.code.buf })
local selected_code_buf = self.renderer:get_component_by_id("selected_code").bufnr
api.nvim_buf_set_option(selected_code_buf, "filetype", filetype)
api.nvim_set_option_value("filetype", filetype, { buf = selected_code_buf })
api.nvim_set_option_value("wrap", false, { win = self.renderer:get_component_by_id("selected_code").winid })

api.nvim_create_autocmd("BufEnter", {
Expand Down Expand Up @@ -209,24 +209,6 @@ function Sidebar:intialize()
return self
end

function Sidebar:refresh()
local buf = vim.api.nvim_get_current_buf()

local focused = self.view.buf == buf or self.bufnr.result == buf or self.bufnr.input == buf
if focused or not self.view:is_open() then
return
end

local ft = vim.api.nvim_get_option_value("filetype", { buf = buf })
local listed = vim.api.nvim_get_option_value("buflisted", { buf = buf })

if ft == "Avante" or not listed then
return
end

return self
end

---@param content string concatenated content of the buffer
---@param focus? boolean whether to focus the result view
function Sidebar:update_content(content, focus, callback)
Expand Down

0 comments on commit 95b42e8

Please sign in to comment.