feat: support windows position (#436)

This commit is contained in:
yetone 2024-09-01 15:52:16 +08:00 committed by GitHub
parent 77d344db77
commit d9aa7c78af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 17 deletions

View File

@ -18,8 +18,6 @@ M.defaults = {
-- For most providers that we support we will determine this automatically. -- For most providers that we support we will determine this automatically.
-- If you wish to use a given implementation, then you can override it here. -- If you wish to use a given implementation, then you can override it here.
tokenizer = "tiktoken", tokenizer = "tiktoken",
---@type "vertical" | "horizontal"
layout = "vertical",
---@type AvanteSupportedProvider ---@type AvanteSupportedProvider
openai = { openai = {
endpoint = "https://api.openai.com/v1", endpoint = "https://api.openai.com/v1",
@ -137,6 +135,8 @@ M.defaults = {
}, },
}, },
windows = { windows = {
---@type "right" | "left" | "top" | "bottom"
position = "right",
wrap = true, -- similar to vim.o.wrap wrap = true, -- similar to vim.o.wrap
width = 30, -- default % based on available width in vertical layout width = 30, -- default % based on available width in vertical layout
height = 30, -- default % based on available height in horizontal layout height = 30, -- default % based on available height in horizontal layout

View File

@ -1076,6 +1076,13 @@ local function get_chat_record_prefix(timestamp, provider, model, request)
.. "\n\n" .. "\n\n"
end end
function Sidebar:get_layout()
if Config.windows.position == "left" or Config.windows.position == "right" then
return "vertical"
end
return "horizontal"
end
function Sidebar:update_content_with_history(history) function Sidebar:update_content_with_history(history)
local content = "" local content = ""
for idx, entry in ipairs(history) do for idx, entry in ipairs(history) do
@ -1217,7 +1224,7 @@ function Sidebar:create_selected_code()
}, },
}) })
self.selected_code:mount() self.selected_code:mount()
if Config.layout == "horizontal" then if self:get_layout() == "horizontal" then
api.nvim_win_set_height(self.result.winid, api.nvim_win_get_height(self.result.winid) - selected_code_size - 3) api.nvim_win_set_height(self.result.winid, api.nvim_win_get_height(self.result.winid) - selected_code_size - 3)
end end
end end
@ -1370,14 +1377,14 @@ function Sidebar:create_input()
end end
local get_position = function() local get_position = function()
if Config.layout == "vertical" then if self:get_layout() == "vertical" then
return "bottom" return "bottom"
end end
return "right" return "right"
end end
local get_size = function() local get_size = function()
if Config.layout == "vertical" then if self:get_layout() == "vertical" then
return { return {
height = 8, height = 8,
} }
@ -1582,32 +1589,25 @@ function Sidebar:render()
local chat_history = Path.history.load(self.code.bufnr) local chat_history = Path.history.load(self.code.bufnr)
local get_position = function() local get_position = function()
if Config.layout == "vertical" then return Config.windows.position
return "right"
end
return "bottom"
end end
local get_height = function() local get_height = function()
local selected_code_size = self:get_selected_code_size() local selected_code_size = self:get_selected_code_size()
if Config.layout == "horizontal" then if self:get_layout() == "horizontal" then
return math.floor(Config.windows.height / 100 * api.nvim_win_get_height(self.code.winid)) return math.floor(Config.windows.height / 100 * api.nvim_win_get_height(self.code.winid))
end end
if Config.layout == "vertical" then return math.max(1, api.nvim_win_get_height(self.code.winid) - selected_code_size - 3 - 8)
return math.max(1, api.nvim_win_get_height(self.code.winid) - selected_code_size - 3 - 8)
end
end end
local get_width = function() local get_width = function()
if Config.layout == "vertical" then if self:get_layout() == "vertical" then
return math.floor(Config.windows.width / 100 * api.nvim_win_get_width(self.code.winid)) return math.floor(Config.windows.width / 100 * api.nvim_win_get_width(self.code.winid))
end end
if Config.layout == "horizontal" then return math.max(1, api.nvim_win_get_width(self.code.winid))
return math.max(1, api.nvim_win_get_width(self.code.winid))
end
end end
self.result = Split({ self.result = Split({