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