From ff887af901420493d2a7f8cdd0ca974ccdaf8b63 Mon Sep 17 00:00:00 2001 From: tang-hi Date: Sat, 31 Aug 2024 19:21:54 +0800 Subject: [PATCH] feat: add horizontal layout --- lua/avante/config.lua | 3 +++ lua/avante/sidebar.lua | 61 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/lua/avante/config.lua b/lua/avante/config.lua index 4f57fbc..d90623e 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -18,6 +18,8 @@ M.defaults = { -- 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. tokenizer = "tiktoken", + ---@type "vertical" | "horizontal" + layout = "vertical", ---@type AvanteSupportedProvider openai = { endpoint = "https://api.openai.com/v1", @@ -137,6 +139,7 @@ M.defaults = { windows = { wrap = true, -- similar to vim.o.wrap width = 30, -- default % based on available width + height = 30, -- default % based on available height sidebar_header = { align = "center", -- left, center, right for title rounded = true, diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 23a675c..56c7010 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -1374,6 +1374,26 @@ function Sidebar:create_input() end end + local get_position = function() + if Config.layout == "vertical" then + return "bottom" + end + return "right" + end + + local get_size = function() + if Config.layout == "vertical" then + return { + height = 8, + } + end + + return { + width = "40%", + height = api.nvim_win_get_height(self.result.winid), + } + end + self.input = Split({ enter = false, relative = { @@ -1381,10 +1401,8 @@ function Sidebar:create_input() winid = self.result.winid, }, win_options = vim.tbl_deep_extend("force", get_win_options(), { signcolumn = "yes" }), - position = "bottom", - size = { - height = 8, - }, + position = get_position(), + size = get_size(), }) local function on_submit() @@ -1567,12 +1585,39 @@ function Sidebar:render() local chat_history = History.load(self.code.bufnr) local sidebar_height = api.nvim_win_get_height(self.code.winid) - local selected_code_size = self:get_selected_code_size() + local get_position = function() + if Config.layout == "vertical" then + return "right" + end + return "bottom" + end + + local get_height = function() + local selected_code_size = self:get_selected_code_size() + vim.print(selected_code_size) + if Config.layout == "horizontal" then + return math.floor(Config.windows.height / 100 * api.nvim_win_get_height(self.code.winid)) + end + + if Config.layout == "vertical" then + return math.max(1, api.nvim_win_get_height(self.code.winid) - selected_code_size - 3 - 8) + end + end + + local get_width = function() + if Config.layout == "vertical" then + return math.floor(Config.windows.width / 100 * api.nvim_win_get_width(self.code.winid)) + end + + if Config.layout == "horizontal" then + return math.max(1, api.nvim_win_get_width(self.code.winid)) + end + end self.result = Split({ enter = false, relative = "editor", - position = "right", + position = get_position(), buf_options = vim.tbl_deep_extend("force", buf_options, { modifiable = false, swapfile = false, @@ -1582,8 +1627,8 @@ function Sidebar:render() }), win_options = get_win_options(), size = { - width = string.format("%d%%", Config.windows.width), - height = math.max(1, sidebar_height - selected_code_size - 3 - 8), + width = get_width(), + height = get_height(), }, })