chore(types): add annotations for block and refresh on winchanged (#40)

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
Aaron Pham 2024-08-17 10:39:59 -04:00 committed by GitHub
parent 5d5ded5c55
commit 83402e4a2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 59 additions and 11 deletions

View File

@ -42,6 +42,10 @@ M.defaults = {
next = "]x",
prev = "[x",
},
jump = {
next = "]]",
prev = "[[",
},
},
windows = {
width = 30, -- default % based on available width

View File

@ -29,6 +29,7 @@ local Sidebar = {}
---@field code avante.SidebarState
---@field renderer NuiRenderer
---@field winid {result: integer, input: integer}
---@field bufnr {result: integer, input: integer}
---@param id integer the tabpage id retrieved from vim.api.nvim_get_current_tabpage()
function Sidebar:new(id)
@ -36,6 +37,7 @@ function Sidebar:new(id)
id = id,
code = { buf = 0, win = 0, selection = nil },
winid = { result = 0, input = 0 },
bufnr = { result = 0, input = 0 },
view = View:new(),
renderer = nil,
}, { __index = self })
@ -60,6 +62,7 @@ end
function Sidebar:reset()
self.code = { buf = 0, win = 0 }
self.winid = { result = 0, input = 0 }
self.bufnr = { result = 0, input = 0 }
self:delete_autocmds()
end
@ -109,15 +112,8 @@ function Sidebar:toggle()
end
end
function Sidebar:has_code_win()
return self.code.win
and self.code.buf
and self.code.win ~= 0
and self.code.buf ~= 0
and api.nvim_win_is_valid(self.code.win)
and api.nvim_buf_is_valid(self.code.buf)
end
--- Initialize the sidebar instance.
--- @return avante.Sidebar The Sidebar instance.
function Sidebar:intialize()
self.code.win = api.nvim_get_current_win()
self.code.buf = api.nvim_get_current_buf()
@ -139,6 +135,8 @@ function Sidebar:intialize()
self.renderer:on_mount(function()
self.winid.result = self.renderer:get_component_by_id("result").winid
self.winid.input = self.renderer:get_component_by_id("input").winid
self.bufnr.result = vim.api.nvim_win_get_buf(self.winid.result)
self.bufnr.input = vim.api.nvim_win_get_buf(self.winid.input)
self.augroup = api.nvim_create_augroup("avante_" .. self.id .. self.view.win, { clear = true })
local filetype = api.nvim_get_option_value("filetype", { buf = self.code.buf })
@ -207,6 +205,26 @@ function Sidebar:intialize()
self:reset()
end,
})
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
@ -239,6 +257,12 @@ function Sidebar:update_content(content, focus, callback)
return self
end
---@class AvanteCodeblock
---@field start_line integer
---@field end_line integer
---@field lang string
---@return AvanteCodeblock[]
local function parse_codeblocks(buf)
local codeblocks = {}
local in_codeblock = false
@ -527,6 +551,7 @@ function Sidebar:render()
pcall(vim.keymap.del, "n", "A", { buffer = self.view.buf })
end
---@type AvanteCodeblock[]
local codeblocks = {}
api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
@ -558,6 +583,7 @@ function Sidebar:render()
end,
})
---@type NuiSignal
local signal = N.create_signal({ is_loading = false, text = "" })
local chat_history = load_chat_history(self)

View File

@ -1,15 +1,33 @@
---@meta
---@class NuiSignalValue: boolean
local NuiSignalValue = require("nui-components.signal.value")
---@return boolean
function NuiSignalValue:negate() end
---@class NuiSignal
---@field is_loading boolean | NuiSignalValue
---@field text string
local AvanteSignal = require("nui-components.signal")
---@return any
function AvanteSignal:get_value() end
---@class NuiRenderer
_G.AvanteRenderer = require("nui-components.renderer")
local AvanteRenderer = require("nui-components.renderer")
---@class NuiComponent
_G.AvanteComponent = require("nui-components.component")
---@field winid integer | nil
local AvanteComponent = require("nui-components.component")
---@param opts table<string, any>
---@return NuiRenderer
function AvanteRenderer.create(opts) end
---@return NuiComponent[]
function AvanteRenderer:get_focusable_components() end
---@param body fun():NuiComponent
function AvanteRenderer:render(body) end