feat(api): add switch sidebar focus (#709)

This commit is contained in:
Jakkapat Paijit 2024-10-15 10:24:48 +07:00 committed by GitHub
parent 964715be64
commit 86ba5a468b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 79 additions and 44 deletions

View File

@ -159,13 +159,11 @@ require('avante').setup ({
</details> </details>
> [!IMPORTANT] > [!IMPORTANT]
> >
> For `avante.tokenizers` and templates to work, make sure to call `require('avante_lib').load()` somewhere when entering the editor. > 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) > 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] > [!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. > `avante.nvim` is currently only compatible with Neovim 0.10.1 or later. Please ensure that your Neovim version meets these requirements before proceeding.
@ -342,9 +340,10 @@ Given its early stage, `avante.nvim` currently supports the following basic func
The following key bindings are available for use with `avante.nvim`: The following key bindings are available for use with `avante.nvim`:
| Key Binding | Description | | Key Binding | Description |
|-------------|-------------| | ----------------------------------------- | -------------------------------------------- |
| <kbd>Leader</kbd><kbd>a</kbd><kbd>a</kbd> | show sidebar | | <kbd>Leader</kbd><kbd>a</kbd><kbd>a</kbd> | show sidebar |
| <kbd>Leader</kbd><kbd>a</kbd><kbd>r</kbd> | refresh sidebar | | <kbd>Leader</kbd><kbd>a</kbd><kbd>r</kbd> | refresh sidebar |
| <kbd>Leader</kbd><kbd>a</kbd><kbd>f</kbd> | switch sidebar focus |
| <kbd>Leader</kbd><kbd>a</kbd><kbd>e</kbd> | edit selected blocks | | <kbd>Leader</kbd><kbd>a</kbd><kbd>e</kbd> | edit selected blocks |
| <kbd>c</kbd><kbd>o</kbd> | choose ours | | <kbd>c</kbd><kbd>o</kbd> | choose ours |
| <kbd>c</kbd><kbd>t</kbd> | choose theirs | | <kbd>c</kbd><kbd>t</kbd> | choose theirs |
@ -364,9 +363,8 @@ The following key bindings are available for use with `avante.nvim`:
## Highlight Groups ## Highlight Groups
| Highlight Group | Description | Notes | | Highlight Group | Description | Notes |
|-----------------|-------------|-------| | --------------------------- | --------------------------------------------- | -------------------------------------------- |
| AvanteTitle | Title | | | AvanteTitle | Title | |
| AvanteReversedTitle | Used for rounded border | | | AvanteReversedTitle | Used for rounded border | |
| AvanteSubtitle | Selected code title | | | AvanteSubtitle | Selected code title | |
@ -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 following patterns: `*.{mode}.avanterules`.
The rules for root hierarchy: The rules for root hierarchy:
- lsp workspace folders - lsp workspace folders
- lsp root_dir - lsp root_dir
- root pattern of filename of the current buffer - root pattern of filename of the current buffer
@ -415,14 +414,15 @@ The rules for root hierarchy:
<summary>Example folder structure for custom prompt</summary> <summary>Example folder structure for custom prompt</summary>
If you have the following structure: If you have the following structure:
```bash
```bash
. .
├── .git/ ├── .git/
├── typescript.planning.avanterules ├── typescript.planning.avanterules
├── snippets.editing.avanterules ├── snippets.editing.avanterules
└── src/ └── src/
``` ```
- `typescript.planning.avanterules` will be used for `planning` mode - `typescript.planning.avanterules` will be used for `planning` mode
- `snippets.editing.avanterules`` will be used for `editing` 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. > `*.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 ## TODOs
- [x] Chat with current file - [x] Chat with current file
@ -462,7 +461,7 @@ 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: 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 | | 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) | | [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) | | [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) | | [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) |

View File

@ -175,6 +175,32 @@ M.refresh = function(opts)
sidebar:render(opts) sidebar:render(opts)
end 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, { return setmetatable(M, {
__index = function(t, k) __index = function(t, k)
local module = require("avante") local module = require("avante")

View File

@ -144,6 +144,7 @@ Respect and use existing conventions, libraries, etc that are already present in
ask = "<leader>aa", ask = "<leader>aa",
edit = "<leader>ae", edit = "<leader>ae",
refresh = "<leader>ar", refresh = "<leader>ar",
focus = "<leader>af",
toggle = { toggle = {
default = "<leader>at", default = "<leader>at",
debug = "<leader>ad", debug = "<leader>ad",

View File

@ -77,6 +77,7 @@ H.commands = function()
{ desc = "avante: edit selected block", nargs = "*" } { desc = "avante: edit selected block", nargs = "*" }
) )
cmd("Refresh", function() require("avante.api").refresh() end, { desc = "avante: refresh windows" }) 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) cmd("Build", function(opts)
local args = {} local args = {}
for _, arg in ipairs(opts.fargs) do for _, arg in ipairs(opts.fargs) do
@ -113,6 +114,7 @@ H.keymaps = function()
) )
vim.keymap.set("v", "<Plug>(AvanteEdit)", function() require("avante.api").edit() end, { noremap = true }) vim.keymap.set("v", "<Plug>(AvanteEdit)", function() require("avante.api").edit() end, { noremap = true })
vim.keymap.set("n", "<Plug>(AvanteRefresh)", function() require("avante.api").refresh() end, { noremap = true }) vim.keymap.set("n", "<Plug>(AvanteRefresh)", function() require("avante.api").refresh() end, { noremap = true })
vim.keymap.set("n", "<Plug>(AvanteFocus)", function() require("avante.api").focus() end, { noremap = true })
vim.keymap.set("n", "<Plug>(AvanteBuild)", function() require("avante.api").build() end, { noremap = true }) vim.keymap.set("n", "<Plug>(AvanteBuild)", function() require("avante.api").build() end, { noremap = true })
vim.keymap.set("n", "<Plug>(AvanteToggle)", function() M.toggle() end, { noremap = true }) vim.keymap.set("n", "<Plug>(AvanteToggle)", function() M.toggle() end, { noremap = true })
vim.keymap.set("n", "<Plug>(AvanteToggleDebug)", function() M.toggle.debug() end) vim.keymap.set("n", "<Plug>(AvanteToggleDebug)", function() M.toggle.debug() end)
@ -146,6 +148,13 @@ H.keymaps = function()
function() require("avante.api").refresh() end, function() require("avante.api").refresh() end,
{ desc = "avante: refresh" } { 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", Config.mappings.toggle.default, function() M.toggle() end, { desc = "avante: toggle" })
Utils.safe_keymap_set( Utils.safe_keymap_set(
"n", "n",