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

« back to all changes in this revision

Viewing changes to languages/ada/adasupportpart.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
 
#include <qfileinfo.h>
3
 
#include <qlistview.h>
4
 
#include <qstringlist.h>
5
 
#include <qtimer.h>
6
 
#include <qvbox.h>
7
 
#include <qprogressbar.h>
8
 
#include <qwhatsthis.h>
9
 
 
10
 
#include <kgenericfactory.h>
11
 
#include <kapplication.h>
12
 
#include <kdebug.h>
13
 
#include <klocale.h>
14
 
#include <kstatusbar.h>
15
 
#include <kdialogbase.h>
16
 
#include <kiconloader.h>
17
 
 
18
 
#include <fstream>
19
 
#include <sstream>
20
 
 
21
 
#include "kdevgenericfactory.h"
22
 
#include "kdevcore.h"
23
 
#include "kdevproject.h"
24
 
#include "kdevmainwindow.h"
25
 
#include "kdevpartcontroller.h"
26
 
#include "codemodel.h"
27
 
#include "adasupportpart.h"
28
 
#include "problemreporter.h"
29
 
#include "backgroundparser.h"
30
 
 
31
 
#include "AdaLexer.hpp"
32
 
#include "AdaParser.hpp"
33
 
#include "AdaStoreWalker.hpp"
34
 
#include "AdaAST.hpp"
35
 
 
36
 
#include <kdevplugininfo.h>
37
 
 
38
 
enum { KDEV_DB_VERSION = 6 };
39
 
enum { KDEV_PCS_VERSION = 6 };
40
 
 
41
 
typedef KDevGenericFactory<AdaSupportPart> AdaSupportPartFactory;
42
 
 
43
 
static const KDevPluginInfo data("kdevadasupport");
44
 
K_EXPORT_COMPONENT_FACTORY (libkdevadasupport, AdaSupportPartFactory (data))
45
 
 
46
 
 
47
 
struct AdaSupportPartData {
48
 
    ProblemReporter* problemReporter;
49
 
 
50
 
    AdaSupportPartData () : problemReporter (0) {}
51
 
};
52
 
 
53
 
AdaSupportPart::AdaSupportPart (QObject *parent, const char *name, const QStringList &)
54
 
    : KDevLanguageSupport (&data, parent, name ? name : "AdaSupportPart"), d (new AdaSupportPartData())
55
 
{
56
 
    setInstance (AdaSupportPartFactory::instance ());
57
 
 
58
 
    d->problemReporter = new ProblemReporter (this);
59
 
//    connect (core (), SIGNAL (configWidget (KDialogBase*)),
60
 
//             d->problemReporter, SLOT (configWidget (KDialogBase*)));
61
 
    d->problemReporter->setIcon( SmallIcon("info") );
62
 
    mainWindow( )->embedOutputView( d->problemReporter, i18n("Problems"), i18n("Problem reporter"));
63
 
    QWhatsThis::add(d->problemReporter, i18n("<b>Problem reporter</b><p>This window shows various \"problems\" in your project. "
64
 
        "It displays errors reported by a language parser."));
65
 
 
66
 
    setXMLFile ("adasupportpart.rc");
67
 
 
68
 
    connect (core (), SIGNAL (projectOpened ()), this, SLOT (projectOpened ()));
69
 
    connect (core (), SIGNAL (projectClosed ()), this, SLOT (projectClosed ()));
70
 
 
71
 
    connect (partController (), SIGNAL (savedFile (const KURL&)),
72
 
             this, SLOT (savedFile (const KURL&)));
73
 
 
74
 
//    connect (core (), SIGNAL (configWidget (KDialogBase*)), this, SLOT (configWidget (KDialogBase*)));
75
 
    connect( core(), SIGNAL(configWidget(KDialogBase*)),
76
 
             d->problemReporter, SLOT(configWidget(KDialogBase*)) );
77
 
 
78
 
    // a small hack (robe)
79
 
    //classStore ()->globalScope ()->setName ("(default packages)");
80
 
    //classStore ()->addScope (classStore ()->globalScope ());
81
 
    //classStore ()->globalScope ()->setName (QString::null);
82
 
}
83
 
 
84
 
 
85
 
AdaSupportPart::~AdaSupportPart ()
86
 
{
87
 
    mainWindow ()->removeView (d->problemReporter);
88
 
    delete (d->problemReporter);
89
 
    d->problemReporter = 0;
90
 
 
91
 
    delete (d);
92
 
    d = 0;
93
 
}
94
 
 
95
 
 
96
 
KDevLanguageSupport::Features AdaSupportPart::features ()
97
 
{
98
 
    return KDevLanguageSupport::Features
99
 
        (  // TBD: Classes |
100
 
         Functions | Namespaces);
101
 
}
102
 
 
103
 
void AdaSupportPart::projectOpened ()
104
 
{
105
 
    connect (project (), SIGNAL (addedFilesToProject (const QStringList &)),
106
 
            this, SLOT (addedFilesToProject (const QStringList &)));
107
 
    connect (project (), SIGNAL (removedFilesFromProject (const QStringList &)),
108
 
            this, SLOT (removedFilesFromProject (const QStringList &)));
109
 
    connect( project( ), SIGNAL( changedFilesInProject( const QStringList & ) ),
110
 
             this, SLOT( changedFilesInProject( const QStringList & ) ) );
111
 
 
112
 
    QTimer::singleShot (0, this, SLOT (initialParse ()));
113
 
}
114
 
 
115
 
 
116
 
void AdaSupportPart::projectClosed ()
117
 
{
118
 
    saveProjectSourceInfo();
119
 
}
120
 
 
121
 
 
122
 
void AdaSupportPart::initialParse ()
123
 
{
124
 
    kdDebug () << "------------------------------------------> initialParse ()" << endl;
125
 
 
126
 
    if (project ())
127
 
    {
128
 
        mainWindow()->statusBar()->message( i18n("Updating...") );
129
 
        kapp->processEvents( );
130
 
        kapp->setOverrideCursor (waitCursor);
131
 
 
132
 
        int n = 0;
133
 
        QStringList files = project ()->allFiles ();
134
 
 
135
 
        QProgressBar* bar = new QProgressBar( files.count( ), mainWindow( )->statusBar( ) );
136
 
        bar->setMinimumWidth( 120 );
137
 
        bar->setCenterIndicator( true );
138
 
        mainWindow( )->statusBar( )->addWidget( bar );
139
 
        bar->show( );
140
 
 
141
 
        for (QStringList::Iterator it = files.begin (); it != files.end (); ++it) {
142
 
            bar->setProgress( n++ );
143
 
 
144
 
            QString fn = project ()->projectDirectory () + "/" + *it;
145
 
            maybeParse (fn);
146
 
            kapp->processEvents (500);
147
 
        }
148
 
 
149
 
        emit updatedSourceInfo();
150
 
 
151
 
        mainWindow( )->statusBar( )->removeWidget( bar );
152
 
        delete bar;
153
 
 
154
 
        kapp->restoreOverrideCursor ();
155
 
        mainWindow( )->statusBar( )->message( i18n( "Done" ), 2000 );
156
 
/*        mainWindow ()->statusBar ()->message
157
 
            (i18n ("Found 1 problem", "Found %n problems", d->problemReporter->childCount ()));*/
158
 
    }
159
 
}
160
 
 
161
 
QStringList AdaSupportPart::fileExtensions ()
162
 
{
163
 
    return QStringList () << "ads" << "adb";
164
 
}
165
 
 
166
 
void AdaSupportPart::maybeParse (const QString &fileName)
167
 
{
168
 
    kdDebug () << "AdaSupportPart::maybeParse: " << fileName << endl;
169
 
 
170
 
    if (!fileExtensions ().contains (QFileInfo (fileName).extension ()))
171
 
        return;
172
 
 
173
 
//    mainWindow ()->statusBar ()->message (i18n ("Parsing file: %1").arg (fileName));
174
 
    parse (fileName);
175
 
}
176
 
 
177
 
 
178
 
void AdaSupportPart::addedFilesToProject (const QStringList &fileList)
179
 
{
180
 
        QStringList::ConstIterator it;
181
 
 
182
 
        for (it = fileList.begin (); it != fileList.end (); ++it)
183
 
        {
184
 
            QString path = project ()->projectDirectory () + "/" + (*it);
185
 
            maybeParse (path);
186
 
            emit addedSourceInfo( path );
187
 
        }
188
 
}
189
 
 
190
 
 
191
 
void AdaSupportPart::removedFilesFromProject (const QStringList &fileList)
192
 
{
193
 
        QStringList::ConstIterator it;
194
 
 
195
 
        for (it = fileList.begin (); it != fileList.end (); ++it)
196
 
        {
197
 
            kdDebug () << "AdaSupportPart::removedFileFromProject () -- " << (*it) << endl;
198
 
            QString path = project ()->projectDirectory () + "/" + (*it);
199
 
 
200
 
            if( codeModel()->hasFile(path) )
201
 
            {
202
 
                emit aboutToRemoveSourceInfo( path );
203
 
                codeModel()->removeFile( codeModel()->fileByName(path) );
204
 
            }
205
 
        }
206
 
 
207
 
//        emit updatedSourceInfo();
208
 
}
209
 
 
210
 
 
211
 
void AdaSupportPart::parse (const QString &fileName)
212
 
{
213
 
    kdDebug () << "AdaSupportPart::parse () -- " << fileName << endl;
214
 
 
215
 
    std::ifstream stream (QFile::encodeName( fileName ).data());
216
 
    QCString _fn = fileName.utf8 ();
217
 
    std::string fn (_fn.data ());
218
 
 
219
 
    AdaLexer lexer (stream);
220
 
    lexer.setFilename (fn);
221
 
    lexer.setProblemReporter (d->problemReporter);
222
 
 
223
 
    AdaParser parser (lexer);
224
 
    parser.setFilename (fn);
225
 
    parser.setProblemReporter (d->problemReporter);
226
 
 
227
 
    // make an ast factory
228
 
    antlr::ASTFactory ast_factory;
229
 
    // initialize and put it in the parser...
230
 
    parser.initializeASTFactory (ast_factory);
231
 
    parser.setASTFactory (&ast_factory);
232
 
    // parser.setASTNodeType ("RefAdaAST");
233
 
 
234
 
    try {
235
 
        // old: parser.setASTNodeFactory (AdaAST::factory);
236
 
        lexer.resetErrors ();
237
 
        parser.resetErrors ();
238
 
 
239
 
        parser.compilation_unit ();
240
 
        int errors = lexer.numberOfErrors () + parser.numberOfErrors ();
241
 
 
242
 
        RefAdaAST ast = RefAdaAST (parser.getAST ());
243
 
 
244
 
        if (errors == 0 && ast != antlr::nullAST) {
245
 
            kdDebug () << "-------------------> start StoreWalker" << endl;
246
 
            AdaStoreWalker walker;
247
 
            walker.setFileName (fileName);
248
 
            walker.setCodeModel (codeModel ());
249
 
            walker.compilation_unit (ast);
250
 
        }
251
 
    } catch (antlr::ANTLRException& ex) {
252
 
        kdDebug () << "*exception*: " << ex.toString ().c_str () << endl;
253
 
        d->problemReporter->reportError (QString::fromLatin1( ex.getMessage ().c_str() ),
254
 
                                         fileName,
255
 
                                         lexer.getLine (),
256
 
                                         lexer.getColumn ());
257
 
    }
258
 
}
259
 
 
260
 
void AdaSupportPart::parseContents (const QString& contents, const QString& fileName)
261
 
{
262
 
    kdDebug () << "AdaSupportPart::parseContents () -- " << fileName << endl;
263
 
 
264
 
    QCString _fn = QFile::encodeName (fileName);
265
 
    std::string fn (_fn.data ());
266
 
 
267
 
    QCString text = contents.utf8 ();
268
 
    std::istringstream stream ((const char *)text);
269
 
 
270
 
    AdaLexer lexer (stream);
271
 
    lexer.setFilename (fn);
272
 
    lexer.setProblemReporter (d->problemReporter);
273
 
 
274
 
    AdaParser parser (lexer);
275
 
    parser.setFilename (fn);
276
 
    parser.setProblemReporter (d->problemReporter);
277
 
 
278
 
    try {
279
 
        lexer.resetErrors ();
280
 
        parser.resetErrors ();
281
 
 
282
 
        parser.compilation_unit ();
283
 
        int errors = lexer.numberOfErrors () + parser.numberOfErrors ();
284
 
        Q_UNUSED( errors );
285
 
 
286
 
    } catch (antlr::ANTLRException& ex) {
287
 
        kdDebug () << "*exception*: " << ex.toString ().c_str () << endl;
288
 
        d->problemReporter->reportError (QString::fromLatin1( ex.getMessage().c_str() ),
289
 
                                         fileName,
290
 
                                         lexer.getLine (),
291
 
                                         lexer.getColumn ());
292
 
    }
293
 
}
294
 
 
295
 
 
296
 
 
297
 
void AdaSupportPart::savedFile (const KURL& fileName)
298
 
{
299
 
    kdDebug () << "AdaSupportPart::savedFile ()" << endl;
300
 
 
301
 
    if (project ()->allFiles ().contains (fileName.path().mid (project ()->projectDirectory ().length () + 1))) {
302
 
        maybeParse (fileName.path());
303
 
        emit updatedSourceInfo();
304
 
    }
305
 
}
306
 
 
307
 
KMimeType::List AdaSupportPart::mimeTypes( )
308
 
{
309
 
    KMimeType::List list;
310
 
 
311
 
    list << KMimeType::mimeType( "text/x-adasrc" );
312
 
 
313
 
    return list;
314
 
}
315
 
 
316
 
//@todo adymo: implement source info loading and saving
317
 
//hint: check javasupport for an example
318
 
//      and modify initialParse() method
319
 
void AdaSupportPart::saveProjectSourceInfo( )
320
 
{
321
 
/*    const FileList fileList = codeModel()->fileList();
322
 
 
323
 
    if( !project() || fileList.isEmpty() )
324
 
        return;
325
 
 
326
 
    QFile f( project()->projectDirectory() + "/" + project()->projectName() + ".pcs" );
327
 
    if( !f.open( IO_WriteOnly ) )
328
 
        return;
329
 
 
330
 
    QDataStream stream( &f );
331
 
    QMap<QString, Q_ULONG> offsets;
332
 
 
333
 
    QString pcs( "PCS" );
334
 
    stream << pcs << KDEV_PCS_VERSION;
335
 
 
336
 
    stream << int( fileList.size() );
337
 
    for( FileList::ConstIterator it=fileList.begin(); it!=fileList.end(); ++it ){
338
 
        const FileDom dom = (*it);
339
 
#if QT_VERSION >= 0x030100
340
 
        stream << dom->name() << m_timestamp[ dom->name() ].toTime_t();
341
 
#else
342
 
        stream << dom->name() << toTime_t(m_timestamp[ dom->name() ]);
343
 
#endif
344
 
        offsets.insert( dom->name(), stream.device()->at() );
345
 
        stream << (Q_ULONG)0; // dummy offset
346
 
    }
347
 
 
348
 
    for( FileList::ConstIterator it=fileList.begin(); it!=fileList.end(); ++it ){
349
 
        const FileDom dom = (*it);
350
 
        int offset = stream.device()->at();
351
 
 
352
 
        dom->write( stream );
353
 
 
354
 
        int end = stream.device()->at();
355
 
 
356
 
        stream.device()->at( offsets[dom->name()] );
357
 
        stream << offset;
358
 
        stream.device()->at( end );
359
 
    }*/
360
 
}
361
 
 
362
 
void AdaSupportPart::changedFilesInProject( const QStringList & fileList )
363
 
{
364
 
    QStringList files = fileList;
365
 
 
366
 
    for ( QStringList::ConstIterator it = files.begin(); it != files.end(); ++it )
367
 
    {
368
 
        QString path = project ()->projectDirectory () + "/" + *it ;
369
 
 
370
 
        maybeParse( path );
371
 
        emit addedSourceInfo( path );
372
 
    }
373
 
}
374
 
 
375
 
 
376
 
#include "adasupportpart.moc"