-
Hello, Thanks a lot for this great plugin! It looks amazing. I'm looking for a bit more convenient way of sharing files with LLM to ask questions. Instead of |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 12 replies
-
That's a really awesome idea. To enable tighter integration between plugins, I've implemented events which are fired throughout the plugin's lifecycle. Lazy.nvim does the same too. It essentially pushes the focus of integration onto the user rather than us, the plugin maintainers. I'd advise investigating whether nvim-tree has a similar event system which you could create an autocmd around. You could then programmatically add references to a CodeCompanion chat buffer based on those events. |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot. I'm only confused about how to add references to a CodeCompanion chat buffer. I would love to define some function like: local function add_file_to_context(file_path)
local codecompanion = require('codecompanion')
codecompanion.chat_buffer().add_to_context(file_path
end Which I could call when reacting to some event (or actually I would initally use it in my custom command). The problem is - I'm not sure how to implement this function. I will try to dig deeper into the source code. |
Beta Was this translation helpful? Give feedback.
-
Thanks to your suggestions @olimorris I've implemented following custom command: vim.api.nvim_create_user_command('Cga', function(opts)
require("codecompanion").last_chat().References:add({
id = opts.args,
path = opts.args,
source = "codecompanion.strategies.chat.slash_commands.file",
opts = {
pinned = true
}
})
end, { nargs = 1 }); Now this is extremely useful for me, because I can just navigate on nvim tree and press |
Beta Was this translation helpful? Give feedback.
-
It looks like this project is changing fast and now you should use What I wanted is to add opened buffer other than {
'<leader>aa',
function()
local bufnr = vim.api.nvim_get_current_buf();
local name = require("codecompanion").last_chat().references:make_id_from_buf(bufnr)
if name == "" then
name = "Buffer " .. bufnr
end
local id = "<buf>" .. name .. "</buf>"
require("codecompanion").last_chat().references:add({
bufnr = bufnr,
id = id,
source = "codecompanion.strategies.chat.variables.buffer",
opts = {
watched = true
}
})
vim.print('buffer added to chat')
end,
silent = true,
desc = 'buffer to CodeCompanion chat'
}, |
Beta Was this translation helpful? Give feedback.
-
I use It is a keymap that executes a command for the entry under the cursor. Note: I purposely decided not to add files from dirs under dirs recursively. -- plugins/utils/ai-assistant.lua
local M = {}
M.add_file_path_to_chat = function(file_path, chat)
for _, ref in ipairs(chat.refs) do
if ref.path == file_path then return print("Already added") end
end
local ref = {
id = "<file>" .. file_path .. "</file>",
path = file_path,
source = "codecompanion.strategies.chat.slash_commands.file",
opts = { pinned = true },
}
chat.references:add(ref)
end
M.list_file_paths = function(path)
local stat = vim.uv.fs_stat(path)
if stat == nil then return print("Path does not exist") end
if stat.type == "directory" then
local paths = {}
for item in vim.fs.dir(path, { depth = 1 }) do
local item_path = vim.fs.joinpath(path, item)
local item_stat = vim.uv.fs_stat(item_path)
if item_stat ~= nil and item_stat.type == "file" then paths[#paths + 1] = item_path end
end
return paths
end
if stat.type == "file" then return { path } end
return {}
end
return M and then on my local add_path_to_codecompanion_chat = function()
local path = MiniFiles.get_fs_entry().path
if path == nil then return vim.notify("Cursor is not on valid entry") end
local utils = require("plugins.utils.ai-assistant")
local file_paths = utils.list_file_paths(path)
if #file_paths == 0 then return print("No files found") end
local codecompanion = require("codecompanion")
local chat = codecompanion.last_chat()
if chat == nil then --if no chat, create one
chat = codecompanion.chat()
end
for _, file_path in ipairs(file_paths) do
utils.add_file_path_to_chat(file_path, chat)
end
end
vim.api.nvim_create_autocmd("User", {
pattern = "MiniFilesBufferCreate",
callback = function(args)
local b = args.data.buf_id
vim.keymap.set("n", "ga", add_path_to_codecompanion_chat, { buffer = b, desc = "Add to CodeCompanion" })
end,
}) I'm not a pro lua dev so I'm open to any feedback :D |
Beta Was this translation helpful? Give feedback.
-
Looks pretty good to me! Nice work! |
Beta Was this translation helpful? Give feedback.
-
I use oil.nvim and able to make it work. I able to come up with this thanks to the example code in /file slash command, as usual great work making that readable @olimorris here is my oil config, it is not very clean because I am not experienced with lua and just trying out things now oil.setup({
default_file_explorer = true,
keymaps = {
["a"] = {
desc = "share file with code companion",
callback = function()
local Path = require("plenary.path")
local oil = require("oil")
local cc = require("codecompanion")
local fmt = string.format
local cur_dir = oil.get_current_dir()
local fullpath = cur_dir .. oil.get_cursor_entry().name
local path = Path:new(fullpath)
local ok, content = pcall(function()
return Path.new(fullpath):read()
end)
local relpath = path:make_relative()
local ft = vim.filetype.match({ filename = fullpath })
local description = fmt(
[[%s %s:
```%s
%s
```]],
"Here is the content of the file",
"located at `" .. relpath .. "`",
ft,
content
)
local id = "<file>" .. relpath .. "</file>"
cc.last_chat():add_message({
role = require("codecompanion.config").config.constants.USER_ROLE,
content = description,
}, { reference = id, visible = false })
cc.last_chat().references:add({
id = id,
path = relpath,
source = "codecompanion.strategies.chat.slash_commands.file",
})
vim.notify(relpath .. " added to codecompanion")
end,
},
},
})
|
Beta Was this translation helpful? Give feedback.
Thanks to your suggestions @olimorris I've implemented following custom command:
Now this is extremely useful for me, because I can just navigate on nvim tree and press
.
which is a shortcut for providing command with file path already provided and I can just typeCga
(ChatGPT add :P) and I'm there. Many thanks for help!