diff options
author | Christian Kandeler <christian.kandeler@qt.io> | 2022-06-10 10:47:38 +0200 |
---|---|---|
committer | Christian Kandeler <christian.kandeler@qt.io> | 2022-06-13 06:54:58 +0000 |
commit | e128c8cbde63ae42329943adf3a7265b193656a4 (patch) | |
tree | ef8b2a0d6ad0e94e141de7d230ff9a600d266894 /src/libs/cplusplus/CppDocument.cpp | |
parent | 737877984d7a2c2570daf0adf8015208944cc129 (diff) | |
download | qt-creator-e128c8cbde63ae42329943adf3a7265b193656a4.tar.gz |
CppEditor: Fix looking up containing function for search result
- Move look-up to CplusPlus::FindUsages, where we are guaranteed that
we actually have the document source.
- Use the same straightforward algorithm as with clangd.
- Undo the changes to CppDocument::functionAt(), which broke
the autotest.
Amends 6f7e7980d2b604c79507f9165098f783db8ab2e3.
Change-Id: I008d05ba41a3b63b71e3131d7021e0d4e7d0641f
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Diffstat (limited to 'src/libs/cplusplus/CppDocument.cpp')
-rw-r--r-- | src/libs/cplusplus/CppDocument.cpp | 90 |
1 files changed, 9 insertions, 81 deletions
diff --git a/src/libs/cplusplus/CppDocument.cpp b/src/libs/cplusplus/CppDocument.cpp index 60144b2320..90660f2b55 100644 --- a/src/libs/cplusplus/CppDocument.cpp +++ b/src/libs/cplusplus/CppDocument.cpp @@ -96,86 +96,6 @@ protected: } }; -class ContainingFunctionAt: protected SymbolVisitor -{ - TranslationUnit *translationUnit; - Symbol *root; - int line; - int column; - Symbol *functionSymbol; - bool foundFunction; - bool foundBlock; - - bool scopeContains(Scope* scope, int line, int column){ - if (!scope) - return false; - - int scopeStartLine{-1}, scopeStartColumn{-1}, scopeEndLine{-1}, scopeEndColumn{-1}; - translationUnit->getPosition(scope->startOffset(), &scopeStartLine, &scopeStartColumn); - translationUnit->getPosition(scope->endOffset(), &scopeEndLine, &scopeEndColumn); - - if (line < scopeStartLine || line > scopeEndLine) - return false; - - if (line > scopeStartLine && line < scopeEndLine) - return true; - - if (scopeStartLine == line && column >= scopeStartColumn) - return true; - - if (scopeEndLine == line && column <= scopeEndColumn) - return true; - - return false; - } - -public: - ContainingFunctionAt(TranslationUnit *unit, Symbol *root) - : translationUnit(unit), root(root), line(0), column(0), functionSymbol(nullptr) - , foundFunction(false), foundBlock(false) {} - - Symbol *operator()(int line, int column) - { - this->line = line; - this->column = column; - this->functionSymbol = nullptr; - accept(root); - - return foundBlock ? functionSymbol : nullptr; - } - -protected: - bool preVisit(Symbol *s) final - { - if (foundBlock) - return false; - - if (foundFunction) { - auto block = s->asBlock(); - if (!block) - return true; - - if (scopeContains(block->asScope(), line, column)) { - foundBlock = true; - return false; - } - return true; - } - - auto asFunction = s->asFunction(); - if (asFunction) { - if (s->line() < line || (s->line() == line && s->column() <= column)) { - foundFunction = scopeContains(s->asScope(), line, column); - if (foundFunction) - functionSymbol = asFunction; - } - } - - return true; - } -}; - - class FindScopeAt: protected SymbolVisitor { TranslationUnit *_unit; @@ -592,12 +512,20 @@ QString Document::functionAt(int line, int column, int *lineOpeningDeclaratorPar if (line < 1 || column < 1) return QString(); - Symbol *symbol = ContainingFunctionAt{translationUnit(), globalNamespace()}(line, column); + Symbol *symbol = lastVisibleSymbolAt(line, column); if (!symbol) return QString(); + // Find the enclosing function scope (which might be several levels up, or we might be standing + // on it) Scope *scope = symbol->asScope(); if (!scope) + scope = symbol->enclosingScope(); + + while (scope && !scope->isFunction() ) + scope = scope->enclosingScope(); + + if (!scope) return QString(); // We found the function scope |