-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLnProject.cpp
More file actions
975 lines (876 loc) · 25.2 KB
/
Copy pathLnProject.cpp
File metadata and controls
975 lines (876 loc) · 25.2 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
/*
* Copyright 2024 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file is part of the Luon parser/compiler library.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to me@rochus-keller.ch.
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
// Adopted from the Oberon+ IDE
#include "LnProject.h"
#include "LnParser2.h"
#include "LnPpLexer.h"
#include <QBuffer>
#include <QDir>
#include <QtDebug>
#include <QSettings>
#include <QCoreApplication>
#include <qdatetime.h>
using namespace Ln;
struct HitTest
{
quint32 line, col;
QList<Declaration*> scopes;
Declaration* scopeHit;
HitTest():scopeHit(0){}
QVariant findHit( Declaration* module, int row, int col )
{
this->col = row;
this->line = line;
try
{
visitDecl(module);
}catch( Expression* e )
{
Q_ASSERT( !scopes.isEmpty() );
scopeHit = scopes.back();
return QVariant::fromValue(e);
}catch( Declaration* d )
{
Q_ASSERT( !scopes.isEmpty() );
scopeHit = scopes.back();
return QVariant::fromValue(d);
}catch(...)
{
}
return 0;
}
void test( Declaration* d )
{
if( line == d->pos.d_row && col >= d->pos.d_col && col <= d->pos.d_col + d->name.size() )
throw d;
}
void test(Expression* e)
{
if( e == 0 )
return;
if( e->pos.d_row > line )
return;
Declaration* n = e->val.value<Declaration*>();
if( n == 0 )
return;
if( line == e->pos.d_row && col >= e->pos.d_col && col <= e->pos.d_col + n->name.size() )
throw e;
}
void visitDecl(Declaration* d)
{
test(d);
}
};
Project::Project(QObject *parent) : QObject(parent),d_dirty(false),d_useBuiltInOakwood(false)
{
d_suffixes << ".luon";
d_groups.append( FileGroup() ); // root
}
Project::~Project()
{
clearModules();
}
void Project::clear()
{
clearModules();
d_groups.clear();
d_groups.append( FileGroup() ); // root
d_filePath.clear();
d_files.clear();
d_byName.clear();
}
void Project::createNew()
{
clear();
d_filePath.clear();
d_dirty = false;
emit sigModified(d_dirty);
emit sigRenamed();
}
bool Project::initializeFromDir(const QDir& dir, bool recursive)
{
clear();
d_dirty = false;
QStringList files = findFiles(dir, recursive);
foreach( const QString& filePath, files )
addFile(filePath);
emit sigRenamed();
return true;
}
void Project::setSuffixes(const QStringList& s)
{
d_suffixes = s;
touch();
}
void Project::setMain(const Project::ModProc& mp)
{
d_main = mp;
touch();
}
QString Project::renderMain() const
{
QString res = d_main.first;
if( !d_main.second.isEmpty() )
res += "." + d_main.second;
return res;
}
void Project::setUseBuiltInOakwood(bool on)
{
d_useBuiltInOakwood = on;
touch();
}
bool Project::addFile(const QString& filePath, const QByteArrayList& package)
{
if( d_files.contains(filePath) )
return false;
int pos = findPackage(package);
if( pos == -1 )
{
pos = d_groups.size();
d_groups.append( FileGroup() );
d_groups.back().d_package = package;
}
FileGroup& fg = d_groups[pos];
FileRef ref( new File() );
fg.d_files.append(ref.data());
ref->d_group = &fg;
ref->d_filePath = filePath;
ref->d_name = QFileInfo(filePath).baseName().toUtf8();
d_files.insert(filePath,ref);
touch();
return true;
}
bool Project::addPackagePath(const QByteArrayList& path)
{
int pos = findPackage(path);
if( pos == -1 )
{
pos = d_groups.size();
d_groups.append( FileGroup() );
d_groups.back().d_package = path;
touch();
return true;
}else
return false;
}
bool Project::removeFile(const QString& filePath)
{
FileHash::iterator i = d_files.find(filePath);
if( i == d_files.end() )
return false;
const int pos = findPackage( i.value()->d_group->d_package );
Q_ASSERT( pos != -1 );
d_groups[pos].d_files.removeAll(i.value().data());
d_files.erase(i);
touch();
return true;
}
bool Project::removePackagePath(const QByteArrayList& path)
{
if( path.isEmpty() )
return false;
int pos = findPackage(path);
if( pos == -1 )
return false;
if( !d_groups[pos].d_files.isEmpty() )
return false;
d_groups.removeAt(pos);
touch();
return true;
}
const Project::FileGroup* Project::getRootFileGroup() const
{
return findFileGroup(QByteArrayList());
}
const Project::FileGroup* Project::findFileGroup(const QByteArrayList& package) const
{
for( int i = 0; i < d_groups.size(); i++ )
{
if( d_groups[i].d_package == package )
return &d_groups[i];
}
return 0;
}
Symbol* Project::findSymbolBySourcePos(const QString& file, quint32 line, quint16 col, Declaration** scopePtr) const
{
File* f = findFile(file);
if( f == 0 || f->d_mod == 0 )
return 0;
return findSymbolByModule(f->d_mod,line,col, scopePtr);
}
Symbol* Project::findSymbolByModule(Declaration* m, quint32 line, quint16 col, Declaration** scopePtr) const
{
Q_ASSERT(m && m->kind == Declaration::Module);
const ModuleSlot* module = findModule(m);
if( module == 0 || module->xref.syms == 0 )
return 0;
Symbol* s = module->xref.syms;
do
{
if( line == s->pos.d_row && col >= s->pos.d_col && col <= s->pos.d_col + s->len )
return s;
s = s->next;
}while( s && s != module->xref.syms );
return 0;
}
Symbol*Project::findSymbolByModuleName(const QByteArray& fullName, quint32 line, quint16 col, Declaration** scopePtr) const
{
Declaration* module = findModule(fullName);
if( module )
return findSymbolByModule(module, line, col, scopePtr);
else
return 0;
}
Project::File* Project::findFile(const QString& file) const
{
File* f = d_files.value(file).data();
if( f == 0 )
f = d_byName.value(file.toUtf8()).first; // includes also generic instances
return f;
}
Declaration*Project::findModule(const QByteArray& fullName) const
{
return d_byName.value(fullName).second;
}
void Project::addPreload(const QByteArray& name, const QByteArray& code)
{
for( int i = 0; i < preloads.size(); i++ )
{
if( preloads[i]->d_name == name )
{
preloads[i]->d_cache = code;
return;
}
}
// else
File* f = new File();
f->d_name = name;
f->d_cache = code;
preloads.append(FileRef(f));
}
Project::UsageByMod Project::getUsage(Declaration* n) const
{
UsageByMod res;
for( int i = 0; i < modules.size(); i++ )
{
const SymList& syms = modules[i].xref.uses.value(n);
if( !syms.isEmpty() )
res << qMakePair(modules[i].decl, syms);
}
return res;
}
Symbol*Project::getSymbolsOfModule(Declaration* module) const
{
for( int i = 0; i < modules.size(); i++ )
{
if( modules[i].decl == module )
return modules[i].xref.syms;
}
return 0;
}
DeclList Project::getSubs(Declaration* d) const
{
return subs.value(d);
}
static inline bool nonGeneric( const MetaActualList& args )
{
for( int i = 0; i < args.size(); i++ )
if( args[i]->type && args[i]->type->deref()->form == Type::Generic )
return false;
return true;
}
QString Project::getWorkingDir(bool resolved) const
{
if( d_workingDir.isEmpty() )
{
if( !d_filePath.isEmpty() )
return QFileInfo(d_filePath).dir().path();
else
return QCoreApplication::applicationDirPath();
}
else if( !resolved )
return d_workingDir;
// else
QString wd = d_workingDir;
wd.replace("%PRODIR%", QFileInfo(d_filePath).dir().path() );
QString path;
#ifdef Q_OS_MAC
QDir cur = QCoreApplication::applicationDirPath();
if( cur.path().endsWith("/Contents/MacOS") )
{
// we're in a bundle
cur.cdUp();
cur.cdUp();
cur.cdUp();
}
path = cur.path();
#else
path = QCoreApplication::applicationDirPath();
#endif
wd.replace("%APPDIR%", path );
return wd;
}
void Project::setWorkingDir(const QString& wd)
{
d_workingDir = wd;
touch();
}
QString Project::getBuildDir(bool resolved) const
{
if( d_buildDir.isEmpty() )
{
if( !d_filePath.isEmpty() )
return QFileInfo(d_filePath).dir().absoluteFilePath("build");
else
return QCoreApplication::applicationDirPath() + "/build";
}
else if( !resolved )
return d_buildDir;
// else
QString bd = d_buildDir;
bd.replace("%PRODIR%", QFileInfo(d_filePath).dir().path() );
QString path;
#ifdef Q_OS_MAC
QDir cur = QCoreApplication::applicationDirPath();
if( cur.path().endsWith("/Contents/MacOS") )
{
// we're in a bundle
cur.cdUp();
cur.cdUp();
cur.cdUp();
}
path = cur.path();
#else
path = QCoreApplication::applicationDirPath();
#endif
bd.replace("%APPDIR%", path );
return bd;
}
void Project::setBuildDir(const QString& bd)
{
d_buildDir = bd;
touch();
}
void Project::setOptions(const QByteArrayList& o)
{
d_options = o;
touch();
}
void Project::setArguments(const QStringList& args)
{
d_arguments = args;
touch();
}
bool Project::printTreeShaken(const QString& module, const QString& fileName)
{
#if 0
// TODO
FileRef f = d_files.value(module);
if( f->d_mod.isNull() )
return false;
Declaration* m = d_mdl->treeShaken(f->d_mod.data());
QFile out(fileName);
if( !out.open(QIODevice::WriteOnly) )
return false;
ObxModuleDump dump;
dump.out.setDevice( &out );
m->accept(&dump);
#endif
return true;
}
static inline QByteArray escapeDot(QByteArray name)
{
return "\"" + name + "\"";
}
#if 0
static void removeRedundantImports(Declaration* cur, QSet<Declaration*>& imports )
{
foreach( Import* i, cur->d_imports )
{
imports.remove( i->d_mod.data() );
removeRedundantImports( i->d_mod.data(), imports );
}
}
static QList<Declaration*> removeRedundantImports(Declaration* m)
{
QSet<Declaration*> imports;
foreach( Import* i, m->d_imports )
{
if( !i->d_mod->isFullyInstantiated() || i->d_mod->d_synthetic )
continue;
imports << i->d_mod.data();
}
foreach( Import* i, m->d_imports )
removeRedundantImports(i->d_mod.data(), imports);
return imports.toList();
}
#endif
bool Project::printImportDependencies(const QString& fileName, bool pruned)
{
QFile f(fileName);
if( !f.open(QIODevice::WriteOnly) )
return false;
QTextStream s(&f);
s << "digraph \"Import Dependency Tree\" {" << endl;
if( !pruned )
s << " graph [splines=ortho]" << endl;
s << " node [shape=box]" << endl;
#if 0
// TODO
QList<Declaration*> mods = getModulesToGenerate(true);
foreach( Declaration* m, mods )
{
if( !m->isFullyInstantiated() || m->d_synthetic )
continue;
QSet<QByteArray> names;
if( pruned )
{
QList<Declaration*> imports = removeRedundantImports(m);
foreach( Declaration* i, imports )
names << escapeDot(i->getFullName());
}else
{
foreach( Import* i, m->d_imports )
{
if( i->d_mod.isNull() || i->d_mod->d_synthetic )
continue;
names << escapeDot(i->d_mod->getFullName());
}
}
const QByteArray name = escapeDot(m->getFullName());
// s << " " << name << " [shape=box];" << endl;
s << " " << name << " -> {";
bool first = true;
foreach( const QByteArray& to, names )
{
if( !first )
s << ", ";
first = false;
s << to;
}
s << "};" << endl;
}
#endif
s << "}";
return true;
}
void Project::addError(const QString& file, const RowCol& pos, const QString& msg)
{
errors << Error(msg, pos, file);
}
QStringList Project::findFiles(const QDir& dir, bool recursive)
{
QStringList res;
QStringList files;
if( recursive )
{
files = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name );
foreach( const QString& f, files )
res += findFiles( QDir( dir.absoluteFilePath(f) ), recursive );
}
QStringList suff = d_suffixes;
for(int i = 0; i < suff.size(); i++ )
suff[i] = "*" + suff[i];
files = dir.entryList( suff, QDir::Files, QDir::Name );
foreach( const QString& f, files )
{
res.append( dir.absoluteFilePath(f) );
}
return res;
}
void Project::touch()
{
if( !d_dirty )
{
d_dirty = true;
emit sigModified(d_dirty);
}
}
int Project::findPackage(const QByteArrayList& path) const
{
int pos = -1;
for( int i = 0; i < d_groups.size(); i++ )
{
if( d_groups[i].d_package == path )
{
pos = i;
break;
}
}
return pos;
}
QByteArray Project::modulePath(const QByteArrayList& path)
{
return path.join('$');
}
QByteArray Project::moduleSuffix(const MetaActualList& ma)
{
// TODO: this is an intermediate solution assuming everything is built from sources in full everytime.
if( ma.isEmpty() )
return QByteArray();
else
return "$" + QByteArray::number(modules.size());
}
static inline QByteArray failWhen(const Import& imp)
{
QByteArray str;
QTextStream out(&str);
out << "when importing " << imp.path.join('.') << " from ";
if( imp.importer )
{
out << imp.importer->data.value<ModuleData>().path.join('.')
<< " at " << imp.importedAt.d_row << ":" << imp.importedAt.d_col;
}else
out << "top level";
out.flush();
return str;
}
Declaration*Project::loadModule(const Import& imp)
{
// toFile also finds modules with incomplete path, such as Interface instead of som.Interface
// we must thus look for the file first, so that we can complete the Import spec if necessary
File* file = toFile(imp);
if( file == 0 )
{
QString importer;
if( imp.importer )
importer = imp.importer->data.value<ModuleData>().source;
errors << Error("cannot find source file of imported module", imp.importedAt, importer);
modules.append(ModuleSlot(imp,QString(),0));
return 0;
}
Import fixedImp = imp;
if( file->d_group )
{
// Take care that the new module is in the package where it was actually found
// So that e.g. Interface is in som.Interface, even when imported from Vector
fixedImp.path = file->d_group->d_package;
fixedImp.path.append(imp.path.back());
}
ModuleSlot* ms = find(fixedImp);
if( ms != 0 )
return ms->decl;
// immediately add it so that circular module refs lead to an error
modules.append(ModuleSlot(fixedImp,file->d_filePath,0));
ms = &modules.back();
class Lex2 : public Scanner2
{
public:
QString sourcePath;
PpLexer lex;
Token next()
{
return lex.nextToken();
}
Token peek(int offset)
{
return lex.peekToken(offset);
}
QString source() const { return sourcePath; }
};
Lex2 lex;
lex.sourcePath = file->d_filePath; // to keep file name if invalid
QBuffer buf;
if( !file->d_cache.isEmpty() )
{
buf.setData(file->d_cache);
buf.open(QIODevice::ReadOnly);
lex.lex.setStream(&buf, file->d_filePath);
}else
lex.lex.setStream(file->d_filePath);
lex.lex.reset(d_options);
AstModel mdl;
Parser2 p(&mdl,&lex);
p.RunParser();
Ln::Declaration* module = 0;
if( !p.errors.isEmpty() )
{
foreach( const Parser2::Error& e, p.errors )
errors << Error(e.msg, e.pos, e.path);
qDebug() << "### parser failed" << failWhen(fixedImp).constData();
}else
{
module = p.takeResult();
Validator v(&mdl, this, true);
if( !v.validate(module, fixedImp) )
{
foreach( const Validator::Error& e, v.errors )
errors << Error(e.msg, e.pos, e.path);
qDebug() << "### validator failed" << failWhen(fixedImp).constData();
module->hasErrors = true;
}
if( fixedImp.metaActuals.isEmpty() )
file->d_mod = module; // in case of generic modules, file->d_mod points to the non-instantiated version
ms->xref = v.takeXref();
QHash<Declaration*,DeclList>::const_iterator i;
for( i = ms->xref.subs.begin(); i != ms->xref.subs.end(); ++i )
subs[i.key()] += i.value();
ModuleData md = module->data.value<ModuleData>();
if( md.metaParams.isEmpty() ||
(md.metaParams.size() == md.metaActuals.size() && nonGeneric(md.metaActuals) ) )
{
dependencyOrder << module;
d_byName.insert(md.fullName,qMakePair(file,module));
}
}
ms->decl = module;
return module;
}
static bool operator==(const Import& lhs, const Import& rhs)
{
if( lhs.path != rhs.path )
return false;
if( lhs.metaActuals.size() != rhs.metaActuals.size() )
return false;
for( int i = 0; i < lhs.metaActuals.size(); i++ )
{
if( lhs.metaActuals[i]->kind != rhs.metaActuals[i]->kind )
return false;
if( lhs.metaActuals[i]->type != rhs.metaActuals[i]->type )
return false;
if( lhs.metaActuals[i]->val != rhs.metaActuals[i]->val )
return false;
}
return true;
}
Project::ModuleSlot*Project::find(const Import& imp)
{
for(int i = 0; i < modules.size(); i++ )
{
if( modules[i].imp.equals(imp) )
return &modules[i];
}
return 0;
}
Project::File* Project::toFile(const Import& imp)
{
QByteArrayList path = imp.path;
path.pop_back();
const QByteArray name = imp.path.back();
const Project::FileGroup* fg = findFileGroup(path);
if( fg != 0 )
{
for( int i = 0; i < fg->d_files.size(); i++ )
{
if( fg->d_files[i]->d_name == name )
return fg->d_files[i];
}
if( imp.importer )
{
path = imp.importer->data.value<ModuleData>().path;
path.pop_back();
path += imp.path;
path.pop_back();
fg = findFileGroup(path);
if( fg != 0 )
{
for( int i = 0; i < fg->d_files.size(); i++ )
{
if( fg->d_files[i]->d_name == name )
return fg->d_files[i];
}
}
}
}
for( int i = 0; i < preloads.size(); i++ )
{
if( preloads[i]->d_name == name )
return preloads[i].data();
}
return 0;
}
void Project::clearModules()
{
errors.clear();
d_byName.clear();
dependencyOrder.clear();
Modules::const_iterator i;
for( i = modules.begin(); i != modules.end(); ++i )
{
Symbol::deleteAll((*i).xref.syms);
Declaration::deleteAll((*i).decl);
}
modules.clear();
subs.clear();
FileHash::const_iterator j;
for( j = d_files.begin(); j != d_files.end(); ++j )
j.value()->d_mod = 0;
}
const Project::ModuleSlot*Project::findModule(Declaration* m) const
{
for( int i = 0; i < modules.size(); i++ )
{
if( modules[i].decl == m )
return &modules[i];
}
return 0;
}
bool Project::parse()
{
clearModules();
int all = 0, ok = 0;
for( int i = 0; i < d_groups.size(); i++ )
{
for( int j = 0; j < d_groups[i].d_files.size(); j++ )
{
QFileInfo info(d_groups[i].d_files[j]->d_filePath);
Import imp;
imp.path = d_groups[i].d_package;
imp.path.append(Token::getSymbol(info.baseName().toUtf8()));
Declaration* module = loadModule(imp); // recursively compiles all imported files
all++;
if( module && !module->hasErrors )
ok++;
}
}
emit sigReparsed();
return all == ok;
}
bool Project::save()
{
if( d_filePath.isEmpty() )
return false;
QDir dir = QFileInfo(d_filePath).dir();
QSettings out(d_filePath,QSettings::IniFormat);
if( !out.isWritable() )
return false;
out.setValue("Suffixes", d_suffixes );
out.setValue("BuiltInOakwood", d_useBuiltInOakwood );
out.setValue("MainModule", d_main.first );
out.setValue("MainProc", d_main.second );
out.setValue("WorkingDir", d_workingDir );
out.setValue("BuildDir", d_buildDir );
out.setValue("Options", d_options.join(' ') );
out.setValue("Arguments", d_arguments );
const FileGroup* root = getRootFileGroup();
if( root )
{
out.beginWriteArray("Modules", root->d_files.size() ); // nested arrays don't work
for( int i = 0; i < root->d_files.size(); i++ )
{
const QString absPath = root->d_files[i]->d_filePath;
const QString relPath = dir.relativeFilePath( absPath );
out.setArrayIndex(i);
out.setValue("AbsPath", absPath );
out.setValue("RelPath", relPath );
}
out.endArray();
}
out.beginWriteArray("Packages", d_groups.size() );
for( int i = 0; i < d_groups.size(); i++ )
{
out.setArrayIndex(i);
out.setValue("Name", d_groups[i].d_package.join('.') ); // '/' in key gives strange effects
}
out.endArray();
for( int i = 0; i < d_groups.size(); i++ )
{
if(d_groups[i].d_package.isEmpty())
continue;
out.beginWriteArray(QString::fromUtf8("." + d_groups[i].d_package.join('.')), d_groups[i].d_files.size() );
for( int j = 0; j < d_groups[i].d_files.size(); j++ )
{
const QString absPath = d_groups[i].d_files[j]->d_filePath;
const QString relPath = dir.relativeFilePath( absPath );
out.setArrayIndex(j);
out.setValue("AbsPath", absPath );
out.setValue("RelPath", relPath );
}
out.endArray();
}
d_dirty = false;
emit sigModified(d_dirty);
return true;
}
bool Project::loadFrom(const QString& filePath)
{
clear();
d_filePath = filePath;
QDir dir = QFileInfo(d_filePath).dir();
QSettings in(d_filePath,QSettings::IniFormat);
d_suffixes = in.value("Suffixes").toStringList();
d_useBuiltInOakwood = in.value("BuiltInOakwood").toBool();
d_main.first = in.value("MainModule").toByteArray();
d_main.second = in.value("MainProc").toByteArray();
d_workingDir = in.value("WorkingDir").toString();
d_buildDir = in.value("BuildDir").toString();
d_options = in.value("Options").toByteArray().split(' ');
d_arguments = in.value("Arguments").toStringList();
int count = in.beginReadArray("Modules");
for( int i = 0; i < count; i++ )
{
in.setArrayIndex(i);
QString absPath = in.value("AbsPath").toString();
const QString relPath = in.value("RelPath").toString();
if( QFileInfo(absPath).exists() )
addFile(absPath);
else
{
absPath = dir.absoluteFilePath(relPath);
QFileInfo info(absPath);
if( info.exists() && info.isFile() )
addFile(absPath);
else
qCritical() << "Could not open module" << relPath;
}
}
in.endArray();
QList<QByteArrayList> paths;
count = in.beginReadArray("Packages");
for( int i = 0; i < count; i++ )
{
in.setArrayIndex(i);
QString name = in.value("Name").toString();
paths << name.toLatin1().split('.');
addPackagePath( paths.back() );
}
in.endArray();
for( int j = 0; j < paths.size(); j++ )
{
count = in.beginReadArray(QString::fromUtf8("." + paths[j].join('.')));
for( int i = 0; i < count; i++ )
{
in.setArrayIndex(i);
QString absPath = in.value("AbsPath").toString();
const QString relPath = in.value("RelPath").toString();
if( QFileInfo(absPath).exists() )
addFile(absPath, paths[j]);
else
{
absPath = dir.absoluteFilePath(relPath);
if( QFileInfo(absPath).exists() )
addFile(absPath, paths[j]);
else
qCritical() << "Could not open module" << relPath;
}
}
in.endArray();
}
d_dirty = false;
emit sigModified(d_dirty);
emit sigRenamed();
return true;
}
bool Project::saveTo(const QString& filePath)
{
d_filePath = filePath;
const bool res = save();
emit sigRenamed();
return res;
}