~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to parts/classview/navigator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2004 by Alexander Dymo                                  *
3
 
 *   adymo@mksat.net                                                       *
4
 
 *   Portions Copyright (C) 2002-2003 by Roberto Raggi                     *
5
 
 *   roberto@kdevelop.org                                                  *
6
 
 *                                                                         *
7
 
 *   This program is free software; you can redistribute it and/or modify  *
8
 
 *   it under the terms of the GNU General Public License as published by  *
9
 
 *   the Free Software Foundation; either version 2 of the License, or     *
10
 
 *   (at your option) any later version.                                   *
11
 
 *                                                                         *
12
 
 *   This program is distributed in the hope that it will be useful,       *
13
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
 
 *   GNU General Public License for more details.                          *
16
 
 *                                                                         *
17
 
 *   You should have received a copy of the GNU General Public License     *
18
 
 *   along with this program; if not, write to the                         *
19
 
 *   Free Software Foundation, Inc.,                                       *
20
 
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21
 
 ***************************************************************************/
22
 
#include "navigator.h"
23
 
 
24
 
#include <qtimer.h>
25
 
 
26
 
#include <kconfig.h>
27
 
#include <kdebug.h>
28
 
#include <kiconloader.h>
29
 
#include <kapplication.h>
30
 
#include <ktexteditor/viewcursorinterface.h>
31
 
 
32
 
#include <kcomboview.h>
33
 
#include <klistviewaction.h>
34
 
#include <kdevpartcontroller.h>
35
 
#include <kdevlanguagesupport.h>
36
 
#include <kdevmainwindow.h>
37
 
#include <codemodel_utils.h>
38
 
#include "classviewpart.h"
39
 
#include "classviewwidget.h"
40
 
 
41
 
//using namespace Widgets;
42
 
 
43
 
//#define BOLDFONTSMALLER
44
 
 
45
 
 
46
 
namespace
47
 
{
48
 
    template<class T>
49
 
    QValueList<T> QValueList_reversed ( const QValueList<T> & list )
50
 
    {
51
 
        QValueList<T> rList;
52
 
        typename QValueList<T>::ConstIterator it = list.begin();
53
 
        while ( it != list.end() )
54
 
        {
55
 
            rList.push_front ( *it );
56
 
            ++it;
57
 
        }
58
 
        return rList;
59
 
    }
60
 
}
61
 
 
62
 
struct NavOp
63
 
{
64
 
   NavOp(Navigator *navigator, const QString &fullName)
65
 
       :m_navigator(navigator), m_fullName(fullName) {}
66
 
 
67
 
   bool operator() ( const FunctionDefinitionDom& def ) const
68
 
   {
69
 
       return (m_navigator->fullFunctionDefinitionName(def) == m_fullName);
70
 
   }
71
 
   bool operator() ( const FunctionDom& def ) const
72
 
   {
73
 
       return (m_navigator->fullFunctionDeclarationName(def) == m_fullName);
74
 
   }
75
 
 
76
 
private:
77
 
    Navigator *m_navigator;
78
 
    QString m_fullName;
79
 
};
80
 
 
81
 
 
82
 
 
83
 
class FunctionNavItem: public FancyListViewItem {
84
 
private:
85
 
public:
86
 
    enum Type { Declaration, Definition };
87
 
 
88
 
    FunctionNavItem(TextPaintStyleStore& styles, ClassViewPart *part, QListView *parent, QString name, Type type)
89
 
        :FancyListViewItem(styles, parent, name, ""), m_part(part), m_type(type) {}
90
 
    FunctionNavItem(TextPaintStyleStore& styles, ClassViewPart *part, QListViewItem *parent, QString name, Type type)
91
 
        :FancyListViewItem(styles, parent, name, ""), m_part(part), m_type(type) {}
92
 
    ~FunctionNavItem() {}
93
 
 
94
 
    virtual void setup()
95
 
    {
96
 
        FancyListViewItem::setup();
97
 
        setPixmap( 0, UserIcon("CVpublic_meth", KIcon::DefaultState, m_part->instance()) );
98
 
    }
99
 
    Type type() { return m_type; }
100
 
 
101
 
private:
102
 
    ClassViewPart *m_part;
103
 
    Type m_type;
104
 
};
105
 
 
106
 
 
107
 
Navigator::Navigator(ClassViewPart *parent, const char *name)
108
 
 : QObject(parent, name), m_part(parent)
109
 
{
110
 
    m_state = GoToDefinitions;
111
 
    m_navNoDefinition = true;
112
 
 
113
 
    m_actionSyncWithEditor = new KAction( i18n("Sync ClassView"), "view_tree", KShortcut(), this,
114
 
        SLOT(slotSyncWithEditor()), m_part->actionCollection(), "classview_sync_with_editor" );
115
 
 
116
 
    KAction * action = new KAction( i18n("Jump to next function"), CTRL+ALT+Key_PageDown, this,
117
 
        SLOT(slotJumpToNextFunction()), m_part->actionCollection(), "navigator_jump_to_next_function" );
118
 
    action->plug( &m_dummyActionWidget );
119
 
 
120
 
    action = new KAction( i18n("Jump to previous function"), CTRL+ALT+Key_PageUp, this,
121
 
        SLOT(slotJumpToPreviousFunction()), m_part->actionCollection(), "navigator_jump_to_previous_function" );
122
 
    action->plug( &m_dummyActionWidget );
123
 
 
124
 
    m_syncTimer = new QTimer(this);
125
 
    connect(m_syncTimer, SIGNAL(timeout()), this, SLOT(syncFunctionNav()));
126
 
}
127
 
 
128
 
Navigator::~Navigator()
129
 
{
130
 
}
131
 
 
132
 
void Navigator::selectFunctionNav(QListViewItem *item)
133
 
{
134
 
    FunctionNavItem *nav = dynamic_cast<FunctionNavItem*>(item);
135
 
    if (!nav)
136
 
        return;
137
 
 
138
 
    FileDom file = m_part->codeModel()->fileByName(m_part->m_activeFileName);
139
 
    if (!file)
140
 
        return;
141
 
 
142
 
    switch (nav->type())
143
 
    {
144
 
        case FunctionNavItem::Definition: //jump to definition
145
 
        {
146
 
            FileList files = file->wholeGroup();
147
 
            FunctionDefinitionList deflist;
148
 
            CodeModelUtils::findFunctionDefinitions(NavOp(this, nav->text(0)), files, deflist);
149
 
            if (deflist.count() < 1)
150
 
                return;
151
 
 
152
 
            FunctionDefinitionDom fun = deflist.first();
153
 
            if (!fun)
154
 
                return;
155
 
            int startLine = 0, startColumn = 0;
156
 
            fun->getStartPosition(&startLine, &startColumn);
157
 
            m_part->partController()->editDocument(KURL(fun->fileName()), startLine);
158
 
            break;
159
 
        }
160
 
        case FunctionNavItem::Declaration: //jump to declaration
161
 
        {
162
 
            FileList files = file->wholeGroup();
163
 
            FunctionList declist;
164
 
            CodeModelUtils::findFunctionDeclarations(NavOp(this, nav->text(0)), files, declist);
165
 
            if (declist.count() < 1)
166
 
                return;
167
 
 
168
 
            FunctionDom fun = declist.first();
169
 
            if (!fun)
170
 
                return;
171
 
            int startLine = 0, startColumn = 0;
172
 
            fun->getStartPosition(&startLine, &startColumn);
173
 
            m_part->partController()->editDocument(KURL(fun->fileName()), startLine);
174
 
            break;
175
 
        }
176
 
    }
177
 
}
178
 
 
179
 
void Navigator::functionNavUnFocused()
180
 
{
181
 
    /*if (m_navNoDefinition)
182
 
        m_part->m_functionsnav->view()->setCurrentText(NAV_NODEFINITION);
183
 
    else*/
184
 
    if (m_part->m_functionsnav->view()->currentItem())
185
 
        m_part->m_functionsnav->view()->setCurrentText(m_part->m_functionsnav->view()->currentItem()->text(0));
186
 
    else
187
 
        m_part->m_functionsnav->view()->setCurrentText(NAV_NODEFINITION);
188
 
}
189
 
 
190
 
void Navigator::functionNavFocused()
191
 
{
192
 
    m_navNoDefinition = (m_part->m_functionsnav->view()->currentText() == NAV_NODEFINITION);
193
 
    m_part->m_functionsnav->view()->setCurrentText("");
194
 
}
195
 
 
196
 
void Navigator::slotCursorPositionChanged()
197
 
{
198
 
    //FIXME: we assume that background parser delay is set globally in kdeveloprc file
199
 
    //for all available language supports
200
 
    //this is reasonable assumption because problem reporter should be the same for all languages
201
 
    KConfig* config = kapp->config();
202
 
    config->setGroup( "General Options" );
203
 
    int m_delay = config->readNumEntry( "BgParserDelay", 500 );
204
 
 
205
 
    m_syncTimer->changeInterval(500 >= m_delay+100 ? 500 : m_delay+100 );
206
 
}
207
 
 
208
 
void Navigator::stopTimer( )
209
 
{
210
 
    m_syncTimer->stop();
211
 
}
212
 
 
213
 
void Navigator::syncFunctionNavDelayed(int delay)
214
 
{
215
 
    m_syncTimer->changeInterval(delay);
216
 
}
217
 
 
218
 
void Navigator::syncFunctionNav()
219
 
{
220
 
    m_syncTimer->stop();
221
 
 
222
 
    if (FunctionDom fun = currentFunction())
223
 
    {
224
 
        if ( m_part->widget()->doFollowEditor() )
225
 
        {
226
 
            ItemDom dom(fun);
227
 
            m_part->jumpedToItem( dom );
228
 
        }
229
 
 
230
 
        if( !fun->isFunctionDefinition() ) {
231
 
        if (m_functionNavDecls[fullFunctionDeclarationName(fun)])
232
 
        {
233
 
            m_part->m_functionsnav->view()->blockSignals(true);
234
 
            m_part->m_functionsnav->view()->setCurrentActiveItem(m_functionNavDecls[fullFunctionDeclarationName(fun)]);
235
 
            m_part->m_functionsnav->view()->blockSignals(false);
236
 
        }
237
 
        } else {
238
 
        if (m_functionNavDefs[fullFunctionDeclarationName(fun)])
239
 
        {
240
 
            m_part->m_functionsnav->view()->blockSignals(true);
241
 
            m_part->m_functionsnav->view()->setCurrentActiveItem(m_functionNavDefs[fullFunctionDeclarationName(fun)]);
242
 
            m_part->m_functionsnav->view()->blockSignals(false);
243
 
        }
244
 
        }
245
 
    }
246
 
    else
247
 
        m_part->m_functionsnav->view()->setCurrentText(NAV_NODEFINITION);
248
 
}
249
 
 
250
 
void Navigator::refreshNavBars(const QString &activeFileName, bool clear)
251
 
{
252
 
    kdDebug(9003) << k_funcinfo << endl;
253
 
    if (clear)
254
 
    {
255
 
        m_part->m_functionsnav->view()->clear();
256
 
        m_functionNavDefs.clear();
257
 
        m_functionNavDecls.clear();
258
 
    }
259
 
 
260
 
    FileDom file = m_part->codeModel()->fileByName(activeFileName);
261
 
    if (!file)
262
 
        return;
263
 
 
264
 
    QStringList toLeave;
265
 
 
266
 
    FunctionList list1 = CodeModelUtils::allFunctionsExhaustive(file);
267
 
    FunctionList::const_iterator flEnd = list1.end();
268
 
    for (FunctionList::const_iterator it = list1.begin(); it != flEnd; ++it)
269
 
    {
270
 
        QString fullName = fullFunctionDeclarationName(*it);
271
 
 
272
 
        if (clear || !m_functionNavDecls[fullName] && m_part->m_functionsnav->view()->listView())
273
 
        {
274
 
            FunctionNavItem *item = new FunctionNavItem(m_styles, m_part,
275
 
                m_part->m_functionsnav->view()->listView(), fullName,
276
 
                FunctionNavItem::Declaration);
277
 
            m_functionNavDecls[fullName] = item;
278
 
            item->setItem( 0, fullFunctionItem( *it ) );
279
 
 
280
 
            m_part->m_functionsnav->view()->addItem(item);
281
 
        }
282
 
        toLeave << fullName;
283
 
    }
284
 
    kdDebug(9003) << k_funcinfo << "leave list: " << toLeave << endl;
285
 
 
286
 
    //remove items not in toLeave list
287
 
    QMap<QString, QListViewItem*>::iterator it = m_functionNavDecls.begin();
288
 
    while ( it != m_functionNavDecls.end() )
289
 
    {
290
 
        QMap<QString, QListViewItem*>::iterator it2 = it;
291
 
        ++it;
292
 
        if ( !toLeave.contains( it2.key() ) )
293
 
        {
294
 
            if (it2.data())
295
 
            {
296
 
                m_part->m_functionsnav->view()->removeItem(it2.data());
297
 
            }
298
 
            m_functionNavDecls.remove(it2);
299
 
        }
300
 
    }
301
 
 
302
 
    toLeave.clear();
303
 
    FunctionDefinitionList list = CodeModelUtils::allFunctionDefinitionsExhaustive(file);
304
 
    for (FunctionDefinitionList::const_iterator it = list.begin(); it != list.end(); ++it)
305
 
    {
306
 
        QString fullName = fullFunctionDefinitionName(*it);
307
 
 
308
 
        if (clear || !m_functionNavDefs[fullName])
309
 
        {
310
 
            FunctionNavItem *item = new FunctionNavItem(m_styles, m_part,
311
 
                m_part->m_functionsnav->view()->listView(), fullName, FunctionNavItem::Definition);
312
 
            m_functionNavDefs[fullName] = item;
313
 
            item->setItem( 0, fullFunctionItem( *it ) );
314
 
            m_part->m_functionsnav->view()->addItem(item);
315
 
        }
316
 
 
317
 
        //remove unnecessary items with function declarations for which a definition item was created
318
 
        if (m_functionNavDecls[fullName])
319
 
        {
320
 
            m_part->m_functionsnav->view()->removeItem(m_functionNavDecls[fullName]);
321
 
            m_functionNavDecls.remove(fullName);
322
 
        }
323
 
 
324
 
        toLeave << fullName;
325
 
    }
326
 
 
327
 
    kdDebug(9003) << k_funcinfo << "leave list: " << toLeave << endl;
328
 
    //remove items not in toLeave list
329
 
    QMap<QString, QListViewItem*>::iterator itt = m_functionNavDefs.begin();
330
 
    while ( itt != m_functionNavDefs.end() )
331
 
    {
332
 
        QMap<QString, QListViewItem*>::iterator it2 = itt;
333
 
        ++itt;
334
 
        if ( !toLeave.contains( it2.key() ) )
335
 
        {
336
 
            if (it2.data())
337
 
            {
338
 
                m_part->m_functionsnav->view()->removeItem(it2.data());
339
 
            }
340
 
            m_functionNavDefs.remove(it2);
341
 
        }
342
 
    }
343
 
}
344
 
 
345
 
void Navigator::refresh()
346
 
{
347
 
    refreshNavBars(m_part->m_activeFileName, true);
348
 
}
349
 
 
350
 
void Navigator::addFile(const QString & file)
351
 
{
352
 
    kdDebug(9003) << k_funcinfo << "file: " << file << endl;
353
 
    if (file == m_part->m_activeFileName)
354
 
    {
355
 
        kdDebug(9003) << k_funcinfo << "processing active file" << endl;
356
 
        refreshNavBars(m_part->m_activeFileName, false);
357
 
    }
358
 
}
359
 
 
360
 
 
361
 
 
362
 
 
363
 
FunctionDom Navigator::currentFunction()
364
 
{
365
 
    if (!m_part->m_activeViewCursor)
366
 
        return FunctionDom();
367
 
 
368
 
    unsigned int line, column;
369
 
    m_part->m_activeViewCursor->cursorPositionReal(&line, &column);
370
 
    CodeModelUtils::CodeModelHelper hlp( m_part->codeModel(), m_part->codeModel()->fileByName( m_part->m_activeFileName) );
371
 
    return hlp.functionAt( line, column );
372
 
}
373
 
 
374
 
 
375
 
///Some time this might be moved into the language-support-part, so each language
376
 
///can highlight as it likes
377
 
TextPaintItem highlightFunctionName(QString function, int type, TextPaintStyleStore& styles) {
378
 
    TextPaintItem ret;
379
 
 
380
 
    if( !styles.hasStyle( type ) ) {
381
 
        QFont font = styles.getStyle( 0 ).font;
382
 
 
383
 
        switch(type) {
384
 
            case 1:
385
 
            default:
386
 
                font.setWeight( QFont::DemiBold );
387
 
                ///since bold makes the font a little bigger, make it smaller again
388
 
#ifdef BOLDFONTSMALLER
389
 
                font.setPointSize( (font.pointSize() * 9) / 10 );
390
 
#endif
391
 
        }
392
 
 
393
 
        styles.addStyle( type, font );
394
 
    }
395
 
 
396
 
    QString args;
397
 
    QString fScope;
398
 
    int cutpos;
399
 
 
400
 
    if((cutpos = function.find('(')) != -1) {
401
 
        args = function.right( function.length() - cutpos );
402
 
        function = function.left( cutpos );
403
 
    } else {
404
 
        ret.addItem( function );
405
 
        return ret;
406
 
    }
407
 
    if((cutpos = function.findRev(':')) != -1 || (cutpos = function.findRev('.')) != -1) {
408
 
        fScope = function.left( cutpos + 1 );
409
 
        function = function.right( function.length() - cutpos - 1);
410
 
    }
411
 
    if( !fScope.isEmpty() ) ret.addItem(fScope);
412
 
    ret.addItem( function, type );
413
 
    if( !args.isEmpty() ) ret.addItem(args);
414
 
    return ret;
415
 
}
416
 
 
417
 
 
418
 
template <class DomType>
419
 
TextPaintItem Navigator::fullFunctionItem(DomType fun)
420
 
{
421
 
 
422
 
    QStringList scope = fun->scope();
423
 
    QString function = scope.join(".");
424
 
    if (!function.isEmpty())
425
 
        function += ".";
426
 
    function += m_part->languageSupport()->formatModelItem(fun, true);
427
 
    function = m_part->languageSupport()->formatClassName(function);
428
 
 
429
 
    return highlightFunctionName(function, 1, m_styles);
430
 
}
431
 
 
432
 
QString Navigator::fullFunctionDefinitionName(FunctionDefinitionDom fun)
433
 
{
434
 
    QStringList scope = fun->scope();
435
 
    QString funName = scope.join(".");
436
 
    if (!funName.isEmpty())
437
 
        funName += ".";
438
 
    funName += m_part->languageSupport()->formatModelItem(fun, true);
439
 
    funName = m_part->languageSupport()->formatClassName(funName);
440
 
 
441
 
    return funName;
442
 
}
443
 
 
444
 
QString Navigator::fullFunctionDeclarationName(FunctionDom fun)
445
 
{
446
 
    QStringList scope = fun->scope();
447
 
    QString funName = scope.join(".");
448
 
    if (!funName.isEmpty())
449
 
        funName += ".";
450
 
    funName += m_part->languageSupport()->formatModelItem(fun, true);
451
 
    funName = m_part->languageSupport()->formatClassName(funName);
452
 
 
453
 
    return funName;
454
 
}
455
 
 
456
 
void Navigator::slotSyncWithEditor()
457
 
{
458
 
    kdDebug(9003) << k_funcinfo << endl;
459
 
 
460
 
    if (FunctionDom fun = currentFunction())
461
 
    {
462
 
        m_part->mainWindow()->raiseView( m_part->widget() );
463
 
 
464
 
        ItemDom dom(fun);
465
 
        m_part->jumpedToItem( dom );
466
 
    }
467
 
}
468
 
 
469
 
void Navigator::slotJumpToNextFunction()
470
 
{
471
 
    kdDebug ( 9003 ) << k_funcinfo << endl;
472
 
 
473
 
    if ( !m_part->m_activeViewCursor ) return;
474
 
    unsigned int currentLine, currentCol;
475
 
    m_part->m_activeViewCursor->cursorPositionReal ( &currentLine, &currentCol );
476
 
 
477
 
    QValueList<int> lines = functionStartLines();
478
 
    if ( lines.isEmpty() ) return;
479
 
 
480
 
    QValueList<int>::iterator it = lines.begin();
481
 
    while ( it != lines.end() )
482
 
    {
483
 
        if ( *it > currentLine )
484
 
        {
485
 
            KURL url;
486
 
            url.setPath ( m_part->m_activeFileName );
487
 
            m_part->partController()->editDocument ( url, *it );
488
 
            break;
489
 
        }
490
 
        ++it;
491
 
    }
492
 
}
493
 
 
494
 
void Navigator::slotJumpToPreviousFunction()
495
 
{
496
 
    kdDebug ( 9003 ) << k_funcinfo << endl;
497
 
 
498
 
    if ( !m_part->m_activeViewCursor ) return;
499
 
    unsigned int currentLine, currentCol;
500
 
    m_part->m_activeViewCursor->cursorPositionReal ( &currentLine, &currentCol );
501
 
 
502
 
    QValueList<int> lines = QValueList_reversed<int> ( functionStartLines() );
503
 
    if ( lines.isEmpty() ) return;
504
 
 
505
 
    QValueList<int>::iterator it = lines.begin();
506
 
    while ( it != lines.end() )
507
 
    {
508
 
        if ( *it < currentLine )
509
 
        {
510
 
            KURL url;
511
 
            url.setPath ( m_part->m_activeFileName );
512
 
            m_part->partController()->editDocument ( url, *it );
513
 
            break;
514
 
        }
515
 
        ++it;
516
 
    }
517
 
}
518
 
 
519
 
QValueList<int> Navigator::functionStartLines()
520
 
{
521
 
    FileDom file = m_part->codeModel()->fileByName ( m_part->m_activeFileName );
522
 
    if ( !file ) return QValueList<int>();
523
 
 
524
 
    QValueList<int> lines;
525
 
    FunctionDefinitionList list = CodeModelUtils::allFunctionDefinitionsExhaustive ( file );
526
 
    FunctionDefinitionList::const_iterator it = list.begin();
527
 
    while ( it != list.end() )
528
 
    {
529
 
        int line, col;
530
 
        ( *it )->getStartPosition ( &line, &col );
531
 
        lines << line;
532
 
        ++it;
533
 
    }
534
 
    qHeapSort ( lines );
535
 
 
536
 
    return lines;
537
 
}
538
 
 
539
 
#include "navigator.moc"