Skip to content

Commit c260c7f

Browse files
chore: some improvements suggested by the reviewer
1 parent e725159 commit c260c7f

File tree

1 file changed

+7
-40
lines changed

1 file changed

+7
-40
lines changed

lsp/gitlab_duo.lua

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,12 @@ local function save_token(token_data)
116116
end
117117

118118
local function load_token()
119-
local file = io.open(config.token_file, 'r')
120-
if file then
121-
local content = file:read('*all')
122-
file:close()
123-
local ok, token_data = pcall(vim.json.decode, content)
124-
if ok then
125-
return token_data
126-
end
119+
if vim.fn.filereadable(config.token_file) == 0 then
120+
return nil
127121
end
128-
return nil
122+
123+
local blob = vim.fn.readblob(config.token_file)
124+
return vim.json.decode(blob)
129125
end
130126

131127
local function is_token_expired(token_data)
@@ -195,11 +191,7 @@ local function device_authorization()
195191
return nil
196192
end
197193

198-
local ok, data = pcall(vim.json.decode, response.body)
199-
if not ok then
200-
vim.notify('Failed to parse authorization response', vim.log.levels.ERROR)
201-
return nil
202-
end
194+
local data = vim.json.decode(response.body)
203195

204196
return data
205197
end
@@ -273,32 +265,7 @@ local function sign_in(_bufnr, client)
273265
return
274266
end
275267

276-
local message = string.format(
277-
'GitLab Duo Authorization\n\n'
278-
.. '1. Open: %s\n'
279-
.. '2. Enter code: %s\n\n'
280-
.. 'Code copied to clipboard. Waiting for authorization...',
281-
auth_data.verification_uri,
282-
auth_data.user_code
283-
)
284-
285-
-- Copy code to clipboard
286-
vim.fn.setreg('+', auth_data.user_code)
287-
vim.fn.setreg('*', auth_data.user_code)
288-
289-
-- Show message with option to open browser
290-
local choice = vim.fn.confirm(message, '&Open Browser\n&Continue')
291-
292-
if choice == 1 then
293-
-- Try to open browser
294-
local open_cmd = vim.fn.has('mac') == 1 and 'open'
295-
or vim.fn.has('unix') == 1 and 'xdg-open'
296-
or vim.fn.has('win32') == 1 and 'start'
297-
298-
if open_cmd then
299-
vim.fn.system(open_cmd .. ' ' .. auth_data.verification_uri)
300-
end
301-
end
268+
vim.ui.open(auth_data.verification_uri .. '?user_code=' .. auth_data.user_code)
302269

303270
poll_for_token(auth_data.device_code, auth_data.interval or 5, client)
304271
end

0 commit comments

Comments
 (0)