diff --git a/src/plugins/cppeditor/cppcanonicalsymbol.cpp b/src/plugins/cppeditor/cppcanonicalsymbol.cpp index 102a5d07819..4728f643096 100644 --- a/src/plugins/cppeditor/cppcanonicalsymbol.cpp +++ b/src/plugins/cppeditor/cppcanonicalsymbol.cpp @@ -54,7 +54,7 @@ Scope *CanonicalSymbol::getScopeAndExpression(const QTextCursor &cursor, QString return m_document->scopeAt(line, column); } -Symbol *CanonicalSymbol::operator()(const QTextCursor &cursor) +Symbol *CanonicalSymbol::operator()(const QTextCursor &cursor) & { QString code; diff --git a/src/plugins/cppeditor/cppcanonicalsymbol.h b/src/plugins/cppeditor/cppcanonicalsymbol.h index 010949bbbb3..6bf95c3181e 100644 --- a/src/plugins/cppeditor/cppcanonicalsymbol.h +++ b/src/plugins/cppeditor/cppcanonicalsymbol.h @@ -21,7 +21,7 @@ public: CPlusPlus::Scope *getScopeAndExpression(const QTextCursor &cursor, QString *code); - CPlusPlus::Symbol *operator()(const QTextCursor &cursor); + CPlusPlus::Symbol *operator()(const QTextCursor &cursor) &; CPlusPlus::Symbol *operator()(CPlusPlus::Scope *scope, const QString &code); public: diff --git a/src/plugins/cppeditor/cpptoolsreuse.cpp b/src/plugins/cppeditor/cpptoolsreuse.cpp index c098aca99c8..543f046237d 100644 --- a/src/plugins/cppeditor/cpptoolsreuse.cpp +++ b/src/plugins/cppeditor/cpptoolsreuse.cpp @@ -49,6 +49,7 @@ #include #include +#include #include using namespace CPlusPlus; @@ -687,8 +688,14 @@ SearchResultItems symbolOccurrencesInDeclarationComments( } }; + struct ClassInfo { + FilePath filePath; + int startOffset = -1; + int endOffset = -1; + }; + std::optional classInfo; + // Collect comment blocks associated with replace locations. - Symbol *canonicalSymbol = nullptr; for (const SearchResultItem &item : symbolOccurrencesInCode) { const FilePath filePath = FilePath::fromUserInput(item.path().last()); auto &[doc, content, cppDoc, allCommentTokens] = fileData(filePath); @@ -705,10 +712,19 @@ SearchResultItems symbolOccurrencesInDeclarationComments( for (const Token &tok : commentTokens) addToken(allCommentTokens, tok); - if (!canonicalSymbol) { + if (!classInfo) { QTextCursor cursor(doc); cursor.setPosition(Text::positionInText(doc, range.begin.line, range.begin.column + 1)); - canonicalSymbol = Internal::CanonicalSymbol(cppDoc, snapshot)(cursor); + Internal::CanonicalSymbol cs(cppDoc, snapshot); + Symbol * const canonicalSymbol = cs(cursor); + if (canonicalSymbol) { + classInfo.emplace(); + if (Class * const klass = canonicalSymbol->asClass()) { + classInfo->filePath = canonicalSymbol->filePath(); + classInfo->startOffset = klass->startOffset(); + classInfo->endOffset = klass->endOffset(); + } + } } // We hook in between the end of the "regular" search and (possibly non-interactive) @@ -722,14 +738,14 @@ SearchResultItems symbolOccurrencesInDeclarationComments( } // If the symbol is a class, collect all comment blocks in the class body. - if (Class * const klass = canonicalSymbol ? canonicalSymbol->asClass() : nullptr) { - auto &[_1, _2, symbolCppDoc, commentTokens] = fileData(canonicalSymbol->filePath()); + if (classInfo && !classInfo->filePath.isEmpty()) { + auto &[_1, _2, symbolCppDoc, commentTokens] = fileData(classInfo->filePath); TranslationUnit * const tu = symbolCppDoc->translationUnit(); for (int i = 0; i < tu->commentCount(); ++i) { const Token &tok = tu->commentAt(i); - if (tok.bytesBegin() < klass->startOffset()) + if (tok.bytesBegin() < classInfo->startOffset) continue; - if (tok.bytesBegin() >= klass->endOffset()) + if (tok.bytesBegin() >= classInfo->endOffset) break; addToken(commentTokens, tok); } @@ -806,7 +822,8 @@ QList symbolOccurrencesInDeclarationComments(CppEditorWidget *edito const Document::Ptr &cppDoc = semanticInfo.doc; if (!cppDoc) return {}; - const Symbol * const symbol = Internal::CanonicalSymbol(cppDoc, semanticInfo.snapshot)(cursor); + Internal::CanonicalSymbol cs(cppDoc, semanticInfo.snapshot); + const Symbol * const symbol = cs(cursor); if (!symbol || !symbol->asArgument()) return {}; const QTextDocument * const textDoc = editorWidget->textDocument()->document();