Last active
February 13, 2025 06:16
-
-
Save itsfrank/942780f88472a14c9cbb3169012a3328 to your computer and use it in GitHub Desktop.
Snippet to add the ability to save/load CodeCompanion chats in neovim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- add 2 commands: | |
-- CodeCompanionSave [space delimited args] | |
-- CodeCompanionLoad | |
-- Save will save current chat in a md file named 'space-delimited-args.md' | |
-- Load will use a telescope filepicker to open a previously saved chat | |
-- create a folder to store our chats | |
local Path = require("plenary.path") | |
local data_path = vim.fn.stdpath("data") | |
local save_folder = Path:new(data_path, "cc_saves") | |
if not save_folder:exists() then | |
save_folder:mkdir({ parents = true }) | |
end | |
-- telescope picker for our saved chats | |
vim.api.nvim_create_user_command("CodeCompanionLoad", function() | |
local t_builtin = require("telescope.builtin") | |
local t_actions = require("telescope.actions") | |
local t_action_state = require("telescope.actions.state") | |
local function start_picker() | |
t_builtin.find_files({ | |
prompt_title = "Saved CodeCompanion Chats | <c-d>: delete", | |
cwd = save_folder:absolute(), | |
attach_mappings = function(_, map) | |
map("i", "<c-d>", function(prompt_bufnr) | |
local selection = t_action_state.get_selected_entry() | |
local filepath = selection.path or selection.filename | |
os.remove(filepath) | |
t_actions.close(prompt_bufnr) | |
start_picker() | |
end) | |
return true | |
end, | |
}) | |
end | |
start_picker() | |
end, {}) | |
-- save current chat, `CodeCompanionSave foo bar baz` will save as 'foo-bar-baz.md' | |
vim.api.nvim_create_user_command("CodeCompanionSave", function(opts) | |
local codecompanion = require("codecompanion") | |
local success, chat = pcall(function() | |
return codecompanion.buf_get_chat(0) | |
end) | |
if not success or chat == nil then | |
vim.notify( | |
"CodeCompanionSave should only be called from CodeCompanion chat buffers", | |
vim.log.levels.ERROR | |
) | |
return | |
end | |
if #opts.fargs == 0 then | |
vim.notify("CodeCompanionSave requires at least 1 arg to make a file name", vim.log.levels.ERROR) | |
end | |
local save_name = table.concat(opts.fargs, "-") .. ".md" | |
local save_path = Path:new(save_folder, save_name) | |
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) | |
save_path:write(table.concat(lines, "\n"), "w") | |
end, { nargs = "*" }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Super nice and thank you so much for this. Here's an fzf-lua adaptation: