Skip to content

Commit dc4006f

Browse files
committed
feat(command): add attach and detach
1 parent 267959c commit dc4006f

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

lua/copilot/command.lua

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,15 @@ function mod.status()
105105
end
106106

107107
---@param opts? { force?: boolean }
108-
function mod.toggle(opts)
108+
function mod.attach(opts)
109109
opts = opts or {}
110110

111-
if c.buf_is_attached(0) then
112-
c.buf_detach()
113-
return
114-
end
115-
116111
if not opts.force then
117112
local should_attach, no_attach_reason = u.should_attach()
118113
if not should_attach then
119114
vim.api.nvim_echo({
120115
{ "[Copilot] " .. no_attach_reason .. "\n" },
121-
{ "[Copilot] to force enable, run ':Copilot! toggle'" },
116+
{ "[Copilot] to force attach, run ':Copilot! attach'" },
122117
}, true, {})
123118
return
124119
end
@@ -129,6 +124,24 @@ function mod.toggle(opts)
129124
c.buf_attach(opts.force)
130125
end
131126

127+
function mod.detach()
128+
if c.buf_is_attached(0) then
129+
c.buf_detach()
130+
end
131+
end
132+
133+
---@param opts? { force?: boolean }
134+
function mod.toggle(opts)
135+
opts = opts or {}
136+
137+
if c.buf_is_attached(0) then
138+
mod.detach()
139+
return
140+
end
141+
142+
mod.attach(opts)
143+
end
144+
132145
function mod.enable()
133146
c.setup()
134147
require("copilot.panel").setup()

lua/copilot/config.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ local default_config = {
1414
},
1515
layout = {
1616
position = "bottom",
17-
ratio = 0.4
18-
}
17+
ratio = 0.4,
18+
},
1919
},
2020
---@class copilot_config_suggestion
2121
suggestion = {
@@ -46,7 +46,7 @@ local mod = {
4646

4747
function mod.setup(opts)
4848
if mod.config then
49-
vim.notify("[copilot] config is already set", vim.log.levels.WARN)
49+
vim.notify("[Copilot] config is already set", vim.log.levels.WARN)
5050
return mod.config
5151
end
5252

@@ -68,7 +68,11 @@ end
6868

6969
---@param key? string
7070
function mod.get(key)
71-
if mod.config and key then
71+
if not mod.config then
72+
error("[Copilot] not initialized")
73+
end
74+
75+
if key then
7276
return mod.config[key]
7377
end
7478

plugin/copilot.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
local completion_store = {
2-
[""] = { "auth", "panel", "suggestion", "status", "toggle", "version", "enable", "disable" },
2+
[""] = { "auth", "attach", "detach", "disable", "enable", "panel", "status", "suggestion", "toggle", "version" },
33
auth = { "signin", "signout" },
44
panel = { "accept", "jump_next", "jump_prev", "open", "refresh" },
5-
suggestion = { "accept", "accept_word", "accept_line", "dismiss", "next", "prev", "toggle_auto_trigger" },
5+
suggestion = { "accept", "accept_line", "accept_word", "dismiss", "next", "prev", "toggle_auto_trigger" },
66
}
77

88
vim.api.nvim_create_user_command("Copilot", function(opts)

0 commit comments

Comments
 (0)