diff options
Diffstat (limited to 'src/plugins/cppeditor/cpphoverhandler.cpp')
-rw-r--r-- | src/plugins/cppeditor/cpphoverhandler.cpp | 141 |
1 files changed, 31 insertions, 110 deletions
diff --git a/src/plugins/cppeditor/cpphoverhandler.cpp b/src/plugins/cppeditor/cpphoverhandler.cpp index dabf25c791..4135d0b6cc 100644 --- a/src/plugins/cppeditor/cpphoverhandler.cpp +++ b/src/plugins/cppeditor/cpphoverhandler.cpp @@ -178,82 +178,6 @@ static QString buildHelpId(Symbol *symbol, const Name *name) return qualifiedNames.join(QLatin1String("::")); } -// ### move me -static FullySpecifiedType resolve(const FullySpecifiedType &ty, - const LookupContext &context, - Symbol **resolvedSymbol, - const Name **resolvedName) -{ - Control *control = context.control(); - - if (const PointerType *ptrTy = ty->asPointerType()) { - return control->pointerType(resolve(ptrTy->elementType(), context, - resolvedSymbol, resolvedName)); - - } else if (const ReferenceType *refTy = ty->asReferenceType()) { - return control->referenceType(resolve(refTy->elementType(), context, - resolvedSymbol, resolvedName)); - - } else if (const PointerToMemberType *ptrToMemTy = ty->asPointerToMemberType()) { - return control->pointerToMemberType(ptrToMemTy->memberName(), - resolve(ptrToMemTy->elementType(), context, - resolvedSymbol, resolvedName)); - - } else if (const NamedType *namedTy = ty->asNamedType()) { - if (resolvedName) - *resolvedName = namedTy->name(); - - const QList<Symbol *> candidates = context.resolve(namedTy->name()); - - foreach (Symbol *c, candidates) { - if (c->isClass() || c->isEnum()) { - if (resolvedSymbol) - *resolvedSymbol = c; - - return c->type(); - } - } - - } else if (const Namespace *nsTy = ty->asNamespaceType()) { - if (resolvedName) - *resolvedName = nsTy->name(); - - if (resolvedSymbol) - *resolvedSymbol = const_cast<Namespace *>(nsTy); - - } else if (const Class *classTy = ty->asClassType()) { - if (resolvedName) - *resolvedName = classTy->name(); - - if (resolvedSymbol) - *resolvedSymbol = const_cast<Class *>(classTy); - - } else if (const ForwardClassDeclaration *fwdClassTy = ty->asForwardClassDeclarationType()) { - if (resolvedName) - *resolvedName = fwdClassTy->name(); - - if (resolvedSymbol) - *resolvedSymbol = const_cast<ForwardClassDeclaration *>(fwdClassTy); - - } else if (const Enum *enumTy = ty->asEnumType()) { - if (resolvedName) - *resolvedName = enumTy->name(); - - if (resolvedSymbol) - *resolvedSymbol = const_cast<Enum *>(enumTy); - - } else if (const Function *funTy = ty->asFunctionType()) { - if (resolvedName) - *resolvedName = funTy->name(); - - if (resolvedSymbol) - *resolvedSymbol = const_cast<Function *>(funTy); - - } - - return ty; -} - void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, int pos) { m_helpId.clear(); @@ -266,9 +190,9 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in if (!edit) return; - const Snapshot documents = m_modelManager->snapshot(); + const Snapshot snapshot = m_modelManager->snapshot(); const QString fileName = editor->file()->fileName(); - Document::Ptr doc = documents.document(fileName); + Document::Ptr doc = snapshot.document(fileName); if (!doc) return; // nothing to do @@ -281,10 +205,10 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in // Find the last symbol up to the cursor position int line = 0, column = 0; editor->convertPosition(tc.position(), &line, &column); - Symbol *lastSymbol = doc->findSymbolAt(line, column); + Scope *scope = doc->scopeAt(line, column); TypeOfExpression typeOfExpression; - typeOfExpression.setSnapshot(documents); + typeOfExpression.init(doc, snapshot); // We only want to show F1 if the tooltip matches the help id bool showF1 = true; @@ -333,52 +257,49 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in ExpressionUnderCursor expressionUnderCursor; const QString expression = expressionUnderCursor(tc); - const QList<LookupItem> types = typeOfExpression(expression, doc, lastSymbol); + const QList<LookupItem> types = typeOfExpression(expression, scope); - if (!types.isEmpty()) { - const LookupItem result = types.first(); - FullySpecifiedType firstType = result.type(); // result of `type of expression'. - Symbol *lookupSymbol = result.lastVisibleSymbol(); // lookup symbol - - Symbol *resolvedSymbol = lookupSymbol; - const Name *resolvedName = lookupSymbol ? lookupSymbol->name() : 0; - firstType = resolve(firstType, typeOfExpression.lookupContext(), - &resolvedSymbol, &resolvedName); - - if (resolvedSymbol && resolvedSymbol->scope() - && resolvedSymbol->scope()->isClassScope()) { - Class *enclosingClass = resolvedSymbol->scope()->owner()->asClass(); + if (!types.isEmpty()) { + Overview overview; + overview.setShowArgumentNames(true); + overview.setShowReturnTypes(true); + overview.setShowFullyQualifiedNamed(true); + + const LookupItem result = types.first(); // ### TODO: select the best candidate. + FullySpecifiedType symbolTy = result.type(); // result of `type of expression'. + Symbol *declaration = result.declaration(); // lookup symbol + const Name *declarationName = declaration ? declaration->name() : 0; + + if (declaration && declaration->scope() + && declaration->scope()->isClassScope()) { + Class *enclosingClass = declaration->scope()->owner()->asClass(); if (const Identifier *id = enclosingClass->identifier()) { - if (id->isEqualTo(resolvedSymbol->identifier())) - resolvedSymbol = enclosingClass; + if (id->isEqualTo(declaration->identifier())) + declaration = enclosingClass; } } - m_helpId = buildHelpId(resolvedSymbol, resolvedName); + m_helpId = buildHelpId(declaration, declarationName); if (m_toolTip.isEmpty()) { - Symbol *symbol = result.lastVisibleSymbol(); - if (resolvedSymbol) - symbol = resolvedSymbol; + Symbol *symbol = declaration; - Overview overview; - overview.setShowArgumentNames(true); - overview.setShowReturnTypes(true); - overview.setShowFullyQualifiedNamed(true); + if (declaration) + symbol = declaration; - if (symbol && symbol == resolvedSymbol && symbol->isClass()) { + if (symbol && symbol == declaration && symbol->isClass()) { m_toolTip = m_helpId; - } else if (lookupSymbol && (lookupSymbol->isDeclaration() || lookupSymbol->isArgument())) { - m_toolTip = overview.prettyType(firstType, buildHelpId(lookupSymbol, lookupSymbol->name())); + } else if (declaration && (declaration->isDeclaration() || declaration->isArgument())) { + m_toolTip = overview.prettyType(symbolTy, buildHelpId(declaration, declaration->name())); - } else if (firstType->isClassType() || firstType->isEnumType() || - firstType->isForwardClassDeclarationType()) { + } else if (symbolTy->isClassType() || symbolTy->isEnumType() || + symbolTy->isForwardClassDeclarationType()) { m_toolTip = m_helpId; } else { - m_toolTip = overview.prettyType(firstType, m_helpId); + m_toolTip = overview.prettyType(symbolTy, m_helpId); } } |