Fixes: Completing switch statements with enums in namespaces or classes.

We need to add the fully qualified name to the case statements.

Reviewed-by: Roberto Raggi
This commit is contained in:
con
2010-07-19 16:03:49 +02:00
parent caffe2c672
commit 5b989680ff
5 changed files with 73 additions and 14 deletions

View File

@@ -137,6 +137,18 @@ QString Overview::prettyName(const Name *name) const
return pp(name); return pp(name);
} }
QString Overview::prettyName(const QList<const Name *> &fullyQualifiedName) const
{
QString result;
const int size = fullyQualifiedName.size();
for (int i = 0; i < size; ++i) {
result.append(prettyName(fullyQualifiedName.at(i)));
if (i < size - 1)
result.append(QLatin1String("::"));
}
return result;
}
QString Overview::prettyType(const FullySpecifiedType &ty, const Name *name) const QString Overview::prettyType(const FullySpecifiedType &ty, const Name *name) const
{ {
return prettyType(ty, prettyName(name)); return prettyType(ty, prettyName(name));

View File

@@ -72,10 +72,14 @@ public:
QString operator()(const Name *name) const QString operator()(const Name *name) const
{ return prettyName(name); } { return prettyName(name); }
QString operator()(const QList<const Name *> &fullyQualifiedName) const
{ return prettyName(fullyQualifiedName); }
QString operator()(const FullySpecifiedType &type, const Name *name = 0) const QString operator()(const FullySpecifiedType &type, const Name *name = 0) const
{ return prettyType(type, name); } { return prettyType(type, name); }
QString prettyName(const Name *name) const; QString prettyName(const Name *name) const;
QString prettyName(const QList<const Name *> &fullyQualifiedName) const;
QString prettyType(const FullySpecifiedType &type, const Name *name = 0) const; QString prettyType(const FullySpecifiedType &type, const Name *name = 0) const;
QString prettyType(const FullySpecifiedType &type, const QString &name) const; QString prettyType(const FullySpecifiedType &type, const QString &name) const;

View File

@@ -289,14 +289,8 @@ void CppHoverHandler::handleLookupItemMatch(const LookupItem &lookupItem, const
if (matchingDeclaration->enclosingSymbol()->isClass() || if (matchingDeclaration->enclosingSymbol()->isClass() ||
matchingDeclaration->enclosingSymbol()->isNamespace() || matchingDeclaration->enclosingSymbol()->isNamespace() ||
matchingDeclaration->enclosingSymbol()->isEnum()) { matchingDeclaration->enclosingSymbol()->isEnum()) {
const QList<const Name *> &names = qualifiedName.append(overview.prettyName(
LookupContext::fullyQualifiedName(matchingDeclaration); LookupContext::fullyQualifiedName(matchingDeclaration)));
const int size = names.size();
for (int i = 0; i < size; ++i) {
qualifiedName.append(overview.prettyName(names.at(i)));
if (i < size - 1)
qualifiedName.append(QLatin1String("::"));
}
} else { } else {
qualifiedName.append(overview.prettyName(matchingDeclaration->name())); qualifiedName.append(overview.prettyName(matchingDeclaration->name()));
} }

View File

@@ -1257,11 +1257,13 @@ public:
Overview prettyPrint; Overview prettyPrint;
for (unsigned i = 0; i < e->memberCount(); ++i) { for (unsigned i = 0; i < e->memberCount(); ++i) {
if (Declaration *decl = e->memberAt(i)->asDeclaration()) { if (Declaration *decl = e->memberAt(i)->asDeclaration()) {
values << prettyPrint(decl->name()); values << prettyPrint(LookupContext::fullyQualifiedName(decl));
} }
} }
// Get the used values // Get the used values
CaseStatementCollector caseValues(document()->translationUnit()); Block *block = switchStatement->symbol;
CaseStatementCollector caseValues(document(), snapshot(),
document()->scopeAt(block->line(), block->column()));
QStringList usedValues = caseValues(switchStatement); QStringList usedValues = caseValues(switchStatement);
// save the values that would be added // save the values that would be added
foreach (const QString &usedValue, usedValues) foreach (const QString &usedValue, usedValues)
@@ -1318,7 +1320,15 @@ protected:
class CaseStatementCollector : public ASTVisitor class CaseStatementCollector : public ASTVisitor
{ {
public: public:
CaseStatementCollector(TranslationUnit *unit) : ASTVisitor(unit) {} CaseStatementCollector(Document::Ptr document, const Snapshot &snapshot,
Scope *scope)
: ASTVisitor(document->translationUnit()),
document(document),
scope(scope)
{
typeOfExpression.init(document, snapshot);
}
QStringList operator ()(AST *ast) QStringList operator ()(AST *ast)
{ {
values.clear(); values.clear();
@@ -1330,9 +1340,14 @@ protected:
bool preVisit(AST *ast) { bool preVisit(AST *ast) {
if (CaseStatementAST *cs = ast->asCaseStatement()) { if (CaseStatementAST *cs = ast->asCaseStatement()) {
foundCaseStatementLevel = true; foundCaseStatementLevel = true;
if (SimpleNameAST *sm = cs->expression->asSimpleName()) { ExpressionAST *expression = cs->expression->asSimpleName();
Overview prettyPrint; if (!expression)
values << prettyPrint(sm->name); expression = cs->expression->asQualifiedName();
if (expression) {
LookupItem item = typeOfExpression(expression,
document,
scope).first();
values << prettyPrint(LookupContext::fullyQualifiedName(item.declaration()));
} }
return true; return true;
} else if (foundCaseStatementLevel) { } else if (foundCaseStatementLevel) {
@@ -1341,8 +1356,12 @@ protected:
return true; return true;
} }
Overview prettyPrint;
bool foundCaseStatementLevel; bool foundCaseStatementLevel;
QStringList values; QStringList values;
TypeOfExpression typeOfExpression;
Document::Ptr document;
Scope *scope;
}; };
protected: protected:

View File

@@ -1,3 +1,17 @@
namespace Foo {
enum Orientation {
Vertical,
Horizontal
};
}
class Bar {
enum AnEnum {
AValue,
AnotherValue
};
};
enum Types { enum Types {
TypeA, TypeA,
TypeC, TypeC,
@@ -6,6 +20,8 @@ enum Types {
TypeE = TypeD TypeE = TypeD
}; };
using namespace Foo;
int main() int main()
{ {
int j; int j;
@@ -44,6 +60,20 @@ int main()
case TypeA:; case TypeA:;
} }
// Namespaces
Foo::Orientation o;
switch (o) {
case Vertical:
break;
}
// Class members
Bar::AnEnum a;
switch (a) {
case Bar::AnotherValue:
break;
}
// Not a named type // Not a named type
switch (i) { switch (i) {
case bla: case bla: