1
/***************************************************************************
2
* Copyright (C) 2004 by Alexander Dymo *
4
* Portions Copyright (C) 2002-2003 by Roberto Raggi *
5
* roberto@kdevelop.org *
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. *
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. *
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"
28
#include <kiconloader.h>
29
#include <kapplication.h>
30
#include <ktexteditor/viewcursorinterface.h>
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"
41
//using namespace Widgets;
43
//#define BOLDFONTSMALLER
49
QValueList<T> QValueList_reversed ( const QValueList<T> & list )
52
typename QValueList<T>::ConstIterator it = list.begin();
53
while ( it != list.end() )
55
rList.push_front ( *it );
64
NavOp(Navigator *navigator, const QString &fullName)
65
:m_navigator(navigator), m_fullName(fullName) {}
67
bool operator() ( const FunctionDefinitionDom& def ) const
69
return (m_navigator->fullFunctionDefinitionName(def) == m_fullName);
71
bool operator() ( const FunctionDom& def ) const
73
return (m_navigator->fullFunctionDeclarationName(def) == m_fullName);
77
Navigator *m_navigator;
83
class FunctionNavItem: public FancyListViewItem {
86
enum Type { Declaration, Definition };
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) {}
96
FancyListViewItem::setup();
97
setPixmap( 0, UserIcon("CVpublic_meth", KIcon::DefaultState, m_part->instance()) );
99
Type type() { return m_type; }
102
ClassViewPart *m_part;
107
Navigator::Navigator(ClassViewPart *parent, const char *name)
108
: QObject(parent, name), m_part(parent)
110
m_state = GoToDefinitions;
111
m_navNoDefinition = true;
113
m_actionSyncWithEditor = new KAction( i18n("Sync ClassView"), "view_tree", KShortcut(), this,
114
SLOT(slotSyncWithEditor()), m_part->actionCollection(), "classview_sync_with_editor" );
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 );
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 );
124
m_syncTimer = new QTimer(this);
125
connect(m_syncTimer, SIGNAL(timeout()), this, SLOT(syncFunctionNav()));
128
Navigator::~Navigator()
132
void Navigator::selectFunctionNav(QListViewItem *item)
134
FunctionNavItem *nav = dynamic_cast<FunctionNavItem*>(item);
138
FileDom file = m_part->codeModel()->fileByName(m_part->m_activeFileName);
144
case FunctionNavItem::Definition: //jump to definition
146
FileList files = file->wholeGroup();
147
FunctionDefinitionList deflist;
148
CodeModelUtils::findFunctionDefinitions(NavOp(this, nav->text(0)), files, deflist);
149
if (deflist.count() < 1)
152
FunctionDefinitionDom fun = deflist.first();
155
int startLine = 0, startColumn = 0;
156
fun->getStartPosition(&startLine, &startColumn);
157
m_part->partController()->editDocument(KURL(fun->fileName()), startLine);
160
case FunctionNavItem::Declaration: //jump to declaration
162
FileList files = file->wholeGroup();
163
FunctionList declist;
164
CodeModelUtils::findFunctionDeclarations(NavOp(this, nav->text(0)), files, declist);
165
if (declist.count() < 1)
168
FunctionDom fun = declist.first();
171
int startLine = 0, startColumn = 0;
172
fun->getStartPosition(&startLine, &startColumn);
173
m_part->partController()->editDocument(KURL(fun->fileName()), startLine);
179
void Navigator::functionNavUnFocused()
181
/*if (m_navNoDefinition)
182
m_part->m_functionsnav->view()->setCurrentText(NAV_NODEFINITION);
184
if (m_part->m_functionsnav->view()->currentItem())
185
m_part->m_functionsnav->view()->setCurrentText(m_part->m_functionsnav->view()->currentItem()->text(0));
187
m_part->m_functionsnav->view()->setCurrentText(NAV_NODEFINITION);
190
void Navigator::functionNavFocused()
192
m_navNoDefinition = (m_part->m_functionsnav->view()->currentText() == NAV_NODEFINITION);
193
m_part->m_functionsnav->view()->setCurrentText("");
196
void Navigator::slotCursorPositionChanged()
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 );
205
m_syncTimer->changeInterval(500 >= m_delay+100 ? 500 : m_delay+100 );
208
void Navigator::stopTimer( )
213
void Navigator::syncFunctionNavDelayed(int delay)
215
m_syncTimer->changeInterval(delay);
218
void Navigator::syncFunctionNav()
222
if (FunctionDom fun = currentFunction())
224
if ( m_part->widget()->doFollowEditor() )
227
m_part->jumpedToItem( dom );
230
if( !fun->isFunctionDefinition() ) {
231
if (m_functionNavDecls[fullFunctionDeclarationName(fun)])
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);
238
if (m_functionNavDefs[fullFunctionDeclarationName(fun)])
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);
247
m_part->m_functionsnav->view()->setCurrentText(NAV_NODEFINITION);
250
void Navigator::refreshNavBars(const QString &activeFileName, bool clear)
252
kdDebug(9003) << k_funcinfo << endl;
255
m_part->m_functionsnav->view()->clear();
256
m_functionNavDefs.clear();
257
m_functionNavDecls.clear();
260
FileDom file = m_part->codeModel()->fileByName(activeFileName);
266
FunctionList list1 = CodeModelUtils::allFunctionsExhaustive(file);
267
FunctionList::const_iterator flEnd = list1.end();
268
for (FunctionList::const_iterator it = list1.begin(); it != flEnd; ++it)
270
QString fullName = fullFunctionDeclarationName(*it);
272
if (clear || !m_functionNavDecls[fullName] && m_part->m_functionsnav->view()->listView())
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 ) );
280
m_part->m_functionsnav->view()->addItem(item);
284
kdDebug(9003) << k_funcinfo << "leave list: " << toLeave << endl;
286
//remove items not in toLeave list
287
QMap<QString, QListViewItem*>::iterator it = m_functionNavDecls.begin();
288
while ( it != m_functionNavDecls.end() )
290
QMap<QString, QListViewItem*>::iterator it2 = it;
292
if ( !toLeave.contains( it2.key() ) )
296
m_part->m_functionsnav->view()->removeItem(it2.data());
298
m_functionNavDecls.remove(it2);
303
FunctionDefinitionList list = CodeModelUtils::allFunctionDefinitionsExhaustive(file);
304
for (FunctionDefinitionList::const_iterator it = list.begin(); it != list.end(); ++it)
306
QString fullName = fullFunctionDefinitionName(*it);
308
if (clear || !m_functionNavDefs[fullName])
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);
317
//remove unnecessary items with function declarations for which a definition item was created
318
if (m_functionNavDecls[fullName])
320
m_part->m_functionsnav->view()->removeItem(m_functionNavDecls[fullName]);
321
m_functionNavDecls.remove(fullName);
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() )
332
QMap<QString, QListViewItem*>::iterator it2 = itt;
334
if ( !toLeave.contains( it2.key() ) )
338
m_part->m_functionsnav->view()->removeItem(it2.data());
340
m_functionNavDefs.remove(it2);
345
void Navigator::refresh()
347
refreshNavBars(m_part->m_activeFileName, true);
350
void Navigator::addFile(const QString & file)
352
kdDebug(9003) << k_funcinfo << "file: " << file << endl;
353
if (file == m_part->m_activeFileName)
355
kdDebug(9003) << k_funcinfo << "processing active file" << endl;
356
refreshNavBars(m_part->m_activeFileName, false);
363
FunctionDom Navigator::currentFunction()
365
if (!m_part->m_activeViewCursor)
366
return FunctionDom();
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 );
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) {
380
if( !styles.hasStyle( type ) ) {
381
QFont font = styles.getStyle( 0 ).font;
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 );
393
styles.addStyle( type, font );
400
if((cutpos = function.find('(')) != -1) {
401
args = function.right( function.length() - cutpos );
402
function = function.left( cutpos );
404
ret.addItem( function );
407
if((cutpos = function.findRev(':')) != -1 || (cutpos = function.findRev('.')) != -1) {
408
fScope = function.left( cutpos + 1 );
409
function = function.right( function.length() - cutpos - 1);
411
if( !fScope.isEmpty() ) ret.addItem(fScope);
412
ret.addItem( function, type );
413
if( !args.isEmpty() ) ret.addItem(args);
418
template <class DomType>
419
TextPaintItem Navigator::fullFunctionItem(DomType fun)
422
QStringList scope = fun->scope();
423
QString function = scope.join(".");
424
if (!function.isEmpty())
426
function += m_part->languageSupport()->formatModelItem(fun, true);
427
function = m_part->languageSupport()->formatClassName(function);
429
return highlightFunctionName(function, 1, m_styles);
432
QString Navigator::fullFunctionDefinitionName(FunctionDefinitionDom fun)
434
QStringList scope = fun->scope();
435
QString funName = scope.join(".");
436
if (!funName.isEmpty())
438
funName += m_part->languageSupport()->formatModelItem(fun, true);
439
funName = m_part->languageSupport()->formatClassName(funName);
444
QString Navigator::fullFunctionDeclarationName(FunctionDom fun)
446
QStringList scope = fun->scope();
447
QString funName = scope.join(".");
448
if (!funName.isEmpty())
450
funName += m_part->languageSupport()->formatModelItem(fun, true);
451
funName = m_part->languageSupport()->formatClassName(funName);
456
void Navigator::slotSyncWithEditor()
458
kdDebug(9003) << k_funcinfo << endl;
460
if (FunctionDom fun = currentFunction())
462
m_part->mainWindow()->raiseView( m_part->widget() );
465
m_part->jumpedToItem( dom );
469
void Navigator::slotJumpToNextFunction()
471
kdDebug ( 9003 ) << k_funcinfo << endl;
473
if ( !m_part->m_activeViewCursor ) return;
474
unsigned int currentLine, currentCol;
475
m_part->m_activeViewCursor->cursorPositionReal ( ¤tLine, ¤tCol );
477
QValueList<int> lines = functionStartLines();
478
if ( lines.isEmpty() ) return;
480
QValueList<int>::iterator it = lines.begin();
481
while ( it != lines.end() )
483
if ( *it > currentLine )
486
url.setPath ( m_part->m_activeFileName );
487
m_part->partController()->editDocument ( url, *it );
494
void Navigator::slotJumpToPreviousFunction()
496
kdDebug ( 9003 ) << k_funcinfo << endl;
498
if ( !m_part->m_activeViewCursor ) return;
499
unsigned int currentLine, currentCol;
500
m_part->m_activeViewCursor->cursorPositionReal ( ¤tLine, ¤tCol );
502
QValueList<int> lines = QValueList_reversed<int> ( functionStartLines() );
503
if ( lines.isEmpty() ) return;
505
QValueList<int>::iterator it = lines.begin();
506
while ( it != lines.end() )
508
if ( *it < currentLine )
511
url.setPath ( m_part->m_activeFileName );
512
m_part->partController()->editDocument ( url, *it );
519
QValueList<int> Navigator::functionStartLines()
521
FileDom file = m_part->codeModel()->fileByName ( m_part->m_activeFileName );
522
if ( !file ) return QValueList<int>();
524
QValueList<int> lines;
525
FunctionDefinitionList list = CodeModelUtils::allFunctionDefinitionsExhaustive ( file );
526
FunctionDefinitionList::const_iterator it = list.begin();
527
while ( it != list.end() )
530
( *it )->getStartPosition ( &line, &col );
539
#include "navigator.moc"