Skip to content

Commit 267959c

Browse files
committed
feat(command): add enable and disable
1 parent 27f31b6 commit 267959c

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

lua/copilot/client.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ local api = require("copilot.api")
22
local config = require("copilot.config")
33
local util = require("copilot.util")
44

5+
local is_disabled = false
6+
57
local M = {
68
id = nil,
79
augroup = "copilot.client",
8-
disabled = false,
910
}
1011

1112
local function store_client_id(id)
@@ -55,7 +56,7 @@ end
5556

5657
---@param force? boolean
5758
function M.buf_attach(force)
58-
if M.disabled then
59+
if is_disabled then
5960
print("[Copilot] Offline")
6061
return
6162
end
@@ -78,9 +79,13 @@ function M.get()
7879
return vim.lsp.get_client_by_id(M.id)
7980
end
8081

82+
function M.is_disabled()
83+
return is_disabled
84+
end
85+
8186
---@param callback fun(client:table):nil
8287
function M.use_client(callback)
83-
if M.disabled then
88+
if is_disabled then
8489
print("[Copilot] Offline")
8590
return
8691
end
@@ -146,7 +151,7 @@ M.merge_server_opts = function(params)
146151
end
147152

148153
function M.setup()
149-
M.disabled = false
154+
is_disabled = false
150155

151156
M.config = M.merge_server_opts(config.get())
152157

@@ -174,7 +179,7 @@ function M.setup()
174179
end
175180

176181
function M.teardown()
177-
M.disabled = true
182+
is_disabled = true
178183

179184
vim.api.nvim_clear_autocmds({ group = M.augroup })
180185

lua/copilot/command.lua

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,14 @@ function mod.status()
4949
vim.api.nvim_echo(lines, true, {})
5050
end
5151

52+
if c.is_disabled() then
53+
flush_lines("Offline")
54+
return
55+
end
56+
5257
local client = c.get()
5358
if not client then
54-
flush_lines("Not running")
59+
flush_lines("Not Started")
5560
return
5661
end
5762

@@ -124,4 +129,16 @@ function mod.toggle(opts)
124129
c.buf_attach(opts.force)
125130
end
126131

132+
function mod.enable()
133+
c.setup()
134+
require("copilot.panel").setup()
135+
require("copilot.suggestion").setup()
136+
end
137+
138+
function mod.disable()
139+
c.teardown()
140+
require("copilot.panel").teardown()
141+
require("copilot.suggestion").teardown()
142+
end
143+
127144
return mod

plugin/copilot.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local completion_store = {
2-
[""] = { "auth", "panel", "suggestion", "status", "toggle", "version" },
2+
[""] = { "auth", "panel", "suggestion", "status", "toggle", "version", "enable", "disable" },
33
auth = { "signin", "signout" },
44
panel = { "accept", "jump_next", "jump_prev", "open", "refresh" },
55
suggestion = { "accept", "accept_word", "accept_line", "dismiss", "next", "prev", "toggle_auto_trigger" },
@@ -35,6 +35,13 @@ vim.api.nvim_create_user_command("Copilot", function(opts)
3535
return
3636
end
3737

38+
if mod_name == "command" then
39+
mod[action_name]({
40+
force = opts.bang,
41+
})
42+
return
43+
end
44+
3845
require("copilot.client").use_client(function()
3946
mod[action_name]({
4047
force = opts.bang,

0 commit comments

Comments
 (0)