Warning, file /kdevelop/kdev-python/duchain/navigation/declarationnavigationcontext.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
0003     SPDX-FileCopyrightText: 2008 Niko Sams <niko.sams@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "declarationnavigationcontext.h"
0009 
0010 #include <klocalizedstring.h>
0011 
0012 #include <language/duchain/functiondefinition.h>
0013 #include <language/duchain/namespacealiasdeclaration.h>
0014 #include <language/duchain/forwarddeclaration.h>
0015 #include <language/duchain/duchainutils.h>
0016 #include <language/duchain/types/typepointer.h>
0017 #include <language/duchain/aliasdeclaration.h>
0018 
0019 #include <language/duchain/types/containertypes.h>
0020 
0021 #include "helpers.h"
0022 #include <types/indexedcontainer.h>
0023 
0024 namespace Python
0025 {
0026 using namespace KDevelop;
0027 
0028 DeclarationNavigationContext::DeclarationNavigationContext(DeclarationPointer decl, KDevelop::TopDUContextPointer topContext, AbstractNavigationContext* previousContext)
0029         : AbstractDeclarationNavigationContext(decl, topContext, previousContext)
0030 {
0031 }
0032 
0033 QString DeclarationNavigationContext::getLink(const QString& name, DeclarationPointer declaration, NavigationAction::Type actionType) {
0034     NavigationAction action( declaration, actionType );
0035     QString targetId = QString::number((quint64)declaration.data() * actionType);
0036     return createLink(name, targetId, action);
0037 };
0038 
0039 void DeclarationNavigationContext::htmlClass()
0040 {
0041     Q_ASSERT(declaration()->abstractType());
0042     auto klass = declaration()->abstractType().staticCast<StructureType>();
0043 
0044     modifyHtml() += QStringLiteral("class ");
0045     eventuallyMakeTypeLinks( klass );
0046 
0047     auto classDecl = dynamic_cast<ClassDeclaration*>(klass->declaration(topContext().data()));
0048     if ( classDecl && classDecl->baseClassesSize() ) {
0049         int count = 0;
0050         FOREACH_FUNCTION( const BaseClassInstance& base, classDecl->baseClasses ) {
0051             modifyHtml() += count++ ? QStringLiteral(", ") : QStringLiteral(" (");
0052             eventuallyMakeTypeLinks(base.baseClass.abstractType());
0053         }
0054         modifyHtml() += QStringLiteral(")");
0055     }
0056 }
0057 
0058 QString DeclarationNavigationContext::typeLinkOrString(const AbstractType::Ptr type) {
0059     if ( type ) {
0060         if ( auto idType = dynamic_cast<IdentifiedType*>(type.data()) ) {
0061             return getLink(type->toString(),
0062                            DeclarationPointer(idType->declaration(topContext().data())),
0063                            NavigationAction::NavigateDeclaration);
0064         }
0065         return type->toString().toHtmlEscaped();
0066     }
0067     return i18nc("refers to an unknown type in programming", "unknown");
0068 }
0069 
0070 void DeclarationNavigationContext::htmlIdentifiedType(AbstractType::Ptr type, const IdentifiedType* idType)
0071 {
0072     // TODO this code is duplicate of variablelengthcontainer::toString, resolve that somehow
0073     if ( auto listType = type.dynamicCast<ListType>() ) {
0074         QString contentType;
0075         const QString containerType = getLink(listType->containerToString(),
0076                                               DeclarationPointer(idType->declaration(topContext().data())),
0077                                               NavigationAction::NavigateDeclaration );
0078         if ( auto map = listType.dynamicCast<MapType>() ) {
0079             contentType.append(typeLinkOrString(map->keyType().abstractType()));
0080             contentType.append(" : ");
0081         }
0082         contentType.append(typeLinkOrString(listType->contentType().abstractType()));
0083         modifyHtml() += i18nc("as in list of int, set of string", "%1 of %2", containerType, contentType);
0084     }
0085     else if (auto indexedContainer = type.dynamicCast<IndexedContainer>()) {
0086         const QString containerType = getLink(indexedContainer->containerToString(),
0087                                               DeclarationPointer(idType->declaration(topContext().data())),
0088                                               NavigationAction::NavigateDeclaration );
0089         QStringList typesArray;
0090         for ( int i = 0; i < indexedContainer->typesCount(); i++ ) {
0091             if ( i >= 5 ) {
0092                 // Don't print more than five types explicitly
0093                 typesArray << "...";
0094                 break;
0095             }
0096             typesArray << typeLinkOrString(indexedContainer->typeAt(i).abstractType());
0097         }
0098         const QString contentType = QStringLiteral("(") + typesArray.join(", ") + ")";
0099         modifyHtml() += i18nc("as in list of int, set of string", "%1 of %2", containerType, contentType);
0100     }
0101     else {
0102         KDevelop::AbstractDeclarationNavigationContext::htmlIdentifiedType(type, idType);
0103     }
0104 }
0105 
0106 void DeclarationNavigationContext::eventuallyMakeTypeLinks(AbstractType::Ptr type)
0107 {
0108     KDevelop::AbstractDeclarationNavigationContext::eventuallyMakeTypeLinks(Helper::resolveAliasType(type));
0109 }
0110 
0111 }
0112 
0113 #include "moc_declarationnavigationcontext.cpp"