Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ui): bounding popover #13

Merged
merged 11 commits into from
Aug 16, 2024
9 changes: 0 additions & 9 deletions .luarc.json

This file was deleted.

156 changes: 95 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@

https://github.com/user-attachments/assets/510e6270-b6cf-459d-9a2f-15b397d1fe53



https://github.com/user-attachments/assets/86140bfd-08b4-483d-a887-1b701d9e37dd



## Features

- **AI-Powered Code Assistance**: Interact with AI to ask questions about your current code file and receive intelligent suggestions for improvement or modification.
Expand All @@ -22,74 +18,89 @@ https://github.com/user-attachments/assets/86140bfd-08b4-483d-a887-1b701d9e37dd

Install `avante.nvim` using [lazy.nvim](https://github.com/folke/lazy.nvim):

```lua
{
"yetone/avante.nvim",
event = "VeryLazy",
opts = {},
build = "make",
dependencies = {
"nvim-tree/nvim-web-devicons",
{
"grapp-dev/nui-components.nvim",
dependencies = {
"MunifTanjim/nui.nvim"
}
},
"nvim-lua/plenary.nvim",
"MeanderingProgrammer/render-markdown.nvim",
```lua
{
"yetone/avante.nvim",
event = "VeryLazy",
opts = {},
build = "make",
dependencies = {
"nvim-tree/nvim-web-devicons",
{
"grapp-dev/nui-components.nvim",
dependencies = {
"MunifTanjim/nui.nvim"
}
},
}
```
"nvim-lua/plenary.nvim",
"MeanderingProgrammer/render-markdown.nvim", -- optional
},
}
```

> [!IMPORTANT]
>
> If your neovim doesn't use LuaJIT, then change `build` to `make lua51`. By default running make will install luajit.
> For ARM-based setup, make sure to also install cargo as we will have to build the tiktoken_core from source.

> [!note] `render-markdown.nvim`
>
> `render-markdown.nvim` is an optional dependency that is used to render the markdown content of the chat history. Make sure to also include `Avante` as a filetype
> to its setup:
>
> ```lua
> {
> "MeanderingProgrammer/markdown.nvim",
> opts = {
> file_types = { "markdown", "Avante" },
> },
> ft = { "markdown", "Avante" },
> }
> ```

Default setup configuration:

```lua
{
provider = "claude", -- "claude" or "openai" or "azure"
openai = {
endpoint = "https://api.openai.com",
model = "gpt-4o",
temperature = 0,
max_tokens = 4096,
```lua
{
provider = "claude", -- "claude" or "openai" or "azure"
openai = {
endpoint = "https://api.openai.com",
model = "gpt-4o",
temperature = 0,
max_tokens = 4096,
},
azure = {
endpoint = "", -- Example: "https://<your-resource-name>.openai.azure.com"
deployment = "", -- Azure deployment name (e.g., "gpt-4o", "my-gpt-4o-deployment")
api_version = "2024-06-01",
temperature = 0,
max_tokens = 4096,
},
claude = {
endpoint = "https://api.anthropic.com",
model = "claude-3-5-sonnet-20240620",
temperature = 0,
max_tokens = 4096,
},
highlights = {
diff = {
current = "DiffText", -- need have background color
incoming = "DiffAdd", -- need have background color
},
azure = {
endpoint = "", -- Example: "https://<your-resource-name>.openai.azure.com"
deployment = "", -- Azure deployment name (e.g., "gpt-4o", "my-gpt-4o-deployment")
api_version = "2024-06-01",
temperature = 0,
max_tokens = 4096,
},
mappings = {
show_sidebar = "<leader>aa",
diff = {
ours = "co",
theirs = "ct",
none = "c0",
both = "cb",
next = "]x",
prev = "[x",
},
claude = {
endpoint = "https://api.anthropic.com",
model = "claude-3-5-sonnet-20240620",
temperature = 0,
max_tokens = 4096,
},
highlights = {
diff = {
current = "DiffText", -- need have background color
incoming = "DiffAdd", -- need have background color
},
},
mappings = {
show_sidebar = "<leader>aa",
diff = {
ours = "co",
theirs = "ct",
none = "c0",
both = "cb",
next = "]x",
prev = "[x",
},
},
}
```
},
}
```

## Usage

Expand Down Expand Up @@ -165,6 +176,29 @@ To set up the development environment:
pre-commit install --install-hooks
```

For setting up lua_ls you can use the following for `nvim-lspconfig`:

```lua
lua_ls = {
settings = {
Lua = {
runtime = {
version = "LuaJIT",
special = { reload = "require" },
},
workspace = {
library = {
vim.fn.expand "$VIMRUNTIME/lua",
vim.fn.expand "$VIMRUNTIME/lua/vim/lsp",
vim.fn.stdpath "data" .. "/lazy/lazy.nvim/lua/lazy",
"${3rd}/luv/library",
},
},
},
},
},
```

## License

avante.nvim is licensed under the Apache License. For more details, please refer to the [LICENSE](./LICENSE) file.
4 changes: 2 additions & 2 deletions lua/avante/ai_bot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ local function call_claude_api_stream(question, code_lang, code_content, on_chun

local url = utils.trim_suffix(config.get().claude.endpoint, "/") .. "/v1/messages"

print("Sending request to Claude API...")
-- print("Sending request to Claude API...")

curl.post(url, {
---@diagnostic disable-next-line: unused-local
Expand Down Expand Up @@ -210,7 +210,7 @@ local function call_openai_api_stream(question, code_lang, code_content, on_chun
}
end

print("Sending request to " .. (config.get().provider == "azure" and "Azure OpenAI" or "OpenAI") .. " API...")
-- print("Sending request to " .. (config.get().provider == "azure" and "Azure OpenAI" or "OpenAI") .. " API...")

curl.post(url, {
---@diagnostic disable-next-line: unused-local
Expand Down
101 changes: 97 additions & 4 deletions lua/avante/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,74 @@
local M = {}
local sidebar = require("avante.sidebar")
local tiktoken = require("avante.tiktoken")
local Sidebar = require("avante.sidebar")
local config = require("avante.config")
local diff = require("avante.diff")

local api = vim.api

local M = {
---@type avante.Sidebar
sidebars = {},
---@type avante.Sidebar
current = nil,
}

---@param current boolean? false to disable setting current, otherwise use this to track across tabs.
---@return avante.Sidebar
function M._get(current)
local tab = api.nvim_get_current_tabpage()
local sidebar = M.sidebars[tab]
if current ~= false then
M.current = sidebar
end
return sidebar
end

---Run a sidebar method by getting the sidebar of current tabpage, with args
---noop if sidebar is nil
---@param method "open"|"close"|"toggle"|"focus"
---@param args table? arguments to parse
---@return any return_of_method
function M._call(method, args)
local sidebar = M._get()
if not sidebar then
return
end

args = args or {}
return sidebar[method](sidebar, unpack(args))
end

M.open = function()
local tab = api.nvim_get_current_tabpage()
local sidebar = M.sidebars[tab]

if not sidebar then
sidebar = Sidebar:new(tab)
M.sidebars[tab] = sidebar
end

M.current = sidebar

return sidebar:open()
end

M.close = function()
return M._call("close")
end

M.focus = function()
return M._call("focus")
end

M.toggle = function()
local sidebar = M._get()
if not sidebar then
M.open()
return true
end

return sidebar:toggle()
end

function M.setup(opts)
local ok, LazyConfig = pcall(require, "lazy.core.config")
Expand All @@ -11,7 +79,7 @@ function M.setup(opts)
require("tiktoken_lib").load()
end)
else
vim.api.nvim_create_autocmd("User", {
api.nvim_create_autocmd("User", {
pattern = "LazyLoad",
callback = function(event)
if event.data == name then
Expand All @@ -24,7 +92,32 @@ function M.setup(opts)
end

config.update(opts)
sidebar.setup()

tiktoken.setup("gpt-4o")

diff.setup({
debug = false, -- log output to console
default_mappings = config.get().mappings.diff, -- disable buffer local mapping created by this plugin
default_commands = true, -- disable commands created by this plugin
disable_diagnostics = true, -- This will disable the diagnostics in a buffer whilst it is conflicted
list_opener = "copen",
highlights = config.get().highlights.diff,
})

api.nvim_create_user_command("AvanteAsk", function()
M.toggle()
end, { nargs = 0 })
api.nvim_set_keymap("n", config.get().mappings.show_sidebar, "<cmd>AvanteAsk<CR>", { noremap = true, silent = true })

api.nvim_create_autocmd("FileType", {
group = api.nvim_create_augroup("Avante", { clear = true }),
pattern = "Avante",
callback = function(event)
api.nvim_buf_set_keymap(event.buf, "n", "q", '<cmd>:lua require("avante").close()<cr>', { silent = true })
end,
})

vim.treesitter.language.register("markdown", "Avante")
end

return M
Loading