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

Update plugin code to work with Lazy.nvim and on nvim 0.10.0 #13

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0c801ef
Changed the way the plugin is called so that it doesn't need to be ca…
jondkinney Jul 29, 2024
3f30c36
Added logging with print statements to help determine where the plugi…
jondkinney Jul 29, 2024
142c980
Improved logging in AiderOpen function.
jondkinney Jul 29, 2024
b63d6f5
Implemented a function to ignore special buffers that are not backed …
jondkinney Jul 29, 2024
a7951e7
Improved the `add_buffers_to_command` function to exclude special buf…
jondkinney Jul 29, 2024
7c3c613
Moved `is_valid_buffer` function from `helpers.lua` to `aider.lua` to…
jondkinney Jul 29, 2024
def4d7c
Passed `is_valid_buffer` function to `add_buffers_to_command` functio…
jondkinney Jul 29, 2024
2a68d6c
Improved the AiderOpen function to prevent the terminal buffer from c…
jondkinney Jul 29, 2024
31fe1a1
Added a check to only include readable files, not directories, in the…
jondkinney Jul 29, 2024
0a8b886
Enabled debug mode and updated the README to explain the debug option.
jondkinney Jul 29, 2024
214519b
Improved the user experience when Aider process exits by providing mo…
jondkinney Jul 29, 2024
9cb559a
Updated the default keybindings to have which-key labels, with the to…
jondkinney Jul 29, 2024
3f024fe
Reorganized keybindings under the top Aider level without nested fold…
jondkinney Jul 29, 2024
32c1fe8
Updated the README.md file with the proper keymaps and instructions f…
jondkinney Jul 29, 2024
c21f5ec
Update readme with new keybindings
jondkinney Jul 29, 2024
53241c8
Update readme with some fixes
jondkinney Jul 29, 2024
0dd5073
Added 'i' to the end of the Aider keybindings to enter insert mode af…
jondkinney Jul 29, 2024
23d4447
Added functionality to add all modified git files to the Aider chat.
jondkinney Jul 29, 2024
cebceb1
Added a new user command `AiderAddModifiedFiles` to make the `AiderAd…
jondkinney Jul 29, 2024
2178640
Updated the README.md file to highlight the new user command and upda…
jondkinney Jul 29, 2024
a78f7a2
Changed the keybindings to use the `<CMD>` prefix instead of `:` for …
jondkinney Jul 29, 2024
d82df55
Updated the keybindings with the latest which-key spec.
jondkinney Jul 29, 2024
c963699
Added new keybindings and updated existing ones for the Aider plugin.
jondkinney Jul 29, 2024
c14b826
Updated the keybindings to switch to insert mode after running Aider …
jondkinney Jul 29, 2024
d2005e3
Added new Aider keybindings and descriptions.
jondkinney Jul 29, 2024
ecf7aaa
Implemented conditional use of which-key for setting up keybindings
jondkinney Jul 29, 2024
7988612
Opened Aider chat before adding modified files to it
jondkinney Jul 29, 2024
8421423
Added Aider keybindings and functionality to the Neovim configuration.
jondkinney Jul 29, 2024
5747f12
Added support for a `vim` option to pass `--vim` to the AiderOpen com…
jondkinney Jul 29, 2024
8ed960c
Added the `vim = true` config option to the README.
jondkinney Jul 29, 2024
42870c0
Added support for the `--vim` flag to the Aider command when opening …
jondkinney Jul 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 65 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,66 +24,96 @@ Using [dein](https://github.com/Shougo/dein.vim)
call dein#add('joshuavial/aider.nvim')
```

Using [lazy.nvim](https://github.com/folke/lazy.nvim)

```lua
{
"joshuavial/aider.nvim",
config = function()
require("aider").setup({
-- your configuration comes here
-- if you don't want to use the default settings
auto_manage_context = true,
default_bindings = true,
debug = false, -- Set to true to enable debug logging
})
end,
}
```

## Usage

The Aider Plugin for Neovim provides the `AiderOpen` and `AiderBackground` lua functions.
The Aider Plugin for Neovim provides several functions and commands:

The `AiderOpen` function opens a terminal window with the Aider command. It accepts the following arguments:
- `args`: The command line arguments to pass to `aider` - defaults to ""
- `window_type`: The window style to use 'vsplit' (default), 'hsplit' or 'editor'
1. `AiderOpen`: Opens a terminal window with the Aider command.

NOTE: if an Aider job is already running calling AiderOpen will reattach to it, even if it is called with different flags
- Arguments:
- `args`: Command line arguments to pass to `aider` (default: "")
- `window_type`: Window style to use ('vsplit' (default), 'hsplit', or 'editor')
- Note: If an Aider job is already running, calling AiderOpen will reattach to it, even with different flags.

The `AiderBackground` function runs the Aider command in the background. It accepts the following arguments:
- `args`: The command line arguments to pass to `aider` - defaults to ""
- `message`: The message to pass to the Aider command - defaults to "Complete as many todo items as you can and remove the comment for any item you complete."
2. `AiderBackground`: Runs the Aider command in the background.

When Aider opens (through either function), it will automatically add all open buffers to both commands. If you are going to use this plugin you will want to actively manage open buffers with commands like `:ls` and `:bd`.
- Arguments:
- `args`: Command line arguments to pass to `aider` (default: "")
- `message`: Message to pass to the Aider command (default: "Complete as many todo items as you can and remove the comment for any item you complete.")

Here are some examples of how to use the `AiderOpen` and `AiderBackground` functions:
3. `AiderAddModifiedFiles`: Adds all git-modified files to the Aider chat.
- This function can be called directly or through the user command `:AiderAddModifiedFiles`

When Aider opens (through either function), it automatically adds all open buffers to both commands. It's recommended to actively manage open buffers with commands like `:ls` and `:bd`.

Examples of using these commands:

```vim
:lua AiderOpen()
:lua AiderOpen("-3", "hsplit")
:lua AiderOpen("AIDER_NO_AUTO_COMMITS=1 aider -3", "editor")
:lua AiderBackground()
:lua AiderBackground("-3")
:lua AiderBackground("AIDER_NO_AUTO_COMMITS=1 aider -3")
:AiderOpen
:AiderOpen -3 hsplit
:AiderOpen "AIDER_NO_AUTO_COMMITS=1 aider -3" editor
:AiderBackground
:AiderBackground -3
:AiderBackground "AIDER_NO_AUTO_COMMITS=1 aider -3"
:AiderAddModifiedFiles
```

You can also set keybindings for the `AiderOpen` and `AiderBackground` functions in Lua. Here's an example:
You can set custom keybindings for these commands in your Neovim configuration. For example:

```lua
-- set a keybinding for the AiderOpen function
vim.api.nvim_set_keymap('n', '<leader>oa', '<cmd>lua AiderOpen()<cr>', {noremap = true, silent = true})
-- set a keybinding for the AiderBackground function
vim.api.nvim_set_keymap('n', '<leader>ob', '<cmd>lua AiderBackground()<cr>', {noremap = true, silent = true})
vim.api.nvim_set_keymap('n', '<leader>ao', ':AiderOpen<CR>', {noremap = true, silent = true})
vim.api.nvim_set_keymap('n', '<leader>ab', ':AiderBackground<CR>', {noremap = true, silent = true})
vim.api.nvim_set_keymap('n', '<leader>am', ':AiderAddModifiedFiles<CR>', {noremap = true, silent = true})
```

In this example, pressing `<leader>oa` in normal mode will call the `AiderOpen` function, and `<leader>ob` will call the `AiderBackground` function.

Run `aider --help` to see all the options you can pass to the cli.
Run `aider --help` to see all the options you can pass to the CLI.

The plugin provides the following default keybindings:

- `<leader><Space><Space>` to open a terminal window with the Aider defaults (gpt-4).
- `<leader><Space>3` to open a terminal window with the Aider command using the gpt-3.5-turbo-16k model for chat.
- `<leader><space>b` to run the Aider command in the background with the defaults.
- `<leader><space>b3` to run the Aider command in the background using the gpt-3.5-turbo-16k model for chat.
- `<leader>Ao`: Open a terminal window with the Aider defaults (gpt-4).
- `<leader>AO`: Open a terminal window with the Aider command using the gpt-3.5-turbo-16k model for chat.
- `<leader>Ab`: Run the Aider command in the background with the defaults.
- `<leader>AB`: Run the Aider command in the background using the gpt-3.5-turbo-16k model for chat.
- `<leader>Am`: Add all git-modified files to the Aider chat.

These keybindings are set up using which-key, providing a descriptive popup menu when you press `<leader>A`.

## Setup

The Aider Plugin for Neovim provides a `setup` function that you can use to configure the plugin. This function accepts a table with the following keys:

- `auto_manage_context`: A boolean value that determines whether the plugin should automatically manage the context. If set to `true`, the plugin will automatically add and remove buffers from the context as they are opened and closed. Defaults to `true`.
- `default_bindings`: A boolean value that determines whether the plugin should use the default keybindings. If set to `true`, the plugin will require the keybindings file and set the default keybindings. Defaults to `true`.
- `debug`: A boolean value that determines whether the plugin should enable debug logging. When set to true, it will print debug information to help troubleshoot issues. Defaults to false.

Here is an example of how to use the `setup` function:

```lua
require('aider').setup({
auto_manage_context = false,
default_bindings = false
default_bindings = false,
debug = true,
vim = true, -- Pass the `--vim` flag to Aider when opening a new chat

-- only necessary if you want to change the default keybindings. <Leader>C is not a particularly good choice. It's just shown as an example.
vim.api.nvim_set_keymap('n', '<leader>C', ':AiderOpen<CR>', {noremap = true, silent = true})
})
```

Expand All @@ -95,40 +125,29 @@ The plugin exposes a global variable called `aider_background_status` that you c

```lua
lualine_x = {{
function()
function()
return 'A'
end,
color = { fg = '#8FBCBB' }, -- green
cond = function()
cond = function()
return _G.aider_background_status == 'idle'
end
},
{
function()
function()
return 'A'
end,
color = { fg = '#BF616A' }, -- red
cond = function()
cond = function()
return _G.aider_background_status == 'working'
end
}
}
```

## Reloading buffers
## Reloading buffers after Aider updates the underlying code

Because the AiderOnBufferOpen command is bound to BufReadPost it will fire whenever a buffer is reloaded if you just use a `:e!`. The ReloadBuffer function below will prevent a file from being added to aider every time it's openeed.

```lua
function ReloadBuffer()
local temp_sync_value = vim.g.aider_buffer_sync
vim.g.aider_buffer_sync = 0
vim.api.nvim_exec2('e!', {output = false})
vim.g.aider_buffer_sync = temp_sync_value
end
```

To use this function, simply call `:lua ReloadBuffer()` (or bind it to your favourite shortcut). This will refresh the current buffer and display any changes made by Aider.
Run the `:e` command to re-edit the current buffer updating its contents with any changes made since initially loading it.

## Tips for Working with Buffers in Vim

Expand All @@ -140,24 +159,6 @@ If you're not familiar with buffers in Vim, here are some tips:
- Use `:bd <number>` or `:bdelete <number>` to close a specific buffer. Replace `<number>` with the buffer number.
- Use `:bufdo bd` to close all buffers.

This helper function may be useful for closing all buffers that are hidden

```lua
_G.close_hidden_buffers = function()
local curr_buf_num = vim.api.nvim_get_current_buf()
local all_buf_nums = vim.api.nvim_list_bufs()

for _, buf_num in ipairs(all_buf_nums) do
if buf_num ~= curr_buf_num and vim.api.nvim_buf_is_valid(buf_num) and vim.api.nvim_buf_is_loaded(buf_num) and vim.fn.bufwinnr(buf_num) == -1 then
if vim.fn.getbufvar(buf_num, '&buftype') ~= 'terminal' then
vim.api.nvim_buf_delete(buf_num, { force = true })
end
end
end
end
```

## NOTE

if you resize a split the nvim buffer can truncate the text output, chatGPT tells me there isn't an easy work around for this. Feel free to make a PR if you think it's easy to solve without rearchitecting and using tmux or something similar.

Loading