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