forked from qt-creator/qt-creator
Lua: Make FilePath:searchInPath async
Change-Id: Ida94856c26bab38d95753fb4d5475d57e3315212 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -34,40 +34,52 @@ void addUtilsModule()
|
|||||||
timer->start();
|
timer->start();
|
||||||
});
|
});
|
||||||
|
|
||||||
auto dirEntries_cb = [&futureSync](
|
auto dirEntries_cb =
|
||||||
const FilePath &p,
|
[&futureSync](const FilePath &p, const sol::table &options, const sol::function &cb) {
|
||||||
const sol::table &options,
|
const QStringList nameFilters = options.get_or<QStringList>("nameFilters", {});
|
||||||
const sol::function &cb) {
|
QDir::Filters fileFilters
|
||||||
const QStringList nameFilters = options.get_or<QStringList>("nameFilters", {});
|
= (QDir::Filters) options.get_or<int>("fileFilters", QDir::NoFilter);
|
||||||
QDir::Filters fileFilters
|
QDirIterator::IteratorFlags flags
|
||||||
= (QDir::Filters) options.get_or<int>("fileFilters", QDir::NoFilter);
|
= (QDirIterator::IteratorFlags)
|
||||||
QDirIterator::IteratorFlags flags
|
options.get_or<int>("flags", QDirIterator::NoIteratorFlags);
|
||||||
= (QDirIterator::IteratorFlags)
|
|
||||||
options.get_or<int>("flags", QDirIterator::NoIteratorFlags);
|
|
||||||
|
|
||||||
FileFilter filter(nameFilters, fileFilters, flags);
|
FileFilter filter(nameFilters, fileFilters, flags);
|
||||||
|
|
||||||
QFuture<FilePath> future = Utils::asyncRun([p, filter](QPromise<FilePath> &promise) {
|
QFuture<FilePath> future = Utils::asyncRun(
|
||||||
p.iterateDirectory(
|
[p, filter](QPromise<FilePath> &promise) {
|
||||||
[&promise](const FilePath &item) {
|
p.iterateDirectory(
|
||||||
if (promise.isCanceled())
|
[&promise](const FilePath &item) {
|
||||||
return IterationPolicy::Stop;
|
if (promise.isCanceled())
|
||||||
|
return IterationPolicy::Stop;
|
||||||
|
|
||||||
promise.addResult(item);
|
promise.addResult(item);
|
||||||
return IterationPolicy::Continue;
|
return IterationPolicy::Continue;
|
||||||
},
|
},
|
||||||
filter);
|
filter);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
futureSync.addFuture<FilePath>(future);
|
||||||
|
|
||||||
|
Utils::onFinished<FilePath>(
|
||||||
|
future, &LuaEngine::instance(), [cb](const QFuture<FilePath> &future) {
|
||||||
|
cb(future.results());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
futureSync.addFuture<FilePath>(future);
|
||||||
|
|
||||||
Utils::onFinished<FilePath>(
|
Utils::onFinished<FilePath>(
|
||||||
future, &LuaEngine::instance(), [cb](const QFuture<FilePath> &future) {
|
future, &LuaEngine::instance(), [cb](const QFuture<FilePath> &future) {
|
||||||
cb(future.results());
|
cb(future.result());
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
utils.set_function("__dirEntries_cb__", dirEntries_cb);
|
utils.set_function("__dirEntries_cb__", dirEntries_cb);
|
||||||
|
utils.set_function("__searchInPath_cb__", searchInPath_cb);
|
||||||
|
|
||||||
sol::function wrap = async["wrap"].get<sol::function>();
|
sol::function wrap = async["wrap"].get<sol::function>();
|
||||||
|
|
||||||
@@ -96,8 +108,6 @@ void addUtilsModule()
|
|||||||
&FilePath::toUserOutput,
|
&FilePath::toUserOutput,
|
||||||
"fromUserInput",
|
"fromUserInput",
|
||||||
&FilePath::fromUserInput,
|
&FilePath::fromUserInput,
|
||||||
"searchInPath",
|
|
||||||
[](const FilePath &self) { return self.searchInPath(); },
|
|
||||||
"exists",
|
"exists",
|
||||||
&FilePath::exists,
|
&FilePath::exists,
|
||||||
"resolveSymlinks",
|
"resolveSymlinks",
|
||||||
@@ -124,6 +134,9 @@ void addUtilsModule()
|
|||||||
utils["FilePath"]["dirEntries_cb"] = utils["__dirEntries_cb__"];
|
utils["FilePath"]["dirEntries_cb"] = utils["__dirEntries_cb__"];
|
||||||
utils["FilePath"]["dirEntries"] = wrap(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;
|
return utils;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@@ -20,7 +20,7 @@ utils.FilePath = {}
|
|||||||
function utils.FilePath.fromUserInput(path) end
|
function utils.FilePath.fromUserInput(path) end
|
||||||
|
|
||||||
---@return FilePath The new absolute path
|
---@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
|
function utils.FilePath:searchInPath() end
|
||||||
|
|
||||||
---@class (exact) DirEntriesOptions
|
---@class (exact) DirEntriesOptions
|
||||||
|
@@ -146,15 +146,15 @@ local function setup(parameters)
|
|||||||
print("Setting up Lua Language Server ...")
|
print("Setting up Lua Language Server ...")
|
||||||
setupAspect()
|
setupAspect()
|
||||||
local serverPath = Utils.FilePath.fromUserInput("lua-language-server")
|
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
|
if absolute:isExecutableFile() == true then
|
||||||
Settings.binary.defaultPath = absolute
|
Settings.binary.defaultPath = absolute
|
||||||
else
|
else
|
||||||
a.sync(installServer)()
|
installServer()
|
||||||
end
|
end
|
||||||
setupClient()
|
setupClient()
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
setup = setup,
|
setup = function() a.sync(setup)() end,
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user