Skip to content

Commit d03e265

Browse files
committed
breaking change: require('copilot') -> require('copilot').setup() + formatting
1 parent 427cf22 commit d03e265

File tree

7 files changed

+130
-112
lines changed

7 files changed

+130
-112
lines changed

.stylua.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
column_width = 120
2+
line_endings = "Unix"
3+
indent_type = "Spaces"
4+
indent_width = 2
5+
quote_style = "AutoPreferDouble"
6+
no_call_parentheses = false

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ The following is the default configuration:
5656

5757
```lua
5858
{
59-
plugin_manager_path = vim.fn.stdpath("data") .. "/site/pack/packer", -- Installation Path of packer, change to the plugin manager installation path of your choice
59+
plugin_manager_path = vim.fn.stdpath("data") .. "/site/pack/packer", -- Installation Path of packer, change to the plugin manager installation path of your choice
6060
}
6161
```

lua/copilot/config.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
local M = {}
22

33
local defaults = {
4-
plugin_manager_path = vim.fn.stdpath("data") .. "/site/pack/packer",
4+
plugin_manager_path = vim.fn.stdpath("data") .. "/site/pack/packer",
55
}
66

77
M.params = {}
88

99
M.setup = function(options)
10-
if not options then
11-
M.params = defaults
12-
else
13-
for key, value in pairs(defaults) do
14-
M.params[key] = options[key] or value
15-
end
16-
end
10+
if not options then
11+
M.params = defaults
12+
else
13+
for key, value in pairs(defaults) do
14+
M.params[key] = options[key] or value
15+
end
16+
end
1717
end
1818

1919
return M

lua/copilot/copilot_handler.lua

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,58 @@
1-
local user_data = require('copilot.setup').get_cred()
2-
local util = require('copilot.util')
1+
local user_data = require("copilot.setup").get_cred()
2+
local util = require("copilot.util")
33
local M = {}
44

5-
local send_editor_info = function (a, b, c, d)
6-
local responses = vim.lsp.buf_request_sync(0, 'setEditorInfo', {
7-
editorPluginInfo = {
8-
name = 'copilot.vim',
9-
version = '1.1.0',
10-
},
11-
editorInfo= {
12-
version = '0.7.0-dev+1343-g4d3acd6be-dirty',
13-
name = "Neovim",
14-
},
15-
}, 600)
5+
local send_editor_info = function(a, b, c, d)
6+
local responses = vim.lsp.buf_request_sync(0, "setEditorInfo", {
7+
editorPluginInfo = {
8+
name = "copilot.vim",
9+
version = "1.1.0",
10+
},
11+
editorInfo = {
12+
version = "0.7.0-dev+1343-g4d3acd6be-dirty",
13+
name = "Neovim",
14+
},
15+
}, 600)
1616
end
1717

1818
local capabilities = vim.lsp.protocol.make_client_capabilities()
1919
capabilities.getCompletions = true
2020

21-
M.start = function (plugin_path)
22-
vim.lsp.start_client({
23-
cmd = {require('copilot.util').get_copilot_path(plugin_path)},
24-
cmd_env = {
25-
["GITHUB_USER"] = user_data.user,
26-
["GITHUB_TOKEN"] = user_data.token,
27-
["COPILOT_AGENT_VERBOSE"] = 1,
28-
},
29-
handlers={
30-
["getCompletions"] = function () print("get completions") end,
31-
["textDocumentSync"] = function () print("handle") end,
32-
},
33-
name = "copilot",
34-
trace = "messages",
35-
root_dir = vim.loop.cwd(),
36-
autostart = true,
37-
on_init = function(client, _)
38-
vim.lsp.buf_attach_client(0, client.id)
39-
vim.api.nvim_create_autocmd({'BufEnter'}, {
40-
callback = function ()
41-
if not vim.lsp.buf_get_clients(0)[client.id] then
42-
vim.lsp.buf_attach_client(0, client.id)
43-
end
44-
end,
45-
once = false,
46-
})
21+
M.start = function(plugin_path)
22+
vim.lsp.start_client({
23+
cmd = { require("copilot.util").get_copilot_path(plugin_path) },
24+
cmd_env = {
25+
["GITHUB_USER"] = user_data.user,
26+
["GITHUB_TOKEN"] = user_data.token,
27+
["COPILOT_AGENT_VERBOSE"] = 1,
28+
},
29+
handlers = {
30+
["getCompletions"] = function()
31+
print("get completions")
4732
end,
48-
on_attach = function()
49-
send_editor_info()
50-
end
51-
})
33+
["textDocumentSync"] = function()
34+
print("handle")
35+
end,
36+
},
37+
name = "copilot",
38+
trace = "messages",
39+
root_dir = vim.loop.cwd(),
40+
autostart = true,
41+
on_init = function(client, _)
42+
vim.lsp.buf_attach_client(0, client.id)
43+
vim.api.nvim_create_autocmd({ "BufEnter" }, {
44+
callback = function()
45+
if not vim.lsp.buf_get_clients(0)[client.id] then
46+
vim.lsp.buf_attach_client(0, client.id)
47+
end
48+
end,
49+
once = false,
50+
})
51+
end,
52+
on_attach = function()
53+
send_editor_info()
54+
end,
55+
})
5256
end
5357

5458
return M

lua/copilot/init.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
local config = require("copilot.config")
22
local M = {}
33

4-
M.setup = function (params)
5-
config.setup(params)
6-
config.params.plugin_manager_path = vim.fn.expand(config.params.plugin_manager_path) -- resolve wildcard and variable containing paths
7-
require("copilot.copilot_handler").start(config.params.plugin_manager_path)
4+
M.setup = function(params)
5+
config.setup(params)
6+
config.params.plugin_manager_path = vim.fn.expand(config.params.plugin_manager_path) -- resolve wildcard and variable containing paths
7+
require("copilot.copilot_handler").start(config.params.plugin_manager_path)
88
end
99

10-
1110
return M

lua/copilot/setup.lua

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,49 @@
11
local M = {}
22

33
local function find_config_path()
4-
local config = vim.fn.expand('$XDG_CONFIG_HOME')
5-
if config and vim.fn.isdirectory(config) > 0 then
4+
local config = vim.fn.expand("$XDG_CONFIG_HOME")
5+
if config and vim.fn.isdirectory(config) > 0 then
6+
return config
7+
elseif vim.fn.has("win32") > 0 then
8+
config = vim.fn.expand("~/AppData/Local")
9+
if vim.fn.isdirectory(config) > 0 then
610
return config
7-
elseif vim.fn.has('win32') > 0 then
8-
config = vim.fn.expand('~/AppData/Local')
9-
if vim.fn.isdirectory(config) > 0 then return config end
10-
else
11-
config = vim.fn.expand('~/.config')
12-
if vim.fn.isdirectory(config) > 0 then return config
13-
else print("Error: could not find config path")
14-
end
15-
end
11+
end
12+
else
13+
config = vim.fn.expand("~/.config")
14+
if vim.fn.isdirectory(config) > 0 then
15+
return config
16+
else
17+
print("Error: could not find config path")
18+
end
19+
end
1620
end
1721

18-
1922
local function json_body(response)
20-
if response.headers['content-type'] == 'application/json' then
21-
return vim.json.decode(response.body)
22-
end
23+
if response.headers["content-type"] == "application/json" then
24+
return vim.json.decode(response.body)
25+
end
2326
end
2427

2528
local function oauth_user(token)
26-
return vim.json.decode(vim.fn.system("curl -s --header \"Authorization: Bearer " .. token .. "\" https://api.github.com/user"))
29+
return vim.json.decode(
30+
vim.fn.system('curl -s --header "Authorization: Bearer ' .. token .. '" https://api.github.com/user')
31+
)
2732
end
2833

2934
local function oauth_save(oauth_token)
30-
local user_data = oauth_user(oauth_token)
31-
local github = {oauth_token = oauth_token, user = user_data.login}
32-
return github
35+
local user_data = oauth_user(oauth_token)
36+
local github = { oauth_token = oauth_token, user = user_data.login }
37+
return github
3338
end
3439

35-
M.get_cred = function ()
36-
local userdata = vim.json.decode(vim.api.nvim_eval("readfile('" .. find_config_path() .. "/github-copilot/hosts.json')")[1])
37-
local token = userdata["github.com"].oauth_token
38-
local user = oauth_user(token)
39-
return {user = user, token = token}
40+
M.get_cred = function()
41+
local userdata = vim.json.decode(
42+
vim.api.nvim_eval("readfile('" .. find_config_path() .. "/github-copilot/hosts.json')")[1]
43+
)
44+
local token = userdata["github.com"].oauth_token
45+
local user = oauth_user(token)
46+
return { user = user, token = token }
4047
end
4148

4249
return M

lua/copilot/util.lua

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,58 @@
11
local M = {}
22

33
local format_pos = function()
4-
local pos = vim.api.nvim_win_get_cursor(0)
5-
return { character = pos[2], line = pos[1]-1 }
4+
local pos = vim.api.nvim_win_get_cursor(0)
5+
return { character = pos[2], line = pos[1] - 1 }
66
end
77

88
local get_relfile = function()
9-
local file,_= string.gsub(vim.api.nvim_buf_get_name(0), vim.loop.cwd() .. "/", '')
10-
return file
9+
local file, _ = string.gsub(vim.api.nvim_buf_get_name(0), vim.loop.cwd() .. "/", "")
10+
return file
1111
end
1212

1313
M.get_completion_params = function()
14-
local params = {
15-
options = vim.empty_dict(),
16-
doc = {
17-
relativePath = get_relfile(),
18-
source = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), '\n'),
19-
languageId = vim.bo.filetype,
20-
insertSpaces = true,
21-
tabsize = vim.bo.shiftwidth,
22-
indentsize = vim.bo.shiftwidth,
23-
position = format_pos(),
24-
path = vim.api.nvim_buf_get_name(0),
25-
},
26-
}
27-
return params
14+
local params = {
15+
options = vim.empty_dict(),
16+
doc = {
17+
relativePath = get_relfile(),
18+
source = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n"),
19+
languageId = vim.bo.filetype,
20+
insertSpaces = true,
21+
tabsize = vim.bo.shiftwidth,
22+
indentsize = vim.bo.shiftwidth,
23+
position = format_pos(),
24+
path = vim.api.nvim_buf_get_name(0),
25+
},
26+
}
27+
return params
2828
end
2929

3030
M.get_copilot_path = function(plugin_path)
31-
for _, loc in ipairs{"/opt", "/start", ""} do
32-
local copilot_path = plugin_path .. loc .. "/copilot.lua/copilot/index.js"
33-
if vim.fn.filereadable(copilot_path) ~= 0 then
34-
return copilot_path
35-
end
36-
end
31+
for _, loc in ipairs({ "/opt", "/start", "" }) do
32+
local copilot_path = plugin_path .. loc .. "/copilot.lua/copilot/index.js"
33+
if vim.fn.filereadable(copilot_path) ~= 0 then
34+
return copilot_path
35+
end
36+
end
3737
end
3838

39-
local function completion_handler (_, result, _, _)
40-
print(vim.inspect(result))
39+
local function completion_handler(_, result, _, _)
40+
print(vim.inspect(result))
4141
end
4242

43-
M.register_completion_handler = function (handler)
44-
if handler then completion_handler = handler end
43+
M.register_completion_handler = function(handler)
44+
if handler then
45+
completion_handler = handler
46+
end
4547
end
4648

4749
M.send_completion_request = function()
48-
local params = M.get_completion_params()
49-
vim.lsp.buf_request(0, 'getCompletions', params, completion_handler)
50+
local params = M.get_completion_params()
51+
vim.lsp.buf_request(0, "getCompletions", params, completion_handler)
5052
end
5153

5254
M.create_request_autocmd = function(group)
53-
vim.api.nvim_create_autocmd(group, {callback = M.send_completion_request})
55+
vim.api.nvim_create_autocmd(group, { callback = M.send_completion_request })
5456
end
5557

5658
return M

0 commit comments

Comments
 (0)