ctags2_part.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <qwhatsthis.h>
00013 #include <qpopupmenu.h>
00014 #include <qtextstream.h>
00015 #include <qfile.h>
00016 #include <qregexp.h>
00017
00018 #include <klistview.h>
00019 #include <kiconloader.h>
00020 #include <klocale.h>
00021 #include <kparts/part.h>
00022 #include <ktexteditor/editinterface.h>
00023 #include <kprocess.h>
00024 #include <kdebug.h>
00025 #include <kstringhandler.h>
00026
00027 #include <kdevgenericfactory.h>
00028 #include <kdevcore.h>
00029 #include <kdevmainwindow.h>
00030 #include <kdevproject.h>
00031 #include <kdevpartcontroller.h>
00032
00033 #include "ctags2_widget.h"
00034 #include "ctags2_part.h"
00035 #include "tags.h"
00036
00037 namespace ctags
00038 {
00039 #include "readtags.h"
00040 }
00041
00042 typedef KDevGenericFactory<CTags2Part> CTags2Factory;
00043 static const KAboutData data("kdevctags2", I18N_NOOP("CTags2..."), "1.0");
00044 K_EXPORT_COMPONENT_FACTORY( libkdevctags2, CTags2Factory( &data ) )
00045
00046 CTags2Part::CTags2Part(QObject *parent, const char *name, const QStringList& )
00047 : KDevPlugin("ctags2", "ctags2", parent, name ? name : "ctags2Part" )
00048 {
00049 setInstance(CTags2Factory::instance());
00050
00051
00052 Tags::setTagsFile( project()->projectDirectory() + "/tags" );
00053
00054 m_widget = new CTags2Widget(this);
00055
00056 QWhatsThis::add(m_widget, i18n("<b>CTAGS</b><p>Result view for a tag lookup. Click a line to go to the corresponding place in the code."));
00057 m_widget->setCaption(i18n("CTAGS Lookup"));
00058 mainWindow()->embedOutputView( m_widget, "CTAGS", "CTAGS lookup results" );
00059
00060 connect( core(), SIGNAL(contextMenu(QPopupMenu *, const Context *)),
00061 this, SLOT(contextMenu(QPopupMenu *, const Context *)) );
00062 }
00063
00064
00065 CTags2Part::~CTags2Part()
00066 {
00067 if ( m_widget )
00068 {
00069 mainWindow()->removeView( m_widget );
00070 }
00071 delete m_widget;
00072 }
00073
00074 bool CTags2Part::createTagsFile()
00075 {
00076 KProcess proc;
00077 proc.setWorkingDirectory( project()->projectDirectory() );
00078
00079 proc << "ctags";
00080 proc << "-R" << "--c++-types=+px" << "--excmd=pattern" << "--exclude=Makefile";
00081
00082 bool success = proc.start(KProcess::Block);
00083
00084 return success;
00085 }
00086
00087 void CTags2Part::contextMenu(QPopupMenu *popup, const Context *context)
00088 {
00089 if (!context->hasType( Context::EditorContext ))
00090 return;
00091
00092 const EditorContext *econtext = static_cast<const EditorContext*>(context);
00093 QString ident = econtext->currentWord();
00094 if (ident.isEmpty())
00095 return;
00096
00097 if ( Tags::hasTag( ident ) )
00098 {
00099 m_contextString = ident;
00100 QString squeezed = KStringHandler::csqueeze(ident, 30);
00101
00102 int id = popup->insertItem( i18n("CTAGS Lookup: %1").arg(squeezed),
00103 this, SLOT(slotGotoTag()) );
00104 popup->setWhatsThis(id, i18n("<b>Go to ctags declaration</b><p>Searches in the tags database for a symbol "
00105 "under the cursor and opens a file that contains the symbol declaration."));
00106 }
00107 }
00108
00109 void CTags2Part::slotGotoTag( )
00110 {
00111 m_widget->displayHitsAndClear( Tags::getExactMatches( m_contextString ) );
00112
00113 mainWindow()->raiseView( m_widget );
00114 m_widget->output_view->setFocus();
00115 }
00116
00117 int CTags2Part::getFileLineFromStream( QTextStream & istream, QString const & pattern )
00118 {
00119
00120 QString reduced = pattern.mid( 2, pattern.length() -4 );
00121 QString escaped = QRegExp::escape( reduced );
00122 QString re_string( "^" + escaped + "$" );
00123
00124
00125
00126
00127
00128 QRegExp re( re_string );
00129
00130 int n = 0;
00131 while ( !istream.atEnd() )
00132 {
00133 if ( re.exactMatch( istream.readLine() ) )
00134 {
00135 return n;
00136 }
00137 n++;
00138 }
00139 return -1;
00140 }
00141
00142 int CTags2Part::getFileLineFromPattern( KURL const & url, QString const & pattern )
00143 {
00144
00145 if ( KTextEditor::EditInterface * ei = dynamic_cast<KTextEditor::EditInterface*>( partController()->partForURL( url ) ) )
00146 {
00147 kdDebug() << "the file is open - get the line from the editor buffer" << endl;
00148
00149 QString ibuffer = ei->text();
00150 QTextStream istream( &ibuffer, IO_ReadOnly );
00151 return getFileLineFromStream( istream, pattern );
00152 }
00153 else
00154 {
00155 kdDebug() << "the file is not open - get the line from the file on disk" << endl;
00156
00157 QFile file( url.path() );
00158 QString buffer;
00159
00160 if ( file.open( IO_ReadOnly ) )
00161 {
00162 QTextStream istream( &file );
00163 return getFileLineFromStream( istream, pattern );
00164 }
00165 }
00166 return -1;
00167 }
00168
00169 #include "ctags2_part.moc"
00170
00171
This file is part of the documentation for KDevelop Version 3.1.2.