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

Use absolute file path #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.aider*
tags
.env
129 changes: 64 additions & 65 deletions lua/aider.lua
Original file line number Diff line number Diff line change
@@ -1,93 +1,92 @@
local helpers = require('helpers')
local helpers = require("helpers")
local M = {}

M.aider_buf = nil

function M.AiderBackground(args, message)
helpers.showProcessingCue()
local command = helpers.build_background_command(args, message)
local handle = vim.loop.spawn('bash', {
args = {'-c', command}
}, NotifyOnExit)
helpers.showProcessingCue()
local command = helpers.build_background_command(args, message)
local handle = vim.loop.spawn("bash", {
args = { "-c", command },
}, NotifyOnExit)

vim.notify("Aider started " .. (args or ''))
vim.notify("Aider started " .. (args or ""))
end


function OnExit(code, signal)
if M.aider_buf then
vim.api.nvim_command('bd! ' .. M.aider_buf)
M.aider_buf = nil
end
if M.aider_buf then
vim.api.nvim_command("bd! " .. M.aider_buf)
M.aider_buf = nil
end
end

function M.AiderOpen(args, window_type)
window_type = window_type or 'vsplit'
if M.aider_buf and vim.api.nvim_buf_is_valid(M.aider_buf) then
helpers.open_buffer_in_new_window(window_type, M.aider_buf)
else
command = 'aider ' .. (args or '')
helpers.open_window(window_type)
command = helpers.add_buffers_to_command(command)
M.aider_job_id = vim.fn.termopen(command, {on_exit = OnExit})
M.aider_buf = vim.api.nvim_get_current_buf()
end
window_type = window_type or "vsplit"
if M.aider_buf and vim.api.nvim_buf_is_valid(M.aider_buf) then
helpers.open_buffer_in_new_window(window_type, M.aider_buf)
else
command = "aider " .. (args or "")
helpers.open_window(window_type)
command = helpers.add_buffers_to_command(command)
M.aider_job_id = vim.fn.termopen(command, { on_exit = OnExit })
M.aider_buf = vim.api.nvim_get_current_buf()
end
end

function M.AiderOnBufferOpen(bufnr)
if not vim.g.aider_buffer_sync or vim.g.aider_buffer_sync == 0 then
return
end
bufnr = tonumber(bufnr)
local bufname = vim.api.nvim_buf_get_name(bufnr)
local buftype = vim.fn.getbufvar(bufnr, '&buftype')
if not bufname or bufname:match('^term://') or buftype == 'terminal' then
return
end
local relative_filename = vim.fn.fnamemodify(bufname, ':~:.')
if M.aider_buf and vim.api.nvim_buf_is_valid(M.aider_buf) then
local line_to_add = '/add ' .. relative_filename
vim.fn.chansend(M.aider_job_id, line_to_add .. '\n')
end
if not vim.g.aider_buffer_sync or vim.g.aider_buffer_sync == 0 then
return
end
bufnr = tonumber(bufnr)
local bufname = vim.api.nvim_buf_get_name(bufnr)
local buftype = vim.fn.getbufvar(bufnr, "&buftype")
if not bufname or bufname:match("^term://") or buftype == "terminal" then
return
end
local absolute_filename = vim.fn.expand(bufname)
if M.aider_buf and vim.api.nvim_buf_is_valid(M.aider_buf) then
local line_to_add = "/add " .. absolute_filename
vim.fn.chansend(M.aider_job_id, line_to_add .. "\n")
end
end

function M.AiderOnBufferClose(bufnr)
if not vim.g.aider_buffer_sync or vim.g.aider_buffer_sync == 0 then
return
end
bufnr = tonumber(bufnr)
local bufname = vim.api.nvim_buf_get_name(bufnr)
if not bufname or bufname:match('^term://') then
return
end
local relative_filename = vim.fn.fnamemodify(bufname, ':~:.')
if M.aider_buf and vim.api.nvim_buf_is_valid(M.aider_buf) then
local line_to_drop = '/drop ' .. relative_filename
vim.fn.chansend(M.aider_job_id, line_to_drop .. '\n')
end
if not vim.g.aider_buffer_sync or vim.g.aider_buffer_sync == 0 then
return
end
bufnr = tonumber(bufnr)
local bufname = vim.api.nvim_buf_get_name(bufnr)
if not bufname or bufname:match("^term://") then
return
end
local absolute_filename = vim.fn.expand(bufname)
if M.aider_buf and vim.api.nvim_buf_is_valid(M.aider_buf) then
local line_to_drop = "/drop " .. absolute_filename
vim.fn.chansend(M.aider_job_id, line_to_drop .. "\n")
end
end

function M.setup(config)
M.config = config or {}
M.config.auto_manage_context = M.config.auto_manage_context or true
M.config.default_bindings = M.config.default_bindings or true
M.config = config or {}
M.config.auto_manage_context = M.config.auto_manage_context or true
M.config.default_bindings = M.config.default_bindings or true

vim.g.aider_buffer_sync = M.config.auto_manage_context
vim.g.aider_buffer_sync = M.config.auto_manage_context

if M.config.auto_manage_context then
vim.api.nvim_command('autocmd BufReadPost * lua AiderOnBufferOpen(vim.fn.expand("<abuf>"))')
vim.api.nvim_command('autocmd BufDelete * lua AiderOnBufferClose(vim.fn.expand("<abuf>"))')
_G.AiderOnBufferOpen = M.AiderOnBufferOpen
_G.AiderOnBufferClose = M.AiderOnBufferClose
end
if M.config.auto_manage_context then
vim.api.nvim_command('autocmd BufReadPost * lua AiderOnBufferOpen(vim.fn.expand("<abuf>"))')
vim.api.nvim_command('autocmd BufDelete * lua AiderOnBufferClose(vim.fn.expand("<abuf>"))')
_G.AiderOnBufferOpen = M.AiderOnBufferOpen
_G.AiderOnBufferClose = M.AiderOnBufferClose
end

_G.AiderOpen = M.AiderOpen
_G.AiderBackground = M.AiderBackground
_G.aider_background_status = 'idle'
_G.AiderOpen = M.AiderOpen
_G.AiderBackground = M.AiderBackground
_G.aider_background_status = "idle"

if M.config.default_bindings then
require('keybindings')
end
if M.config.default_bindings then
require("keybindings")
end
end

return M