Lua: Make FilePath:searchInPath async

Change-Id: Ida94856c26bab38d95753fb4d5475d57e3315212
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-05-02 15:07:28 +02:00
parent e2612705b1
commit d76082da59
3 changed files with 41 additions and 28 deletions

View File

@@ -34,10 +34,8 @@ void addUtilsModule()
timer->start();
});
auto dirEntries_cb = [&futureSync](
const FilePath &p,
const sol::table &options,
const sol::function &cb) {
auto dirEntries_cb =
[&futureSync](const FilePath &p, const sol::table &options, const sol::function &cb) {
const QStringList nameFilters = options.get_or<QStringList>("nameFilters", {});
QDir::Filters fileFilters
= (QDir::Filters) options.get_or<int>("fileFilters", QDir::NoFilter);
@@ -47,7 +45,8 @@ void addUtilsModule()
FileFilter filter(nameFilters, fileFilters, flags);
QFuture<FilePath> future = Utils::asyncRun([p, filter](QPromise<FilePath> &promise) {
QFuture<FilePath> future = Utils::asyncRun(
[p, filter](QPromise<FilePath> &promise) {
p.iterateDirectory(
[&promise](const FilePath &item) {
if (promise.isCanceled())
@@ -67,7 +66,20 @@ void addUtilsModule()
});
};
auto searchInPath_cb = [&futureSync](const FilePath &p, const sol::function &cb) {
QFuture<FilePath> future = Utils::asyncRun(
[p](QPromise<FilePath> &promise) { promise.addResult(p.searchInPath()); });
futureSync.addFuture<FilePath>(future);
Utils::onFinished<FilePath>(
future, &LuaEngine::instance(), [cb](const QFuture<FilePath> &future) {
cb(future.result());
});
};
utils.set_function("__dirEntries_cb__", dirEntries_cb);
utils.set_function("__searchInPath_cb__", searchInPath_cb);
sol::function wrap = async["wrap"].get<sol::function>();
@@ -96,8 +108,6 @@ void addUtilsModule()
&FilePath::toUserOutput,
"fromUserInput",
&FilePath::fromUserInput,
"searchInPath",
[](const FilePath &self) { return self.searchInPath(); },
"exists",
&FilePath::exists,
"resolveSymlinks",
@@ -124,6 +134,9 @@ void addUtilsModule()
utils["FilePath"]["dirEntries_cb"] = utils["__dirEntries_cb__"];
utils["FilePath"]["dirEntries"] = wrap(utils["__dirEntries_cb__"]);
utils["FilePath"]["searchInPath_cb"] = utils["__searchInPath_cb__"];
utils["FilePath"]["searchInPath"] = wrap(utils["__searchInPath_cb__"]);
return utils;
});
}

View File

@@ -20,7 +20,7 @@ utils.FilePath = {}
function utils.FilePath.fromUserInput(path) end
---@return FilePath The new absolute path
---Searches for the path inside the PATH environment variable
---Searches for the path inside the PATH environment variable. Call `a.wait` on the returned value to get the result.
function utils.FilePath:searchInPath() end
---@class (exact) DirEntriesOptions

View File

@@ -146,15 +146,15 @@ local function setup(parameters)
print("Setting up Lua Language Server ...")
setupAspect()
local serverPath = Utils.FilePath.fromUserInput("lua-language-server")
local absolute = serverPath:searchInPath():resolveSymlinks()
local absolute = a.wait(serverPath:searchInPath()):resolveSymlinks()
if absolute:isExecutableFile() == true then
Settings.binary.defaultPath = absolute
else
a.sync(installServer)()
installServer()
end
setupClient()
end
return {
setup = setup,
setup = function() a.sync(setup)() end,
}