Skip to content

Commit

Permalink
feat: :CodyAsk (sourcegraph#35)
Browse files Browse the repository at this point in the history
* Allow exiting chat with <ESC> and add CodyAsk command
  • Loading branch information
pjlast authored Jul 20, 2023
1 parent ae8a41d commit c599292
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
17 changes: 17 additions & 0 deletions doc/sg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ Default commands for interacting with Cody
:CodyExplain ~
Explain how to use Cody.

Use from visual mode to pass the current selection

*:CodyAsk*
:CodyAsk ~
Ask a question about the current selection.

Use from visual mode to pass the current selection

*:CodyChat*
Expand Down Expand Up @@ -66,6 +72,17 @@ commands.explain({bufnr}, {start_line}, {end_line}) *sg.cody.explain()*
{end_line} (number)


commands.ask({bufnr}, {start_line}, {end_line}, {message}) *sg.cody.ask()*
Ask Cody about the selected code


Parameters: ~
{bufnr} (number)
{start_line} (number)
{end_line} (number)
{message} (string)


commands.chat({name}) *sg.cody.chat()*
Start a new CodyChat

Expand Down
24 changes: 24 additions & 0 deletions lua/sg/cody/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ commands.explain = function(bufnr, start_line, end_line)
end)
end

--- Ask Cody about the selected code
---@param bufnr number
---@param start_line number
---@param end_line number
---@param message string
commands.ask = function(bufnr, start_line, end_line, message)
local selection = vim.api.nvim_buf_get_lines(bufnr, start_line, end_line, false)
local layout = CodyLayout.init {}

local contents = vim.tbl_flatten {
message,
"",
util.format_code(bufnr, selection),
}

layout:run(function()
-- context.add_context(bufnr, table.concat(selection, "\n"), layout.state)

layout.state:append(Message.init(Speaker.user, contents))
layout:mount()
layout:complete()
end)
end

--- Start a new CodyChat
---@param name string?
---@return CodyLayout
Expand Down
4 changes: 4 additions & 0 deletions lua/sg/components/cody_layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ function CodyLayout:mount()
self.prompt:on_close()
end)

keymaps.map(self.prompt.bufnr, "n", "<ESC>", "[cody] quit chat", function()
self.prompt:on_close()
end)

local with_history = function(key, mapped)
if not mapped then
mapped = key
Expand Down
10 changes: 10 additions & 0 deletions plugin/cody.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ vim.api.nvim_create_user_command("CodyExplain", function(command)
cody_commands.explain(bufnr, command.line1 - 1, command.line2)
end, { range = 2 })

---@command CodyAsk [[
--- Ask a question about the current selection.
---
--- Use from visual mode to pass the current selection
---@command ]]
vim.api.nvim_create_user_command("CodyAsk", function(command)
local bufnr = vim.api.nvim_get_current_buf()
cody_commands.ask(bufnr, command.line1 - 1, command.line2, command.args)
end, { range = 2, nargs = 1 })

---@command :CodyChat {module} [[
--- State a new cody chat, with an optional {title}
---@command ]]
Expand Down

0 comments on commit c599292

Please sign in to comment.