Skip to content

Commit 257c801

Browse files
committed
WIP
1 parent e73fe7c commit 257c801

File tree

6 files changed

+106
-12
lines changed

6 files changed

+106
-12
lines changed

README.md

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# copilot.lua
22

33
This plugin is the pure lua replacement for [github/copilot.vim](https://github.com/github/copilot.vim).
4+
A huge thank you to @tris203 for the code behind the nes functionality ([copilot-lsp](https://github.com/copilotlsp-nvim/copilot-lsp)).
45

56
<details>
67
<summary>Motivation behind `copilot.lua`</summary>
@@ -48,7 +49,11 @@ Install the plugin with your preferred plugin manager.
4849
For example, with [packer.nvim](https://github.com/wbthomason/packer.nvim):
4950

5051
```lua
51-
use { "zbirenbaum/copilot.lua" }
52+
use { "zbirenbaum/copilot.lua"
53+
requires = {
54+
"copilotlsp-nvim/copilot-lsp", -- (optional) for NES functionality
55+
},
56+
}
5257
```
5358

5459
### Authentication
@@ -93,6 +98,9 @@ For example:
9398
```lua
9499
use {
95100
"zbirenbaum/copilot.lua",
101+
requires = {
102+
"copilotlsp-nvim/copilot-lsp", -- (optional) for NES functionality
103+
},
96104
cmd = "Copilot",
97105
event = "InsertEnter",
98106
config = function()
@@ -136,16 +144,8 @@ require('copilot').setup({
136144
dismiss = "<C-]>",
137145
},
138146
},
139-
filetypes = {
140-
yaml = false,
141-
markdown = false,
142-
help = false,
143-
gitcommit = false,
144-
gitrebase = false,
145-
hgcommit = false,
146-
svn = false,
147-
cvs = false,
148-
["."] = false,
147+
nes = {
148+
enabled = false, -- requires copilot-lsp as a dependency
149149
},
150150
auth_provider_url = nil, -- URL to authentication provider, if not "https://github.com/"
151151
logger = {
@@ -270,6 +270,30 @@ require("copilot.suggestion").toggle_auto_trigger()
270270
```
271271
These can also be accessed through the `:Copilot suggestion <function>` command (eg. `:Copilot suggestion accept`).
272272

273+
### nes (next edit suggestion)
274+
275+
When `enabled` is `true`, copilot will provide suggestions based on the next edit you are likely to make, through [copilot-lsp](https://github.com/copilotlsp-nvim/copilot-lsp).
276+
For additional configurations, please refer to the [copilot-lsp documentation](https://github.com/copilotlsp-nvim/copilot-lsp/blob/main/README.md).
277+
278+
Configurations are better placed inside the require/dependency definition as such:
279+
280+
```lua
281+
use {
282+
"zbirenbaum/copilot.lua",
283+
requires = {
284+
"copilotlsp-nvim/copilot-lsp",
285+
init = function()
286+
vim.g.copilot_nes_debounce = 500
287+
end,
288+
},
289+
cmd = "Copilot",
290+
event = "InsertEnter",
291+
config = function()
292+
require("copilot").setup({})
293+
end,
294+
}
295+
```
296+
273297
### filetypes
274298

275299
Specify filetypes for attaching copilot.

lua/copilot/client/config.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ function M.prepare_client_config(overrides, client)
122122
if token_env_set then
123123
require("copilot.auth").signin()
124124
end
125+
126+
if config.nes.enabled then
127+
local au = vim.api.nvim_create_augroup("copilotlsp.init", { clear = true })
128+
129+
local ok, err = pcall(function()
130+
require("copilot-lsp.nes").lsp_on_init(lsp_client, au)
131+
end)
132+
133+
if ok then
134+
logger.info("copilot-lsp.nes loaded")
135+
else
136+
logger.error("copilot-lsp.nes failed to load:", err)
137+
end
138+
end
125139
end)
126140
end,
127141
on_exit = function(code, _, client_id)

lua/copilot/config/init.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local logger = require("copilot.logger")
55
---@field suggestion SuggestionConfig
66
---@field logger LoggerConfig
77
---@field server ServerConfig
8+
---@field nes NesConfig
89
---@field filetypes table<string, boolean> Filetypes to enable Copilot for
910
---@field auth_provider_url string|nil URL for the authentication provider
1011
---@field workspace_folders string[] Workspace folders to enable Copilot for
@@ -23,6 +24,7 @@ local M = {
2324
suggestion = require("copilot.config.suggestion").default,
2425
logger = require("copilot.config.logger").default,
2526
server = require("copilot.config.server").default,
27+
nes = require("copilot.config.nes").default,
2628
root_dir = require("copilot.config.root_dir").default,
2729
should_attach = require("copilot.config.should_attach").default,
2830
filetypes = {},
@@ -59,6 +61,7 @@ end
5961
function M.validate(config)
6062
vim.validate("panel", config.panel, "table")
6163
vim.validate("suggestion", config.suggestion, "table")
64+
vim.validate("nes", config.nes, "table")
6265
vim.validate("logger", config.logger, "table")
6366
vim.validate("server", config.server, "table")
6467
vim.validate("filetypes", config.filetypes, "table")
@@ -68,14 +71,15 @@ function M.validate(config)
6871
vim.validate("copilot_model", config.copilot_model, { "string", "nil" })
6972
vim.validate("root_dir", config.root_dir, { "string", "function" })
7073
vim.validate("should_attach", config.should_attach, "function")
71-
vim.validate("copilot_node_command", config.copilot_node_command, {"string", "table"})
74+
vim.validate("copilot_node_command", config.copilot_node_command, { "string", "table" })
7275

7376
require("copilot.config.panel").validate(config.panel)
7477
require("copilot.config.suggestion").validate(config.suggestion)
7578
require("copilot.config.logger").validate(config.logger)
7679
require("copilot.config.server").validate(config.server)
7780
require("copilot.config.root_dir").validate(config.root_dir)
7881
require("copilot.config.should_attach").validate(config.should_attach)
82+
require("copilot.config.nes").validate(config.nes)
7983
end
8084

8185
return M

lua/copilot/config/nes.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
local logger = require("copilot.logger")
2+
---@class NesConfig
3+
---@field enabled boolean Whether to enable nes (next edit suggestions)
4+
5+
local M = {
6+
---@type NesConfig
7+
default = {
8+
enabled = false,
9+
},
10+
}
11+
12+
---@type NesConfig
13+
M.config = vim.deepcopy(M.default)
14+
15+
---@param opts? NesConfig
16+
function M.setup(opts)
17+
opts = opts or {}
18+
M.config = vim.tbl_deep_extend("force", M.default, opts)
19+
end
20+
21+
---@param config NesConfig
22+
function M.validate(config)
23+
vim.validate("enabled", config.enabled, "boolean")
24+
25+
if config.enabled then
26+
local has_nes, _ = pcall(function()
27+
require("copilot-lsp.nes")
28+
end)
29+
30+
if not has_nes then
31+
logger.error(
32+
"copilot-lsp is not available, disabling nes.\nPlease refer to the documentation and ensure it is installed."
33+
)
34+
config.enabled = false
35+
end
36+
end
37+
end
38+
39+
return M

lua/copilot/highlight.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
local logger = require("copilot.logger")
2+
local config = require("copilot.config")
3+
14
local M = {
25
group = {
36
CopilotAnnotation = "CopilotAnnotation",
@@ -19,6 +22,16 @@ function M.setup()
1922
vim.api.nvim_set_hl(0, from_group, { link = to_group })
2023
end
2124
end
25+
26+
if config.nes.enabled then
27+
local ok, err = pcall(function()
28+
require("copilot-lsp.util").set_hl()
29+
end)
30+
31+
if not ok then
32+
logger.error("Error setting copilot-lsp highlights: ", err)
33+
end
34+
end
2235
end)
2336
end
2437

lua/copilot/nes/init.lua

Whitespace-only changes.

0 commit comments

Comments
 (0)