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

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

View File

@@ -28,10 +28,11 @@
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/find/searchresultwindow.h>
#include <coreplugin/icore.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/progressmanager/futureprogress.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <extensionsystem/pluginmanager.h>
#include <texteditor/basefilefind.h>
#include <utils/algorithm.h>
#include <utils/filesearch.h>
#include <utils/runextensions.h>
@@ -745,6 +746,7 @@ public:
future->waitForResume();
if (future->isCanceled())
return usages;
ModelManagerInterface *modelManager = ModelManagerInterface::instance();
Document::Ptr doc = context->snapshot().document(fileName);
if (!doc)
return usages;
@@ -753,7 +755,7 @@ public:
FindUsages findUsages(doc, context);
const FindUsages::Result results = findUsages(name, scope);
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())
future->waitForResume();
return usages;
@@ -896,8 +898,9 @@ static void find_helper(QFutureInterface<FindReferences::Usage> &future,
QStringList files;
for (const Document::Ptr &doc : qAsConst(snapshot)) {
// ### 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());
@@ -976,11 +979,23 @@ QList<FindReferences::Usage> FindReferences::findUsageOfType(const QString &file
QmlJS::Snapshot snapshot = modelManager->snapshot();
QSet<QString> docDone;
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);
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;