Skip to content

Commit 57453a3

Browse files
committed
feat(plugins.resolver): make certain source resolvers async
1 parent 3861097 commit 57453a3

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

lua/cord/plugins/resolver.lua

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
local logger = require 'cord.api.log'
22
local config = require 'cord.api.config'
33
local mappings = require 'cord.internal.activity.mappings'
4-
local uv = vim.uv or vim.loop
4+
local async = require 'cord.core.async'
5+
local fs = require 'cord.core.uv.fs'
6+
local ws_utils = require 'cord.internal.activity.workspace'
57

68
local M = {}
79

@@ -20,7 +22,11 @@ local cache = {
2022
nestjs = {},
2123
}
2224

23-
local function has_file(dir, filename) return vim.fs.root(dir, filename) ~= nil end
25+
local has_file = async.wrap(function(dir, filename)
26+
local path = dir .. '/' .. filename
27+
local stat = fs.stat(path):get()
28+
return stat ~= nil
29+
end)
2430

2531
local sources = {
2632
nestjs = {
@@ -32,18 +38,17 @@ local sources = {
3238
'typescript',
3339
},
3440
},
35-
run = function(opts)
36-
local ws = opts.workspace_dir or uv.cwd()
41+
run = async.wrap(function(opts)
42+
local ws = opts.workspace_dir or vim.uv.cwd()
3743
local cached = cache.nestjs[ws]
3844

3945
if cached == nil then
40-
-- `0` for current buffer
41-
cached = has_file(0, 'nest-cli.json')
46+
cached = has_file(ws, 'nest-cli.json'):get()
4247
cache.nestjs[ws] = cached
4348
end
4449

4550
if cached then opts.force_filetype = 'nest' end
46-
end,
51+
end),
4752
},
4853
toggleterm = {
4954
event = { 'pre_activity' },
@@ -76,16 +81,21 @@ local sources = {
7681
workspace = '.',
7782
filetype = { 'oil', 'oil_preview', 'oil_progress' },
7883
},
79-
run = function(opts)
84+
run = async.wrap(function(opts)
8085
local path = vim.fn.expand '%:p:h'
8186
if path:sub(1, 7) == 'oil:///' then
8287
local stripped = path:sub(7)
8388
local cached = opts.manager.workspace:get(stripped)
8489

90+
local repo_url = cached and cached.repo_url or nil
91+
if not repo_url then
92+
repo_url = ws_utils.find_git_repository(stripped):get()
93+
end
94+
8595
local info = {
8696
dir = stripped,
8797
name = vim.fn.fnamemodify(stripped, ':t'),
88-
repo_url = cached and cached.repo_url or nil,
98+
repo_url = repo_url,
8999
}
90100

91101
opts.manager.workspace:set(path, info)
@@ -95,7 +105,7 @@ local sources = {
95105
opts.workspace_dir = info.dir
96106
opts.repo_url = info.repo_url
97107
end
98-
end,
108+
end),
99109
},
100110
}
101111

@@ -117,10 +127,10 @@ local function check_match(match, opts)
117127
return true
118128
end
119129

120-
function M.setup(config)
121-
if config then M.config = vim.tbl_deep_extend('force', M.config, config) end
130+
function M.setup(user_config)
131+
if user_config then M.config = vim.tbl_deep_extend('force', M.config, user_config) end
122132

123-
local user_resolvers = config and config.resolvers
133+
local user_resolvers = user_config and user_config.resolvers
124134
if type(user_resolvers) == 'boolean' then
125135
user_resolvers = { user_resolvers }
126136
elseif type(user_resolvers) ~= 'table' then
@@ -141,7 +151,11 @@ function M.setup(config)
141151
if enabled then
142152
for _, event in ipairs(source.event) do
143153
if not active_resolvers[event] then active_resolvers[event] = {} end
144-
table.insert(active_resolvers[event], source)
154+
table.insert(active_resolvers[event], {
155+
name = name,
156+
source = source,
157+
is_async = async.is_async(source.run),
158+
})
145159
end
146160

147161
logger.debug('Resolver: Registered resolver ' .. name)
@@ -152,14 +166,15 @@ function M.setup(config)
152166
M.hooks = {}
153167
for event, resolvers in pairs(active_resolvers) do
154168
M.hooks[event] = {
155-
fun = function(opts)
156-
for name, resolver in pairs(resolvers) do
157-
if check_match(resolver.match, opts) then
158-
logger.debug('Resolver: Running resolver ' .. name)
159-
resolver.run(opts)
169+
fun = async.wrap(function(opts)
170+
for _, resolver in ipairs(resolvers) do
171+
if check_match(resolver.source.match, opts) then
172+
logger.debug('Resolver: Running resolver ' .. resolver.name)
173+
local result = resolver.source.run(opts)
174+
if resolver.is_async then result:get() end
160175
end
161176
end
162-
end,
177+
end),
163178
priority = priorities.HIGHEST,
164179
}
165180
end

0 commit comments

Comments
 (0)