Skip to content

Commit

Permalink
feat(chat): ✨ yank the last code block
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Oct 19, 2024
1 parent 8d7ea10 commit f095b77
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lua/codecompanion/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,59 +170,67 @@ local defaults = {
callback = "keymaps.codeblock",
description = "Insert Codeblock",
},
yank_code = {
modes = {
n = "gy",
},
index = 7,
callback = "keymaps.yank_code",
description = "Yank Code",
},
next_chat = {
modes = {
n = "}",
},
index = 7,
index = 8,
callback = "keymaps.next_chat",
description = "Next Chat",
},
previous_chat = {
modes = {
n = "{",
},
index = 8,
index = 9,
callback = "keymaps.previous_chat",
description = "Previous Chat",
},
next_header = {
modes = {
n = "]]",
},
index = 9,
index = 10,
callback = "keymaps.next_header",
description = "Next Header",
},
previous_header = {
modes = {
n = "[[",
},
index = 10,
index = 11,
callback = "keymaps.previous_header",
description = "Previous Header",
},
change_adapter = {
modes = {
n = "ga",
},
index = 11,
index = 12,
callback = "keymaps.change_adapter",
description = "Change adapter",
},
fold_code = {
modes = {
n = "gf",
},
index = 12,
index = 13,
callback = "keymaps.fold_code",
description = "Fold code",
},
debug = {
modes = {
n = "gd",
},
index = 13,
index = 14,
callback = "keymaps.debug",
description = "View debug info",
},
Expand Down
13 changes: 13 additions & 0 deletions lua/codecompanion/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ M.codeblock = {
end,
}

M.yank_code = {
desc = "Yank the last codeblock",
callback = function(chat)
local code = chat:yank_code()
if #code > 0 then
if type(code) == "table" then
code = table.concat(code, "\n")
end
vim.fn.setreg(vim.v.register, code)
end
end,
}

M.next_chat = {
desc = "Move to the next chat",
callback = function(chat)
Expand Down
29 changes: 29 additions & 0 deletions lua/codecompanion/strategies/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ local function buf_parse_message(bufnr)
return {}
end

---Parse the chat buffer for a code block
---@param bufnr integer
---@param cursor? table
---@return table|nil
local function buf_parse_codeblocks(bufnr, cursor)
local parser = vim.treesitter.get_parser(bufnr, "markdown")
local root = parser:parse()[1]:root()
local query = vim.treesitter.query.get("markdown", "chat")

local last_match = nil
for id, node in query:iter_captures(root, bufnr, 0, -1) do
if query.captures[id] == "code" then
last_match = node
end
end

if last_match then
return vim.treesitter.get_node_text(last_match, bufnr)
else
return nil
end
end

---@class CodeCompanion.Chat
---@return nil
local function buf_parse_tools(chat)
Expand Down Expand Up @@ -1159,6 +1182,12 @@ function Chat:fold_code()
return self
end

---Yank the last code block from the chat buffer
---@return table|nil
function Chat:yank_code()
return buf_parse_codeblocks(self.bufnr)
end

---CodeCompanion models completion source
---@param request table
---@param callback fun(request: table)
Expand Down

0 comments on commit f095b77

Please sign in to comment.