summaryrefslogtreecommitdiffstats
path: root/languages/ada/adasupportpart.cpp
blob: 1ca3abfd1687bc18a43888029cc4f1dd1ec93c9d (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

#include <tqfileinfo.h>
#include <tqlistview.h>
#include <tqstringlist.h>
#include <tqtimer.h>
#include <tqvbox.h>
#include <tqprogressbar.h>
#include <tqwhatsthis.h>

#include <kgenericfactory.h>
#include <tdeapplication.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <kstatusbar.h>
#include <kdialogbase.h>
#include <kiconloader.h>

#include <fstream>
#include <sstream>

#include "kdevgenericfactory.h"
#include "kdevcore.h"
#include "kdevproject.h"
#include "kdevmainwindow.h"
#include "kdevpartcontroller.h"
#include "codemodel.h"
#include "adasupportpart.h"
#include "problemreporter.h"
#include "backgroundparser.h"

#include "AdaLexer.h"
#include "AdaParser.h"
#include "AdaStoreWalker.h"
#include "AdaAST.h"

#include <kdevplugininfo.h>

enum { KDEV_DB_VERSION = 6 };
enum { KDEV_PCS_VERSION = 6 };

typedef KDevGenericFactory<AdaSupportPart> AdaSupportPartFactory;

static const KDevPluginInfo pluginData("kdevadasupport");
K_EXPORT_COMPONENT_FACTORY (libkdevadasupport, AdaSupportPartFactory (pluginData))


struct AdaSupportPartData {
    ProblemReporter* problemReporter;

    AdaSupportPartData () : problemReporter (0) {}
};

AdaSupportPart::AdaSupportPart (TQObject *parent, const char *name, const TQStringList &)
    : KDevLanguageSupport (&pluginData, parent, name ? name : "AdaSupportPart"), d (new AdaSupportPartData())
{
    setInstance (AdaSupportPartFactory::instance ());

    d->problemReporter = new ProblemReporter (this);
//    connect (core (), TQT_SIGNAL (configWidget (KDialogBase*)),
//             d->problemReporter, TQT_SLOT (configWidget (KDialogBase*)));
    d->problemReporter->setIcon( SmallIcon("application-vnd.tde.info") );
    mainWindow( )->embedOutputView( d->problemReporter, i18n("Problems"), i18n("Problem reporter"));
    TQWhatsThis::add(d->problemReporter, i18n("<b>Problem reporter</b><p>This window shows various \"problems\" in your project. "
        "It displays errors reported by a language parser."));

    setXMLFile ("adasupportpart.rc");

    connect (core (), TQT_SIGNAL (projectOpened ()), this, TQT_SLOT (projectOpened ()));
    connect (core (), TQT_SIGNAL (projectClosed ()), this, TQT_SLOT (projectClosed ()));

    connect (partController (), TQT_SIGNAL (savedFile (const KURL&)),
             this, TQT_SLOT (savedFile (const KURL&)));

//    connect (core (), TQT_SIGNAL (configWidget (KDialogBase*)), this, TQT_SLOT (configWidget (KDialogBase*)));
    connect( core(), TQT_SIGNAL(configWidget(KDialogBase*)),
             d->problemReporter, TQT_SLOT(configWidget(KDialogBase*)) );

    // a small hack (robe)
    //classStore ()->globalScope ()->setName ("(default packages)");
    //classStore ()->addScope (classStore ()->globalScope ());
    //classStore ()->globalScope ()->setName (TQString());
}


AdaSupportPart::~AdaSupportPart ()
{
    mainWindow ()->removeView (d->problemReporter);
    delete (d->problemReporter);
    d->problemReporter = 0;

    delete (d);
    d = 0;
}


KDevLanguageSupport::Features AdaSupportPart::features ()
{
    return KDevLanguageSupport::Features
        (  // TBD: Classes |
	 Functions | Namespaces);
}

void AdaSupportPart::projectOpened ()
{
    connect (project (), TQT_SIGNAL (addedFilesToProject (const TQStringList &)),
            this, TQT_SLOT (addedFilesToProject (const TQStringList &)));
    connect (project (), TQT_SIGNAL (removedFilesFromProject (const TQStringList &)),
            this, TQT_SLOT (removedFilesFromProject (const TQStringList &)));
    connect( project( ), TQT_SIGNAL( changedFilesInProject( const TQStringList & ) ),
             this, TQT_SLOT( changedFilesInProject( const TQStringList & ) ) );

    TQTimer::singleShot (0, this, TQT_SLOT (initialParse ()));
}


void AdaSupportPart::projectClosed ()
{
    saveProjectSourceInfo();
}


void AdaSupportPart::initialParse ()
{
    kdDebug () << "------------------------------------------> initialParse ()" << endl;

    if (project ())
    {
        mainWindow()->statusBar()->message( i18n("Updating...") );
        kapp->processEvents( );
        kapp->setOverrideCursor (waitCursor);

        int n = 0;
        TQStringList files = project ()->allFiles ();

        TQProgressBar* bar = new TQProgressBar( files.count( ), mainWindow( )->statusBar( ) );
        bar->setMinimumWidth( 120 );
        bar->setCenterIndicator( true );
        mainWindow( )->statusBar( )->addWidget( bar );
        bar->show( );

        for (TQStringList::Iterator it = files.begin (); it != files.end (); ++it) {
            bar->setProgress( n++ );

            TQString fn = project ()->projectDirectory () + "/" + *it;
            maybeParse (fn);
            kapp->processEvents (500);
        }

        emit updatedSourceInfo();

        mainWindow( )->statusBar( )->removeWidget( bar );
        delete bar;

        kapp->restoreOverrideCursor ();
        mainWindow( )->statusBar( )->message( i18n( "Done" ), 2000 );
/*        mainWindow ()->statusBar ()->message
            (i18n ("Found 1 problem", "Found %n problems", d->problemReporter->childCount ()));*/
    }
}

TQStringList AdaSupportPart::fileExtensions ()
{
    return TQStringList () << "ads" << "adb";
}

void AdaSupportPart::maybeParse (const TQString &fileName)
{
    kdDebug () << "AdaSupportPart::maybeParse: " << fileName << endl;

    if (!fileExtensions ().contains (TQFileInfo (fileName).extension ()))
        return;

//    mainWindow ()->statusBar ()->message (i18n ("Parsing file: %1").arg (fileName));
    parse (fileName);
}


void AdaSupportPart::addedFilesToProject (const TQStringList &fileList)
{
        TQStringList::ConstIterator it;

        for (it = fileList.begin (); it != fileList.end (); ++it)
        {
            TQString path = project ()->projectDirectory () + "/" + (*it);
            maybeParse (path);
            emit addedSourceInfo( path );
        }
}


void AdaSupportPart::removedFilesFromProject (const TQStringList &fileList)
{
        TQStringList::ConstIterator it;

        for (it = fileList.begin (); it != fileList.end (); ++it)
        {
            kdDebug () << "AdaSupportPart::removedFileFromProject () -- " << (*it) << endl;
            TQString path = project ()->projectDirectory () + "/" + (*it);

            if( codeModel()->hasFile(path) )
            {
                emit aboutToRemoveSourceInfo( path );
                codeModel()->removeFile( codeModel()->fileByName(path) );
            }
        }

//        emit updatedSourceInfo();
}


void AdaSupportPart::parse (const TQString &fileName)
{
    kdDebug () << "AdaSupportPart::parse () -- " << fileName << endl;

    std::ifstream stream (TQFile::encodeName( fileName ).data());
    TQCString _fn = fileName.utf8 ();
    std::string fn (_fn.data ());

    AdaLexer lexer (stream);
    lexer.setFilename (fn);
    lexer.setProblemReporter (d->problemReporter);

    AdaParser parser (lexer);
    parser.setFilename (fn);
    parser.setProblemReporter (d->problemReporter);

    // make an ast factory
    antlr::ASTFactory ast_factory;
    // initialize and put it in the parser...
    parser.initializeASTFactory (ast_factory);
    parser.setASTFactory (&ast_factory);
    // parser.setASTNodeType ("RefAdaAST");

    try {
        // old: parser.setASTNodeFactory (AdaAST::factory);
        lexer.resetErrors ();
        parser.resetErrors ();

        parser.compilation_unit ();
        int errors = lexer.numberOfErrors () + parser.numberOfErrors ();

        RefAdaAST ast = RefAdaAST (parser.getAST ());

        if (errors == 0 && ast != antlr::nullAST) {
            kdDebug () << "-------------------> start StoreWalker" << endl;
            AdaStoreWalker walker;
            walker.setFileName (fileName);
            walker.setCodeModel (codeModel ());
            walker.compilation_unit (ast);
        }
    } catch (antlr::ANTLRException& ex) {
        kdDebug () << "*exception*: " << ex.toString ().c_str () << endl;
        d->problemReporter->reportError (TQString::fromLatin1( ex.getMessage ().c_str() ),
                                         fileName,
                                         lexer.getLine (),
                                         lexer.getColumn ());
    }
}

void AdaSupportPart::parseContents (const TQString& contents, const TQString& fileName)
{
    kdDebug () << "AdaSupportPart::parseContents () -- " << fileName << endl;

    TQCString _fn = TQFile::encodeName (fileName);
    std::string fn (_fn.data ());

    TQCString text = contents.utf8 ();
    std::istringstream stream ((const char *)text);

    AdaLexer lexer (stream);
    lexer.setFilename (fn);
    lexer.setProblemReporter (d->problemReporter);

    AdaParser parser (lexer);
    parser.setFilename (fn);
    parser.setProblemReporter (d->problemReporter);

    try {
        lexer.resetErrors ();
        parser.resetErrors ();

        parser.compilation_unit ();
        int errors = lexer.numberOfErrors () + parser.numberOfErrors ();
        Q_UNUSED( errors );

    } catch (antlr::ANTLRException& ex) {
        kdDebug () << "*exception*: " << ex.toString ().c_str () << endl;
        d->problemReporter->reportError (TQString::fromLatin1( ex.getMessage().c_str() ),
                                         fileName,
                                         lexer.getLine (),
                                         lexer.getColumn ());
    }
}



void AdaSupportPart::savedFile (const KURL& fileName)
{
    kdDebug () << "AdaSupportPart::savedFile ()" << endl;

    if (project ()->allFiles ().contains (fileName.path().mid (project ()->projectDirectory ().length () + 1))) {
        maybeParse (fileName.path());
        emit updatedSourceInfo();
    }
}

KMimeType::List AdaSupportPart::mimeTypes( )
{
    KMimeType::List list;

    list << KMimeType::mimeType( "text/x-adasrc" );

    return list;
}

//@todo adymo: implement source info loading and saving
//hint: check javasupport for an example
//      and modify initialParse() method
void AdaSupportPart::saveProjectSourceInfo( )
{
/*    const FileList fileList = codeModel()->fileList();

    if( !project() || fileList.isEmpty() )
        return;

    TQFile f( project()->projectDirectory() + "/" + project()->projectName() + ".pcs" );
    if( !f.open( IO_WriteOnly ) )
        return;

    TQDataStream stream( &f );
    TQMap<TQString, TQ_ULONG> offsets;

    TQString pcs( "PCS" );
    stream << pcs << KDEV_PCS_VERSION;

    stream << int( fileList.size() );
    for( FileList::ConstIterator it=fileList.begin(); it!=fileList.end(); ++it ){
        const FileDom dom = (*it);
#if [[[TQT_VERSION IS DEPRECATED]]] >= 0x030100
        stream << dom->name() << m_timestamp[ dom->name() ].toTime_t();
#else
        stream << dom->name() << toTime_t(m_timestamp[ dom->name() ]);
#endif
        offsets.insert( dom->name(), stream.device()->at() );
        stream << (TQ_ULONG)0; // dummy offset
    }

    for( FileList::ConstIterator it=fileList.begin(); it!=fileList.end(); ++it ){
        const FileDom dom = (*it);
        int offset = stream.device()->at();

        dom->write( stream );

        int end = stream.device()->at();

        stream.device()->at( offsets[dom->name()] );
        stream << offset;
        stream.device()->at( end );
    }*/
}

void AdaSupportPart::changedFilesInProject( const TQStringList & fileList )
{
    TQStringList files = fileList;

    for ( TQStringList::ConstIterator it = files.begin(); it != files.end(); ++it )
    {
        TQString path = project ()->projectDirectory () + "/" + *it ;

        maybeParse( path );
        emit addedSourceInfo( path );
    }
}


#include "adasupportpart.moc"