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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
/*
SPDX-FileCopyrightText: 2007-2009 Hamish Rodda <rodda@kde.org>
SPDX-FileCopyrightText: 2009 Lior Mualem <lior.m.kde@gmail.com>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "classmodelnode.h"
#include <typeinfo>
#include <KLocalizedString>
#include "../duchain/duchainlock.h"
#include "../duchain/duchain.h"
#include "../duchain/persistentsymboltable.h"
#include "../duchain/duchainutils.h"
#include "../duchain/classdeclaration.h"
#include "../duchain/classfunctiondeclaration.h"
#include "../duchain/types/functiontype.h"
#include "../duchain/types/enumerationtype.h"
#include <debug.h>
using namespace KDevelop;
using namespace ClassModelNodes;
IdentifierNode::IdentifierNode(KDevelop::Declaration* a_decl,
NodesModelInterface* a_model,
const QString& a_displayName)
: DynamicNode(a_displayName.isEmpty() ? a_decl->identifier().toString() : a_displayName, a_model)
, m_identifier(a_decl->qualifiedIdentifier())
, m_indexedDeclaration(a_decl)
, m_cachedDeclaration(a_decl)
{
}
Declaration* IdentifierNode::declaration()
{
if (!m_cachedDeclaration)
m_cachedDeclaration = m_indexedDeclaration.declaration();
return m_cachedDeclaration.data();
}
bool IdentifierNode::getIcon(QIcon& a_resultIcon)
{
DUChainReadLocker readLock(DUChain::lock());
Declaration* decl = declaration();
if (decl)
a_resultIcon = DUChainUtils::iconForDeclaration(decl);
return !a_resultIcon.isNull();
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
EnumNode::EnumNode(KDevelop::Declaration* a_decl, NodesModelInterface* a_model)
: IdentifierNode(a_decl, a_model)
{
// Set display name for anonymous enums
if (m_displayName.isEmpty())
m_displayName = QStringLiteral("*Anonymous*");
}
bool EnumNode::getIcon(QIcon& a_resultIcon)
{
DUChainReadLocker readLock(DUChain::lock());
auto* decl = dynamic_cast<ClassMemberDeclaration*>(declaration());
if (decl == nullptr) {
a_resultIcon = QIcon::fromTheme(QStringLiteral("enum"));
} else
{
if (decl->accessPolicy() == Declaration::Protected) {
a_resultIcon = QIcon::fromTheme(QStringLiteral("protected_enum"));
} else if (decl->accessPolicy() == Declaration::Private) {
a_resultIcon = QIcon::fromTheme(QStringLiteral("private_enum"));
} else
{
a_resultIcon = QIcon::fromTheme(QStringLiteral("enum"));
}
}
return true;
}
void EnumNode::populateNode()
{
DUChainReadLocker readLock(DUChain::lock());
Declaration* decl = declaration();
if (decl->internalContext()) {
const auto localDeclarations = decl->internalContext()->localDeclarations();
for (Declaration* enumDecl : localDeclarations) {
addNode(new EnumNode(enumDecl, m_model));
}
}
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
ClassNode::ClassNode(Declaration* a_decl, NodesModelInterface* a_model)
: IdentifierNode(a_decl, a_model)
{
}
ClassNode::~ClassNode()
{
if (!m_cachedUrl.isEmpty()) {
ClassModelNodesController::self().unregisterForChanges(m_cachedUrl, this);
m_cachedUrl = IndexedString();
}
}
void ClassNode::populateNode()
{
DUChainReadLocker readLock(DUChain::lock());
if (m_model->features().testFlag(NodesModelInterface::ClassInternals)) {
if (updateClassDeclarations()) {
m_cachedUrl = declaration()->url();
ClassModelNodesController::self().registerForChanges(m_cachedUrl, this);
}
}
// Add special folders
if (m_model->features().testFlag(NodesModelInterface::BaseAndDerivedClasses))
addBaseAndDerived();
}
template <> inline bool qMapLessThanKey(const IndexedIdentifier& key1, const IndexedIdentifier& key2)
{
return key1.index() < key2.index();
}
bool ClassNode::updateClassDeclarations()
{
bool hadChanges = false;
SubIdentifiersMap existingIdentifiers = m_subIdentifiers;
auto* klass = dynamic_cast<ClassDeclaration*>(declaration());
if (klass) {
const auto localDeclarations = klass->internalContext()->localDeclarations();
for (Declaration* decl : localDeclarations) {
// Ignore forward declarations.
if (decl->isForwardDeclaration())
continue;
// Don't add existing declarations.
const auto identifierIt = existingIdentifiers.find(decl->ownIndex());
if (identifierIt != existingIdentifiers.end()) {
existingIdentifiers.erase(identifierIt);
continue;
}
Node* newNode = nullptr;
if (EnumerationType::Ptr enumType = decl->type<EnumerationType>())
newNode = new EnumNode(decl, m_model);
else if (decl->isFunctionDeclaration())
newNode = new FunctionNode(decl, m_model);
else if (auto* classDecl = dynamic_cast<ClassDeclaration*>(decl))
newNode = new ClassNode(classDecl, m_model);
else if (auto* memDecl = dynamic_cast<ClassMemberDeclaration*>(decl))
newNode = new ClassMemberNode(memDecl, m_model);
else
{
// Debug - for reference.
qCDebug(LANGUAGE) << "class: " << klass->toString() << "name: " << decl->toString() <<
" - unknown declaration type: " << typeid(*decl).name();
}
if (newNode) {
addNode(newNode);
// Also remember the identifier.
m_subIdentifiers.insert(decl->ownIndex(), newNode);
hadChanges = true;
}
}
}
// Remove old existing identifiers
for (SubIdentifiersMap::iterator iter = existingIdentifiers.begin();
iter != existingIdentifiers.end();
++iter) {
iter.value()->removeSelf();
m_subIdentifiers.remove(iter.key());
hadChanges = true;
}
return hadChanges;
}
bool ClassNode::addBaseAndDerived()
{
bool added = false;
auto* baseClassesNode = new BaseClassesFolderNode(m_model);
addNode(baseClassesNode);
if (!baseClassesNode->hasChildren())
removeNode(baseClassesNode);
else
added = true;
auto* derivedClassesNode = new DerivedClassesFolderNode(m_model);
addNode(derivedClassesNode);
if (!derivedClassesNode->hasChildren())
removeNode(derivedClassesNode);
else
added = true;
return added;
}
void ClassNode::nodeCleared()
{
if (!m_cachedUrl.isEmpty()) {
ClassModelNodesController::self().unregisterForChanges(m_cachedUrl, this);
m_cachedUrl = IndexedString();
}
m_subIdentifiers.clear();
}
void ClassModelNodes::ClassNode::documentChanged(const KDevelop::IndexedString&)
{
DUChainReadLocker readLock(DUChain::lock());
if (updateClassDeclarations())
recursiveSort();
}
ClassNode* ClassNode::findSubClass(const KDevelop::IndexedQualifiedIdentifier& a_id)
{
// Make sure we have sub nodes.
performPopulateNode();
/// @todo This is slow - we go over all the sub identifiers but the assumption is that
/// this function call is rare and the list is not that long.
for (Node* item : qAsConst(m_subIdentifiers)) {
auto* classNode = dynamic_cast<ClassNode*>(item);
if (classNode == nullptr)
continue;
if (classNode->identifier() == a_id)
return classNode;
}
return nullptr;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
FunctionNode::FunctionNode(Declaration* a_decl, NodesModelInterface* a_model)
: IdentifierNode(a_decl, a_model)
{
// Append the argument signature to the identifier's name (which is what the displayName is.
if (FunctionType::Ptr type = a_decl->type<FunctionType>())
m_displayName += type->partToString(FunctionType::SignatureArguments);
// Add special values for ctor / dtor to sort first
auto* classmember = dynamic_cast<ClassFunctionDeclaration*>(a_decl);
if (classmember) {
if (classmember->isConstructor() || classmember->isDestructor())
m_sortableString = QLatin1Char('0') + m_displayName;
else
m_sortableString = QLatin1Char('1') + m_displayName;
} else {
m_sortableString = m_displayName;
}
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
ClassMemberNode::ClassMemberNode(KDevelop::ClassMemberDeclaration* a_decl, NodesModelInterface* a_model)
: IdentifierNode(a_decl, a_model)
{
}
bool ClassMemberNode::getIcon(QIcon& a_resultIcon)
{
DUChainReadLocker readLock(DUChain::lock());
auto* decl = dynamic_cast<ClassMemberDeclaration*>(declaration());
if (decl == nullptr)
return false;
if (decl->isTypeAlias()) {
a_resultIcon = QIcon::fromTheme(QStringLiteral("typedef"));
} else if (decl->accessPolicy() == Declaration::Protected) {
a_resultIcon = QIcon::fromTheme(QStringLiteral("protected_field"));
} else if (decl->accessPolicy() == Declaration::Private) {
a_resultIcon = QIcon::fromTheme(QStringLiteral("private_field"));
} else
{
a_resultIcon = QIcon::fromTheme(QStringLiteral("field"));
}
return true;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
DynamicFolderNode::DynamicFolderNode(const QString& a_displayName, NodesModelInterface* a_model)
: DynamicNode(a_displayName, a_model)
{
}
bool DynamicFolderNode::getIcon(QIcon& a_resultIcon)
{
a_resultIcon = QIcon::fromTheme(QStringLiteral("folder"));
return true;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
FolderNode::FolderNode(const QString& a_displayName, NodesModelInterface* a_model)
: Node(a_displayName, a_model)
{
}
bool FolderNode::getIcon(QIcon& a_resultIcon)
{
a_resultIcon = QIcon::fromTheme(QStringLiteral("folder"));
return true;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
BaseClassesFolderNode::BaseClassesFolderNode(NodesModelInterface* a_model)
: DynamicFolderNode(i18n("Base classes"), a_model)
{
}
void BaseClassesFolderNode::populateNode()
{
DUChainReadLocker readLock(DUChain::lock());
auto* klass = dynamic_cast<ClassDeclaration*>(static_cast<ClassNode*>(parent())->declaration());
if (klass) {
// I use the imports instead of the baseClasses in the ClassDeclaration because I need
// to get to the base class identifier which is not directly accessible through the
// baseClasses function.
const auto imports = klass->internalContext()->importedParentContexts();
for (const DUContext::Import& import : imports) {
DUContext* baseContext = import.context(klass->topContext());
if (baseContext && baseContext->type() == DUContext::Class) {
Declaration* baseClassDeclaration = baseContext->owner();
if (baseClassDeclaration) {
// Add the base class.
addNode(new ClassNode(baseClassDeclaration, m_model));
}
}
}
}
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
DerivedClassesFolderNode::DerivedClassesFolderNode(NodesModelInterface* a_model)
: DynamicFolderNode(i18n("Derived classes"), a_model)
{
}
void DerivedClassesFolderNode::populateNode()
{
DUChainReadLocker readLock(DUChain::lock());
auto* klass = dynamic_cast<ClassDeclaration*>(static_cast<ClassNode*>(parent())->declaration());
if (klass) {
uint steps = 10000;
const QList<Declaration*> inheriters = DUChainUtils::inheriters(klass, steps, true);
for (Declaration* decl : inheriters) {
addNode(new ClassNode(decl, m_model));
}
}
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Node::Node(const QString& a_displayName, NodesModelInterface* a_model)
: m_parentNode(nullptr)
, m_displayName(a_displayName)
, m_model(a_model)
{
}
Node::~Node()
{
// Notify the model about the removal of this nodes' children.
if (!m_children.empty() && m_model) {
m_model->nodesAboutToBeRemoved(this, 0, m_children.size() - 1);
clear();
m_model->nodesRemoved(this);
}
}
void Node::clear()
{
qDeleteAll(m_children);
m_children.clear();
}
void Node::addNode(Node* a_child)
{
/// @note This is disabled for performance reasons - we add them to the bottom and a
/// sort usually follows which causes a layout change to be fired.
// m_model->nodesAboutToBeAdded(this, m_children.size(), 1);
a_child->m_parentNode = this;
m_children.push_back(a_child);
// m_model->nodesAdded(this);
}
void Node::removeNode(Node* a_child)
{
int row = a_child->row();
m_model->nodesAboutToBeRemoved(this, row, row);
m_children.removeAt(row);
delete a_child;
m_model->nodesRemoved(this);
}
// Sort algorithm for the nodes.
struct SortNodesFunctor
{
bool operator()(Node* a_lhs, Node* a_rhs)
{
if (a_lhs->score() == a_rhs->score()) {
return a_lhs->sortableString() < a_rhs->sortableString();
} else
return a_lhs->score() < a_rhs->score();
}
};
void Node::recursiveSortInternal()
{
// Sort my nodes.
std::sort(m_children.begin(), m_children.end(), SortNodesFunctor());
// Tell each node to sort it self.
for (Node* node : qAsConst(m_children)) {
node->recursiveSortInternal();
}
}
void Node::recursiveSort()
{
m_model->nodesLayoutAboutToBeChanged(this);
recursiveSortInternal();
m_model->nodesLayoutChanged(this);
}
int Node::row()
{
if (m_parentNode == nullptr)
return -1;
return m_parentNode->m_children.indexOf(this);
}
QIcon ClassModelNodes::Node::cachedIcon()
{
// Load the cached icon if it's null.
if (m_cachedIcon.isNull()) {
if (!getIcon(m_cachedIcon))
m_cachedIcon = QIcon();
}
return m_cachedIcon;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
DynamicNode::DynamicNode(const QString& a_displayName, NodesModelInterface* a_model)
: Node(a_displayName, a_model)
, m_populated(false)
{
}
void DynamicNode::collapse()
{
performNodeCleanup();
}
void DynamicNode::expand()
{
performPopulateNode();
}
void DynamicNode::performNodeCleanup()
{
if (!m_populated)
return;
if (!m_children.empty()) {
// Notify model for this node.
m_model->nodesAboutToBeRemoved(this, 0, m_children.size() - 1);
// Clear sub-nodes.
clear();
m_model->nodesRemoved(this);
}
// This shouldn't be called from clear since clear is called also from the d-tor
// and the function is virtual.
nodeCleared();
// Mark the fact that we've been collapsed
m_populated = false;
}
void DynamicNode::performPopulateNode(bool a_forceRepopulate)
{
if (m_populated) {
if (a_forceRepopulate)
performNodeCleanup();
else
return;
}
populateNode();
// We're populated.
m_populated = true;
// Sort the list.
recursiveSort();
}
bool DynamicNode::hasChildren() const
{
// To get a true status, we'll need to populate the node.
const_cast<DynamicNode*>(this)->performPopulateNode();
return !m_children.empty();
}
|