Skip to content

Commit 4ceb4fd

Browse files
authored
feat: customize slash_command, tool and variable prefixes (#2710)
1 parent eea11d0 commit 4ceb4fd

File tree

20 files changed

+200
-92
lines changed

20 files changed

+200
-92
lines changed

doc/codecompanion.txt

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,50 @@ See the section on |codecompanion-configuration-adapters-acp| and
15931593
|codecompanion-configuration-adapters-http| for more information.
15941594

15951595

1596+
COMPLETION ~
1597+
1598+
By default, CodeCompanion will determine if you have one of blink.cmp
1599+
<https://github.com/saghen/blink.cmp>, nvim-cmp
1600+
<https://github.com/hrsh7th/nvim-cmp>, or coc.nvim
1601+
<https://github.com/neoclide/coc.nvim> installed, selecting it as the default
1602+
provider. Failing this, the default completion engine will be used.
1603+
1604+
You can override this with:
1605+
1606+
>lua
1607+
require("codecompanion").setup({
1608+
interactions = {
1609+
chat = {
1610+
opts = {
1611+
completion_provider = "blink", -- blink|cmp|coc|default
1612+
}
1613+
}
1614+
}
1615+
})
1616+
<
1617+
1618+
1619+
PREFIXES
1620+
1621+
You can also customize the prefixes that trigger completions for
1622+
|codecompanion-usage-chat-buffer-slash-commands|,
1623+
|codecompanion-usage-chat-buffer-tools| and
1624+
|codecompanion-usage-chat-buffer-variables|:
1625+
1626+
>lua
1627+
require("codecompanion").setup({
1628+
opts = {
1629+
triggers = {
1630+
acp_slash_commands = "\\",
1631+
slash_commands = "/",
1632+
tools = "@",
1633+
variables = "#",
1634+
},
1635+
},
1636+
})
1637+
<
1638+
1639+
15961640
DIFF ~
15971641

15981642

@@ -1664,8 +1708,8 @@ SLASH COMMANDS ~
16641708
file
16651709
Slash Commands
16661710
<https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua#L114>
1667-
(invoked with `/`) let you dynamically insert context into the chat buffer,
1668-
such as file contents or date/time.
1711+
(invoked with `/` by default) let you dynamically insert context into the chat
1712+
buffer, such as file contents or date/time.
16691713

16701714
The plugin supports providers like telescope
16711715
<https://github.com/nvim-telescope/telescope.nvim>, mini_pick
@@ -1689,7 +1733,7 @@ Tools
16891733
<https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua#L55>
16901734
perform specific tasks (e.g., running shell commands, editing buffers, etc.)
16911735
when invoked by an LLM. Multiple tools can be grouped together. Both can be
1692-
referenced with `@` when in the chat buffer:
1736+
referenced with `@` (by default), when in the chat buffer:
16931737

16941738
>lua
16951739
require("codecompanion").setup({
@@ -1931,10 +1975,10 @@ VARIABLES ~
19311975

19321976
Variables
19331977
<https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua#L90>
1934-
are placeholders inserted into the chat buffer (using `#`). They provide
1935-
contextual code or information about the current Neovim state. For instance,
1936-
the built-in `#buffer` variable sends the current buffer’s contents to the
1937-
LLM.
1978+
are placeholders inserted into the chat buffer (using `#` by default). They
1979+
provide contextual code or information about the current Neovim state. For
1980+
instance, the built-in `#{buffer}` variable sends the current buffer’s
1981+
contents to the LLM.
19381982

19391983
You can even define your own variables to share specific content:
19401984

doc/configuration/chat-buffer.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,41 @@ require("codecompanion").setup({
2727

2828
See the section on [ACP](/configuration/adapters-acp) and [HTTP](/configuration/adapters-http) for more information.
2929

30+
## Completion
31+
32+
By default, CodeCompanion will determine if you have one of [blink.cmp](https://github.com/saghen/blink.cmp), [nvim-cmp](https://github.com/hrsh7th/nvim-cmp), or [coc.nvim](https://github.com/neoclide/coc.nvim) installed, selecting it as the default provider. Failing this, the default completion engine will be used.
33+
34+
You can override this with:
35+
36+
```lua
37+
require("codecompanion").setup({
38+
interactions = {
39+
chat = {
40+
opts = {
41+
completion_provider = "blink", -- blink|cmp|coc|default
42+
}
43+
}
44+
}
45+
})
46+
```
47+
48+
### Prefixes
49+
50+
You can also customize the prefixes that trigger completions for [slash commands](/usage/chat-buffer/slash-commands), [tools](/usage/chat-buffer/tools) and [variables](/usage/chat-buffer/variables):
51+
52+
```lua
53+
require("codecompanion").setup({
54+
opts = {
55+
triggers = {
56+
acp_slash_commands = "\\",
57+
slash_commands = "/",
58+
tools = "@",
59+
variables = "#",
60+
},
61+
},
62+
})
63+
```
64+
3065
## Diff
3166

3267
<img src="https://github.com/user-attachments/assets/8d80ed10-12f2-4c0b-915f-63b70797a6ca" alt="Diff"/>
@@ -193,7 +228,7 @@ The decorator function also has access to the adapter in the chat buffer alongsi
193228
> [!IMPORTANT]
194229
> Each slash command may have their own unique configuration so be sure to check out the [config.lua](https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua) file
195230
196-
[Slash Commands](https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua#L114) (invoked with `/`) let you dynamically insert context into the chat buffer, such as file contents or date/time.
231+
[Slash Commands](https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua#L114) (invoked with `/` by default) let you dynamically insert context into the chat buffer, such as file contents or date/time.
197232

198233
The plugin supports providers like [telescope](https://github.com/nvim-telescope/telescope.nvim), [mini_pick](https://github.com/echasnovski/mini.pick), [fzf_lua](https://github.com/ibhagwan/fzf-lua) and [snacks.nvim](https://github.com/folke/snacks.nvim). By default, the plugin will automatically detect if you have any of those plugins installed and duly set them as the default provider. Failing that, the in-built `default` provider will be used. Please see the [Chat Buffer](/usage/chat-buffer/index) usage section for information on how to use Slash Commands.
199234

@@ -287,7 +322,7 @@ Credit to [@lazymaniac](https://github.com/lazymaniac) for the [inspiration](htt
287322

288323
## Tools
289324

290-
[Tools](https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua#L55) perform specific tasks (e.g., running shell commands, editing buffers, etc.) when invoked by an LLM. Multiple tools can be grouped together. Both can be referenced with `@` when in the chat buffer:
325+
[Tools](https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua#L55) perform specific tasks (e.g., running shell commands, editing buffers, etc.) when invoked by an LLM. Multiple tools can be grouped together. Both can be referenced with `@` (by default), when in the chat buffer:
291326

292327
```lua
293328
require("codecompanion").setup({
@@ -676,7 +711,7 @@ require("codecompanion").setup({
676711

677712
## Variables
678713

679-
[Variables](https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua#L90) are placeholders inserted into the chat buffer (using `#`). They provide contextual code or information about the current Neovim state. For instance, the built-in `#buffer` variable sends the current buffer’s contents to the LLM.
714+
[Variables](https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua#L90) are placeholders inserted into the chat buffer (using `#` by default). They provide contextual code or information about the current Neovim state. For instance, the built-in `#{buffer}` variable sends the current buffer’s contents to the LLM.
680715

681716
You can even define your own variables to share specific content:
682717

lua/codecompanion/commands/init.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
local codecompanion = require("codecompanion")
1010
local config = require("codecompanion.config")
11+
local triggers = require("codecompanion.triggers")
1112

1213
local _cached_adapters = nil
1314

@@ -40,7 +41,7 @@ return {
4041
cmd = "CodeCompanion",
4142
callback = function(opts)
4243
-- Detect the user calling a prompt from the prompt library
43-
if opts.fargs[1] and string.sub(opts.fargs[1], 1, 1) == "/" then
44+
if opts.fargs[1] and string.sub(opts.fargs[1], 1, 1) == triggers.mappings.slash_commands then
4445
-- Get the prompt minus the slash
4546
local prompt = string.sub(opts.fargs[1], 2)
4647

@@ -103,13 +104,13 @@ return {
103104

104105
-- Add prompt library items
105106
vim.iter(prompt_aliases):each(function(k)
106-
table.insert(completions, "/" .. k)
107+
table.insert(completions, triggers.mappings.slash_commands .. k)
107108
end)
108109

109110
-- Add inline variables
110111
for key, _ in pairs(config.interactions.inline.variables) do
111112
if key ~= "opts" then
112-
table.insert(completions, "#{" .. key .. "}")
113+
table.insert(completions, string.format("%s{" .. key .. "}", triggers.mappings.variables))
113114
end
114115
end
115116

lua/codecompanion/config.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,6 @@ If you are providing code changes, use the insert_edit_into_file tool (if availa
481481
opts = {
482482
acp = {
483483
enabled = true, -- Enable ACP command completion
484-
trigger = "\\", -- Trigger character for ACP commands
485484
},
486485
},
487486
},
@@ -1002,6 +1001,13 @@ The user is working on a %s machine. Please respond with system specific command
10021001
---@return boolean
10031002
send_code = true,
10041003

1004+
triggers = {
1005+
acp_slash_commands = "\\",
1006+
slash_commands = "/",
1007+
tools = "@",
1008+
variables = "#",
1009+
},
1010+
10051011
job_start_delay = 1500, -- Delay in milliseconds between cmd tools
10061012
submit_delay = 2000, -- Delay in milliseconds before auto-submitting the chat buffer
10071013
},

lua/codecompanion/interactions/chat/keymaps/init.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local async = require("plenary.async")
22
local completion = require("codecompanion.providers.completion")
33
local config = require("codecompanion.config")
4+
local triggers = require("codecompanion.triggers")
5+
46
local ts = require("codecompanion.utils.treesitter")
57
local ui_utils = require("codecompanion.utils.ui")
68
local utils = require("codecompanion.utils")
@@ -217,9 +219,9 @@ M.completion = {
217219
if item.label then
218220
-- Add bracket wrapping for variables and tools like cmp/blink do
219221
if item.type == "variable" then
220-
item.word = string.format("#{%s}", item.label:sub(2))
222+
item.word = string.format("%s{%s}", triggers.mappings.variables, item.label:sub(2))
221223
elseif item.type == "tool" then
222-
item.word = string.format("@{%s}", item.label:sub(2))
224+
item.word = string.format("%s{%s}", triggers.mappings.tools, item.label:sub(2))
223225
else
224226
item.word = item.label
225227
end

lua/codecompanion/interactions/chat/tools/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ local tool_filter = require("codecompanion.interactions.chat.tools.filter")
2020
local config = require("codecompanion.config")
2121
local log = require("codecompanion.utils.log")
2222
local regex = require("codecompanion.utils.regex")
23+
local triggers = require("codecompanion.triggers")
2324
local ui_utils = require("codecompanion.utils.ui")
2425
local utils = require("codecompanion.utils")
2526

@@ -28,7 +29,7 @@ local api = vim.api
2829
local show_tools_processing = config.display.chat.show_tools_processing
2930

3031
local CONSTANTS = {
31-
PREFIX = "@",
32+
PREFIX = triggers.mappings.tools,
3233

3334
NS_TOOLS = "CodeCompanion-tools",
3435
AUTOCMD_GROUP = "codecompanion.tools",

lua/codecompanion/interactions/chat/variables/init.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
local config = require("codecompanion.config")
2+
local triggers = require("codecompanion.triggers")
3+
24
local log = require("codecompanion.utils.log")
35
local regex = require("codecompanion.utils.regex")
46

57
local CONSTANTS = {
6-
PREFIX = "#",
8+
PREFIX = triggers.mappings.variables,
79
}
810

911
---Check a message for any parameters that have been given to the variable

lua/codecompanion/interactions/inline/variables/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
local config = require("codecompanion.config")
1414
local log = require("codecompanion.utils.log")
1515
local regex = require("codecompanion.utils.regex")
16+
local triggers = require("codecompanion.triggers")
1617

1718
local CONSTANTS = {
18-
PREFIX = "#",
19+
PREFIX = triggers.mappings.variables,
1920
}
2021

2122
---@class CodeCompanion.Inline.Variable

lua/codecompanion/providers/completion/blink/init.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
local completion = require("codecompanion.providers.completion")
44
local config = require("codecompanion.config")
5+
local triggers = require("codecompanion.triggers")
56

67
--- @class blink.cmp.Source
78
local M = {}
@@ -11,11 +12,12 @@ function M.new()
1112
end
1213

1314
function M:get_trigger_characters()
14-
local triggers = { "/", "#", "@" }
15+
local trigger_chars = { triggers.mappings.slash_commands, triggers.mappings.tools, triggers.mappings.variables }
1516
if config.interactions.chat.slash_commands.opts.acp.enabled then
16-
table.insert(triggers, config.interactions.chat.slash_commands.opts.acp.trigger or "\\")
17+
table.insert(trigger_chars, triggers.mappings.acp_slash_commands)
1718
end
18-
return triggers
19+
20+
return trigger_chars
1921
end
2022

2123
function M:enabled()
@@ -38,7 +40,7 @@ function M:get_completions(ctx, callback)
3840
}
3941

4042
-- Slash commands
41-
if trigger_char == "/" then
43+
if trigger_char == triggers.mappings.slash_commands then
4244
callback({
4345
context = ctx,
4446
is_incomplete_forward = false,
@@ -68,7 +70,7 @@ function M:get_completions(ctx, callback)
6870
})
6971

7072
-- Variables
71-
elseif trigger_char == "#" then
73+
elseif trigger_char == triggers.mappings.variables then
7274
callback({
7375
context = ctx,
7476
is_incomplete_forward = false,
@@ -80,7 +82,7 @@ function M:get_completions(ctx, callback)
8082
kind = vim.lsp.protocol.CompletionItemKind.Variable,
8183
label = item.label:sub(2),
8284
textEdit = {
83-
newText = string.format("#{%s}", item.label:sub(2)),
85+
newText = string.format("%s{%s}", triggers.mappings.variables, item.label:sub(2)),
8486
range = edit_range,
8587
},
8688
documentation = {
@@ -94,7 +96,7 @@ function M:get_completions(ctx, callback)
9496
})
9597

9698
-- Tools
97-
elseif trigger_char == "@" then
99+
elseif trigger_char == triggers.mappings.tools then
98100
callback({
99101
context = ctx,
100102
is_incomplete_forward = false,
@@ -106,7 +108,7 @@ function M:get_completions(ctx, callback)
106108
kind = vim.lsp.protocol.CompletionItemKind.Struct,
107109
label = item.label:sub(2),
108110
textEdit = {
109-
newText = string.format("@{%s}", item.label:sub(2)),
111+
newText = string.format("%s{%s}", triggers.mappings.tools, item.label:sub(2)),
110112
range = edit_range,
111113
},
112114
documentation = {
@@ -120,7 +122,7 @@ function M:get_completions(ctx, callback)
120122
})
121123

122124
-- ACP commands
123-
elseif trigger_char == (config.interactions.chat.slash_commands.opts.acp.trigger or "\\") then
125+
elseif trigger_char == triggers.mappings.acp_slash_commands then
124126
local acp_cmds = completion.acp_commands(ctx.bufnr)
125127
local items = vim
126128
.iter(acp_cmds)

lua/codecompanion/providers/completion/cmp/acp_commands.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local cc_config = require("codecompanion.config")
22
local completion = require("codecompanion.providers.completion")
33

4+
local trigger = require("codecompanion.triggers").mappings.acp_slash_commands
5+
46
local source = {}
57

68
function source.new(config)
@@ -12,12 +14,10 @@ function source:is_available()
1214
end
1315

1416
function source:get_trigger_characters()
15-
local trigger = cc_config.interactions.chat.slash_commands.opts.acp.trigger or "\\"
1617
return { trigger }
1718
end
1819

1920
function source:get_keyword_pattern()
20-
local trigger = cc_config.interactions.chat.slash_commands.opts.acp.trigger or "\\"
2121
local escaped = vim.pesc(trigger)
2222
return escaped .. [[\w\+]]
2323
end
@@ -52,7 +52,7 @@ function source:execute(item, callback)
5252
local line = vim.api.nvim_get_current_line()
5353

5454
-- Remove the trigger character and partial command
55-
local before = line:sub(1, col):gsub("\\%w*$", "")
55+
local before = line:sub(1, col):gsub(string.format("%s%w*$", trigger), "")
5656
local after = line:sub(col + 1)
5757
local new_line = before .. text .. after
5858

0 commit comments

Comments
 (0)