diff --git a/lua/codecompanion/config.lua b/lua/codecompanion/config.lua index 38658dd1..2407a467 100644 --- a/lua/codecompanion/config.lua +++ b/lua/codecompanion/config.lua @@ -204,14 +204,6 @@ local defaults = { callback = "keymaps.change_adapter", description = "Change adapter", }, - fold_code = { - modes = { - n = "gf", - }, - index = 12, - callback = "keymaps.fold_code", - description = "Fold code", - }, debug = { modes = { n = "gd", diff --git a/lua/codecompanion/helpers/slash_commands/buffer.lua b/lua/codecompanion/helpers/slash_commands/buffer.lua index de5101d2..97023565 100644 --- a/lua/codecompanion/helpers/slash_commands/buffer.lua +++ b/lua/codecompanion/helpers/slash_commands/buffer.lua @@ -3,6 +3,7 @@ local config = require("codecompanion.config") local buf = require("codecompanion.utils.buffers") local file_utils = require("codecompanion.utils.files") local log = require("codecompanion.utils.log") +local util = require("codecompanion.utils.util") local api = vim.api @@ -34,30 +35,19 @@ local function output(SlashCommand, selected) end local Chat = SlashCommand.Chat - if config.opts.visible then - Chat:append_to_buf({ - content = "[!" - .. CONSTANTS.NAME - .. ": `" - .. selected[CONSTANTS.DISPLAY] - .. "` / Buf No.: " - .. selected.bufnr - .. "]\n", - }) - Chat:append_to_buf({ content = content }) - Chat:fold_code() - else - Chat:add_message({ - role = "user", - content = string.format( - [[Here is the data from buffer number %d: + Chat:add_message({ + role = "user", + content = string.format( + [[Here is the content from %s (which has a buffer number of _%d_ and a filepath of `%s`): %s]], - selected.bufnr, - content - ), - }, { visible = false }) - end + selected.name, + selected.bufnr, + selected.path, + content + ), + }, { visible = false }) + util.notify(string.format("Buffer `%s` content added to the chat", selected.name)) end local Providers = { diff --git a/lua/codecompanion/helpers/slash_commands/fetch.lua b/lua/codecompanion/helpers/slash_commands/fetch.lua index abd64c7f..3e812df8 100644 --- a/lua/codecompanion/helpers/slash_commands/fetch.lua +++ b/lua/codecompanion/helpers/slash_commands/fetch.lua @@ -2,6 +2,7 @@ local adapters = require("codecompanion.adapters") local client = require("codecompanion.http") local log = require("codecompanion.utils.log") +local util = require("codecompanion.utils.util") local fmt = string.format @@ -82,7 +83,7 @@ function SlashCommand:execute() content = content, }, { visible = false }) - return vim.notify(fmt("Added the data from %s", input), vim.log.levels.INFO, { title = "CodeCompanion" }) + return util.notify(fmt("Added the page contents for: %s", input)) end if chunk.code >= 400 then return log:error("Error: %s", chunk.body.data) diff --git a/lua/codecompanion/helpers/slash_commands/file.lua b/lua/codecompanion/helpers/slash_commands/file.lua index 0ce3b895..5a2e1918 100644 --- a/lua/codecompanion/helpers/slash_commands/file.lua +++ b/lua/codecompanion/helpers/slash_commands/file.lua @@ -2,6 +2,7 @@ local config = require("codecompanion.config") local file_utils = require("codecompanion.utils.files") local log = require("codecompanion.utils.log") +local util = require("codecompanion.utils.util") CONSTANTS = { NAME = "File", @@ -26,9 +27,20 @@ local function output(SlashCommand, selected) local Chat = SlashCommand.Chat local relative_path = selected.relative_path or selected[1] or selected.path - Chat:append_to_buf({ content = "[!" .. CONSTANTS.NAME .. ": `" .. relative_path .. "`]\n" }) - Chat:append_to_buf({ content = "```" .. ft .. "\n" .. content .. "```" }) - Chat:fold_code() + Chat:add_message({ + role = "user", + content = string.format( + [[Here is the content from the file `%s`: + +```%s +%s +```]], + relative_path, + ft, + content + ), + }, { visible = false }) + util.notify("File data added to chat") end local Providers = { diff --git a/lua/codecompanion/helpers/slash_commands/help.lua b/lua/codecompanion/helpers/slash_commands/help.lua index 99f9fee5..33fbeead 100644 --- a/lua/codecompanion/helpers/slash_commands/help.lua +++ b/lua/codecompanion/helpers/slash_commands/help.lua @@ -3,6 +3,7 @@ local config = require("codecompanion.config") local file_utils = require("codecompanion.utils.files") local log = require("codecompanion.utils.log") local tokens_utils = require("codecompanion.utils.tokens") +local util = require("codecompanion.utils.util") local ts = vim.treesitter CONSTANTS = { @@ -85,9 +86,20 @@ local function output(SlashCommand, selected) end local Chat = SlashCommand.Chat - Chat:append_to_buf({ content = "[!" .. CONSTANTS.NAME .. ": `" .. selected.tag .. "`]\n" }) - Chat:append_to_buf({ content = "```" .. ft .. "\n" .. content .. "\n```" }) - Chat:fold_code() + Chat:add_message({ + role = "user", + content = string.format( + [[Here is some additional context related to the tag `%s`: + +```%s +%s +```]], + selected.tag, + ft, + content + ), + }, { visible = false }) + util.notify(string.format("%s help file added to chat", selected.tag)) end local Providers = { diff --git a/lua/codecompanion/helpers/slash_commands/symbols.lua b/lua/codecompanion/helpers/slash_commands/symbols.lua index 43a31732..15ee2b37 100644 --- a/lua/codecompanion/helpers/slash_commands/symbols.lua +++ b/lua/codecompanion/helpers/slash_commands/symbols.lua @@ -1,6 +1,7 @@ local config = require("codecompanion.config") local log = require("codecompanion.utils.log") +local util = require("codecompanion.utils.util") ---The Tree-sitter queries below are used to extract symbols from the buffer ---where the chat was initiated from. If you'd like to add more support @@ -159,20 +160,24 @@ function SlashCommand:execute() if #symbols == 0 then log:info("No symbols found in the buffer") - return vim.notify("No symbols found in the buffer", "info", { title = "CodeCompanion" }) + util.notify("No symbols found in the buffer") end local content = table.concat(symbols, "\n") - Chat:append_to_buf({ content = "[!Symbols]\n" }) - Chat:append_to_buf({ + Chat:add_message({ + role = "user", content = string.format( - "```txt\nFilename: %s\nFiletype: %s\n\n%s\n\n```\n", + [[Here is a symbolic outline of the file `%s` with filetype `%s`: + + +%s +]], Chat.context.filename, Chat.context.filetype, content ), - }) - Chat:fold_code() + }, { visible = false }) + util.notify("Symbolic outline added to chat") end return SlashCommand diff --git a/lua/codecompanion/helpers/slash_commands/terminal.lua b/lua/codecompanion/helpers/slash_commands/terminal.lua index 08954677..2f027593 100644 --- a/lua/codecompanion/helpers/slash_commands/terminal.lua +++ b/lua/codecompanion/helpers/slash_commands/terminal.lua @@ -1,3 +1,5 @@ +local util = require("codecompanion.utils.util") + CONSTANTS = { NAME = "Terminal Output", } @@ -28,20 +30,19 @@ function SlashCommand:execute() local content = vim.api.nvim_buf_get_lines(terminal_buf, 0, -1, false) local Chat = self.Chat - - Chat:append_to_buf({ content = "[!" .. CONSTANTS.NAME .. "]\n" }) - Chat:append_to_buf({ + Chat:add_message({ + role = "user", content = string.format( - [[``` -Buffer Number: %s -Output: + [[Here is the terminal output for buffer number `%s`: + + %s -```]], +]], terminal_buf, table.concat(content, "\n") ), - }) - Chat:fold_code() + }, { visible = false }) + util.notify("Terminal output added to chat") end return SlashCommand diff --git a/lua/codecompanion/keymaps.lua b/lua/codecompanion/keymaps.lua index 6ebed587..d0be6c33 100644 --- a/lua/codecompanion/keymaps.lua +++ b/lua/codecompanion/keymaps.lua @@ -362,12 +362,6 @@ M.change_adapter = { end, } -M.fold_code = { - callback = function(chat) - chat:fold_code() - end, -} - M.debug = { desc = "Show debug information for the current chat", callback = function(chat) diff --git a/lua/codecompanion/strategies/chat.lua b/lua/codecompanion/strategies/chat.lua index 834741ee..338af459 100644 --- a/lua/codecompanion/strategies/chat.lua +++ b/lua/codecompanion/strategies/chat.lua @@ -1085,71 +1085,6 @@ function Chat:fold_heading(heading) return self end ----Fold code under the user's heading in the chat buffer ----@return self -function Chat:fold_code() - -- NOTE: Folding is super brittle in Neovim - if not self.has_folded_code then - api.nvim_create_autocmd("InsertLeave", { - group = self.aug, - buffer = self.bufnr, - desc = "Always fold code when a slash command is used", - callback = function() - self:fold_code() - end, - }) - self.has_folded_code = true - end - - local query = vim.treesitter.query.parse( - "markdown", - [[ -(section -( - (atx_heading - (atx_h2_marker) - heading_content: (_) @role -) -([ - (fenced_code_block) - (indented_code_block) -] @code (#trim! @code)) -)) -]] - ) - - local parser = vim.treesitter.get_parser(self.bufnr, "markdown") - local tree = parser:parse()[1] - - vim.o.foldmethod = "manual" - - local role - for _, matches in query:iter_matches(tree:root(), self.bufnr, nil, nil, { all = false }) do - local match = {} - for id, node in pairs(matches) do - match = vim.tbl_extend("keep", match, { - [query.captures[id]] = { - node = node, - }, - }) - end - - if match.role then - role = vim.trim(vim.treesitter.get_node_text(match.role.node, self.bufnr)) - if role:match(user_role) and match.code then - local start_row, _, end_row, _ = match.code.node:range() - if start_row < end_row then - api.nvim_buf_call(self.bufnr, function() - vim.cmd(string.format("%d,%dfold", start_row, end_row)) - end) - end - end - end - end - - return self -end - ---CodeCompanion models completion source ---@param request table ---@param callback fun(request: table) diff --git a/lua/codecompanion/strategies/chat/slash_commands.lua b/lua/codecompanion/strategies/chat/slash_commands.lua index 09b05360..656bd78e 100644 --- a/lua/codecompanion/strategies/chat/slash_commands.lua +++ b/lua/codecompanion/strategies/chat/slash_commands.lua @@ -1,4 +1,3 @@ -local config = require("codecompanion").config local log = require("codecompanion.utils.log") ---Resolve the callback to the correct module diff --git a/lua/codecompanion/utils/util.lua b/lua/codecompanion/utils/util.lua index 033d9ed3..b3622b7a 100644 --- a/lua/codecompanion/utils/util.lua +++ b/lua/codecompanion/utils/util.lua @@ -10,6 +10,17 @@ function M.fire(event, opts) api.nvim_exec_autocmds("User", { pattern = "CodeCompanion" .. event, data = opts }) end +---Notify the user +---@param msg string +---@param level? string +---@return nil +function M.notify(msg, level) + level = level or vim.log.levels.INFO + return vim.notify(msg, level, { + title = "CodeCompanion", + }) +end + ---Make the first letter uppercase ---@param str string ---@return string