Skip to content

Commit 85249ad

Browse files
authored
feat: allow configuring keymaps (#217)
1 parent 6b47b9b commit 85249ad

File tree

6 files changed

+126
-86
lines changed

6 files changed

+126
-86
lines changed

doc/sg.txt

Lines changed: 61 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,22 @@ sg.config *sg.config*
4747

4848

4949
Configuration Options: ~
50-
{enable_cody} (boolean?) Enable/disable cody integration
51-
{accept_tos} (boolean?) Accept the TOS without being prompted
52-
{download_binaries} (boolean?) Default true, download latest release
53-
from Github
54-
{node_executable} (string?) path to node executable
55-
{skip_node_check} (boolean?) Useful if using other js runtime
56-
{cody_agent} (string?) path to the cody-agent js bundle
57-
{on_attach} (function?) function to run when attaching to
58-
sg://<file> buffers
50+
{enable_cody} (boolean?) Enable/disable cody
51+
integration
52+
{accept_tos} (boolean?) Accept the TOS without being
53+
prompted
54+
{download_binaries} (boolean?) Default true, download latest
55+
release from Github
56+
{node_executable} (string?) path to node executable
57+
{skip_node_check} (boolean?) Useful if using other js
58+
runtime
59+
{cody_agent} (string?) path to the cody-agent js
60+
bundle
61+
{on_attach} (function?) function to run when
62+
attaching to sg://<file>
63+
buffers
64+
{src_headers} (table<string, string>) Headers to be sent with each
65+
sg request
5966

6067

6168

@@ -121,6 +128,12 @@ Default commands for interacting with Cody
121128
:CodyAsk ~
122129
Ask a question about the current selection.
123130

131+
Use from visual mode to pass the current selection
132+
133+
*:CodyExplain*
134+
:CodyExplain ~
135+
Ask a question about the current selection.
136+
124137
Use from visual mode to pass the current selection
125138

126139
*:CodyChat{!}*
@@ -129,6 +142,8 @@ Default commands for interacting with Cody
129142

130143
If {!} is passed, will reset the chat and start a new chat conversation.
131144

145+
For more configuation options, see: `:help sg.cody.commands.chat`
146+
132147
*:CodyToggle*
133148
:CodyToggle ~
134149
Toggles the current Cody Chat window.
@@ -137,25 +152,6 @@ Default commands for interacting with Cody
137152
:CodyTask {task_description} ~
138153
Instruct Cody to perform a task on selected text.
139154

140-
*:CodyTaskView*
141-
:CodyTaskView ~
142-
Opens the last active CodyTask.
143-
144-
*:CodyTaskAccept*
145-
:CodyTaskAccept ~
146-
Accepts the current CodyTask, removing it from the pending tasks list and
147-
applying it to the selection the task was performed on. Can also be
148-
triggered by pressing <CR> while a task is open.
149-
150-
*:CodyTaskPrev*
151-
:CodyTaskPrev ~
152-
Cycles to the previous CodyTask. Navigates to the appropriate buffer
153-
location.
154-
155-
*:CodyTaskNext*
156-
:CodyTaskNext ~
157-
Cycles to the next CodyTask. Navigates to the appropriate buffer location.
158-
159155
*:CodyRestart*
160156
:CodyRestart ~
161157
Restarts Cody and Sourcegraph, clearing all state.
@@ -176,15 +172,7 @@ M.setup({config}) *cody.setup()*
176172
================================================================================
177173
LUA-COMMANDS *cody.lua-commands*
178174

179-
commands.ask({message}) *sg.cody.commands.ask()*
180-
Ask Cody a question, without any selection
181-
182-
183-
Parameters: ~
184-
{message} (string[])
185-
186-
187-
commands.ask_range({bufnr}, {start_row}, {end_row}, {message}) *sg.cody.commands.ask_range()*
175+
commands.ask_range({bufnr}, {start_row}, {end_row}, {message}, {opts}) *sg.cody.commands.ask_range()*
188176
Ask Cody about the selected code
189177

190178

@@ -193,6 +181,7 @@ commands.ask_range({bufnr}, {start_row}, {end_row}, {message}) *sg.cody.commands
193181
{start_row} (number)
194182
{end_row} (number)
195183
{message} (string)
184+
{opts} (cody.ChatOpts)
196185

197186

198187
commands.autocomplete({request}, {callback}) *sg.cody.commands.autocomplete()*
@@ -204,27 +193,45 @@ commands.autocomplete({request}, {callback}) *sg.cody.commands.autocomplete()*
204193
{callback} (function(err: table, data: CodyAutocompleteResult))
205194

206195

207-
commands.float({bufnr}, {start_line}, {end_line}, {message}) *sg.cody.commands.float()*
208-
Ask Cody about the selected code
196+
commands.chat({new}, {opts}) *sg.cody.commands.chat()*
197+
Open a cody chat
209198

199+
To configure keymaps, you can do something like:
210200

211-
Parameters: ~
212-
{bufnr} (number)
213-
{start_line} (number)
214-
{end_line} (number)
215-
{message} (string)
201+
This will disable <c-c> in insert mode from getting mapped by Cody.
216202

203+
>lua
204+
require("sg.cody.commands").chat(true, {
205+
keymaps = {
206+
i = {
207+
["<c-c>"] = false,
208+
},
209+
},
210+
})
211+
<
212+
213+
Additionally, you can map more functionality like so:
214+
215+
>lua
216+
require("sg.cody.commands").chat(true, {
217+
keymaps = {
218+
i = {
219+
["hello"] = { "Says Hello", function(chat) print("hello") end },
220+
},
221+
},
222+
})
223+
<
217224

218-
commands.chat({name}, {opts}) *sg.cody.commands.chat()*
219-
Start a new CodyChat
220225

221226

222227
Parameters: ~
223-
{name} (string?)
224-
{opts} ({ reset: boolean }?)
228+
{new} (boolean)
229+
{opts} (cody.ChatOpts)
230+
231+
232+
commands.toggle() *sg.cody.commands.toggle()*
233+
Toggle a Cody chat
225234

226-
Return: ~
227-
CodyLayoutSplit
228235

229236

230237
commands.do_task({bufnr}, {start_line}, {end_line}, {message}) *sg.cody.commands.do_task()*
@@ -243,16 +250,6 @@ commands.history() *sg.cody.commands.history()*
243250

244251

245252

246-
commands.add_context({start_line}, {end_line}, {state}) *sg.cody.commands.add_context()*
247-
Add context to an existing state
248-
249-
250-
Parameters: ~
251-
{start_line} (any)
252-
{end_line} (any)
253-
{state} (CodyState?)
254-
255-
256253
commands.focus_history() *sg.cody.commands.focus_history()*
257254
Focus the currently active history window.
258255

@@ -319,38 +316,16 @@ Default commands for interacting with Sourcegraph
319316
Run a search. For more sourcegraph search syntax, refer to online
320317
documentation
321318

319+
*:CodyIgnoreNotification*
320+
:CodyIgnoreNotification ~
321+
Ingore particular notifications
322+
322323

323324

324325

325326
================================================================================
326327
RPC *sg.rpc*
327328

328-
rpc.complete({snippet}, {opts}) *sg.rpc.complete()*
329-
Complete a single string snippet
330-
331-
332-
333-
Parameters: ~
334-
{snippet} (string) Code to send as the prompt
335-
{opts} ({ prefix: string? })
336-
337-
Return: ~
338-
string?: The error
339-
string?: The completion
340-
341-
342-
rpc.repository({name}) *sg.rpc.repository()*
343-
Get the repository ID for a repo with a name
344-
345-
346-
Parameters: ~
347-
{name} (string)
348-
349-
Return: ~
350-
string?: The error, if any
351-
string?: The repository ID, if found
352-
353-
354329
rpc.get_entry({path}, {callback}) *sg.rpc.get_entry()*
355330
Get an SgEntry based on a path
356331

lua/sg/cody/commands.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ commands.autocomplete = function(request, callback)
6262
end
6363

6464
--- Open a cody chat
65+
---
66+
--- To configure keymaps, you can do something like:
67+
---
68+
--- This will disable <c-c> in insert mode from getting
69+
--- mapped by Cody.
70+
---
71+
--- <code=lua>
72+
--- require("sg.cody.commands").chat(true, {
73+
--- keymaps = {
74+
--- i = {
75+
--- ["<c-c>"] = false,
76+
--- },
77+
--- },
78+
--- })
79+
--- </code>
80+
---
81+
--- Additionally, you can map more functionality like so:
82+
---
83+
--- <code=lua>
84+
--- require("sg.cody.commands").chat(true, {
85+
--- keymaps = {
86+
--- i = {
87+
--- ["hello"] = { "Says Hello", function(chat) print("hello") end },
88+
--- },
89+
--- },
90+
--- })
91+
--- </code>
92+
---
6593
---@param new boolean
6694
---@param opts cody.ChatOpts
6795
commands.chat = function(new, opts)

lua/sg/cody/plugin/commands.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ commands.CodyExplain = {
9898
--- State a new cody chat, with an optional {title}
9999
---
100100
--- If {!} is passed, will reset the chat and start a new chat conversation.
101+
---
102+
--- For more configuation options, see: `:help sg.cody.commands.chat`
101103
---@command ]]
102104
commands.CodyChat = {
103105
function(command)

lua/sg/cody/rpc/chat.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ local Typewriter = require "sg.components.typewriter"
1313
---@type cody.Chat?
1414
local last_chat = nil
1515

16+
---@alias cody.ChatKeymap fun(self: cody.Chat): nil
17+
---@alias cody.ChatMapping { [1]: string, [2]: cody.ChatKeymap } | false
18+
1619
---@class cody.ChatOpts
1720
---@field id? string: ID sent from cody-agent
1821
---@field name? string
1922
---@field model? string: The name of the model to set for the conversation
2023
---@field interval? number: The interval (in ms) to send a message
24+
---@field keymaps? table<string, table<string, cody.ChatMapping>>: Table of mode, key -> function
2125
---@field window_type? "float" | "split" | "hover"
2226
---@field window_opts? { width: number, height: number, split_cmd: string? }
2327

@@ -158,6 +162,20 @@ function Chat:_add_prompt_keymaps()
158162
keymaps.help(bufnr)
159163
end)
160164

165+
local keymap_overrides = self.opts.keymaps or {}
166+
for mode, overrides in pairs(keymap_overrides) do
167+
for key, value in pairs(overrides) do
168+
if value then
169+
local desc, func = unpack(value)
170+
keymaps.map(bufnr, mode, key, desc, function()
171+
func(self)
172+
end)
173+
else
174+
keymaps.del(bufnr, mode, key)
175+
end
176+
end
177+
end
178+
161179
-- TODO: Need to write a bit more stuff to manage this
162180
-- keymaps.map(bufnr, "n", "<space>m", function()
163181
-- rpc.request("webview/receiveMessage", {

lua/sg/keymaps.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,14 @@ M.help = function(bufnr)
114114
end)
115115
end
116116

117+
M.del = function(bufnr, mode, key)
118+
vim.keymap.del(mode, key, { buffer = bufnr })
119+
120+
store[bufnr].maps = vim.tbl_filter(function(map)
121+
return map.mode ~= mode
122+
or vim.api.nvim_replace_termcodes(map.key, true, false, true)
123+
~= vim.api.nvim_replace_termcodes(key, true, false, true)
124+
end, store[bufnr].maps)
125+
end
126+
117127
return M

scratch/test-keymap.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require("sg.cody.commands").chat(true, {
2+
keymaps = {
3+
i = {
4+
["<c-c>"] = false,
5+
},
6+
},
7+
})

0 commit comments

Comments
 (0)