Skip to content

Commit 723ca0e

Browse files
authored
feat(inline): add keymap to stop request (#2426)
Closes #2420
1 parent 7686e3a commit 723ca0e

File tree

7 files changed

+95
-44
lines changed

7 files changed

+95
-44
lines changed

doc/codecompanion.txt

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*codecompanion.txt* For NVIM v0.11 Last change: 2025 November 19
1+
*codecompanion.txt* For NVIM v0.11 Last change: 2025 November 23
22

33
==============================================================================
44
Table of Contents *codecompanion-table-of-contents*
@@ -878,10 +878,9 @@ The configuration for both types of adapters is exactly the same, however they
878878
sit within their own tables (`adapters.http.*` and `adapters.acp.*`) and have
879879
different options available. HTTP adapters use `models` to allow users to
880880
select the specific LLM they’d like to interact with. ACP adapters use
881-
`commands` to allow users to customize their interaction with agents
882-
(e.g. enabling `yolo` mode). As there is a lot of shared functionality between
883-
the two adapters, it is recommend that you read this page alongside the ACP
884-
one.
881+
`commands` to allow users to customize their interaction with agents (e.g.�
882+
enabling `yolo` mode). As there is a lot of shared functionality between the
883+
two adapters, it is recommend that you read this page alongside the ACP one.
885884

886885

887886
CHANGING THE DEFAULT ADAPTER ~
@@ -913,7 +912,7 @@ the adapter’s URL, headers, parameters and other fields at runtime.
913912

914913
Supported `env` value types: - **Plain environment variable name (string)**: if
915914
the value is the name of an environment variable that has already been set
916-
(e.g. `"HOME"` or `"GEMINI_API_KEY"`), the plugin will read the value. -
915+
(e.g.`"HOME"` or `"GEMINI_API_KEY"`), the plugin will read the value. -
917916
**Command (string prefixed with cmd:)**: any value that starts with `cmd:` will
918917
be executed via the shell. Example: `"cmd:op read
919918
op://personal/Gemini/credential --no-newline"`. - **Function**: you can provide
@@ -2214,8 +2213,26 @@ The inline assistant supports keymaps for accepting or rejecting changes:
22142213
})
22152214
<
22162215

2217-
In this example, `<leader>a` (or `ga` on some keyboards) accepts inline
2218-
changes, while `gr` rejects them.
2216+
In this example, `ga` accepts inline changes, while `gr` rejects them.
2217+
2218+
You can also cancel an inline request with:
2219+
2220+
>lua
2221+
require("codecompanion").setup({
2222+
strategies = {
2223+
inline = {
2224+
keymaps = {
2225+
stop = {
2226+
modes = { n = "q" },
2227+
index = 4,
2228+
callback = "keymaps.stop",
2229+
description = "Stop request",
2230+
},
2231+
},
2232+
},
2233+
},
2234+
})
2235+
<
22192236

22202237

22212238
VARIABLES ~
@@ -2952,7 +2969,7 @@ The fastest way to copy an LLM’s code output is with `gy`. This will yank the
29522969
nearest codeblock.
29532970

29542971

2955-
APPLYING AN LLMS EDITS TO A BUFFER OR FILE ~
2972+
APPLYING AN LLM€�S EDITS TO A BUFFER OR FILE ~
29562973

29572974
The |codecompanion-usage-chat-buffer-tools-files| tool, combined with the
29582975
|codecompanion-usage-chat-buffer-variables.html-buffer| variable or
@@ -5020,7 +5037,7 @@ These handlers manage tool/function calling:
50205037
as a great reference to understand how they’re working with the output of the
50215038
API
50225039

5023-
OPENAIS API OUTPUT
5040+
OPENAI€�S API OUTPUT
50245041

50255042
If we reference the OpenAI documentation
50265043
<https://platform.openai.com/docs/guides/text-generation/chat-completions-api>
@@ -6927,7 +6944,7 @@ tool to function. In the case of Anthropic, we insert additional headers.
69276944
<
69286945

69296946
Some adapter tools can be a `hybrid` in terms of their implementation. That is,
6930-
they’re an adapter tool that requires a client-side component (i.e. a
6947+
they’re an adapter tool that requires a client-side component (i.e.a
69316948
built-in tool). This is the case for the
69326949
|codecompanion-usage-chat-buffer-tools-memory| tool from Anthropic. To allow
69336950
for this, ensure that the tool definition in `available_tools` has

doc/configuration/inline-assistant.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,26 @@ require("codecompanion").setup({
3636
})
3737
```
3838

39-
In this example, `<leader>a` (or `ga` on some keyboards) accepts inline changes, while `gr` rejects them.
39+
In this example, `ga` accepts inline changes, while `gr` rejects them.
40+
41+
You can also cancel an inline request with:
42+
43+
```lua
44+
require("codecompanion").setup({
45+
strategies = {
46+
inline = {
47+
keymaps = {
48+
stop = {
49+
modes = { n = "q" },
50+
index = 4,
51+
callback = "keymaps.stop",
52+
description = "Stop request",
53+
},
54+
},
55+
},
56+
},
57+
})
58+
```
4059

4160
## Variables
4261

lua/codecompanion/config.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,12 @@ The user is working on a %s machine. Please respond with system specific command
688688
callback = "keymaps.always_accept",
689689
description = "Accept and enable auto mode",
690690
},
691+
stop = {
692+
modes = { n = "q" },
693+
index = 4,
694+
callback = "keymaps.stop",
695+
description = "Stop request",
696+
},
691697
},
692698
variables = {
693699
["buffer"] = {

lua/codecompanion/strategies/inline/init.lua

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,21 @@ function Inline:parse_special_syntax(prompt)
258258
return vim.trim(prompt)
259259
end
260260

261+
---Set keymaps for the inline strategy
262+
---@param bufnr? number
263+
---@param opts? table
264+
---@return nil
265+
function Inline:set_keymaps(bufnr, opts)
266+
keymaps
267+
.new({
268+
bufnr = bufnr,
269+
callbacks = require("codecompanion.strategies.inline.keymaps"),
270+
data = self,
271+
keymaps = config.strategies.inline.keymaps,
272+
})
273+
:set(opts)
274+
end
275+
261276
---Prompt the LLM
262277
---@param user_prompt? string The prompt supplied by the user
263278
---@return nil
@@ -411,8 +426,7 @@ function Inline:submit(prompt)
411426
_streaming = self.adapter.opts.stream
412427
self.adapter.opts.stream = false
413428

414-
-- Set keymaps and start diffing
415-
self:setup_buffer()
429+
self:set_keymaps(self.buffer_context.bufnr, { keymaps = { "stop" } })
416430

417431
self.current_request = client
418432
.new({ adapter = self.adapter:map_schema_to_params(), user_args = { event = "InlineStarted" } })
@@ -505,27 +519,11 @@ function Inline:done(output)
505519
end)
506520
end
507521

508-
---Setup the buffer prior to sending the request to the LLM
509-
---@return nil
510-
function Inline:setup_buffer()
511-
-- Add a keymap to cancel the request
512-
api.nvim_buf_set_keymap(self.buffer_context.bufnr, "n", "q", "", {
513-
desc = "Stop the request",
514-
callback = function()
515-
log:trace("[Inline] Cancelling the request")
516-
if self.current_request then
517-
self:stop()
518-
end
519-
end,
520-
})
521-
end
522-
523522
---Reset the inline prompt class
524523
---@return nil
525524
function Inline:reset()
526525
self.adapter.opts.stream = _streaming
527526
self.current_request = nil
528-
api.nvim_buf_del_keymap(self.bufnr, "n", "q")
529527
api.nvim_clear_autocmds({ group = self.aug })
530528
end
531529

@@ -733,14 +731,7 @@ function Inline:start_diff(original_content)
733731
return self:reset()
734732
end
735733

736-
keymaps
737-
.new({
738-
bufnr = self.buffer_context.bufnr,
739-
callbacks = require("codecompanion.strategies.inline.keymaps"),
740-
data = self,
741-
keymaps = config.strategies.inline.keymaps,
742-
})
743-
:set()
734+
self:set_keymaps(self.buffer_context.bufnr, { exclude_keymaps = { "stop" } })
744735

745736
local provider = config.display.diff.provider
746737
local ok, diff = pcall(require, "codecompanion.providers.diff." .. provider)

lua/codecompanion/strategies/inline/keymaps.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,12 @@ M.always_accept = {
5252
end,
5353
}
5454

55+
M.stop = {
56+
callback = function(inline)
57+
inline:stop()
58+
clear_map(config.strategies.inline.keymaps, inline.diff.bufnr)
59+
log:trace("[Inline] Cancelling the request")
60+
end,
61+
}
62+
5563
return M

lua/codecompanion/utils/keymaps.lua

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,30 @@ function Keymaps:resolve(rhs)
6262
end
6363

6464
---Set the keymaps
65+
---@param opts? table
6566
---@return nil
66-
function Keymaps:set()
67-
for _, map in pairs(self.keymaps) do
67+
function Keymaps:set(opts)
68+
opts = opts or {}
69+
70+
for name, map in pairs(self.keymaps) do
71+
if opts.keymaps and not vim.tbl_contains(opts.keymaps, name) then
72+
goto continue
73+
end
74+
if opts.exclude_keymaps and vim.tbl_contains(opts.exclude_keymaps, name) then
75+
goto continue
76+
end
6877
if map == false then
6978
goto continue
7079
end
7180

7281
local callback
7382
local rhs, action_opts = self:resolve(map.callback)
74-
if type(map.condition) == "function" and not map.condition() then
83+
if type(map.condition) == "function" and not map.condition(opts) then
7584
goto continue
7685
end
7786

7887
local default_opts = { desc = map.description or action_opts.desc, buffer = self.bufnr, nowait = true }
79-
local opts = vim.tbl_deep_extend("force", default_opts, map.opts or {})
88+
local key_opts = vim.tbl_deep_extend("force", default_opts, map.opts or {})
8089

8190
if type(rhs) == "function" then
8291
callback = function()
@@ -92,10 +101,10 @@ function Keymaps:set()
92101
if mode ~= "" then
93102
if type(key) == "table" then
94103
for _, v in ipairs(key) do
95-
vim.keymap.set(mode, v, callback, opts)
104+
vim.keymap.set(mode, v, callback, key_opts)
96105
end
97106
else
98-
vim.keymap.set(mode, key, callback, opts)
107+
vim.keymap.set(mode, key, callback, key_opts)
99108
end
100109
end
101110
end

tests/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ return {
369369
},
370370
inline = {
371371
adapter = "test_adapter",
372+
keymaps = og_config.strategies.inline.keymaps,
372373
variables = {
373374
["foo"] = {
374375
callback = vim.fn.getcwd() .. "/tests/strategies/inline/variables/foo.lua",

0 commit comments

Comments
 (0)