From 86ba5a468b9944b11dad8c2951b2c62fbd829fdb Mon Sep 17 00:00:00 2001
From: Jakkapat Paijit <86193685+Bourbxn@users.noreply.github.com>
Date: Tue, 15 Oct 2024 10:24:48 +0700
Subject: [PATCH] feat(api): add switch sidebar focus (#709)
---
README.md | 87 +++++++++++++++++++++----------------------
lua/avante/api.lua | 26 +++++++++++++
lua/avante/config.lua | 1 +
lua/avante/init.lua | 9 +++++
4 files changed, 79 insertions(+), 44 deletions(-)
diff --git a/README.md b/README.md
index cd04d2a..6ac008d 100644
--- a/README.md
+++ b/README.md
@@ -159,13 +159,11 @@ require('avante').setup ({
-
> [!IMPORTANT]
>
> For `avante.tokenizers` and templates to work, make sure to call `require('avante_lib').load()` somewhere when entering the editor.
> We will leave the users to decide where it fits to do this, as this varies among configurations. (But we do recommend running this after where you set your colorscheme)
-
> [!IMPORTANT]
>
> `avante.nvim` is currently only compatible with Neovim 0.10.1 or later. Please ensure that your Neovim version meets these requirements before proceeding.
@@ -341,21 +339,22 @@ Given its early stage, `avante.nvim` currently supports the following basic func
The following key bindings are available for use with `avante.nvim`:
-| Key Binding | Description |
-|-------------|-------------|
-| Leaderaa | show sidebar |
-| Leaderar | refresh sidebar |
-| Leaderae | edit selected blocks |
-| co | choose ours |
-| ct | choose theirs |
-| ca | choose all theirs |
-| c0 | choose none |
-| cb | choose both |
-| cc | choose cursor |
-| ]x | move to previous conflict |
-| [x | move to next conflict |
-| [[ | jump to previous codeblocks (results window) |
-| ]] | jump to next codeblocks (results windows) |
+| Key Binding | Description |
+| ----------------------------------------- | -------------------------------------------- |
+| Leaderaa | show sidebar |
+| Leaderar | refresh sidebar |
+| Leaderaf | switch sidebar focus |
+| Leaderae | edit selected blocks |
+| co | choose ours |
+| ct | choose theirs |
+| ca | choose all theirs |
+| c0 | choose none |
+| cb | choose both |
+| cc | choose cursor |
+| ]x | move to previous conflict |
+| [x | move to next conflict |
+| [[ | jump to previous codeblocks (results window) |
+| ]] | jump to next codeblocks (results windows) |
> [!NOTE]
>
@@ -364,21 +363,20 @@ The following key bindings are available for use with `avante.nvim`:
## Highlight Groups
-
-| Highlight Group | Description | Notes |
-|-----------------|-------------|-------|
-| AvanteTitle | Title | |
-| AvanteReversedTitle | Used for rounded border | |
-| AvanteSubtitle | Selected code title | |
-| AvanteReversedSubtitle | Used for rounded border | |
-| AvanteThirdTitle | Prompt title | |
-| AvanteReversedThirdTitle | Used for rounded border | |
-| AvanteConflictCurrent | Current conflict highlight | Default to `Config.highlights.diff.current` |
-| AvanteConflictIncoming | Incoming conflict highlight | Default to `Config.highlights.diff.incoming` |
-| AvanteConflictCurrentLabel | Current conflict label highlight | Default to shade of `AvanteConflictCurrent` |
-| AvanteConflictIncomingLabel | Incoming conflict label highlight | Default to shade of `AvanteConflictIncoming` |
-| AvantePopupHint | Usage hints in popup menus | |
-| AvanteInlineHint | The end-of-line hint displayed in visual mode | |
+| Highlight Group | Description | Notes |
+| --------------------------- | --------------------------------------------- | -------------------------------------------- |
+| AvanteTitle | Title | |
+| AvanteReversedTitle | Used for rounded border | |
+| AvanteSubtitle | Selected code title | |
+| AvanteReversedSubtitle | Used for rounded border | |
+| AvanteThirdTitle | Prompt title | |
+| AvanteReversedThirdTitle | Used for rounded border | |
+| AvanteConflictCurrent | Current conflict highlight | Default to `Config.highlights.diff.current` |
+| AvanteConflictIncoming | Incoming conflict highlight | Default to `Config.highlights.diff.incoming` |
+| AvanteConflictCurrentLabel | Current conflict label highlight | Default to shade of `AvanteConflictCurrent` |
+| AvanteConflictIncomingLabel | Incoming conflict label highlight | Default to shade of `AvanteConflictIncoming` |
+| AvantePopupHint | Usage hints in popup menus | |
+| AvanteInlineHint | The end-of-line hint displayed in visual mode | |
See [highlights.lua](./lua/avante/highlights.lua) for more information
@@ -405,6 +403,7 @@ If one wish to custom prompts for each mode, `avante.nvim` will check for projec
the following patterns: `*.{mode}.avanterules`.
The rules for root hierarchy:
+
- lsp workspace folders
- lsp root_dir
- root pattern of filename of the current buffer
@@ -415,14 +414,15 @@ The rules for root hierarchy:
Example folder structure for custom prompt
If you have the following structure:
- ```bash
+
+```bash
.
├── .git/
├── typescript.planning.avanterules
├── snippets.editing.avanterules
└── src/
- ```
+```
- `typescript.planning.avanterules` will be used for `planning` mode
- `snippets.editing.avanterules`` will be used for `editing` mode
@@ -434,7 +434,6 @@ If you have the following structure:
>
> `*.avanterules` is a jinja template file, in which will be rendered using [minijinja](https://github.com/mitsuhiko/minijinja). See [templates](https://github.com/yetone/avante.nvim/blob/main/lua/avante/templates) for example on how to extend current templates.
-
## TODOs
- [x] Chat with current file
@@ -461,15 +460,15 @@ See [wiki](https://github.com/yetone/avante.nvim/wiki) for more recipes and tric
We would like to express our heartfelt gratitude to the contributors of the following open-source projects, whose code has provided invaluable inspiration and reference for the development of avante.nvim:
-| Nvim Plugin | License | Functionality | Location |
-| --- | --- | --- | --- |
-| [git-conflict.nvim](https://github.com/akinsho/git-conflict.nvim) | No License | Diff comparison functionality | [lua/avante/diff.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/diff.lua) |
-| [ChatGPT.nvim](https://github.com/jackMort/ChatGPT.nvim) | Apache 2.0 License | Calculation of tokens count | [lua/avante/utils/tokens.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/utils/tokens.lua) |
-| [img-clip.nvim](https://github.com/HakonHarnes/img-clip.nvim) | MIT License | Clipboard image support | [lua/avante/clipboard.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/clipboard.lua) |
-| [copilot.lua](https://github.com/zbirenbaum/copilot.lua) | MIT License | Copilot support | [lua/avante/providers/copilot.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/providers/copilot.lua) |
-| [jinja.vim](https://github.com/HiPhish/jinja.vim) | MIT License | Template filetype support | [syntax/jinja.vim](https://github.com/yetone/avante.nvim/blob/main/syntax/jinja.vim) |
-| [codecompanion.nvim](https://github.com/olimorris/codecompanion.nvim) | MIT License | Secrets logic support | [lua/avante/providers/init.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/providers/init.lua) |
-| [aider](https://github.com/paul-gauthier/aider) | Apache 2.0 License | Planning mode user prompt | [lua/avante/templates/planning.avanterules](https://github.com/yetone/avante.nvim/blob/main/lua/avante/templates/planning.avanterules) |
+| Nvim Plugin | License | Functionality | Location |
+| --------------------------------------------------------------------- | ------------------ | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
+| [git-conflict.nvim](https://github.com/akinsho/git-conflict.nvim) | No License | Diff comparison functionality | [lua/avante/diff.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/diff.lua) |
+| [ChatGPT.nvim](https://github.com/jackMort/ChatGPT.nvim) | Apache 2.0 License | Calculation of tokens count | [lua/avante/utils/tokens.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/utils/tokens.lua) |
+| [img-clip.nvim](https://github.com/HakonHarnes/img-clip.nvim) | MIT License | Clipboard image support | [lua/avante/clipboard.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/clipboard.lua) |
+| [copilot.lua](https://github.com/zbirenbaum/copilot.lua) | MIT License | Copilot support | [lua/avante/providers/copilot.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/providers/copilot.lua) |
+| [jinja.vim](https://github.com/HiPhish/jinja.vim) | MIT License | Template filetype support | [syntax/jinja.vim](https://github.com/yetone/avante.nvim/blob/main/syntax/jinja.vim) |
+| [codecompanion.nvim](https://github.com/olimorris/codecompanion.nvim) | MIT License | Secrets logic support | [lua/avante/providers/init.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/providers/init.lua) |
+| [aider](https://github.com/paul-gauthier/aider) | Apache 2.0 License | Planning mode user prompt | [lua/avante/templates/planning.avanterules](https://github.com/yetone/avante.nvim/blob/main/lua/avante/templates/planning.avanterules) |
The high quality and ingenuity of these projects' source code have been immensely beneficial throughout our development process. We extend our sincere thanks and respect to the authors and contributors of these projects. It is the selfless dedication of the open-source community that drives projects like avante.nvim forward.
diff --git a/lua/avante/api.lua b/lua/avante/api.lua
index a0691d0..13f19a6 100644
--- a/lua/avante/api.lua
+++ b/lua/avante/api.lua
@@ -175,6 +175,32 @@ M.refresh = function(opts)
sidebar:render(opts)
end
+---@param opts? AskOptions
+M.focus = function(opts)
+ opts = opts or {}
+ local sidebar = require("avante").get()
+ if not sidebar then return end
+
+ local curbuf = vim.api.nvim_get_current_buf()
+ local curwin = vim.api.nvim_get_current_win()
+
+ if sidebar:is_open() then
+ if curbuf == sidebar.input.bufnr then
+ if sidebar.code.winid and sidebar.code.winid ~= curwin then vim.api.nvim_set_current_win(sidebar.code.winid) end
+ elseif curbuf == sidebar.result.bufnr then
+ if sidebar.code.winid and sidebar.code.winid ~= curwin then vim.api.nvim_set_current_win(sidebar.code.winid) end
+ else
+ if sidebar.input.winid and sidebar.input.winid ~= curwin then
+ vim.api.nvim_set_current_win(sidebar.input.winid)
+ end
+ end
+ else
+ if sidebar.code.winid then vim.api.nvim_set_current_win(sidebar.code.winid) end
+ sidebar:open(opts)
+ if sidebar.input.winid then vim.api.nvim_set_current_win(sidebar.input.winid) end
+ end
+end
+
return setmetatable(M, {
__index = function(t, k)
local module = require("avante")
diff --git a/lua/avante/config.lua b/lua/avante/config.lua
index 2e6a221..2317df6 100644
--- a/lua/avante/config.lua
+++ b/lua/avante/config.lua
@@ -144,6 +144,7 @@ Respect and use existing conventions, libraries, etc that are already present in
ask = "aa",
edit = "ae",
refresh = "ar",
+ focus = "af",
toggle = {
default = "at",
debug = "ad",
diff --git a/lua/avante/init.lua b/lua/avante/init.lua
index b057240..eb7716d 100644
--- a/lua/avante/init.lua
+++ b/lua/avante/init.lua
@@ -77,6 +77,7 @@ H.commands = function()
{ desc = "avante: edit selected block", nargs = "*" }
)
cmd("Refresh", function() require("avante.api").refresh() end, { desc = "avante: refresh windows" })
+ cmd("Focus", function() require("avante.api").focus() end, { desc = "avante: switch focus windows" })
cmd("Build", function(opts)
local args = {}
for _, arg in ipairs(opts.fargs) do
@@ -113,6 +114,7 @@ H.keymaps = function()
)
vim.keymap.set("v", "(AvanteEdit)", function() require("avante.api").edit() end, { noremap = true })
vim.keymap.set("n", "(AvanteRefresh)", function() require("avante.api").refresh() end, { noremap = true })
+ vim.keymap.set("n", "(AvanteFocus)", function() require("avante.api").focus() end, { noremap = true })
vim.keymap.set("n", "(AvanteBuild)", function() require("avante.api").build() end, { noremap = true })
vim.keymap.set("n", "(AvanteToggle)", function() M.toggle() end, { noremap = true })
vim.keymap.set("n", "(AvanteToggleDebug)", function() M.toggle.debug() end)
@@ -146,6 +148,13 @@ H.keymaps = function()
function() require("avante.api").refresh() end,
{ desc = "avante: refresh" }
)
+ Utils.safe_keymap_set(
+ "n",
+ Config.mappings.focus,
+ function() require("avante.api").focus() end,
+ { desc = "avante: focus" }
+ )
+
Utils.safe_keymap_set("n", Config.mappings.toggle.default, function() M.toggle() end, { desc = "avante: toggle" })
Utils.safe_keymap_set(
"n",