From f27115f7c053d1d8e4b2af4b7506210fffc51c95 Mon Sep 17 00:00:00 2001 From: Elvis Claros Castro Date: Thu, 9 Jan 2025 10:33:28 -0300 Subject: =?UTF-8?q?Feat:=20agrego=20m=C3=A1s=20plugins?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/config/codecompanion-save.lua | 60 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 lua/config/codecompanion-save.lua (limited to 'lua/config/codecompanion-save.lua') diff --git a/lua/config/codecompanion-save.lua b/lua/config/codecompanion-save.lua new file mode 100644 index 0000000..46f2d94 --- /dev/null +++ b/lua/config/codecompanion-save.lua @@ -0,0 +1,60 @@ +-- 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 | : delete", + cwd = save_folder:absolute(), + attach_mappings = function(_, map) + map("i", "", 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 = "*" }) -- cgit v1.2.3