File: documentclassesfolder.cpp

package info (click to toggle)
kdevplatform 1.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 19,868 kB
  • sloc: cpp: 128,247; sh: 697; python: 365; php: 83; makefile: 4
file content (483 lines) | stat: -rw-r--r-- 15,506 bytes parent folder | download
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
/*
 * KDevelop Class Browser
 *
 * Copyright 2009 Lior Mualem <lior.m.kde@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "documentclassesfolder.h"

#include <typeinfo>
#include <language/duchain/duchainlock.h>
#include <language/duchain/duchain.h>
#include <language/duchain/persistentsymboltable.h>
#include <language/duchain/codemodel.h>
#include <QTimer>
#include <KIcon>

#include <boost/foreach.hpp>

using namespace KDevelop;
using namespace ClassModelNodes;

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

/// Contains a static list of classes within the namespace.
class ClassModelNodes::StaticNamespaceFolderNode : public Node
{
public:
  StaticNamespaceFolderNode(const KDevelop::QualifiedIdentifier& a_identifier, NodesModelInterface* a_model);

  /// Returns the qualified identifier for this node
  const KDevelop::QualifiedIdentifier& qualifiedIdentifier() const { return m_identifier; }

public: // Node overrides
  virtual bool getIcon(QIcon& a_resultIcon);
  virtual int getScore() const { return 101; }

private:
  /// The namespace identifier.
  KDevelop::QualifiedIdentifier m_identifier;
};

StaticNamespaceFolderNode::StaticNamespaceFolderNode(const KDevelop::QualifiedIdentifier& a_identifier, NodesModelInterface* a_model)
  : Node(a_identifier.last().toString(), a_model)
  , m_identifier(a_identifier)
{

}

bool StaticNamespaceFolderNode::getIcon(QIcon& a_resultIcon)
{
  static KIcon folderIcon("namespace");
  a_resultIcon = folderIcon;
  return true;
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

DocumentClassesFolder::OpenedFileClassItem::OpenedFileClassItem(const KDevelop::IndexedString& a_file, const KDevelop::IndexedQualifiedIdentifier& a_classIdentifier, ClassModelNodes::ClassNode* a_nodeItem)
  : file(a_file)
  , classIdentifier(a_classIdentifier)
  , nodeItem(a_nodeItem)
{
}

DocumentClassesFolder::DocumentClassesFolder(const QString& a_displayName, NodesModelInterface* a_model)
  : DynamicFolderNode(a_displayName, a_model)
  , m_updateTimer( new QTimer(this) )
{
  connect( m_updateTimer, SIGNAL(timeout()), this, SLOT(updateChangedFiles()));
}

void DocumentClassesFolder::branchModified(KDevelop::DUContextPointer context)
{
  DUChainReadLocker readLock(DUChain::lock());

  if ( !context )
    return;

  // Queue the changed file.
  m_updatedFiles.insert(context->url());
}

void DocumentClassesFolder::branchRemoved(KDevelop::DUContextPointer context)
{
  Q_UNUSED(context);
  //Getting a read-lock may lead to UI lockups when the background-parser is writing the duchain
/*  DUChainReadLocker readLock(DUChain::lock());

  if ( !context )
    return;

  /// @todo
  kDebug() << "Del: " << context->url().toUrl() << " " << context->range().start.line << "-" << context->range().end.line;*/
}

void DocumentClassesFolder::updateChangedFiles()
{
  bool hadChanges = false;

  // re-parse changed documents.
  foreach( const IndexedString& file, m_updatedFiles )
  {
    // Make sure it's one of the monitored files.
    if ( m_openFiles.contains(file) )
      hadChanges |= updateDocument(file);
  }

  // Processed all files.
  m_updatedFiles.clear();

  // Sort if had changes.
  if ( hadChanges )
    recursiveSort();
}

void DocumentClassesFolder::nodeCleared()
{
  disconnect(DUChain::self()->notifier(), SIGNAL(branchModified(KDevelop::DUContextPointer)), this, SLOT(branchModified(KDevelop::DUContextPointer)));
//   disconnect(DUChain::self()->notifier(), SIGNAL(branchRemoved(KDevelop::DUContextPointer)), this, SLOT(branchRemoved(KDevelop::DUContextPointer)));

  // Clear cached namespaces list (node was cleared).
  m_namespaces.clear();

  // Clear open files and classes list
  m_openFiles.clear();
  m_openFilesClasses.clear();

  // Stop the update timer.
  m_updateTimer->stop();
}

void DocumentClassesFolder::populateNode()
{
  // Get notification for file changes.
  connect(DUChain::self()->notifier(), SIGNAL(branchModified(KDevelop::DUContextPointer)), this, SLOT(branchModified(KDevelop::DUContextPointer)), Qt::QueuedConnection);
//   connect(DUChain::self()->notifier(), SIGNAL(branchRemoved(KDevelop::DUContextPointer)), this, SLOT(branchRemoved(KDevelop::DUContextPointer)), Qt::QueuedConnection);

  // Start updates timer - this is the required delay.
  m_updateTimer->start(2000);
}

QSet< KDevelop::IndexedString > DocumentClassesFolder::getAllOpenDocuments()
{
  return m_openFiles;
}

ClassNode* DocumentClassesFolder::findClassNode(const IndexedQualifiedIdentifier& a_id)
{
  // Make sure that the classes node is populated, otherwise
  // the lookup will not work.
  performPopulateNode();

  ClassIdentifierIterator iter = m_openFilesClasses.get<ClassIdentifierIndex>().find(a_id);
  if ( iter == m_openFilesClasses.get<ClassIdentifierIndex>().end() )
    return 0;

  // If the node is invisible - make it visible by going over the identifiers list.
  if ( iter->nodeItem == 0 )
  {
    QualifiedIdentifier qualifiedIdentifier = a_id.identifier();

    // Ignore zero length identifiers.
    if ( qualifiedIdentifier.count() == 0 )
      return 0;
    
    ClassNode* closestNode = 0;
    int closestNodeIdLen = qualifiedIdentifier.count();

    // First find the closest visible class node by reverse iteration over the id list.
    while ( (closestNodeIdLen > 0) && (closestNode == 0) )
    {
      // Omit one from the end.
      --closestNodeIdLen;
      
      // Find the closest class.
      closestNode = findClassNode(qualifiedIdentifier.mid(0, closestNodeIdLen));
    }

    if ( closestNode != 0 )
    {
      // Start iterating forward from this node by exposing each class.
      // By the end of this loop, closestNode should hold the actual node.
      while ( closestNode && (closestNodeIdLen < qualifiedIdentifier.count()) )
      {
        // Try the next Id.
        ++closestNodeIdLen;
        closestNode = closestNode->findSubClass(qualifiedIdentifier.mid(0, closestNodeIdLen));
      }
    }
    
    return closestNode;
  }
  
  return iter->nodeItem;
}

void DocumentClassesFolder::closeDocument(const IndexedString& a_file)
{
   // Get list of nodes associated with this file and remove them.
  std::pair< FileIterator, FileIterator > range = m_openFilesClasses.get<FileIndex>().equal_range( a_file );
  if ( range.first != m_openFilesClasses.get<FileIndex>().end() )
  {
    BOOST_FOREACH( const OpenedFileClassItem& item, range )
    {
      if ( item.nodeItem )
        removeClassNode(item.nodeItem);
    }

    // Clear the lists
    m_openFilesClasses.get<FileIndex>().erase(range.first, range.second);
  }

  // Clear the file from the list of monitored documents.
   m_openFiles.remove(a_file);
}

bool DocumentClassesFolder::updateDocument(const KDevelop::IndexedString& a_file)
{
  uint codeModelItemCount = 0;
  const CodeModelItem* codeModelItems;
  CodeModel::self().items(a_file, codeModelItemCount, codeModelItems);

  // List of declared namespaces in this file.
  QSet< QualifiedIdentifier > declaredNamespaces;

  // List of removed classes - it initially contains all the known classes, we'll eliminate them
  // one by one later on when we encounter them in the document.
  QMap< IndexedQualifiedIdentifier, FileIterator > removedClasses;
  {
    std::pair< FileIterator, FileIterator > range = m_openFilesClasses.get<FileIndex>().equal_range( a_file );
    for ( FileIterator iter = range.first;
          iter != range.second;
          ++iter )
    {
      removedClasses.insert(iter->classIdentifier, iter);
    }
  }

  bool documentChanged = false;

  for(uint codeModelItemIndex = 0; codeModelItemIndex < codeModelItemCount; ++codeModelItemIndex)
  {
    const CodeModelItem& item = codeModelItems[codeModelItemIndex];

    // Don't insert unknown or forward declarations into the class browser
    if ( (item.kind & CodeModelItem::Unknown) || (item.kind & CodeModelItem::ForwardDeclaration) )
      continue;

    KDevelop::QualifiedIdentifier id = item.id.identifier();

    // Don't add empty identifiers.
    if ( id.count() == 0 )
      continue;

    // If it's a namespace, create it in the list.
    if ( item.kind & CodeModelItem::Namespace )
    {
      // This should create the namespace folder and add it to the cache.
      getNamespaceFolder(id);

      // Add to the locally created namespaces.
      declaredNamespaces.insert(id);
    }
    else if ( item.kind & CodeModelItem::Class )
    {
      // Ignore empty unnamed classes.
      if ( id.last().toString().isEmpty() )
        continue;
    
      // See if it matches our filter?
      if ( isClassFiltered(id) )
        continue;

      // Is this a new class or an existing class?
      if ( removedClasses.contains(id) )
      {
        // It already exist - remove it from the known classes and continue.
        removedClasses.remove(id);
        continue;
      }

      // Where should we put this class?
      Node* parentNode = 0;

      // Check if it's namespaced and add it to the proper namespace.
      if ( id.count() > 1 )
      {
        QualifiedIdentifier parentIdentifier(id.left(-1));

        // Look up the namespace in the cache.
        // If we fail to find it we assume that the parent context is a class
        // and in that case, when the parent class gets expanded, it will show it.
        NamespacesMap::iterator iter = m_namespaces.find(parentIdentifier);
        if ( iter != m_namespaces.end() )
        {
          // Add to the namespace node.
          parentNode = iter.value();
        }
        else
        {
          // Reaching here means we didn't encounter any namespace declaration in the document
          // But a class might still be declared under a namespace.
          // So we'll perform a more through search to see if it's under a namespace.

          DUChainReadLocker readLock(DUChain::lock());

          uint declsCount = 0;
          const IndexedDeclaration* decls;
          PersistentSymbolTable::self().declarations(parentIdentifier, declsCount, decls);

          for ( uint i = 0; i < declsCount; ++i )
          {
            // Look for the first valid declaration.
            if ( decls->declaration() )
            {
              // See if it should be namespaced.
              if ( decls->declaration()->kind() == Declaration::Namespace )
              {
                // This should create the namespace folder and add it to the cache.
                parentNode = getNamespaceFolder(parentIdentifier);
                
                // Add to the locally created namespaces.
                declaredNamespaces.insert(parentIdentifier);
              }
              
              break;
            }
          }
        }
      }
      else
      {
        // Add to the main root.
        parentNode = this;
      }

      ClassNode* newNode = 0;
      if ( parentNode != 0 )
      {
        // Create the new node and add it.
        IndexedDeclaration decl;
        uint count = 0;
        const IndexedDeclaration* declarations;
        DUChainReadLocker lock;
        PersistentSymbolTable::self().declarations(item.id, count, declarations);
        for ( uint i = 0; i < count; ++i )
        {
          if (declarations[i].indexedTopContext().url() == a_file)
          {
            decl = declarations[i];
            break;
          }
        }
        if (decl.isValid())
        {
          newNode = new ClassNode(decl.declaration(), m_model);
          parentNode->addNode( newNode );
        }
      }

      // Insert it to the map - newNode can be 0 - meaning the class is hidden.
      m_openFilesClasses.insert( OpenedFileClassItem( a_file, id, newNode ) );
      documentChanged = true;
    }
  }

  // Remove empty namespaces from the list.
  // We need this because when a file gets unloaded, we unload the declared classes in it
  // and if a namespace has no class in it, it'll forever exist and no one will remove it
  // from the children list.
  foreach( const QualifiedIdentifier& id, declaredNamespaces )
    removeEmptyNamespace(id);

  // Clear erased classes.
  foreach( const FileIterator& item, removedClasses )
  {
    if ( item->nodeItem )
      removeClassNode(item->nodeItem);
    m_openFilesClasses.get<FileIndex>().erase(item);
    documentChanged = true;
  }

  return documentChanged;
}

void DocumentClassesFolder::parseDocument(const IndexedString& a_file)
{
  // Add the document to the list of open files - this means we monitor it.
  if ( !m_openFiles.contains(a_file) )
    m_openFiles.insert(a_file);

  updateDocument(a_file);
}

void DocumentClassesFolder::removeClassNode(ClassModelNodes::ClassNode* a_node)
{
  // Get the parent namespace identifier.
  QualifiedIdentifier parentNamespaceIdentifier;
  if ( typeid(*a_node->getParent()) == typeid(StaticNamespaceFolderNode) )
  {
    parentNamespaceIdentifier =
      static_cast<StaticNamespaceFolderNode*>(a_node->getParent())->qualifiedIdentifier();
  }

  // Remove the node.
  a_node->removeSelf();

  // Remove empty namespace
  removeEmptyNamespace(parentNamespaceIdentifier);
}

void DocumentClassesFolder::removeEmptyNamespace(const QualifiedIdentifier& a_identifier)
{
  // Stop condition.
  if ( a_identifier.count() == 0 )
    return;

  // Look it up in the cache.
  NamespacesMap::iterator iter = m_namespaces.find(a_identifier);
  if ( iter != m_namespaces.end() )
  {
    if ( !(*iter)->hasChildren() )
    {
      // Remove this node and try to remove the parent node.
      QualifiedIdentifier parentIdentifier = (*iter)->qualifiedIdentifier().left(-1);
      (*iter)->removeSelf();
      m_namespaces.remove(a_identifier);
      removeEmptyNamespace(parentIdentifier);
    }
  }
}

StaticNamespaceFolderNode* DocumentClassesFolder::getNamespaceFolder(const KDevelop::QualifiedIdentifier& a_identifier)
{
  // Stop condition.
  if ( a_identifier.count() == 0 )
    return 0;

  // Look it up in the cache.
  NamespacesMap::iterator iter = m_namespaces.find(a_identifier);
  if ( iter == m_namespaces.end() )
  {
    // It's not in the cache - create folders up to it.
    Node* parentNode = getNamespaceFolder(a_identifier.left(-1));
    if ( parentNode == 0 )
      parentNode = this;

    // Create the new node.
    StaticNamespaceFolderNode* newNode =
      new StaticNamespaceFolderNode(a_identifier, m_model);
    parentNode->addNode( newNode );

    // Add it to the cache.
    m_namespaces.insert( a_identifier, newNode );

    // Return the result.
    return newNode;
  }
  else
    return *iter;
}

#include "documentclassesfolder.moc"


// kate: space-indent on; indent-width 2; tab-width 4; replace-tabs on; auto-insert-doxygen on