Skip to content

Commit 7604b46

Browse files
refactor(api): extract functionality into API module (#41)
- ✨ Create dedicated API abstraction layer for better organization - πŸ—οΈ Move implementation details from init.lua to api.lua - πŸ§ͺ Add comprehensive tests for all API methods - πŸ”„ Update command implementations to use the new API
1 parent 4672993 commit 7604b46

File tree

7 files changed

+481
-62
lines changed

7 files changed

+481
-62
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 134 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<img width="1280" alt="screenshot_1" src="https://github.com/user-attachments/assets/5d779f73-5441-4d24-8cce-e6dfdc5bf787" />
66
<img width="1280" alt="screenshot_2" src="https://github.com/user-attachments/assets/3c122846-ca27-42d3-8cbf-f6e5f9b10f69" />
77

8-
> 🚧 This plugin is in initial development. Expect breaking changes and rough edges.
8+
> 🚧 This plugin is in initial development. Expect breaking changes and rough edges.
99
> _October 17, 2024_
1010
1111
## 🌟 Features
@@ -17,6 +17,8 @@
1717
- [x] πŸ“€ Send buffers or selections to Aider
1818
- [x] πŸ’¬ Optional user prompt for buffer and selection sends
1919
- [x] πŸ” Aider command selection UI with fuzzy search and input prompt
20+
- [x] πŸ”Œ Fully documented [Lua API](lua/nvim_aider/api.lua) for
21+
programmatic interaction and custom integrations
2022
- [x] πŸŒ²βž• [Neo-tree](https://github.com/nvim-neo-tree/neo-tree.nvim)
2123
integration also with multi-file/directory selection with visual mode support
2224
- [x] 🌳 Integration with [nvim-tree.lua](https://github.com/nvim-tree/nvim-tree.lua)
@@ -41,9 +43,9 @@
4143

4244
## πŸ”— Requirements
4345

44-
🐍 Python: Install `aider-chat`
45-
πŸ“‹ System: **Neovim** >= 0.9.4, ~~Working clipboard~~ thanks to @milanglacier
46-
πŸŒ™ Lua: `folke/snacks.nvim`,
46+
🐍 Python: Install `aider-chat`
47+
πŸ“‹ System: **Neovim** >= 0.9.4, ~~Working clipboard~~ thanks to @milanglacier
48+
πŸŒ™ Lua: `folke/snacks.nvim`,
4749
_optionals_ `catppuccin/nvim`, `nvim-neo-tree/neo-tree.nvim`, `nvim-tree.lua`
4850

4951
## πŸ“¦ Installation
@@ -135,6 +137,134 @@ require("nvim_aider").setup({
135137
})
136138
```
137139

140+
## πŸ“š API Reference
141+
142+
The plugin provides a structured API for programmatic integration. Access via `require("nvim_aider").api`
143+
144+
### Core Functions
145+
146+
```lua
147+
local api = require("nvim_aider").api
148+
```
149+
150+
#### `health_check()`
151+
152+
Verify plugin health status
153+
154+
```lua
155+
api.health_check()
156+
```
157+
158+
#### `toggle_terminal(opts?)`
159+
160+
Toggle Aider terminal window
161+
162+
```lua
163+
api.toggle_terminal()
164+
```
165+
166+
---
167+
168+
### Terminal Operations
169+
170+
#### `send_to_terminal(text, opts?)`
171+
172+
Send raw text directly to Aider
173+
174+
```lua
175+
api.send_to_terminal("Fix the login validation")
176+
```
177+
178+
#### `send_command(command, input?, opts?)`
179+
180+
Execute specific Aider command
181+
182+
```lua
183+
api.send_command("/commit", "Add error handling")
184+
```
185+
186+
---
187+
188+
### File Management
189+
190+
#### `add_file(filepath)`
191+
192+
Add specific file to session
193+
194+
```lua
195+
api.add_file("/src/utils.lua")
196+
```
197+
198+
#### `drop_file(filepath)`
199+
200+
Remove file from session
201+
202+
```lua
203+
api.drop_file("/outdated/legacy.py")
204+
```
205+
206+
#### `add_current_file()`
207+
208+
Add current buffer's file (uses `add_file` internally)
209+
210+
```lua
211+
vim.api.nvim_create_autocmd("BufWritePost", {
212+
callback = function()
213+
api.add_current_file()
214+
end
215+
})
216+
```
217+
218+
#### `drop_current_file()`
219+
220+
Remove current buffer's file
221+
222+
```lua
223+
api.drop_current_file()
224+
```
225+
226+
#### `add_read_only_file()`
227+
228+
Add current buffer as read-only reference
229+
230+
```lua
231+
api.add_read_only_file()
232+
```
233+
234+
---
235+
236+
### Buffer Operations
237+
238+
#### `send_buffer_with_prompt(opts?)`
239+
240+
Send entire buffer content with optional prompt
241+
242+
```lua
243+
api.send_buffer_with_prompt()
244+
```
245+
246+
---
247+
248+
### UI Components
249+
250+
#### `open_command_picker(opts?, callback?)`
251+
252+
Interactive command selector with custom handling
253+
254+
```lua
255+
api.open_command_picker(nil, function(picker, item)
256+
if item.text == "/custom" then
257+
-- Implement custom command handling
258+
else
259+
-- Default behavior
260+
picker:close()
261+
api.send_command(item.text)
262+
end
263+
end)
264+
```
265+
266+
---
267+
138268
## 🧩 Other Aider Neovim plugins
139269

140270
- [aider.nvim](https://github.com/joshuavial/aider.nvim)

β€Žlua/nvim_aider/api.luaβ€Ž

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
local M = {}
2+
local commands = require("nvim_aider.commands")
3+
local config = require("nvim_aider.config")
4+
local picker = require("nvim_aider.picker")
5+
local terminal = require("nvim_aider.terminal")
6+
local utils = require("nvim_aider.utils")
7+
8+
---Run health check
9+
function M.health_check()
10+
vim.cmd([[checkhealth nvim_aider]])
11+
end
12+
13+
---Toggle aider terminal
14+
---@param opts? table Optional configuration override
15+
function M.toggle_terminal(opts)
16+
terminal.toggle(opts or {})
17+
end
18+
19+
---Send text to aider terminal
20+
---@param text string Text to send
21+
---@param opts? table Optional configuration override
22+
function M.send_to_terminal(text, opts)
23+
terminal.send(text, opts or {})
24+
end
25+
26+
---Send command to aider terminal
27+
---@param command string Aider command to execute
28+
---@param input? string Additional input for the command
29+
---@param opts? table Optional configuration override
30+
function M.send_command(command, input, opts)
31+
terminal.command(command, input, opts or {})
32+
end
33+
34+
---Send buffer contents with optional prompt
35+
---@param opts? table Optional configuration override
36+
function M.send_buffer_with_prompt(opts)
37+
local selected_text = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n")
38+
local file_type = vim.bo.filetype
39+
file_type = file_type == "" and "text" or file_type
40+
41+
vim.ui.input({ prompt = "Add a prompt to your buffer (empty to skip):" }, function(input)
42+
if input ~= nil then
43+
if input ~= "" then
44+
selected_text = selected_text .. "\n> " .. input
45+
end
46+
terminal.send(selected_text, opts or {}, true)
47+
end
48+
end)
49+
end
50+
51+
---Add specific file to session
52+
---@param filepath string Path to file to add
53+
---@param opts? table Optional configuration override
54+
function M.add_file(filepath, opts)
55+
if filepath then
56+
terminal.command(commands.add.value, filepath, opts or {})
57+
else
58+
vim.notify("No file path provided", vim.log.levels.ERROR)
59+
end
60+
end
61+
62+
---Add current file to session
63+
---@param opts? table Optional configuration override
64+
function M.add_current_file(opts)
65+
local filepath = utils.get_absolute_path()
66+
if filepath then
67+
M.add_file(filepath, opts)
68+
else
69+
vim.notify("No valid file in current buffer", vim.log.levels.INFO)
70+
end
71+
end
72+
73+
---Remove specific file from session
74+
---@param filepath string Path to file to remove
75+
---@param opts? table Optional configuration override
76+
function M.drop_file(filepath, opts)
77+
if filepath then
78+
terminal.command(commands.drop.value, filepath, opts or {})
79+
else
80+
vim.notify("No file path provided", vim.log.levels.ERROR)
81+
end
82+
end
83+
84+
---Remove current file from session
85+
---@param opts? table Optional configuration override
86+
function M.drop_current_file(opts)
87+
local filepath = utils.get_absolute_path()
88+
if filepath then
89+
M.drop_file(filepath, opts)
90+
else
91+
vim.notify("No valid file in current buffer", vim.log.levels.INFO)
92+
end
93+
end
94+
95+
---Add current file as read-only
96+
---@param opts? table Optional configuration override
97+
function M.add_read_only_file(opts)
98+
local filepath = utils.get_absolute_path()
99+
if filepath then
100+
terminal.command(commands["read-only"].value, filepath, opts)
101+
else
102+
vim.notify("No valid file in current buffer", vim.log.levels.INFO)
103+
end
104+
end
105+
106+
---Open command picker
107+
---@param opts? table Optional configuration override
108+
---@param callback? function Custom callback handler
109+
function M.open_command_picker(opts, callback)
110+
picker.create(opts, callback or function(picker_instance, item)
111+
if item.category == "input" then
112+
vim.ui.input({ prompt = "Enter input for `" .. item.text .. "` (empty to skip):" }, function(input)
113+
if input then
114+
terminal.command(item.text, input, opts)
115+
end
116+
end)
117+
else
118+
terminal.command(item.text, nil, opts)
119+
end
120+
picker_instance:close()
121+
end)
122+
end
123+
124+
return M

0 commit comments

Comments
Β (0)