QmlJsEditor: navigate from QML code to C++ code

Fixes: QTCREATORBUG-28086
Change-Id: I00acfe49b8e74aa057cb59d8c951d477f8ad487e
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Xavier BESSON
2022-12-07 23:19:07 +01:00
committed by Xavier BESSON (Personal)
parent 42c1b15874
commit 90368e2bba
6 changed files with 52 additions and 7 deletions

View File

@@ -269,6 +269,11 @@ QString FakeMetaObject::className() const
void FakeMetaObject::setClassName(const QString &name)
{ m_className = name; }
QString FakeMetaObject::filePath() const
{ return m_filePath; }
void FakeMetaObject::setFilePath(const QString &path)
{ m_filePath = path; }
void FakeMetaObject::addExport(const QString &name, const QString &package, ComponentVersion version)
{
Export exp;

View File

@@ -139,6 +139,7 @@ public:
private:
QString m_className;
QString m_filePath;
QList<Export> m_exports;
QString m_superName;
QList<FakeMetaEnum> m_enums;
@@ -160,6 +161,9 @@ public:
QString className() const;
void setClassName(const QString &name);
QString filePath() const;
void setFilePath(const QString &path);
void addExport(const QString &name, const QString &package, ComponentVersion version);
void setExportMetaObjectRevision(int exportIndex, int metaObjectRevision);
const QList<Export> exports() const;

View File

@@ -251,6 +251,14 @@ const CppComponentValue *CppComponentValue::asCppComponentValue() const
return this;
}
bool CppComponentValue::getSourceLocation(Utils::FilePath *fileName, int *line, int *column) const
{
*fileName = Utils::FilePath::fromString(m_metaObject->filePath());
*line = 0;
*column = 0;
return true;
}
void CppComponentValue::processMembers(MemberProcessor *processor) const
{
// process the meta enums

View File

@@ -573,6 +573,8 @@ public:
const CppComponentValue *asCppComponentValue() const override;
bool getSourceLocation(Utils::FilePath *fileName, int *line, int *column) const override;
void processMembers(MemberProcessor *processor) const override;
const Value *valueForCppName(const QString &typeName) const;

View File

@@ -197,6 +197,8 @@ void TypeDescriptionReader::readComponent(UiObjectDefinition *ast)
QString name = toString(script->qualifiedId);
if (name == QLatin1String("name")) {
fmo->setClassName(readStringBinding(script));
} else if (name == QLatin1String("file")) {
fmo->setFilePath(readStringBinding(script));
} else if (name == QLatin1String("prototype")) {
fmo->setSuperclassName(readStringBinding(script));
} else if (name == QLatin1String("defaultProperty")) {

View File

@@ -789,13 +789,18 @@ void QmlJSEditorWidget::findLinkAt(const QTextCursor &cursor,
return;
}
const ProjectExplorer::Project * const project = ProjectExplorer::ProjectTree::currentProject();
ProjectExplorer::ProjectNode* projectRootNode = nullptr;
if (project) {
projectRootNode = project->rootProjectNode();
}
// string literals that could refer to a file link to them
if (auto literal = cast<const StringLiteral *>(node)) {
const QString &text = literal->value.toString();
if (text.startsWith("qrc:/")) {
const ProjectExplorer::Project * const project = ProjectExplorer::ProjectTree::currentProject();
if (project && project->rootProjectNode()) {
const ProjectExplorer::Node * const nodeForPath = project->rootProjectNode()->findNode(
if (projectRootNode) {
const ProjectExplorer::Node * const nodeForPath = projectRootNode->findNode(
[qrcPath = text.mid(text.indexOf(':') + 1)](ProjectExplorer::Node *n) {
if (!n->asFileNode())
return false;
@@ -854,14 +859,33 @@ void QmlJSEditorWidget::findLinkAt(const QTextCursor &cursor,
if (auto q = AST::cast<const AST::UiQualifiedId *>(node)) {
for (const AST::UiQualifiedId *tail = q; tail; tail = tail->next) {
if (! tail->next && cursorPosition <= tail->identifierToken.end()) {
if (tail->next || !(cursorPosition <= tail->identifierToken.end())) {
continue;
}
link.linkTextStart = tail->identifierToken.begin();
link.linkTextEnd = tail->identifierToken.end();
if (!value->asCppComponentValue() || !projectRootNode) {
processLinkCallback(link);
return;
}
}
const ProjectExplorer::Node * const nodeForPath = projectRootNode->findNode(
[&fileName](ProjectExplorer::Node *n) {
const auto fileNode = n->asFileNode();
if (!fileNode)
return false;
Utils::FilePath filePath = n->filePath();
return filePath.endsWith(fileName.toUserOutput());
});
if (nodeForPath) {
link.targetFilePath = nodeForPath->filePath();
processLinkCallback(link);
return;
}
// else we will process an empty link below to avoid an error dialog
}
} else if (auto id = AST::cast<const AST::IdentifierExpression *>(node)) {
link.linkTextStart = id->firstSourceLocation().begin();
link.linkTextEnd = id->lastSourceLocation().end();