diff options
author | Fawzi Mohamed <fawzi.mohamed@qt.io> | 2022-06-17 15:46:52 +0200 |
---|---|---|
committer | Fawzi Mohamed <fawzi.mohamed@qt.io> | 2022-06-21 10:39:52 +0000 |
commit | a235aa29a1e0efb62f49a9b6586a721800295f77 (patch) | |
tree | 9bb7e4113d2c4e57f468cadc3a852fd88ef38a5b /src/libs/qmljs/qmljsmodelmanagerinterface.cpp | |
parent | 8c2120d15533465506fbb3dadcb31939469b42cb (diff) | |
download | qt-creator-a235aa29a1e0efb62f49a9b6586a721800295f77.tar.gz |
qmljs: avoid linking to files in the build directory
cmake creates a consistent uri structure in the build directory.
We use that as import path, but when we find a type in them we should
refer back to the original file, as editing those is dangerous because
any edit are lost with the next build.
To find the original file we use the qrc, as the qrc path is mostly
the same of as the uri path.
It is possible to add prefixes which would make an exact match fail,
so we compare the paths from the right to the left and find the
longest match.
To acheive this:
* QrcParser keeps a reversedResources, so the match from right can be
done efficiently, and provides a longestReverseMatches method
* the model manager keeps a list of all common prefixes of the
application paths (build directories), and identify all files in
build directories
* the method fileToSource identifies the files in the build directory
and tries to find the corresponding source file, warning if he
cannot find it
* fileToSource is used for follow Symbol and find usages
We could use fileToSource much more aggressively, to use to in editor
content for the files in the build directory, increasing the
consistency, but that is a more dangerous change for later.
Fixes: QTCREATORBUG-27173
Change-Id: Iea61b9825e5f6e433a7390cf2de9564b792458a5
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/libs/qmljs/qmljsmodelmanagerinterface.cpp')
-rw-r--r-- | src/libs/qmljs/qmljsmodelmanagerinterface.cpp | 101 |
1 files changed, 95 insertions, 6 deletions
diff --git a/src/libs/qmljs/qmljsmodelmanagerinterface.cpp b/src/libs/qmljs/qmljsmodelmanagerinterface.cpp index 5f649931ad..444d429a4b 100644 --- a/src/libs/qmljs/qmljsmodelmanagerinterface.cpp +++ b/src/libs/qmljs/qmljsmodelmanagerinterface.cpp @@ -446,6 +446,20 @@ bool pInfoLessThanImports(const ModelManagerInterface::ProjectInfo &p1, } +static QList<Utils::FilePath> generatedQrc(QStringList applicationDirectories) +{ + QList<Utils::FilePath> res; + for (const QString &pathStr : applicationDirectories) { + Utils::FilePath path = Utils::FilePath::fromString(pathStr); + Utils::FilePath generatedQrcDir = path.pathAppended(".rcc"); + if (generatedQrcDir.isReadableDir()) { + for (const Utils::FilePath & qrcPath: generatedQrcDir.dirEntries(FileFilter(QStringList({QStringLiteral(u"*.qrc")}), QDir::Files))) + res.append(qrcPath.canonicalPath()); + } + } + return res; +} + void ModelManagerInterface::iterateQrcFiles( ProjectExplorer::Project *project, QrcResourceSelector resources, const std::function<void(QrcParser::ConstPtr)> &callback) @@ -461,18 +475,21 @@ void ModelManagerInterface::iterateQrcFiles( Utils::sort(pInfos, &pInfoLessThanAll); } - QSet<QString> pathsChecked; + QSet<Utils::FilePath> pathsChecked; for (const ModelManagerInterface::ProjectInfo &pInfo : qAsConst(pInfos)) { QStringList qrcFilePaths; if (resources == ActiveQrcResources) qrcFilePaths = pInfo.activeResourceFiles; else qrcFilePaths = pInfo.allResourceFiles; - for (const QString &qrcFilePath : qAsConst(qrcFilePaths)) { + for (const Utils::FilePath &p : generatedQrc(pInfo.applicationDirectories)) + qrcFilePaths.append(p.toString()); + for (const QString &qrcFilePathStr : qAsConst(qrcFilePaths)) { + auto qrcFilePath = Utils::FilePath::fromString(qrcFilePathStr); if (pathsChecked.contains(qrcFilePath)) continue; pathsChecked.insert(qrcFilePath); - QrcParser::ConstPtr qrcFile = m_qrcCache.parsedPath(qrcFilePath); + QrcParser::ConstPtr qrcFile = m_qrcCache.parsedPath(qrcFilePath.toString()); if (qrcFile.isNull()) continue; callback(qrcFile); @@ -589,6 +606,8 @@ void ModelManagerInterface::updateProjectInfo(const ProjectInfo &pinfo, ProjectE m_qrcContents = pinfo.resourceFileContents; for (const QString &newQrc : qAsConst(pinfo.allResourceFiles)) m_qrcCache.addPath(newQrc, m_qrcContents.value(newQrc)); + for (const Utils::FilePath &newQrc : generatedQrc(pinfo.applicationDirectories)) + m_qrcCache.addPath(newQrc.toString(), m_qrcContents.value(newQrc.toString())); for (const QString &oldQrc : qAsConst(oldInfo.allResourceFiles)) m_qrcCache.removePath(oldQrc); @@ -1180,6 +1199,29 @@ void ModelManagerInterface::maybeScan(const PathsAndLanguages &importPaths) } } +static QList<Utils::FilePath> minimalPrefixPaths(const QStringList &paths) +{ + QList<Utils::FilePath> sortedPaths; + // find minimal prefix, ensure '/' at end + for (const QString &pathStr : qAsConst(paths)) { + Utils::FilePath path = Utils::FilePath::fromString(pathStr); + if (!path.endsWith("/")) + path.setPath(path.path() + "/"); + if (path.path().length() > 1) + sortedPaths.append(path); + } + std::sort(sortedPaths.begin(), sortedPaths.end()); + QList<Utils::FilePath> res; + QString lastPrefix; + for (auto it = sortedPaths.begin(); it != sortedPaths.end(); ++it) { + if (lastPrefix.isEmpty() || !it->startsWith(lastPrefix)) { + lastPrefix = it->path(); + res.append(*it); + } + } + return res; +} + void ModelManagerInterface::updateImportPaths() { if (m_indexerDisabled) @@ -1243,6 +1285,7 @@ void ModelManagerInterface::updateImportPaths() m_allImportPaths = allImportPaths; m_activeBundles = activeBundles; m_extendedBundles = extendedBundles; + m_applicationPaths = minimalPrefixPaths(allApplicationDirectories); } @@ -1253,10 +1296,13 @@ void ModelManagerInterface::updateImportPaths() QSet<QString> newLibraries; for (const Document::Ptr &doc : qAsConst(snapshot)) findNewLibraryImports(doc, snapshot, this, &importedFiles, &scannedPaths, &newLibraries); - for (const QString &path : qAsConst(allApplicationDirectories)) { - allImportPaths.maybeInsert(FilePath::fromString(path), Dialect::Qml); - findNewQmlApplicationInPath(FilePath::fromString(path), snapshot, this, &newLibraries); + for (const QString &pathStr : qAsConst(allApplicationDirectories)) { + Utils::FilePath path = Utils::FilePath::fromString(pathStr); + allImportPaths.maybeInsert(path, Dialect::Qml); + findNewQmlApplicationInPath(path, snapshot, this, &newLibraries); } + for (const Utils::FilePath &qrcPath : generatedQrc(allApplicationDirectories)) + updateQrcFile(qrcPath.toString()); updateSourceFiles(importedFiles, true); @@ -1668,4 +1714,47 @@ void ModelManagerInterface::resetCodeModel() updateImportPaths(); } +Utils::FilePath ModelManagerInterface::fileToSource(const Utils::FilePath &path) +{ + if (!path.scheme().isEmpty()) + return path; + for (const Utils::FilePath &p : m_applicationPaths) { + if (!p.isEmpty() && path.startsWith(p.path())) { + // if it is an applicationPath (i.e. in the build directory) + // try to use the path from the build dir as resource path + // and recover the path of the corresponding source file + QString reducedPath = path.path().mid(p.path().size()); + QString reversePath(reducedPath); + std::reverse(reversePath.begin(), reversePath.end()); + if (!reversePath.endsWith('/')) + reversePath.append('/'); + QrcParser::MatchResult res; + iterateQrcFiles(nullptr, + QrcResourceSelector::AllQrcResources, + [&](const QrcParser::ConstPtr &qrcFile) { + if (!qrcFile) + return; + QrcParser::MatchResult matchNow = qrcFile->longestReverseMatches( + reversePath); + + if (matchNow.matchDepth < res.matchDepth) + return; + if (matchNow.matchDepth == res.matchDepth) { + res.reversedPaths += matchNow.reversedPaths; + res.sourceFiles += matchNow.sourceFiles; + } else { + res = matchNow; + } + }); + std::sort(res.sourceFiles.begin(), res.sourceFiles.end()); + if (!res.sourceFiles.isEmpty()) { + return res.sourceFiles.first(); + } + qCWarning(qmljsLog) << "Could not find source file for file" << path + << "in application path" << p; + } + } + return path; +} + } // namespace QmlJS |