KDevelop API Documentation

ctags2_part.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Jens Dagerbo                                    *
00003  *   jens.dagerbo@swipnet.se                                               *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
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     //setXMLFile("kdevpart_ctags2.rc");
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     kdDebug() << "pattern: " << pattern << endl;
00125     kdDebug() << "escaped: " << escaped << endl;
00126     kdDebug() << "re_string: " << re_string << endl;
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     // if the file is open - get the line from the editor buffer
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 // else the file is not open - get the line from the file on disk
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 // kate: space-indent off; indent-width 4; tab-width 4; show-tabs off;
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 00:03:54 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003