Skip to content

Commit 330ffb9

Browse files
authored
Warning cleanup (zbirenbaum#381)
* refactor: resolve most Lua LSP warnings, leaving the unpack one for now as it seems to break the auth popup
1 parent e41c376 commit 330ffb9

File tree

5 files changed

+42
-41
lines changed

5 files changed

+42
-41
lines changed

lua/copilot/auth.lua

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function M.setup(client)
4646
height = height,
4747
width = width,
4848
})
49-
vim.api.nvim_win_set_option(winid, "winhighlight", "Normal:Normal")
49+
vim.api.nvim_set_option_value("winhighlight", "Normal:Normal", { win = winid })
5050

5151
return function()
5252
vim.api.nvim_win_close(winid, true)
@@ -150,26 +150,13 @@ local function find_config_path()
150150
end
151151
end
152152

153-
local function json_body(response)
154-
if response.headers["content-type"] == "application/json" then
155-
return vim.json.decode(response.body)
156-
end
157-
end
158-
159153
local function oauth_user(token)
160154
return vim.fn.system('curl -s --header "Authorization: Bearer ' .. token .. '" https://api.github.com/user')
161155
end
162156

163-
local function oauth_save(oauth_token)
164-
local user_data = oauth_user(oauth_token)
165-
local github = { oauth_token = oauth_token, user = user_data.login }
166-
return github
167-
end
168-
169157
M.get_cred = function()
170-
local userdata = vim.json.decode(
171-
vim.api.nvim_eval("readfile('" .. find_config_path() .. "/github-copilot/hosts.json')")[1]
172-
)
158+
local userdata =
159+
vim.json.decode(vim.api.nvim_eval("readfile('" .. find_config_path() .. "/github-copilot/hosts.json')")[1])
173160
local token = userdata["github.com"].oauth_token
174161
local user = oauth_user(token)
175162
return { user = user, token = token }

lua/copilot/client.lua

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ local is_disabled = false
77
local M = {
88
augroup = "copilot.client",
99
id = nil,
10+
--- @class copilot_capabilities:lsp.ClientCapabilities
11+
--- @field copilot table<'openURL', boolean>
1012
capabilities = nil,
1113
config = nil,
1214
node_version = nil,
@@ -15,7 +17,7 @@ local M = {
1517
initialized = false,
1618
}
1719

18-
---@param id number
20+
---@param id integer
1921
local function store_client_id(id)
2022
if M.id and M.id ~= id then
2123
if vim.lsp.get_client_by_id(M.id) then
@@ -53,7 +55,7 @@ function M.get_node_version()
5355
local node = config.get("copilot_node_command")
5456

5557
local cmd = { node, "--version" }
56-
local cmd_output_table = vim.fn.executable(node) == 1 and vim.fn.systemlist(cmd, nil, false) or { "" }
58+
local cmd_output_table = vim.fn.executable(node) == 1 and vim.fn.systemlist(cmd, nil, 0) or { "" }
5759
local cmd_output = cmd_output_table[#cmd_output_table]
5860
local cmd_exit_code = vim.v.shell_error
5961

@@ -144,18 +146,30 @@ function M.use_client(callback)
144146
error("copilot.setup is not called yet")
145147
end
146148

147-
local client_id = vim.lsp.start_client(M.config)
149+
local client_id, err = vim.lsp.start_client(M.config)
150+
151+
if not client_id then
152+
error(string.format("[Copilot] Error starting LSP Client: %s", err))
153+
return
154+
end
155+
148156
store_client_id(client_id)
149157

150-
client = M.get()
158+
client = M.get() --[[@as table]]
151159
end
152160

153161
if client.initialized then
154162
callback(client)
155163
return
156164
end
157165

158-
local timer = vim.loop.new_timer()
166+
local timer, err, _ = vim.loop.new_timer()
167+
168+
if not timer then
169+
error(string.format("[Copilot] Error creating timer: %s", err))
170+
return
171+
end
172+
159173
timer:start(
160174
0,
161175
100,
@@ -189,7 +203,7 @@ local function prepare_client_config(overrides)
189203

190204
M.startup_error = nil
191205

192-
local capabilities = vim.lsp.protocol.make_client_capabilities()
206+
local capabilities = vim.lsp.protocol.make_client_capabilities() --[[@as copilot_capabilities]]
193207
capabilities.copilot = {
194208
openURL = true,
195209
}

lua/copilot/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ M.setup = function(opts)
3434
highlight.setup()
3535

3636
local conf = config.setup(opts)
37-
if conf.panel.enabled then
37+
if conf and conf.panel.enabled then
3838
create_cmds()
3939
end
4040

lua/copilot/panel.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ local function is_panel_uri(bufname)
7979
end
8080

8181
function panel:lock()
82-
vim.api.nvim_buf_set_option(self.bufnr, "modifiable", false)
83-
vim.api.nvim_buf_set_option(self.bufnr, "readonly", true)
82+
vim.api.nvim_set_option_value("modifiable", false, { buf = self.bufnr })
83+
vim.api.nvim_set_option_value("readonly", true, { buf = self.bufnr })
8484
return self
8585
end
8686

8787
function panel:unlock()
88-
vim.api.nvim_buf_set_option(self.bufnr, "modifiable", true)
89-
vim.api.nvim_buf_set_option(self.bufnr, "readonly", false)
88+
vim.api.nvim_set_option_value("modifiable", true, { buf = self.bufnr })
89+
vim.api.nvim_set_option_value("readonly", false, { buf = self.bufnr })
9090
return self
9191
end
9292

@@ -301,14 +301,14 @@ function panel:ensure_bufnr()
301301
swapfile = false,
302302
undolevels = 0,
303303
}) do
304-
vim.api.nvim_buf_set_option(self.bufnr, name, value)
304+
vim.api.nvim_set_option_value(name, value, { buf = self.bufnr })
305305
end
306306

307307
set_keymap(self.bufnr)
308308
end
309309

310310
vim.api.nvim_buf_set_name(self.bufnr, self.panel_uri)
311-
vim.api.nvim_buf_set_option(self.bufnr, "filetype", self.filetype)
311+
vim.api.nvim_set_option_value("filetype", self.filetype, { buf = self.bufnr })
312312
end
313313

314314
function panel:ensure_winid()
@@ -362,7 +362,7 @@ function panel:ensure_winid()
362362
relativenumber = false,
363363
signcolumn = "no",
364364
}) do
365-
vim.api.nvim_win_set_option(self.winid, name, value)
365+
vim.api.nvim_set_option_value(name, value, { win = self.winid })
366366
end
367367

368368
vim.api.nvim_create_augroup(self.augroup, { clear = true })
@@ -491,7 +491,7 @@ function panel:refresh()
491491
end
492492
)
493493

494-
self.state.req_id = id
494+
self.state.req_id = id.solutionCountTarget
495495
end
496496

497497
function panel:init()

lua/copilot/util.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local config = require("copilot.config")
2-
32
local unpack = unpack or table.unpack
43

54
local M = {}
@@ -97,7 +96,8 @@ end
9796
---@return boolean should_attach
9897
---@return string? no_attach_reason
9998
function M.should_attach()
100-
local ft_disabled, ft_disabled_reason = is_ft_disabled(vim.bo.filetype, config.get("filetypes"))
99+
local ft = config.get("filetypes") --[[@as table<string, boolean>]]
100+
local ft_disabled, ft_disabled_reason = is_ft_disabled(vim.bo.filetype, ft)
101101

102102
if ft_disabled then
103103
return not ft_disabled, ft_disabled_reason
@@ -195,15 +195,15 @@ end
195195

196196
---@return copilot_editor_configuration
197197
function M.get_editor_configuration()
198-
local conf = config.get()
198+
local conf = config.get() --[[@as copilot_config]]
199199

200-
local filetypes = vim.deepcopy(conf.filetypes)
200+
local filetypes = vim.deepcopy(conf.filetypes) --[[@as table<string, boolean>]]
201201

202202
if filetypes["*"] == nil then
203203
filetypes = vim.tbl_deep_extend("keep", filetypes, internal_filetypes)
204204
end
205205

206-
local copilot_model = conf.copilot_model ~= "" and conf.copilot_model or ""
206+
local copilot_model = conf and conf.copilot_model ~= "" and conf.copilot_model or ""
207207

208208
---@type string[]
209209
local disabled_filetypes = vim.tbl_filter(function(ft)
@@ -303,11 +303,11 @@ end
303303
---@param str string
304304
---@return integer
305305
function M.strutf16len(str)
306-
return vim.fn.strchars(vim.fn.substitute(str, [==[\\%#=2[^\u0001-\uffff]]==], " ", "g"))
307-
end
308-
309-
if vim.fn.exists("*strutf16len") == 1 then
310-
M.strutf16len = vim.fn.strutf16len
306+
if vim.fn.exists("*strutf16len") == 1 then
307+
return vim.fn.strutf16len(str)
308+
else
309+
return vim.fn.strchars(vim.fn.substitute(str, [==[\\%#=2[^\u0001-\uffff]]==], " ", "g"))
310+
end
311311
end
312312

313313
return M

0 commit comments

Comments
 (0)