summaryrefslogtreecommitdiffstats
path: root/parts/classview/navigator.cpp
blob: 67223be895d377812785b87a9e846a1cf0f60d8d (plain)
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
/***************************************************************************
 *   Copyright (C) 2004 by Alexander Dymo                                  *
 *   adymo@mksat.net                                                       *
 *   Portions Copyright (C) 2002-2003 by Roberto Raggi                     *
 *   roberto@tdevelop.org                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU 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 "navigator.h"

#include <tqtimer.h>

#include <tdeconfig.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <tdeapplication.h>
#include <tdetexteditor/viewcursorinterface.h>

#include <kcomboview.h>
#include <tdelistviewaction.h>
#include <kdevpartcontroller.h>
#include <kdevlanguagesupport.h>
#include <kdevmainwindow.h>
#include <codemodel_utils.h>
#include "classviewpart.h"
#include "classviewwidget.h"

//using namespace Widgets;

//#define BOLDFONTSMALLER


namespace
{
    template<class T>
    TQValueList<T> TQValueList_reversed ( const TQValueList<T> & list )
    {
        TQValueList<T> rList;
        typename TQValueList<T>::ConstIterator it = list.begin();
        while ( it != list.end() )
        {
            rList.push_front ( *it );
            ++it;
        }
        return rList;
    }
}

struct NavOp
{
   NavOp(Navigator *navigator, const TQString &fullName)
       :m_navigator(navigator), m_fullName(fullName) {}

   bool operator() ( const FunctionDefinitionDom& def ) const
   {
       return (m_navigator->fullFunctionDefinitionName(def) == m_fullName);
   }
   bool operator() ( const FunctionDom& def ) const
   {
       return (m_navigator->fullFunctionDeclarationName(def) == m_fullName);
   }

private:
    Navigator *m_navigator;
    TQString m_fullName;
};



class FunctionNavItem: public FancyListViewItem {
private:
public:
    enum Type { Declaration, Definition };

    FunctionNavItem(TextPaintStyleStore& styles, ClassViewPart *part, TQListView *parent, TQString name, Type type)
        :FancyListViewItem(styles, parent, name, ""), m_part(part), m_type(type) {}
    FunctionNavItem(TextPaintStyleStore& styles, ClassViewPart *part, TQListViewItem *parent, TQString name, Type type)
        :FancyListViewItem(styles, parent, name, ""), m_part(part), m_type(type) {}
    ~FunctionNavItem() {}

    virtual void setup()
    {
        FancyListViewItem::setup();
        setPixmap( 0, UserIcon("CVpublic_meth", TDEIcon::DefaultState, m_part->instance()) );
    }
    Type type() { return m_type; }

private:
    ClassViewPart *m_part;
    Type m_type;
};


Navigator::Navigator(ClassViewPart *parent, const char *name)
 : TQObject(parent, name), m_part(parent)
{
    m_state = GoToDefinitions;
    m_navNoDefinition = true;

    m_actionSyncWithEditor = new TDEAction( i18n("Sync ClassView"), "view_tree", TDEShortcut(), this,
        TQT_SLOT(slotSyncWithEditor()), m_part->actionCollection(), "classview_sync_with_editor" );

    TDEAction * action = new TDEAction( i18n("Jump to next function"), CTRL+ALT+Key_PageDown, this,
        TQT_SLOT(slotJumpToNextFunction()), m_part->actionCollection(), "navigator_jump_to_next_function" );
    action->plug( &m_dummyActionWidget );

    action = new TDEAction( i18n("Jump to previous function"), CTRL+ALT+Key_PageUp, this,
        TQT_SLOT(slotJumpToPreviousFunction()), m_part->actionCollection(), "navigator_jump_to_previous_function" );
    action->plug( &m_dummyActionWidget );

    m_syncTimer = new TQTimer(this);
    connect(m_syncTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(syncFunctionNav()));
}

Navigator::~Navigator()
{
}

void Navigator::selectFunctionNav(TQListViewItem *item)
{
    FunctionNavItem *nav = dynamic_cast<FunctionNavItem*>(item);
    if (!nav)
        return;

    FileDom file = m_part->codeModel()->fileByName(m_part->m_activeFileName);
    if (!file)
        return;

    switch (nav->type())
    {
        case FunctionNavItem::Definition: //jump to definition
        {
            FileList files = file->wholeGroup();
            FunctionDefinitionList deflist;
            CodeModelUtils::findFunctionDefinitions(NavOp(this, nav->text(0)), files, deflist);
            if (deflist.count() < 1)
                return;

            FunctionDefinitionDom fun = deflist.first();
            if (!fun)
                return;
            int startLine = 0, startColumn = 0;
            fun->getStartPosition(&startLine, &startColumn);
            m_part->partController()->editDocument(KURL(fun->fileName()), startLine);
            break;
        }
        case FunctionNavItem::Declaration: //jump to declaration
        {
            FileList files = file->wholeGroup();
            FunctionList declist;
            CodeModelUtils::findFunctionDeclarations(NavOp(this, nav->text(0)), files, declist);
            if (declist.count() < 1)
                return;

            FunctionDom fun = declist.first();
            if (!fun)
                return;
            int startLine = 0, startColumn = 0;
            fun->getStartPosition(&startLine, &startColumn);
            m_part->partController()->editDocument(KURL(fun->fileName()), startLine);
            break;
        }
    }
}

void Navigator::functionNavUnFocused()
{
    /*if (m_navNoDefinition)
        m_part->m_functionsnav->view()->setCurrentText(NAV_NODEFINITION);
    else*/
    if (m_part->m_functionsnav->view()->currentItem())
        m_part->m_functionsnav->view()->setCurrentText(m_part->m_functionsnav->view()->currentItem()->text(0));
    else
        m_part->m_functionsnav->view()->setCurrentText(NAV_NODEFINITION);
}

void Navigator::functionNavFocused()
{
    m_navNoDefinition = (m_part->m_functionsnav->view()->currentText() == NAV_NODEFINITION);
    m_part->m_functionsnav->view()->setCurrentText("");
}

void Navigator::slotCursorPositionChanged()
{
    //FIXME: we assume that background parser delay is set globally in tdeveloprc file
    //for all available language supports
    //this is reasonable assumption because problem reporter should be the same for all languages
    TDEConfig* config = kapp->config();
    config->setGroup( "General Options" );
    int m_delay = config->readNumEntry( "BgParserDelay", 500 );

    m_syncTimer->changeInterval(500 >= m_delay+100 ? 500 : m_delay+100 );
}

void Navigator::stopTimer( )
{
    m_syncTimer->stop();
}

void Navigator::syncFunctionNavDelayed(int delay)
{
    m_syncTimer->changeInterval(delay);
}

void Navigator::syncFunctionNav()
{
    m_syncTimer->stop();

    if (FunctionDom fun = currentFunction())
    {
        if ( m_part->widget()->doFollowEditor() )
        {
            ItemDom dom(fun);
            m_part->jumpedToItem( dom );
        }

        if( !fun->isFunctionDefinition() ) {
        if (m_functionNavDecls[fullFunctionDeclarationName(fun)])
        {
            m_part->m_functionsnav->view()->blockSignals(true);
            m_part->m_functionsnav->view()->setCurrentActiveItem(m_functionNavDecls[fullFunctionDeclarationName(fun)]);
            m_part->m_functionsnav->view()->blockSignals(false);
        }
        } else {
        if (m_functionNavDefs[fullFunctionDeclarationName(fun)])
        {
            m_part->m_functionsnav->view()->blockSignals(true);
            m_part->m_functionsnav->view()->setCurrentActiveItem(m_functionNavDefs[fullFunctionDeclarationName(fun)]);
            m_part->m_functionsnav->view()->blockSignals(false);
        }
        }
    }
    else
        m_part->m_functionsnav->view()->setCurrentText(NAV_NODEFINITION);
}

void Navigator::refreshNavBars(const TQString &activeFileName, bool clear)
{
    kdDebug(9003) << k_funcinfo << endl;
    if (clear)
    {
        m_part->m_functionsnav->view()->clear();
        m_functionNavDefs.clear();
        m_functionNavDecls.clear();
    }

    FileDom file = m_part->codeModel()->fileByName(activeFileName);
    if (!file)
        return;

    TQStringList toLeave;

    FunctionList list1 = CodeModelUtils::allFunctionsExhaustive(file);
    FunctionList::const_iterator flEnd = list1.end();
    for (FunctionList::const_iterator it = list1.begin(); it != flEnd; ++it)
    {
        TQString fullName = fullFunctionDeclarationName(*it);

        if (clear || !m_functionNavDecls[fullName] && m_part->m_functionsnav->view()->listView())
        {
            FunctionNavItem *item = new FunctionNavItem(m_styles, m_part,
                m_part->m_functionsnav->view()->listView(), fullName,
                FunctionNavItem::Declaration);
            m_functionNavDecls[fullName] = item;
            item->setItem( 0, fullFunctionItem( *it ) );

            m_part->m_functionsnav->view()->addItem(item);
        }
        toLeave << fullName;
    }
    kdDebug(9003) << k_funcinfo << "leave list: " << toLeave << endl;

    //remove items not in toLeave list
    TQMap<TQString, TQListViewItem*>::iterator it = m_functionNavDecls.begin();
    while ( it != m_functionNavDecls.end() )
    {
        TQMap<TQString, TQListViewItem*>::iterator it2 = it;
        ++it;
        if ( !toLeave.contains( it2.key() ) )
        {
            if (it2.data())
            {
                m_part->m_functionsnav->view()->removeItem(it2.data());
            }
            m_functionNavDecls.remove(it2);
        }
    }

    toLeave.clear();
    FunctionDefinitionList list = CodeModelUtils::allFunctionDefinitionsExhaustive(file);
    for (FunctionDefinitionList::const_iterator it = list.begin(); it != list.end(); ++it)
    {
        TQString fullName = fullFunctionDefinitionName(*it);

        if (clear || !m_functionNavDefs[fullName])
        {
            FunctionNavItem *item = new FunctionNavItem(m_styles, m_part,
                m_part->m_functionsnav->view()->listView(), fullName, FunctionNavItem::Definition);
            m_functionNavDefs[fullName] = item;
            item->setItem( 0, fullFunctionItem( *it ) );
            m_part->m_functionsnav->view()->addItem(item);
        }

        //remove unnecessary items with function declarations for which a definition item was created
        if (m_functionNavDecls[fullName])
        {
            m_part->m_functionsnav->view()->removeItem(m_functionNavDecls[fullName]);
            m_functionNavDecls.remove(fullName);
        }

        toLeave << fullName;
    }

    kdDebug(9003) << k_funcinfo << "leave list: " << toLeave << endl;
    //remove items not in toLeave list
    TQMap<TQString, TQListViewItem*>::iterator itt = m_functionNavDefs.begin();
    while ( itt != m_functionNavDefs.end() )
    {
        TQMap<TQString, TQListViewItem*>::iterator it2 = itt;
        ++itt;
        if ( !toLeave.contains( it2.key() ) )
        {
            if (it2.data())
            {
                m_part->m_functionsnav->view()->removeItem(it2.data());
            }
            m_functionNavDefs.remove(it2);
        }
    }
}

void Navigator::refresh()
{
    refreshNavBars(m_part->m_activeFileName, true);
}

void Navigator::addFile(const TQString & file)
{
    kdDebug(9003) << k_funcinfo << "file: " << file << endl;
    if (file == m_part->m_activeFileName)
    {
        kdDebug(9003) << k_funcinfo << "processing active file" << endl;
        refreshNavBars(m_part->m_activeFileName, false);
    }
}




FunctionDom Navigator::currentFunction()
{
    if (!m_part->m_activeViewCursor)
        return FunctionDom();

    unsigned int line, column;
    m_part->m_activeViewCursor->cursorPositionReal(&line, &column);
    CodeModelUtils::CodeModelHelper hlp( m_part->codeModel(), m_part->codeModel()->fileByName( m_part->m_activeFileName) );
    return hlp.functionAt( line, column );
}


///Some time this might be moved into the language-support-part, so each language
///can highlight as it likes
TextPaintItem highlightFunctionName(TQString function, int type, TextPaintStyleStore& styles) {
    TextPaintItem ret;

    if( !styles.hasStyle( type ) ) {
        TQFont font = styles.getStyle( 0 ).font;

        switch(type) {
            case 1:
            default:
                font.setWeight( TQFont::DemiBold );
                ///since bold makes the font a little bigger, make it smaller again
#ifdef BOLDFONTSMALLER
                font.setPointSize( (font.pointSize() * 9) / 10 );
#endif
        }

        styles.addStyle( type, font );
    }

    TQString args;
    TQString fScope;
    int cutpos;

    if((cutpos = function.find('(')) != -1) {
        args = function.right( function.length() - cutpos );
        function = function.left( cutpos );
    } else {
        ret.addItem( function );
        return ret;
    }
    if((cutpos = function.findRev(':')) != -1 || (cutpos = function.findRev('.')) != -1) {
        fScope = function.left( cutpos + 1 );
        function = function.right( function.length() - cutpos - 1);
    }
    if( !fScope.isEmpty() ) ret.addItem(fScope);
    ret.addItem( function, type );
    if( !args.isEmpty() ) ret.addItem(args);
    return ret;
}


template <class DomType>
TextPaintItem Navigator::fullFunctionItem(DomType fun)
{

    TQStringList scope = fun->scope();
    TQString function = scope.join(".");
    if (!function.isEmpty())
        function += ".";
    function += m_part->languageSupport()->formatModelItem(fun, true);
    function = m_part->languageSupport()->formatClassName(function);

    return highlightFunctionName(function, 1, m_styles);
}

TQString Navigator::fullFunctionDefinitionName(FunctionDefinitionDom fun)
{
    TQStringList scope = fun->scope();
    TQString funName = scope.join(".");
    if (!funName.isEmpty())
        funName += ".";
    funName += m_part->languageSupport()->formatModelItem(fun, true);
    funName = m_part->languageSupport()->formatClassName(funName);

    return funName;
}

TQString Navigator::fullFunctionDeclarationName(FunctionDom fun)
{
    TQStringList scope = fun->scope();
    TQString funName = scope.join(".");
    if (!funName.isEmpty())
        funName += ".";
    funName += m_part->languageSupport()->formatModelItem(fun, true);
    funName = m_part->languageSupport()->formatClassName(funName);

    return funName;
}

void Navigator::slotSyncWithEditor()
{
    kdDebug(9003) << k_funcinfo << endl;

    if (FunctionDom fun = currentFunction())
    {
        m_part->mainWindow()->raiseView( m_part->widget() );

        ItemDom dom(fun);
        m_part->jumpedToItem( dom );
    }
}

void Navigator::slotJumpToNextFunction()
{
    kdDebug ( 9003 ) << k_funcinfo << endl;

    if ( !m_part->m_activeViewCursor ) return;
    unsigned int currentLine, currentCol;
    m_part->m_activeViewCursor->cursorPositionReal ( &currentLine, &currentCol );

    TQValueList<int> lines = functionStartLines();
    if ( lines.isEmpty() ) return;

    TQValueList<int>::iterator it = lines.begin();
    while ( it != lines.end() )
    {
        if ( *it > currentLine )
        {
            KURL url;
            url.setPath ( m_part->m_activeFileName );
            m_part->partController()->editDocument ( url, *it );
            break;
        }
        ++it;
    }
}

void Navigator::slotJumpToPreviousFunction()
{
    kdDebug ( 9003 ) << k_funcinfo << endl;

    if ( !m_part->m_activeViewCursor ) return;
    unsigned int currentLine, currentCol;
    m_part->m_activeViewCursor->cursorPositionReal ( &currentLine, &currentCol );

    TQValueList<int> lines = TQValueList_reversed<int> ( functionStartLines() );
    if ( lines.isEmpty() ) return;

    TQValueList<int>::iterator it = lines.begin();
    while ( it != lines.end() )
    {
        if ( *it < currentLine )
        {
            KURL url;
            url.setPath ( m_part->m_activeFileName );
            m_part->partController()->editDocument ( url, *it );
            break;
        }
        ++it;
    }
}

TQValueList<int> Navigator::functionStartLines()
{
    FileDom file = m_part->codeModel()->fileByName ( m_part->m_activeFileName );
    if ( !file ) return TQValueList<int>();

    TQValueList<int> lines;
    FunctionDefinitionList list = CodeModelUtils::allFunctionDefinitionsExhaustive ( file );
    FunctionDefinitionList::const_iterator it = list.begin();
    while ( it != list.end() )
    {
        int line, col;
        ( *it )->getStartPosition ( &line, &col );
        lines << line;
        ++it;
    }
    qHeapSort ( lines );

    return lines;
}

#include "navigator.moc"