Skip to content

Commit

Permalink
feat: slash command output is now hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Oct 9, 2024
1 parent 54b3107 commit dba83b9
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 124 deletions.
8 changes: 0 additions & 8 deletions lua/codecompanion/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,6 @@ local defaults = {
callback = "keymaps.change_adapter",
description = "Change adapter",
},
fold_code = {

This comment has been minimized.

Copy link
@jellydn

jellydn Oct 10, 2024

Contributor

Hi @olimorris, how I could add this mapping back? I really heavy use the fold_code mapping. Thanks?

This comment has been minimized.

Copy link
@olimorris

olimorris Oct 10, 2024

Author Owner

Ah sorry, duly noted. I will add this back later today

modes = {
n = "gf",
},
index = 12,
callback = "keymaps.fold_code",
description = "Fold code",
},
debug = {
modes = {
n = "gd",
Expand Down
34 changes: 12 additions & 22 deletions lua/codecompanion/helpers/slash_commands/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = {
Expand Down
3 changes: 2 additions & 1 deletion lua/codecompanion/helpers/slash_commands/fetch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
18 changes: 15 additions & 3 deletions lua/codecompanion/helpers/slash_commands/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 = {
Expand Down
18 changes: 15 additions & 3 deletions lua/codecompanion/helpers/slash_commands/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down
17 changes: 11 additions & 6 deletions lua/codecompanion/helpers/slash_commands/symbols.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<symbols>\n%s\n</symbols>\n```\n",
[[Here is a symbolic outline of the file `%s` with filetype `%s`:
<symbols>
%s
</symbols>]],
Chat.context.filename,
Chat.context.filetype,
content
),
})
Chat:fold_code()
}, { visible = false })
util.notify("Symbolic outline added to chat")
end

return SlashCommand
19 changes: 10 additions & 9 deletions lua/codecompanion/helpers/slash_commands/terminal.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local util = require("codecompanion.utils.util")

CONSTANTS = {
NAME = "Terminal Output",
}
Expand Down Expand Up @@ -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`:
<terminal>
%s
```]],
</terminal>]],
terminal_buf,
table.concat(content, "\n")
),
})
Chat:fold_code()
}, { visible = false })
util.notify("Terminal output added to chat")
end

return SlashCommand
6 changes: 0 additions & 6 deletions lua/codecompanion/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
65 changes: 0 additions & 65 deletions lua/codecompanion/strategies/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion lua/codecompanion/strategies/chat/slash_commands.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local config = require("codecompanion").config
local log = require("codecompanion.utils.log")

---Resolve the callback to the correct module
Expand Down
11 changes: 11 additions & 0 deletions lua/codecompanion/utils/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dba83b9

Please sign in to comment.