refactor: get rid of nui-components (#118)

This commit is contained in:
yetone 2024-08-21 21:28:17 +08:00 committed by GitHub
parent b995e4aa7b
commit 2e48d387a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 1018 additions and 608 deletions

View File

@ -156,12 +156,11 @@ function M.get_window_width()
return math.ceil(vim.o.columns * (M.windows.width / 100))
end
---@return {width: integer, height: integer, position: integer}
function M.get_renderer_layout_options()
---@return {width: integer, height: integer}
function M.get_sidebar_layout_options()
local width = M.get_window_width()
local height = vim.o.lines
local position = vim.o.columns - width
return { width = width, height = height, position = position }
return { width = width, height = height }
end
return M

22
lua/avante/highlights.lua Normal file
View File

@ -0,0 +1,22 @@
local M = {
TITLE = "AvanteTitle",
REVERSED_TITLE = "AvanteReversedTitle",
SUBTITLE = "AvanteSubtitle",
REVERSED_SUBTITLE = "AvanteReversedSubtitle",
THRIDTITLE = "AvanteThirdTitle",
REVERSED_THRIDTITLE = "AvanteReversedThirdTitle",
REVERSED_NORMAL = "AvanteReversedNormal",
}
M.setup = function()
local normal = vim.api.nvim_get_hl(0, { name = "Normal" })
vim.api.nvim_set_hl(0, M.REVERSED_NORMAL, { fg = normal.bg })
vim.api.nvim_set_hl(0, M.TITLE, { fg = "#1e222a", bg = "#98c379" })
vim.api.nvim_set_hl(0, M.REVERSED_TITLE, { fg = "#98c379" })
vim.api.nvim_set_hl(0, M.SUBTITLE, { fg = "#1e222a", bg = "#56b6c2" })
vim.api.nvim_set_hl(0, M.REVERSED_SUBTITLE, { fg = "#56b6c2" })
vim.api.nvim_set_hl(0, M.THRIDTITLE, { fg = "#ABB2BF", bg = "#353B45" })
vim.api.nvim_set_hl(0, M.REVERSED_THRIDTITLE, { fg = "#353B45" })
end
return M

View File

@ -163,8 +163,8 @@ M.refresh = function()
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
local focused = sidebar.result.bufnr == curbuf or sidebar.input.bufnr == curbuf
if focused or not sidebar:is_open() then
return
end
@ -177,8 +177,8 @@ M.refresh = function()
local curwin = vim.api.nvim_get_current_win()
sidebar.code.win = curwin
sidebar.code.buf = curbuf
sidebar.code.winid = curwin
sidebar.code.bufnr = curbuf
sidebar:render()
end
@ -202,6 +202,7 @@ function M.setup(opts)
return
end
require("avante.highlights").setup()
require("avante.diff").setup()
require("avante.llm").setup()

View File

@ -376,7 +376,10 @@ end
---@type AvanteResponseParser
M.parse_claude_response = function(data_stream, event_state, opts)
if event_state == "content_block_delta" then
local json = vim.json.decode(data_stream)
local ok, json = pcall(vim.json.decode, data_stream)
if not ok then
return
end
opts.on_chunk(json.delta.text)
elseif event_state == "message_stop" then
opts.on_complete(nil)
@ -739,7 +742,7 @@ M.stream = function(question, code_lang, code_content, selected_content_content,
return active_job
end
---@private
---@public
function M.setup()
if Config.provider == "copilot" and not M.copilot then
M.copilot = {

File diff suppressed because it is too large Load Diff

View File

@ -22,6 +22,31 @@ local AvanteRenderer = require("nui-components.renderer")
---@field bufnr integer | nil
local AvanteComponent = require("nui-components.component")
---@class NuiSplit
---@field winid integer | nil
---@field bufnr integer | nil
local AvanteSplit = require("nui.split")
---@return nil
function AvanteSplit:mount() end
---@return nil
function AvanteSplit:unmount() end
---@param event string | string[]
---@param handler string | function
---@param options? table<"'once'" | "'nested'", boolean>
---@return nil
function AvanteSplit:on(event, handler, options) end
-- set keymap for this split
---@param mode string check `:h :map-modes`
---@param key string|string[] key for the mapping
---@param handler string | fun(): nil handler for the mapping
---@param opts? table<"'expr'"|"'noremap'"|"'nowait'"|"'remap'"|"'script'"|"'silent'"|"'unique'", boolean>
---@return nil
function AvanteSplit:map(mode, key, handler, opts, ___force___) end
---@param opts table<string, any>
---@return NuiRenderer
function AvanteRenderer.create(opts) end

View File

@ -212,4 +212,42 @@ function M.debug(msg, opts)
end
end
function M.tbl_indexof(tbl, value)
for i, v in ipairs(tbl) do
if v == value then
return i
end
end
return nil
end
function M.update_win_options(winid, opt_name, key, value)
local cur_opt_value = api.nvim_get_option_value(opt_name, { win = winid })
if cur_opt_value:find(key .. ":") then
cur_opt_value = cur_opt_value:gsub(key .. ":[^,]*", key .. ":" .. value)
else
if #cur_opt_value > 0 then
cur_opt_value = cur_opt_value .. ","
end
cur_opt_value = cur_opt_value .. key .. ":" .. value
end
api.nvim_set_option_value(opt_name, cur_opt_value, { win = winid })
end
function M.get_win_options(winid, opt_name, key)
local cur_opt_value = api.nvim_get_option_value(opt_name, { win = winid })
if not cur_opt_value then
return
end
local pieces = vim.split(cur_opt_value, ",")
for _, piece in ipairs(pieces) do
local kv_pair = vim.split(piece, ":")
if kv_pair[1] == key then
return kv_pair[2]
end
end
end
return M

View File

@ -1,72 +0,0 @@
local Config = require("avante.config")
local api = vim.api
---@class avante.View
---@field buf integer
---@field win integer
---@field RESULT_BUF_NAME string
local View = {}
local RESULT_BUF_NAME = "AVANTE_RESULT"
function View:new()
return setmetatable({ buf = nil, win = nil }, { __index = View })
end
---setup view buffer
---@param split_command string A split command to position the side bar to
---@param size integer a given % to resize the chat window
---@return avante.View
function View:setup(split_command, size)
-- create a scratch unlisted buffer
self.buf = api.nvim_create_buf(false, true)
-- set filetype
api.nvim_set_option_value("filetype", "Avante", { buf = self.buf })
api.nvim_set_option_value("bufhidden", "wipe", { buf = self.buf })
api.nvim_set_option_value("modifiable", false, { buf = self.buf })
api.nvim_set_option_value("swapfile", false, { buf = self.buf })
-- create a split
vim.cmd(split_command)
--get current window and attach the buffer to it
self.win = api.nvim_get_current_win()
api.nvim_win_set_buf(self.win, self.buf)
vim.cmd("vertical resize " .. size)
-- win stuff
api.nvim_set_option_value("spell", false, { win = self.win })
api.nvim_set_option_value("signcolumn", "no", { win = self.win })
api.nvim_set_option_value("foldcolumn", "0", { win = self.win })
api.nvim_set_option_value("number", false, { win = self.win })
api.nvim_set_option_value("relativenumber", false, { win = self.win })
api.nvim_set_option_value("winfixwidth", true, { win = self.win })
api.nvim_set_option_value("list", false, { win = self.win })
api.nvim_set_option_value("wrap", Config.windows.wrap_line, { win = self.win })
api.nvim_set_option_value("winhl", "", { win = self.win })
api.nvim_set_option_value("linebreak", true, { win = self.win }) -- only has effect when wrap=true
api.nvim_set_option_value("breakindent", true, { win = self.win }) -- only has effect when wrap=true
-- buffer stuff
xpcall(function()
api.nvim_buf_set_name(self.buf, RESULT_BUF_NAME)
end, function(_) end)
return self
end
function View:close()
if self.win then
api.nvim_win_close(self.win, true)
self.win = nil
self.buf = nil
end
end
function View:is_open()
return self.win and self.buf and api.nvim_buf_is_valid(self.buf) and api.nvim_win_is_valid(self.win)
end
return View