aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorSami Shalayel <sami.shalayel@qt.io>2024-04-10 14:30:32 +0200
committerSami Shalayel <sami.shalayel@qt.io>2024-04-18 11:39:16 +0200
commit1bef3c4d0db67bd9dcfc7289b6322649af980c22 (patch)
tree6a1231078a137020cabf0add5e1767fe0451ee23 /tools
parent547de2d4e5d5619e5d37b91ad534f9260492e339 (diff)
qmlls: add -I and -E option
Add a -I option that specifies additional import paths used to find QML modules. Add a -E option that searches QML modules in the import paths specified by the environment variables QML_IMPORT_PATH. Add an import path field to QQmlCodeModel with setters and getters that may now consist of more than just `QLibraryInfo::path(QLibraryInfo::QmlImportsPath)`. Extract some common code used to read, check and inform about directories used from environment variables or command line options into static methods. This would allow other tooling, like qmllint, to print the same warnings when using environment variables, for example. Add a new test tst_qmlls_cli that allows to test qmlls behavior when fed with different combinations of -I, -E and -b (for build directories) options. The test also enables checking that warnings are correctly emitted and that import paths are correctly used during linting via qmlls. Note that the warnings emitted by qmlls when using -b were enhanced, partially due to the refactoring of the static helpers to read directory names from environment and command line options. Also note that qmlls was informing twice about possibly using .qmlls.ini files before this commit, so also remove the redundant warning. Task-number: QTBUG-114969 Change-Id: I102ca8b50299f277746c5bba6832551a76898fc0 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tools')
-rw-r--r--tools/qmlls/qmllanguageservertool.cpp91
1 files changed, 57 insertions, 34 deletions
diff --git a/tools/qmlls/qmllanguageservertool.cpp b/tools/qmlls/qmllanguageservertool.cpp
index 6427c4ac09..34138638b7 100644
--- a/tools/qmlls/qmllanguageservertool.cpp
+++ b/tools/qmlls/qmllanguageservertool.cpp
@@ -8,6 +8,7 @@
#include <QtCore/qfileinfo.h>
#include <QtCore/qcoreapplication.h>
#include <QtQmlToolingSettings/private/qqmltoolingsettings_p.h>
+#include <QtQmlToolingSettings/private/qqmltoolingutils_p.h>
#include <QtCore/qdiriterator.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qjsonarray.h>
@@ -164,6 +165,17 @@ int main(int argv, char *argc[])
parser.addOption(buildDirOption);
settings.addOption(buildDir);
+ QString qmlImportPath = QStringLiteral(u"qml-import-path");
+ QCommandLineOption qmlImportPathOption(
+ QStringList() << "I", QLatin1String("Look for QML modules in the specified directory"),
+ qmlImportPath);
+ parser.addOption(qmlImportPathOption);
+
+ QCommandLineOption environmentOption(
+ QStringList() << "E",
+ QLatin1String("Use the QML_IMPORT_PATH environment variable to look for QML Modules"));
+ parser.addOption(environmentOption);
+
QCommandLineOption writeDefaultsOption(
QStringList() << "write-defaults",
QLatin1String("Writes defaults settings to .qmlls.ini and exits (Warning: This "
@@ -232,49 +244,60 @@ int main(int argv, char *argc[])
qmlServer.codeModel()->disableCMakeCalls();
}
- const QStringList envPaths =
- qEnvironmentVariable("QMLLS_BUILD_DIRS").split(u',', Qt::SkipEmptyParts);
- for (const QString &envPath : envPaths) {
- QFileInfo info(envPath);
- if (!info.exists()) {
- qWarning() << "Argument" << buildDir << "passed via QMLLS_BUILD_DIRS does not exist.";
- } else if (!info.isDir()) {
- qWarning() << "Argument" << buildDir
- << "passed via QMLLS_BUILD_DIRS is not a directory.";
- }
- }
-
- QStringList buildDirs;
if (parser.isSet(buildDirOption)) {
- buildDirs = parser.values(buildDirOption);
- for (const QString &buildDir : buildDirs) {
- QFileInfo info(buildDir);
- if (!info.exists()) {
- qWarning() << "Argument" << buildDir << "passed to --build-dir does not exist.";
- } else if (!info.isDir()) {
- qWarning() << "Argument" << buildDir << "passed to --build-dir is not a directory.";
- }
- }
- qmlServer.codeModel()->setBuildPathsForRootUrl(QByteArray(), buildDirs);
- }
+ const QStringList dirs =
+ QQmlToolingUtils::getAndWarnForInvalidDirsFromOption(parser, buildDirOption);
+
+ qInfo().nospace().noquote()
+ << "Using build directories passed by -b: \"" << dirs.join(u"\", \""_s) << "\".";
+
+ qmlServer.codeModel()->setBuildPathsForRootUrl(QByteArray(), dirs);
+ } else if (QStringList dirsFromEnv =
+ QQmlToolingUtils::getAndWarnForInvalidDirsFromEnv("QMLLS_BUILD_DIRS");
+ !dirsFromEnv.isEmpty()) {
+
+ // warn now at qmlls startup that those directories will be used later in qqmlcodemodel when
+ // searching for build folders.
+ qInfo().nospace().noquote() << "Using build directories passed from environment variable "
+ "\"QMLLS_BUILD_DIRS\": \""
+ << dirsFromEnv.join(u"\", \""_s) << "\".";
- if (!buildDirs.isEmpty()) {
- qInfo() << "Using the build directories passed via the --build-dir option:"
- << buildDirs.join(", ");
- } else if (!envPaths.isEmpty()) {
- qInfo() << "Using the build directories passed via the QMLLS_BUILD_DIRS environment "
- "variable"
- << buildDirs.join(", ");
} else {
qInfo() << "Using the build directories found in the .qmlls.ini file. Your build folder "
"might not be found if no .qmlls.ini files are present in the root source "
"folder.";
}
+ QStringList importPaths{ QLibraryInfo::path(QLibraryInfo::QmlImportsPath) };
+ if (parser.isSet(qmlImportPathOption)) {
+ const QStringList pathsFromOption =
+ QQmlToolingUtils::getAndWarnForInvalidDirsFromOption(parser, qmlImportPathOption);
+ qInfo().nospace().noquote() << "Using import directories passed by -I: \""
+ << pathsFromOption.join(u"\", \""_s) << "\".";
+ importPaths << pathsFromOption;
+ }
+ if (parser.isSet(environmentOption)) {
+ if (const QStringList dirsFromEnv =
+ QQmlToolingUtils::getAndWarnForInvalidDirsFromEnv(u"QML_IMPORT_PATH"_s);
+ !dirsFromEnv.isEmpty()) {
+ qInfo().nospace().noquote()
+ << "Using import directories passed from environment variable "
+ "\"QML_IMPORT_PATH\": \""
+ << dirsFromEnv.join(u"\", \""_s) << "\".";
+ importPaths << dirsFromEnv;
+ }
- if (buildDirs.isEmpty() && envPaths.isEmpty()) {
- qInfo() << "Build directory path omitted: Your source folders will be searched for "
- ".qmlls.ini files.";
+ if (const QStringList dirsFromEnv2 =
+ QQmlToolingUtils::getAndWarnForInvalidDirsFromEnv(u"QML2_IMPORT_PATH"_s);
+ !dirsFromEnv2.isEmpty()) {
+ qInfo().nospace().noquote()
+ << "Using import directories passed from the deprecated environment variable "
+ "\"QML2_IMPORT_PATH\": \""
+ << dirsFromEnv2.join(u"\", \""_s) << "\".";
+ importPaths << dirsFromEnv2;
+ }
}
+ qmlServer.codeModel()->setImportPaths(importPaths);
+
StdinReader r;
QObject::connect(&r, &StdinReader::receivedData,
qmlServer.server(), &QLanguageServer::receiveData);