refactor(types): extract inline anonymous types into named classes (#110)
Problem: several functions used inline `{...}` table types in their
`@param` and `@return` annotations, making them hard to read and
impossible to reference from other modules.
Solution: extract each into a named `---@class`: `pending.Metadata`,
`pending.TaskFields`, `pending.CompletionItem`, `pending.SystemResult`,
and `pending.OAuthClientOpts`.
This commit is contained in:
parent
24bc1e395b
commit
34a68db6d0
5 changed files with 123 additions and 45 deletions
|
|
@ -17,62 +17,39 @@ local BUNDLED_CLIENT_SECRET = 'PLACEHOLDER'
|
|||
---@field expires_in? integer
|
||||
---@field obtained_at? integer
|
||||
|
||||
---@class pending.OAuthClient
|
||||
---@class pending.OAuthClientOpts
|
||||
---@field name string
|
||||
---@field scope string
|
||||
---@field port integer
|
||||
---@field config_key string
|
||||
|
||||
---@class pending.OAuthClient : pending.OAuthClientOpts
|
||||
local OAuthClient = {}
|
||||
OAuthClient.__index = OAuthClient
|
||||
|
||||
local util = require('pending.sync.util')
|
||||
|
||||
local _active_close = nil
|
||||
local _sync_in_flight = false
|
||||
|
||||
---@class pending.oauth
|
||||
local M = {}
|
||||
|
||||
---@param args string[]
|
||||
---@param opts? table
|
||||
---@return { code: integer, stdout: string, stderr: string }
|
||||
function M.system(args, opts)
|
||||
local co = coroutine.running()
|
||||
if not co then
|
||||
return vim.system(args, opts or {}):wait() --[[@as { code: integer, stdout: string, stderr: string }]]
|
||||
end
|
||||
vim.system(args, opts or {}, function(result)
|
||||
vim.schedule(function()
|
||||
coroutine.resume(co, result)
|
||||
end)
|
||||
end)
|
||||
return coroutine.yield() --[[@as { code: integer, stdout: string, stderr: string }]]
|
||||
end
|
||||
|
||||
---@param fn fun(): nil
|
||||
function M.async(fn)
|
||||
coroutine.resume(coroutine.create(fn))
|
||||
end
|
||||
M.system = util.system
|
||||
M.async = util.async
|
||||
|
||||
---@param client pending.OAuthClient
|
||||
---@param name string
|
||||
---@param callback fun(access_token: string): nil
|
||||
function M.with_token(client, name, callback)
|
||||
if _sync_in_flight then
|
||||
require('pending.log').warn(name .. ': Sync already in progress — please wait.')
|
||||
return
|
||||
end
|
||||
_sync_in_flight = true
|
||||
M.async(function()
|
||||
local token = client:get_access_token()
|
||||
if not token then
|
||||
_sync_in_flight = false
|
||||
require('pending.log').warn(name .. ': Not authenticated — run :Pending auth.')
|
||||
return
|
||||
end
|
||||
local ok, err = pcall(callback, token)
|
||||
_sync_in_flight = false
|
||||
if not ok then
|
||||
require('pending.log').error(name .. ': ' .. tostring(err))
|
||||
end
|
||||
util.async(function()
|
||||
util.with_guard(name, function()
|
||||
local token = client:get_access_token()
|
||||
if not token then
|
||||
require('pending.log').warn(name .. ': Not authenticated — run :Pending auth.')
|
||||
return
|
||||
end
|
||||
callback(token)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
|
|
@ -547,7 +524,7 @@ function OAuthClient:clear_tokens()
|
|||
os.remove(self:token_path())
|
||||
end
|
||||
|
||||
---@param opts { name: string, scope: string, port: integer, config_key: string }
|
||||
---@param opts pending.OAuthClientOpts
|
||||
---@return pending.OAuthClient
|
||||
function M.new(opts)
|
||||
return setmetatable({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue