From 560d16a0968770cf73a36e8b2cb90eb08df9521b Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 26 Oct 2021 17:49:54 +0200 Subject: [PATCH] CppEditor: Follow symbols in generated UI headers to .ui file Fixes: QTCREATORBUG-2374 Change-Id: I784fe39c11c834525cdd37ca719efa65efcb307c Reviewed-by: hjk --- src/plugins/cppeditor/cppeditorwidget.cpp | 38 ++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/plugins/cppeditor/cppeditorwidget.cpp b/src/plugins/cppeditor/cppeditorwidget.cpp index 182691c2a45..6ef037d36be 100644 --- a/src/plugins/cppeditor/cppeditorwidget.cpp +++ b/src/plugins/cppeditor/cppeditorwidget.cpp @@ -62,6 +62,10 @@ #include #include +#include +#include +#include + #include #include #include @@ -76,8 +80,6 @@ #include #include -#include - #include #include #include @@ -101,6 +103,7 @@ enum { UPDATE_FUNCTION_DECL_DEF_LINK_INTERVAL = 200 }; using namespace Core; using namespace CPlusPlus; +using namespace ProjectExplorer; using namespace TextEditor; using namespace Utils; @@ -1045,7 +1048,7 @@ void CppEditorWidget::switchDeclarationDefinition(bool inNextSplit) } void CppEditorWidget::findLinkAt(const QTextCursor &cursor, - Utils::ProcessLinkCallback &&processLinkCallback, + ProcessLinkCallback &&processLinkCallback, bool resolveTarget, bool inNextSplit) { @@ -1054,9 +1057,36 @@ void CppEditorWidget::findLinkAt(const QTextCursor &cursor, const Utils::FilePath &filePath = textDocument()->filePath(); + // Let following a "leaf" C++ symbol take us to the designer, if we are in a generated + // UI header. + QTextCursor c(cursor); + c.select(QTextCursor::WordUnderCursor); + ProcessLinkCallback callbackWrapper = [start = c.selectionStart(), end = c.selectionEnd(), + doc = QPointer(cursor.document()), callback = std::move(processLinkCallback), + filePath](const Link &link) { + const int linkPos = doc ? Text::positionInText(doc, link.targetLine, link.targetColumn + 1) + : -1; + if (link.targetFilePath == filePath && linkPos >= start && linkPos < end) { + const QString fileName = filePath.fileName(); + if (fileName.startsWith("ui_") && fileName.endsWith(".h")) { + const QString uiFileName = fileName.mid(3, fileName.length() - 4) + "ui"; + for (const Project * const project : SessionManager::projects()) { + const auto nodeMatcher = [uiFileName](Node *n) { + return n->filePath().fileName() == uiFileName; + }; + if (const Node * const uiNode = project->rootProjectNode() + ->findNode(nodeMatcher)) { + EditorManager::openEditor(uiNode->filePath()); + return; + } + } + } + } + callback(link); + }; followSymbolInterface().findLink( CursorInEditor{cursor, filePath, this, textDocument()}, - std::move(processLinkCallback), + std::move(callbackWrapper), resolveTarget, d->m_modelManager->snapshot(), d->m_lastSemanticInfo.doc,