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>
This commit is contained in:
Fawzi Mohamed
2022-06-17 15:46:52 +02:00
parent 8c2120d155
commit a235aa29a1
6 changed files with 178 additions and 19 deletions

View File

@@ -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( void ModelManagerInterface::iterateQrcFiles(
ProjectExplorer::Project *project, QrcResourceSelector resources, ProjectExplorer::Project *project, QrcResourceSelector resources,
const std::function<void(QrcParser::ConstPtr)> &callback) const std::function<void(QrcParser::ConstPtr)> &callback)
@@ -461,18 +475,21 @@ void ModelManagerInterface::iterateQrcFiles(
Utils::sort(pInfos, &pInfoLessThanAll); Utils::sort(pInfos, &pInfoLessThanAll);
} }
QSet<QString> pathsChecked; QSet<Utils::FilePath> pathsChecked;
for (const ModelManagerInterface::ProjectInfo &pInfo : qAsConst(pInfos)) { for (const ModelManagerInterface::ProjectInfo &pInfo : qAsConst(pInfos)) {
QStringList qrcFilePaths; QStringList qrcFilePaths;
if (resources == ActiveQrcResources) if (resources == ActiveQrcResources)
qrcFilePaths = pInfo.activeResourceFiles; qrcFilePaths = pInfo.activeResourceFiles;
else else
qrcFilePaths = pInfo.allResourceFiles; 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)) if (pathsChecked.contains(qrcFilePath))
continue; continue;
pathsChecked.insert(qrcFilePath); pathsChecked.insert(qrcFilePath);
QrcParser::ConstPtr qrcFile = m_qrcCache.parsedPath(qrcFilePath); QrcParser::ConstPtr qrcFile = m_qrcCache.parsedPath(qrcFilePath.toString());
if (qrcFile.isNull()) if (qrcFile.isNull())
continue; continue;
callback(qrcFile); callback(qrcFile);
@@ -589,6 +606,8 @@ void ModelManagerInterface::updateProjectInfo(const ProjectInfo &pinfo, ProjectE
m_qrcContents = pinfo.resourceFileContents; m_qrcContents = pinfo.resourceFileContents;
for (const QString &newQrc : qAsConst(pinfo.allResourceFiles)) for (const QString &newQrc : qAsConst(pinfo.allResourceFiles))
m_qrcCache.addPath(newQrc, m_qrcContents.value(newQrc)); 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)) for (const QString &oldQrc : qAsConst(oldInfo.allResourceFiles))
m_qrcCache.removePath(oldQrc); 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() void ModelManagerInterface::updateImportPaths()
{ {
if (m_indexerDisabled) if (m_indexerDisabled)
@@ -1243,6 +1285,7 @@ void ModelManagerInterface::updateImportPaths()
m_allImportPaths = allImportPaths; m_allImportPaths = allImportPaths;
m_activeBundles = activeBundles; m_activeBundles = activeBundles;
m_extendedBundles = extendedBundles; m_extendedBundles = extendedBundles;
m_applicationPaths = minimalPrefixPaths(allApplicationDirectories);
} }
@@ -1253,10 +1296,13 @@ void ModelManagerInterface::updateImportPaths()
QSet<QString> newLibraries; QSet<QString> newLibraries;
for (const Document::Ptr &doc : qAsConst(snapshot)) for (const Document::Ptr &doc : qAsConst(snapshot))
findNewLibraryImports(doc, snapshot, this, &importedFiles, &scannedPaths, &newLibraries); findNewLibraryImports(doc, snapshot, this, &importedFiles, &scannedPaths, &newLibraries);
for (const QString &path : qAsConst(allApplicationDirectories)) { for (const QString &pathStr : qAsConst(allApplicationDirectories)) {
allImportPaths.maybeInsert(FilePath::fromString(path), Dialect::Qml); Utils::FilePath path = Utils::FilePath::fromString(pathStr);
findNewQmlApplicationInPath(FilePath::fromString(path), snapshot, this, &newLibraries); allImportPaths.maybeInsert(path, Dialect::Qml);
findNewQmlApplicationInPath(path, snapshot, this, &newLibraries);
} }
for (const Utils::FilePath &qrcPath : generatedQrc(allApplicationDirectories))
updateQrcFile(qrcPath.toString());
updateSourceFiles(importedFiles, true); updateSourceFiles(importedFiles, true);
@@ -1668,4 +1714,47 @@ void ModelManagerInterface::resetCodeModel()
updateImportPaths(); 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 } // namespace QmlJS

View File

@@ -34,6 +34,7 @@
#include <utils/environment.h> #include <utils/environment.h>
#include <utils/futuresynchronizer.h> #include <utils/futuresynchronizer.h>
#include <utils/qrcparser.h> #include <utils/qrcparser.h>
#include <utils/filepath.h>
#include <QFuture> #include <QFuture>
#include <QHash> #include <QHash>
@@ -149,6 +150,7 @@ public:
ProjectExplorer::Project *project = nullptr, ProjectExplorer::Project *project = nullptr,
bool addDirs = false, bool addDirs = false,
QrcResourceSelector resources = AllQrcResources); QrcResourceSelector resources = AllQrcResources);
Utils::FilePath fileToSource(const Utils::FilePath &file);
QList<ProjectInfo> projectInfos() const; QList<ProjectInfo> projectInfos() const;
bool containsProject(ProjectExplorer::Project *project) const; bool containsProject(ProjectExplorer::Project *project) const;
@@ -254,6 +256,7 @@ private:
QmlJS::Snapshot m_validSnapshot; QmlJS::Snapshot m_validSnapshot;
QmlJS::Snapshot m_newestSnapshot; QmlJS::Snapshot m_newestSnapshot;
PathsAndLanguages m_allImportPaths; PathsAndLanguages m_allImportPaths;
QList<Utils::FilePath> m_applicationPaths;
QStringList m_defaultImportPaths; QStringList m_defaultImportPaths;
QmlJS::QmlLanguageBundles m_activeBundles; QmlJS::QmlLanguageBundles m_activeBundles;
QmlJS::QmlLanguageBundles m_extendedBundles; QmlJS::QmlLanguageBundles m_extendedBundles;

View File

@@ -57,6 +57,7 @@ public:
const QLocale *locale = nullptr) const; const QLocale *locale = nullptr) const;
void collectResourceFilesForSourceFile(const QString &sourceFile, QStringList *res, void collectResourceFilesForSourceFile(const QString &sourceFile, QStringList *res,
const QLocale *locale = nullptr) const; const QLocale *locale = nullptr) const;
QrcParser::MatchResult longestReverseMatches(const QString &) const;
QStringList errorMessages() const; QStringList errorMessages() const;
QStringList languages() const; QStringList languages() const;
@@ -65,6 +66,7 @@ private:
const QStringList allUiLanguages(const QLocale *locale) const; const QStringList allUiLanguages(const QLocale *locale) const;
SMap m_resources; SMap m_resources;
SMap m_reverseResources;
SMap m_files; SMap m_files;
QStringList m_languages; QStringList m_languages;
QStringList m_errorMessages; QStringList m_errorMessages;
@@ -207,6 +209,11 @@ void QrcParser::collectFilesAtPath(const QString &path, QStringList *res, const
d->collectFilesAtPath(path, res, locale); d->collectFilesAtPath(path, res, locale);
} }
QrcParser::MatchResult QrcParser::longestReverseMatches(const QString &p) const
{
return d->longestReverseMatches(p);
}
/*! /*!
Returns \c true if \a path is a non-empty directory and matches \a locale. Returns \c true if \a path is a non-empty directory and matches \a locale.
@@ -414,8 +421,14 @@ bool QrcParserPrivate::parseFile(const QString &path, const QString &contents)
else else
accessPath = language + prefix + fileName; accessPath = language + prefix + fileName;
QStringList &resources = m_resources[accessPath]; QStringList &resources = m_resources[accessPath];
if (!resources.contains(filePath)) if (!resources.contains(filePath)) {
resources.append(filePath); resources.append(filePath);
QString reversePath(accessPath);
std::reverse(reversePath.begin(), reversePath.end());
if (!reversePath.endsWith('/'))
reversePath.append('/');
m_reverseResources[reversePath].append(filePath);
}
QStringList &files = m_files[filePath]; QStringList &files = m_files[filePath];
if (!files.contains(accessPath)) if (!files.contains(accessPath))
files.append(accessPath); files.append(accessPath);
@@ -517,6 +530,37 @@ void QrcParserPrivate::collectResourceFilesForSourceFile(const QString &sourceFi
} }
} }
QrcParser::MatchResult QrcParserPrivate::longestReverseMatches(const QString &reversePath) const
{
QrcParser::MatchResult res;
if (reversePath.length() == 1)
return res;
auto lastMatch = m_reverseResources.end();
qsizetype matchedUntil = 0;
for (qsizetype i = 1, j = 0; i < reversePath.size(); i = j + 1) {
j = reversePath.indexOf(u'/', i);
if (j == -1)
j = reversePath.size() - 1;
auto match = m_reverseResources.lowerBound(reversePath.mid(0, j + 1));
QString pNow = reversePath.left(j + 1);
if (match == m_reverseResources.end() || match.key().left(j + 1) != pNow)
break;
++res.matchDepth;
matchedUntil = j + 1;
lastMatch = match;
}
res.reversedPaths.clear();
res.sourceFiles.clear();
for (auto it = lastMatch; it != m_reverseResources.end()
&& it.key().left(matchedUntil) == reversePath.left(matchedUntil);
++it) {
res.reversedPaths.append(it.key());
for (const QString &filePath : it.value())
res.sourceFiles.append(Utils::FilePath::fromString(filePath));
}
return res;
}
QStringList QrcParserPrivate::errorMessages() const QStringList QrcParserPrivate::errorMessages() const
{ {
return m_errorMessages; return m_errorMessages;

View File

@@ -26,6 +26,7 @@
#pragma once #pragma once
#include "utils_global.h" #include "utils_global.h"
#include "utils/filepath.h"
#include <QSharedPointer> #include <QSharedPointer>
#include <QString> #include <QString>
@@ -47,12 +48,20 @@ class QrcCachePrivate;
class QTCREATOR_UTILS_EXPORT QrcParser class QTCREATOR_UTILS_EXPORT QrcParser
{ {
public: public:
struct MatchResult
{
int matchDepth = {};
QStringList reversedPaths;
QList<Utils::FilePath> sourceFiles;
};
typedef QSharedPointer<QrcParser> Ptr; typedef QSharedPointer<QrcParser> Ptr;
typedef QSharedPointer<const QrcParser> ConstPtr; typedef QSharedPointer<const QrcParser> ConstPtr;
~QrcParser(); ~QrcParser();
bool parseFile(const QString &path, const QString &contents); bool parseFile(const QString &path, const QString &contents);
QString firstFileAtPath(const QString &path, const QLocale &locale) const; QString firstFileAtPath(const QString &path, const QLocale &locale) const;
void collectFilesAtPath(const QString &path, QStringList *res, const QLocale *locale = nullptr) const; void collectFilesAtPath(const QString &path, QStringList *res, const QLocale *locale = nullptr) const;
MatchResult longestReverseMatches(const QString &) const;
bool hasDirAtPath(const QString &path, const QLocale *locale = nullptr) const; bool hasDirAtPath(const QString &path, const QLocale *locale = nullptr) const;
void collectFilesInPath(const QString &path, QMap<QString, QStringList> *res, bool addDirs = false, void collectFilesInPath(const QString &path, QMap<QString, QStringList> *res, bool addDirs = false,
const QLocale *locale = nullptr) const; const QLocale *locale = nullptr) const;

View File

@@ -771,7 +771,8 @@ void QmlJSEditorWidget::findLinkAt(const QTextCursor &cursor,
const QList<ImportInfo> imports = semanticInfo.document->bind()->imports(); const QList<ImportInfo> imports = semanticInfo.document->bind()->imports();
for (const ImportInfo &import : imports) { for (const ImportInfo &import : imports) {
if (import.ast() == importAst && import.type() == ImportType::File) { if (import.ast() == importAst && import.type() == ImportType::File) {
Utils::Link link(Utils::FilePath::fromString(import.path())); Utils::Link link(
m_modelManager->fileToSource(FilePath::fromString(import.path())));
link.linkTextStart = importAst->firstSourceLocation().begin(); link.linkTextStart = importAst->firstSourceLocation().begin();
link.linkTextEnd = importAst->lastSourceLocation().end(); link.linkTextEnd = importAst->lastSourceLocation().end();
processLinkCallback(Utils::Link()); processLinkCallback(Utils::Link());
@@ -793,11 +794,9 @@ void QmlJSEditorWidget::findLinkAt(const QTextCursor &cursor,
processLinkCallback(link); processLinkCallback(link);
return; return;
} }
const QString relative = QString::fromLatin1("%1/%2").arg( const Utils::FilePath relative = Utils::FilePath::fromString(semanticInfo.document->path()).pathAppended(text);
semanticInfo.document->path(), if (relative.exists()) {
text); link.targetFilePath = m_modelManager->fileToSource(relative);
if (QFileInfo::exists(relative)) {
link.targetFilePath = Utils::FilePath::fromString(relative);
processLinkCallback(link); processLinkCallback(link);
return; return;
} }
@@ -814,7 +813,7 @@ void QmlJSEditorWidget::findLinkAt(const QTextCursor &cursor,
return processLinkCallback(Utils::Link()); return processLinkCallback(Utils::Link());
Utils::Link link; Utils::Link link;
link.targetFilePath = Utils::FilePath::fromString(fileName); link.targetFilePath = m_modelManager->fileToSource(FilePath::fromString(fileName));
link.targetLine = line; link.targetLine = line;
link.targetColumn = column - 1; // adjust the column link.targetColumn = column - 1; // adjust the column

View File

@@ -28,10 +28,11 @@
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/find/searchresultwindow.h> #include <coreplugin/find/searchresultwindow.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/progressmanager/futureprogress.h> #include <coreplugin/progressmanager/futureprogress.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <texteditor/basefilefind.h> #include <texteditor/basefilefind.h>
#include <utils/algorithm.h>
#include <utils/filesearch.h> #include <utils/filesearch.h>
#include <utils/runextensions.h> #include <utils/runextensions.h>
@@ -745,6 +746,7 @@ public:
future->waitForResume(); future->waitForResume();
if (future->isCanceled()) if (future->isCanceled())
return usages; return usages;
ModelManagerInterface *modelManager = ModelManagerInterface::instance();
Document::Ptr doc = context->snapshot().document(fileName); Document::Ptr doc = context->snapshot().document(fileName);
if (!doc) if (!doc)
return usages; return usages;
@@ -753,7 +755,7 @@ public:
FindUsages findUsages(doc, context); FindUsages findUsages(doc, context);
const FindUsages::Result results = findUsages(name, scope); const FindUsages::Result results = findUsages(name, scope);
for (const SourceLocation &loc : results) for (const SourceLocation &loc : results)
usages.append(Usage(fileName, matchingLine(loc.offset, doc->source()), loc.startLine, loc.startColumn - 1, loc.length)); usages.append(Usage(modelManager->fileToSource(Utils::FilePath::fromString(fileName)).toString(), matchingLine(loc.offset, doc->source()), loc.startLine, loc.startColumn - 1, loc.length));
if (future->isPaused()) if (future->isPaused())
future->waitForResume(); future->waitForResume();
return usages; return usages;
@@ -896,8 +898,9 @@ static void find_helper(QFutureInterface<FindReferences::Usage> &future,
QStringList files; QStringList files;
for (const Document::Ptr &doc : qAsConst(snapshot)) { for (const Document::Ptr &doc : qAsConst(snapshot)) {
// ### skip files that don't contain the name token // ### skip files that don't contain the name token
files.append(doc->fileName()); files.append(modelManager->fileToSource(Utils::FilePath::fromString(doc->fileName())).toString());
} }
files = Utils::filteredUnique(files);
future.setProgressRange(0, files.size()); future.setProgressRange(0, files.size());
@@ -976,11 +979,23 @@ QList<FindReferences::Usage> FindReferences::findUsageOfType(const QString &file
QmlJS::Snapshot snapshot = modelManager->snapshot(); QmlJS::Snapshot snapshot = modelManager->snapshot();
QSet<QString> docDone;
for (const QmlJS::Document::Ptr &doc : qAsConst(snapshot)) { for (const QmlJS::Document::Ptr &doc : qAsConst(snapshot)) {
FindTypeUsages findUsages(doc, context); QString sourceFile = modelManager->fileToSource(Utils::FilePath::fromString(doc->fileName())).toString();
if (docDone.contains(sourceFile))
continue;
docDone.insert(sourceFile);
QmlJS::Document::Ptr sourceDoc = doc;
if (sourceFile != doc->fileName())
sourceDoc = snapshot.document(sourceFile);
FindTypeUsages findUsages(sourceDoc, context);
const FindTypeUsages::Result results = findUsages(typeName, targetValue); const FindTypeUsages::Result results = findUsages(typeName, targetValue);
for (const SourceLocation &loc : results) { for (const SourceLocation &loc : results) {
usages.append(Usage(doc->fileName(), matchingLine(loc.offset, doc->source()), loc.startLine, loc.startColumn - 1, loc.length)); usages.append(Usage(sourceFile,
matchingLine(loc.offset, doc->source()),
loc.startLine,
loc.startColumn - 1,
loc.length));
} }
} }
return usages; return usages;