summaryrefslogtreecommitdiff
path: root/src/libs/cplusplus/CppDocument.cpp
diff options
context:
space:
mode:
authorErik Verbruggen <erik.verbruggen@digia.com>2013-05-02 14:55:56 +0200
committerNikolai Kosjar <nikolai.kosjar@digia.com>2013-06-04 15:24:33 +0200
commitca291fbc7ba860b1856b45eff5a02daa56a39d65 (patch)
tree0795f101ce78b9373d368687ce89fbe984d68f0d /src/libs/cplusplus/CppDocument.cpp
parent6a4a9266225d1e2570f602e11e3218c9667f9570 (diff)
downloadqt-creator-ca291fbc7ba860b1856b45eff5a02daa56a39d65.tar.gz
C++: fix functionAt(), moved it, and added test.
Thanks to Jesper K. Pedersen for the fix! Change-Id: Ie49c3352e26a9632b1500596b00d559bfe932dff Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com>
Diffstat (limited to 'src/libs/cplusplus/CppDocument.cpp')
-rw-r--r--src/libs/cplusplus/CppDocument.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/libs/cplusplus/CppDocument.cpp b/src/libs/cplusplus/CppDocument.cpp
index ee6d7f3a9d..84092fc93d 100644
--- a/src/libs/cplusplus/CppDocument.cpp
+++ b/src/libs/cplusplus/CppDocument.cpp
@@ -464,6 +464,57 @@ void Document::setGlobalNamespace(Namespace *globalNamespace)
_globalNamespace = globalNamespace;
}
+/*!
+ * Extract the function name including scope at the given position.
+ *
+ * Note that a function (scope) starts at the name of that function, not at the return type. The
+ * implication is that this method will return an empty string when the line/column is on the
+ * return type.
+ *
+ * \param line the line number, starting with line 1
+ * \param column the column number, starting with column 1
+ */
+QString Document::functionAt(int line, int column) const
+{
+ if (line < 1 || column < 1)
+ return QString();
+
+ CPlusPlus::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;
+ if (symbol->isScope())
+ scope = symbol->asScope();
+ else
+ scope = symbol->enclosingScope();
+
+ while (scope && !scope->isFunction() )
+ scope = scope->enclosingScope();
+
+ if (!scope)
+ return QString();
+
+ // We found the function scope, extract its name.
+ const Overview o;
+ QString rc = o.prettyName(scope->name());
+
+ // Prepend namespace "Foo::Foo::foo()" up to empty root namespace
+ for (const Symbol *owner = scope->enclosingNamespace();
+ owner; owner = owner->enclosingNamespace()) {
+ const QString name = o.prettyName(owner->name());
+ if (name.isEmpty()) {
+ break;
+ } else {
+ rc.prepend(QLatin1String("::"));
+ rc.prepend(name);
+ }
+ }
+ return rc;
+}
+
Scope *Document::scopeAt(unsigned line, unsigned column)
{
FindScopeAt findScopeAt(_translationUnit, line, column);