1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
SPDX-FileCopyrightText: 2008 Niko Sams <niko.sams@gmail.com>
SPDX-License-Identifier: LGPL-2.0-only
*/
#include "declarationnavigationcontext.h"
#include <QTextDocument>
#include <klocalizedstring.h>
#include <language/duchain/functiondefinition.h>
#include <language/duchain/namespacealiasdeclaration.h>
#include <language/duchain/forwarddeclaration.h>
#include <language/duchain/duchainutils.h>
#include <language/duchain/types/structuretype.h>
#include <language/duchain/types/functiontype.h>
#include <language/duchain/types/integraltype.h>
#include <language/duchain/types/arraytype.h>
#include "../declarations/classdeclaration.h"
#include <declarations/classmethoddeclaration.h>
#include <declarations/traitmethodaliasdeclaration.h>
#include <declarations/traitmemberaliasdeclaration.h>
#include <declarations/variabledeclaration.h>
#include "helper.h"
namespace Php
{
using namespace KDevelop;
DeclarationNavigationContext::DeclarationNavigationContext(DeclarationPointer decl, KDevelop::TopDUContextPointer topContext, AbstractNavigationContext* previousContext)
: AbstractDeclarationNavigationContext(decl, topContext, previousContext)
{
}
NavigationContextPointer DeclarationNavigationContext::registerChild(DeclarationPointer declaration)
{
return AbstractDeclarationNavigationContext::registerChild(new DeclarationNavigationContext(declaration, topContext(), this));
}
void DeclarationNavigationContext::htmlClass()
{
Q_ASSERT(declaration()->abstractType());
auto klass = declaration()->abstractType().staticCast<StructureType>();
ClassDeclaration* classDecl = dynamic_cast<ClassDeclaration*>(klass->declaration(topContext().data()));
if (classDecl) {
// write class modifier
switch (classDecl->classModifier()) {
case ClassDeclarationData::Abstract:
modifyHtml() += QStringLiteral("abstract ");
break;
case ClassDeclarationData::Final:
modifyHtml() += QStringLiteral("final ");
break;
default:
//nothing
break;
}
// write class type
if (classDecl->classType() == ClassDeclarationData::Interface) {
modifyHtml() += QStringLiteral("interface ");
} else if (classDecl->classType() == ClassDeclarationData::Trait) {
modifyHtml() += QStringLiteral("trait ");
} else {
modifyHtml() += QStringLiteral("class ");
}
// write class identifier
eventuallyMakeTypeLinks(declaration()->abstractType());
// write inheritance
if (classDecl->baseClassesSize() > 0) {
AbstractType::Ptr extends;
QList<AbstractType::Ptr> implements;
FOREACH_FUNCTION(const BaseClassInstance& base, classDecl->baseClasses) {
StructureType::Ptr stype = base.baseClass.type<StructureType>();
if (stype) {
ClassDeclaration *classDecl = dynamic_cast<ClassDeclaration*>(stype->declaration(topContext().data()));
if (classDecl) {
if (classDecl->classType() == ClassDeclarationData::Interface) {
implements.append(base.baseClass.abstractType());
} else {
extends = base.baseClass.abstractType();
}
}
}
}
// write parent class
if (extends) {
modifyHtml() += QStringLiteral(" extends ");
eventuallyMakeTypeLinks(extends);
}
// write implemented interfaces
if (!implements.isEmpty()) {
modifyHtml() += QStringLiteral(" implements ");
for (QList<AbstractType::Ptr>::iterator i = implements.begin(); ;) {
eventuallyMakeTypeLinks(*i);
++i;
if (i != implements.end()) {
modifyHtml() += QStringLiteral(", ");
} else {
break;
}
}
}
}
modifyHtml() += QStringLiteral(" ");
}
}
void DeclarationNavigationContext::htmlAdditionalNavigation()
{
if (auto member = dynamic_cast<TraitMethodAliasDeclaration*>(declaration().data())) {
Declaration *dec = member->aliasedDeclaration().data();
if (dec && dec->context() && dec->context()->owner()) {
modifyHtml() += i18n("Use of %1 from %2<br />")
.arg(createLink(prettyQualifiedIdentifier(DeclarationPointer(dec)).toString(),
QStringLiteral("jump_to_used"),
NavigationAction(DeclarationPointer(dec),
KDevelop::NavigationAction::NavigateDeclaration)))
.arg(createLink(prettyQualifiedIdentifier(DeclarationPointer(dec->context()->owner())).toString(),
QStringLiteral("jump_to_used_container"),
NavigationAction(DeclarationPointer(dec->context()->owner()),
KDevelop::NavigationAction::NavigateDeclaration)));
}
} else if (auto member = dynamic_cast<TraitMemberAliasDeclaration*>(declaration().data())) {
Declaration *dec = member->aliasedDeclaration().data();
if (dec && dec->context() && dec->context()->owner()) {
modifyHtml() += i18n("Use of %1 from %2<br />")
.arg(createLink(prettyQualifiedIdentifier(DeclarationPointer(dec)).toString(),
QStringLiteral("jump_to_used"),
NavigationAction(DeclarationPointer(dec),
KDevelop::NavigationAction::NavigateDeclaration)))
.arg(createLink(prettyQualifiedIdentifier(DeclarationPointer(dec->context()->owner())).toString(),
QStringLiteral("jump_to_used_container"),
NavigationAction(DeclarationPointer(dec->context()->owner()),
KDevelop::NavigationAction::NavigateDeclaration)));
} else {
modifyHtml() += i18n("Broken member alias trait.");
}
}
KDevelop::AbstractDeclarationNavigationContext::htmlAdditionalNavigation();
}
void DeclarationNavigationContext::htmlFunction()
{
const AbstractFunctionDeclaration* function = dynamic_cast<const AbstractFunctionDeclaration*>(declaration().data());
Q_ASSERT(function);
const ClassFunctionDeclaration* classFunDecl = dynamic_cast<const ClassFunctionDeclaration*>(declaration().data());
const auto type = declaration()->abstractType().dynamicCast<FunctionType>();
if( !type ) {
modifyHtml() += errorHighlight(QStringLiteral("Invalid type<br />"));
return;
}
if( !classFunDecl || (!classFunDecl->isConstructor() && !classFunDecl->isDestructor()) ) {
// only print return type for global functions and non-ctor/dtor methods
eventuallyMakeTypeLinks( type->returnType() );
}
modifyHtml() += ' ' + identifierHighlight(prettyIdentifier(declaration()).toString().toHtmlEscaped(), declaration());
if( type->indexedArgumentsSize() == 0 )
{
modifyHtml() += QStringLiteral("()");
} else {
modifyHtml() += QStringLiteral("( ");
bool first = true;
int firstDefaultParam = type->indexedArgumentsSize() - function->defaultParametersSize();
int currentArgNum = 0;
QVector<Declaration*> decls;
if (DUContext* argumentContext = DUChainUtils::argumentContext(declaration().data())) {
decls = argumentContext->localDeclarations(topContext().data());
}
foreach(const AbstractType::Ptr& argType, type->arguments()) {
if( !first )
modifyHtml() += QStringLiteral(", ");
first = false;
VariableDeclaration *argDec = nullptr;
if (!decls.isEmpty()) {
argDec = dynamic_cast<VariableDeclaration*>(decls[currentArgNum]);
}
if (argDec && argDec->isVariadic()) {
AbstractType::Ptr variadicType;
const auto a_type = argType.dynamicCast<KDevelop::ArrayType>();
if (a_type) {
variadicType = a_type->elementType();
} else {
variadicType = AbstractType::Ptr(new IntegralType(IntegralType::TypeMixed));
}
modifyHtml() += QStringLiteral("[");
eventuallyMakeTypeLinks( variadicType );
if (currentArgNum < decls.size()) {
modifyHtml() += QStringLiteral(" ...") + identifierHighlight(decls[currentArgNum]->identifier().toString().toHtmlEscaped(), declaration());
}
modifyHtml() += QStringLiteral("]");
} else {
eventuallyMakeTypeLinks( argType );
if (currentArgNum < decls.size()) {
modifyHtml() += ' ' + identifierHighlight(decls[currentArgNum]->identifier().toString().toHtmlEscaped(), declaration());
}
if (currentArgNum >= firstDefaultParam) {
IndexedString defaultStr = function->defaultParameters()[currentArgNum - firstDefaultParam];
if (!defaultStr.isEmpty()) {
modifyHtml() += " = " + defaultStr.str().toHtmlEscaped();
}
}
}
++currentArgNum;
}
modifyHtml() += QStringLiteral(" )");
}
modifyHtml() += QStringLiteral("<br />");
}
QualifiedIdentifier DeclarationNavigationContext::prettyQualifiedIdentifier(const DeclarationPointer& decl) const
{
return QualifiedIdentifier(prettyName(decl.data()));
}
void DeclarationNavigationContext::makeLink(const QString& name, const DeclarationPointer& declaration, NavigationAction::Type actionType)
{
if ( actionType == NavigationAction::JumpToSource && declaration->url() == internalFunctionFile() ) {
modifyHtml() += i18n("PHP internal");
return;
}
AbstractDeclarationNavigationContext::makeLink(name, declaration, actionType);
}
QString DeclarationNavigationContext::declarationKind(const DeclarationPointer& decl)
{
if ( decl->kind() == Declaration::Instance && decl->abstractType()
&& decl->abstractType()->modifiers() & AbstractType::ConstModifier ) {
return i18nc("kind of a php-constant, as shown in the declaration tooltip", "Constant");
}
return AbstractDeclarationNavigationContext::declarationKind(decl);
}
}
|