forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASBeautifier.cpp
2389 lines (2082 loc) · 91.5 KB
/
ASBeautifier.cpp
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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 The CodeLite Team
// file name : ASBeautifier.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* ASBeautifier.cpp
*
* Copyright (C) 2006-2011 by Jim Pattee <jimp03@email.com>
* Copyright (C) 1998-2002 by Tal Davidson
* <http://www.gnu.org/licenses/lgpl-3.0.html>
*
* This file is a part of Artistic Style - an indentation and
* reformatting tool for C, C++, C# and Java source files.
* <http://astyle.sourceforge.net>
*
* Artistic Style is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Artistic Style is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Artistic Style. If not, see <http://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
#include "astyle.h"
#include <algorithm>
#include <iostream>
namespace astyle
{
/**
* ASBeautifier's constructor
*/
ASBeautifier::ASBeautifier()
{
waitingBeautifierStack = NULL;
activeBeautifierStack = NULL;
waitingBeautifierStackLengthStack = NULL;
activeBeautifierStackLengthStack = NULL;
headerStack = NULL;
tempStacks = NULL;
blockParenDepthStack = NULL;
blockStatementStack = NULL;
parenStatementStack = NULL;
bracketBlockStateStack = NULL;
inStatementIndentStack = NULL;
inStatementIndentStackSizeStack = NULL;
parenIndentStack = NULL;
sourceIterator = NULL;
isMinConditionalManuallySet = false;
isModeManuallySet = false;
shouldForceTabIndentation = false;
setSpaceIndentation(4);
setMinConditionalIndentOption(MINCOND_TWO);
setMaxInStatementIndentLength(40);
classInitializerIndents = 1;
tabLength = 0;
setClassIndent(false);
setSwitchIndent(false);
setCaseIndent(false);
setBlockIndent(false);
setBracketIndent(false);
setNamespaceIndent(false);
setLabelIndent(false);
setEmptyLineFill(false);
setCStyle();
setPreprocessorIndent(false);
// initialize ASBeautifier member vectors
beautifierFileType = 9; // reset to an invalid type
headers = new vector<const string*>;
nonParenHeaders = new vector<const string*>;
assignmentOperators = new vector<const string*>;
nonAssignmentOperators = new vector<const string*>;
preBlockStatements = new vector<const string*>;
preCommandHeaders = new vector<const string*>;
indentableHeaders = new vector<const string*>;
}
/**
* ASBeautifier's copy constructor
* must explicitly call the base class copy constructor
*/
ASBeautifier::ASBeautifier(const ASBeautifier& other)
: ASBase(other)
{
// Copy the vector objects to vectors in the new ASBeautifier
// object so the new object can be destroyed without deleting
// the vector objects in the copied vector.
// This is the reason a copy constructor is needed.
// these don't need to copy the stack
waitingBeautifierStack = NULL;
activeBeautifierStack = NULL;
waitingBeautifierStackLengthStack = NULL;
activeBeautifierStackLengthStack = NULL;
// vector '=' operator performs a DEEP copy of all elements in the vector
headerStack = new vector<const string*>;
*headerStack = *other.headerStack;
tempStacks = copyTempStacks(other);
blockParenDepthStack = new vector<int>;
*blockParenDepthStack = *other.blockParenDepthStack;
blockStatementStack = new vector<bool>;
*blockStatementStack = *other.blockStatementStack;
parenStatementStack = new vector<bool>;
*parenStatementStack = *other.parenStatementStack;
bracketBlockStateStack = new vector<bool>;
*bracketBlockStateStack = *other.bracketBlockStateStack;
inStatementIndentStack = new vector<int>;
*inStatementIndentStack = *other.inStatementIndentStack;
inStatementIndentStackSizeStack = new vector<int>;
*inStatementIndentStackSizeStack = *other.inStatementIndentStackSizeStack;
parenIndentStack = new vector<int>;
*parenIndentStack = *other.parenIndentStack;
// Copy the pointers to vectors.
// This is ok because the original ASBeautifier object
// is not deleted until end of job.
beautifierFileType = other.beautifierFileType;
headers = other.headers;
nonParenHeaders = other.nonParenHeaders;
assignmentOperators = other.assignmentOperators;
nonAssignmentOperators = other.nonAssignmentOperators;
preBlockStatements = other.preBlockStatements;
preCommandHeaders = other.preCommandHeaders;
indentableHeaders = other.indentableHeaders;
// protected variables
// variables set by ASFormatter
// must also be updated in activeBeautifierStack
inLineNumber = other.inLineNumber;
horstmannIndentInStatement = other.horstmannIndentInStatement;
nonInStatementBracket = other.nonInStatementBracket;
lineCommentNoBeautify = other.lineCommentNoBeautify;
isNonInStatementArray = other.isNonInStatementArray;
isSharpAccessor = other.isSharpAccessor;
isSharpDelegate = other.isSharpDelegate;
isInExtern = other.isInExtern;
isInBeautifySQL = other.isInBeautifySQL;
isInIndentableStruct = other.isInIndentableStruct;
// private variables
sourceIterator = other.sourceIterator;
currentHeader = other.currentHeader;
previousLastLineHeader = other.previousLastLineHeader;
probationHeader = other.probationHeader;
lastLineHeader = other.lastLineHeader;
indentString = other.indentString;
isInQuote = other.isInQuote;
isInVerbatimQuote = other.isInVerbatimQuote;
haveLineContinuationChar = other.haveLineContinuationChar;
isInAsm = other.isInAsm;
isInAsmOneLine = other.isInAsmOneLine;
isInAsmBlock = other.isInAsmBlock;
isInComment = other.isInComment;
isInPreprocessorComment = other.isInPreprocessorComment;
isInHorstmannComment = other.isInHorstmannComment;
isInCase = other.isInCase;
isInQuestion = other.isInQuestion;
isInStatement = other.isInStatement;
isInHeader = other.isInHeader;
isInTemplate = other.isInTemplate;
isInDefine = other.isInDefine;
isInDefineDefinition = other.isInDefineDefinition;
classIndent = other.classIndent;
isInClassInitializer = other.isInClassInitializer;
isInClassHeaderTab = other.isInClassHeaderTab;
isInEnum = other.isInEnum;
switchIndent = other.switchIndent;
caseIndent = other.caseIndent;
namespaceIndent = other.namespaceIndent;
bracketIndent = other.bracketIndent;
blockIndent = other.blockIndent;
labelIndent = other.labelIndent;
preprocessorIndent = other.preprocessorIndent;
isInConditional = other.isInConditional;
isMinConditionalManuallySet = other.isMinConditionalManuallySet;
isModeManuallySet = other.isModeManuallySet;
shouldForceTabIndentation = other.shouldForceTabIndentation;
emptyLineFill = other.emptyLineFill;
lineOpensComment = other.lineOpensComment;
backslashEndsPrevLine = other.backslashEndsPrevLine;
blockCommentNoIndent = other.blockCommentNoIndent;
blockCommentNoBeautify = other.blockCommentNoBeautify;
previousLineProbationTab = other.previousLineProbationTab;
lineBeginsWithBracket = other.lineBeginsWithBracket;
shouldIndentBrackettedLine = other.shouldIndentBrackettedLine;
isInClass = other.isInClass;
isInSwitch = other.isInSwitch;
foundPreCommandHeader = other.foundPreCommandHeader;
indentCount = other.indentCount;
spaceIndentCount = other.spaceIndentCount;
lineOpeningBlocksNum = other.lineOpeningBlocksNum;
lineClosingBlocksNum = other.lineClosingBlocksNum;
fileType = other.fileType;
minConditionalOption = other.minConditionalOption;
minConditionalIndent = other.minConditionalIndent;
parenDepth = other.parenDepth;
indentLength = other.indentLength;
tabLength = other.tabLength;
blockTabCount = other.blockTabCount;
maxInStatementIndent = other.maxInStatementIndent;
classInitializerIndents = other.classInitializerIndents;
templateDepth = other.templateDepth;
preprocessorCppExternCBracket = other.preprocessorCppExternCBracket;
prevFinalLineSpaceIndentCount = other.prevFinalLineSpaceIndentCount;
prevFinalLineIndentCount = other.prevFinalLineIndentCount;
defineIndentCount = other.defineIndentCount;
quoteChar = other.quoteChar;
prevNonSpaceCh = other.prevNonSpaceCh;
currentNonSpaceCh = other.currentNonSpaceCh;
currentNonLegalCh = other.currentNonLegalCh;
prevNonLegalCh = other.prevNonLegalCh;
}
/**
* ASBeautifier's destructor
*/
ASBeautifier::~ASBeautifier()
{
deleteContainer(waitingBeautifierStack);
deleteContainer(activeBeautifierStack);
deleteContainer(waitingBeautifierStackLengthStack);
deleteContainer(activeBeautifierStackLengthStack);
deleteContainer(headerStack);
deleteContainer(tempStacks);
deleteContainer(blockParenDepthStack);
deleteContainer(blockStatementStack);
deleteContainer(parenStatementStack);
deleteContainer(bracketBlockStateStack);
deleteContainer(inStatementIndentStack);
deleteContainer(inStatementIndentStackSizeStack);
deleteContainer(parenIndentStack);
}
/**
* initialize the ASBeautifier.
*
* init() should be called every time a ABeautifier object is to start
* beautifying a NEW source file.
* init() recieves a pointer to a ASSourceIterator object that will be
* used to iterate through the source code.
*
* @param iter a pointer to the ASSourceIterator or ASStreamIterator object.
*/
void ASBeautifier::init(ASSourceIterator* iter)
{
sourceIterator = iter;
init();
}
/**
* initialize the ASBeautifier.
*/
void ASBeautifier::init()
{
initVectors();
ASBase::init(getFileType());
initContainer(waitingBeautifierStack, new vector<ASBeautifier*>);
initContainer(activeBeautifierStack, new vector<ASBeautifier*>);
initContainer(waitingBeautifierStackLengthStack, new vector<int>);
initContainer(activeBeautifierStackLengthStack, new vector<int>);
initContainer(headerStack, new vector<const string*>);
initContainer(tempStacks, new vector<vector<const string*>*>);
tempStacks->push_back(new vector<const string*>);
initContainer(blockParenDepthStack, new vector<int>);
initContainer(blockStatementStack, new vector<bool>);
initContainer(parenStatementStack, new vector<bool>);
initContainer(bracketBlockStateStack, new vector<bool>);
bracketBlockStateStack->push_back(true);
initContainer(inStatementIndentStack, new vector<int>);
initContainer(inStatementIndentStackSizeStack, new vector<int>);
inStatementIndentStackSizeStack->push_back(0);
initContainer(parenIndentStack, new vector<int>);
previousLastLineHeader = NULL;
currentHeader = NULL;
isInQuote = false;
isInVerbatimQuote = false;
haveLineContinuationChar = false;
isInAsm = false;
isInAsmOneLine = false;
isInAsmBlock = false;
isInComment = false;
isInPreprocessorComment = false;
isInHorstmannComment = false;
isInStatement = false;
isInCase = false;
isInQuestion = false;
isInClassInitializer = false;
isInClassHeaderTab = false;
isInEnum = false;
isInHeader = false;
isInTemplate = false;
isInConditional = false;
indentCount = 0;
spaceIndentCount = 0;
lineOpeningBlocksNum = 0;
lineClosingBlocksNum = 0;
templateDepth = 0;
parenDepth = 0;
blockTabCount = 0;
preprocessorCppExternCBracket = 0;
prevFinalLineSpaceIndentCount = 0;
prevFinalLineIndentCount = 0;
defineIndentCount = 0;
prevNonSpaceCh = '{';
currentNonSpaceCh = '{';
prevNonLegalCh = '{';
currentNonLegalCh = '{';
quoteChar = ' ';
probationHeader = NULL;
lastLineHeader = NULL;
backslashEndsPrevLine = false;
lineOpensComment = false;
isInDefine = false;
isInDefineDefinition = false;
lineCommentNoBeautify = false;
blockCommentNoIndent = false;
blockCommentNoBeautify = false;
previousLineProbationTab = false;
lineBeginsWithBracket = false;
shouldIndentBrackettedLine = true;
isInClass = false;
isInSwitch = false;
foundPreCommandHeader = false;
isNonInStatementArray = false;
isSharpAccessor = false;
isSharpDelegate = false;
isInExtern = false;
isInBeautifySQL = false;
isInIndentableStruct = false;
inLineNumber = 0;
horstmannIndentInStatement = 0;
nonInStatementBracket = 0;
}
/*
* initialize the vectors
*/
void ASBeautifier::initVectors()
{
if(fileType == beautifierFileType) // don't build unless necessary
return;
beautifierFileType = fileType;
headers->clear();
nonParenHeaders->clear();
assignmentOperators->clear();
nonAssignmentOperators->clear();
preBlockStatements->clear();
preCommandHeaders->clear();
indentableHeaders->clear();
ASResource::buildHeaders(headers, fileType, true);
ASResource::buildNonParenHeaders(nonParenHeaders, fileType, true);
ASResource::buildAssignmentOperators(assignmentOperators);
ASResource::buildNonAssignmentOperators(nonAssignmentOperators);
ASResource::buildPreBlockStatements(preBlockStatements, fileType);
ASResource::buildPreCommandHeaders(preCommandHeaders, fileType);
ASResource::buildIndentableHeaders(indentableHeaders);
}
/**
* set indentation style to C/C++.
*/
void ASBeautifier::setCStyle() { fileType = C_TYPE; }
/**
* set indentation style to Java.
*/
void ASBeautifier::setJavaStyle() { fileType = JAVA_TYPE; }
/**
* set indentation style to C#.
*/
void ASBeautifier::setSharpStyle() { fileType = SHARP_TYPE; }
/**
* set mode manually set flag
*/
void ASBeautifier::setModeManuallySet(bool state) { isModeManuallySet = state; }
/**
* set tabLength equal to indentLength.
* This is done when tabLength is not explicitely set by
* "indent=force-tab-x"
*
*/
void ASBeautifier::setDefaultTabLength() { tabLength = indentLength; }
/**
* indent using a different tab setting for indent=force-tab
*
* @param length number of spaces per tab.
*/
void ASBeautifier::setForceTabXIndentation(int length)
{
// set tabLength instead of indentLength
indentString = "\t";
tabLength = length;
shouldForceTabIndentation = true;
}
/**
* indent using one tab per indentation
*/
void ASBeautifier::setTabIndentation(int length, bool forceTabs)
{
indentString = "\t";
indentLength = length;
shouldForceTabIndentation = forceTabs;
}
/**
* indent using a number of spaces per indentation.
*
* @param length number of spaces per indent.
*/
void ASBeautifier::setSpaceIndentation(int length)
{
indentString = string(length, ' ');
indentLength = length;
}
/**
* set the maximum indentation between two lines in a multi-line statement.
*
* @param max maximum indentation length.
*/
void ASBeautifier::setMaxInStatementIndentLength(int max) { maxInStatementIndent = max; }
/**
* set the minimum conditional indentation option.
*
* @param min minimal indentation option.
*/
void ASBeautifier::setMinConditionalIndentOption(int min) { minConditionalOption = min; }
/**
* set minConditionalIndent from the minConditionalOption.
*/
void ASBeautifier::setMinConditionalIndentLength()
{
if(minConditionalOption == MINCOND_ZERO)
minConditionalIndent = 0;
else if(minConditionalOption == MINCOND_ONE)
minConditionalIndent = indentLength;
else if(minConditionalOption == MINCOND_ONEHALF)
minConditionalIndent = indentLength / 2;
// minConditionalOption = INDENT_TWO
else
minConditionalIndent = indentLength * 2;
}
/**
* set the state of the bracket indentation option. If true, brackets will
* be indented one additional indent.
*
* @param state state of option.
*/
void ASBeautifier::setBracketIndent(bool state) { bracketIndent = state; }
/**
* set the state of the block indentation option. If true, entire blocks
* will be indented one additional indent, similar to the GNU indent style.
*
* @param state state of option.
*/
void ASBeautifier::setBlockIndent(bool state) { blockIndent = state; }
/**
* set the state of the class indentation option. If true, C++ class
* definitions will be indented one additional indent.
*
* @param state state of option.
*/
void ASBeautifier::setClassIndent(bool state) { classIndent = state; }
/**
* set the state of the switch indentation option. If true, blocks of 'switch'
* statements will be indented one additional indent.
*
* @param state state of option.
*/
void ASBeautifier::setSwitchIndent(bool state) { switchIndent = state; }
/**
* set the state of the case indentation option. If true, lines of 'case'
* statements will be indented one additional indent.
*
* @param state state of option.
*/
void ASBeautifier::setCaseIndent(bool state) { caseIndent = state; }
/**
* set the state of the namespace indentation option.
* If true, blocks of 'namespace' statements will be indented one
* additional indent. Otherwise, NO indentation will be added.
*
* @param state state of option.
*/
void ASBeautifier::setNamespaceIndent(bool state) { namespaceIndent = state; }
/**
* set the state of the label indentation option.
* If true, labels will be indented one indent LESS than the
* current indentation level.
* If false, labels will be flushed to the left with NO
* indent at all.
*
* @param state state of option.
*/
void ASBeautifier::setLabelIndent(bool state) { labelIndent = state; }
/**
* set the state of the preprocessor indentation option.
* If true, multiline #define statements will be indented.
*
* @param state state of option.
*/
void ASBeautifier::setPreprocessorIndent(bool state) { preprocessorIndent = state; }
/**
* set the state of the empty line fill option.
* If true, empty lines will be filled with the whitespace.
* of their previous lines.
* If false, these lines will remain empty.
*
* @param state state of option.
*/
void ASBeautifier::setEmptyLineFill(bool state) { emptyLineFill = state; }
/**
* get the file type.
*/
int ASBeautifier::getFileType() const { return fileType; }
/**
* get the number of spaces per indent
*
* @return value of indentLength option.
*/
int ASBeautifier::getIndentLength(void) const { return indentLength; }
/**
* get the char used for indentation, space or tab
*
* @return the char used for indentation.
*/
string ASBeautifier::getIndentString(void) const { return indentString; }
/**
* get mode manually set flag
*/
bool ASBeautifier::getModeManuallySet() const { return isModeManuallySet; }
/**
* get the state of the force tab indentation option.
*
* @return state of force tab indentation.
*/
bool ASBeautifier::getForceTabIndentation(void) const { return shouldForceTabIndentation; }
/**
* get the state of the block indentation option.
*
* @return state of blockIndent option.
*/
bool ASBeautifier::getBlockIndent(void) const { return blockIndent; }
/**
* get the state of the bracket indentation option.
*
* @return state of bracketIndent option.
*/
bool ASBeautifier::getBracketIndent(void) const { return bracketIndent; }
/**
* get the state of the class indentation option. If true, blocks of
* the 'class' statement will be indented one additional indent.
*
* @return state of classIndent option.
*/
bool ASBeautifier::getClassIndent(void) const { return classIndent; }
/**
* get the state of the switch indentation option. If true, blocks of
* the 'switch' statement will be indented one additional indent.
*
* @return state of switchIndent option.
*/
bool ASBeautifier::getSwitchIndent(void) const { return switchIndent; }
/**
* get the state of the case indentation option. If true, lines of 'case'
* statements will be indented one additional indent.
*
* @return state of caseIndent option.
*/
bool ASBeautifier::getCaseIndent(void) const { return caseIndent; }
/**
* get the state of the empty line fill option.
* If true, empty lines will be filled with the whitespace.
* of their previous lines.
* If false, these lines will remain empty.
*
* @return state of emptyLineFill option.
*/
bool ASBeautifier::getEmptyLineFill(void) const { return emptyLineFill; }
/**
* get the state of the preprocessor indentation option.
* If true, preprocessor "define" lines will be indented.
* If false, preprocessor "define" lines will be unchanged.
*
* @return state of preprocessorIndent option.
*/
bool ASBeautifier::getPreprocessorIndent(void) const { return preprocessorIndent; }
/**
* get the length of the tab indentation option.
*
* @return length of tab indent option.
*/
int ASBeautifier::getTabLength(void) const { return tabLength; }
/**
* beautify a line of source code.
* every line of source code in a source code file should be sent
* one after the other to the beautify method.
*
* @return the indented line.
* @param originalLine the original unindented line.
*/
string ASBeautifier::beautify(const string& originalLine)
{
string line;
bool lineStartsInComment = false;
bool isInQuoteContinuation = isInVerbatimQuote | haveLineContinuationChar;
size_t iPrelim;
currentHeader = NULL;
lastLineHeader = NULL;
lineStartsInComment = isInComment;
blockCommentNoBeautify = blockCommentNoIndent;
isInClass = false;
isInSwitch = false;
lineBeginsWithBracket = false;
shouldIndentBrackettedLine = true;
isInAsmOneLine = false;
lineOpensComment = false;
previousLineProbationTab = false;
haveLineContinuationChar = false;
indentCount = 0;
spaceIndentCount = 0;
lineOpeningBlocksNum = 0;
lineClosingBlocksNum = 0;
// handle and remove white spaces around the line:
// If not in comment, first find out size of white space before line,
// so that possible comments starting in the line continue in
// relation to the preliminary white-space.
if(isInQuoteContinuation) {
// trim a single space added by ASFormatter, otherwise leave it alone
if(!(originalLine.length() == 1 && originalLine[0] == ' ')) line = originalLine;
} else if(isInComment || isInBeautifySQL) {
// trim the end of comment and SQL lines
line = originalLine;
size_t trimEnd = line.find_last_not_of(" \t");
if(trimEnd == string::npos)
trimEnd = 0;
else
trimEnd++;
if(trimEnd < line.length()) line.erase(trimEnd);
} else {
line = trim(originalLine);
if(line.length() > 0 && line[0] == '{') lineBeginsWithBracket = true;
isInHorstmannComment = false;
size_t j = line.find_first_not_of(" \t{");
if(j != string::npos && line.compare(j, 2, "/*") == 0) {
lineOpensComment = true;
size_t k = line.find_first_not_of(" \t");
if(k != string::npos && line.compare(k, 1, "{") == 0) isInHorstmannComment = true;
}
}
if(line.length() == 0) {
if(backslashEndsPrevLine) // must continue to clear variables
line = ' ';
else if(emptyLineFill && !isInQuoteContinuation && (!headerStack->empty() || isInEnum))
return preLineWS(prevFinalLineSpaceIndentCount, prevFinalLineIndentCount);
else
return line;
}
// handle preprocessor commands
// except C# region and endregion
if(!isInComment && !isInQuoteContinuation && line.length() > 0 &&
((line[0] == '#' && !isIndentedPreprocessor(line, 0)) || backslashEndsPrevLine)) {
if(line.length() > 0 && line[0] == '#') processProcessor(line);
// check if the last char is a backslash
if(line.length() > 0) backslashEndsPrevLine = (line[line.length() - 1] == '\\');
// comments within the definition line can be continued without the backslash
if(isInPreprocessorUnterminatedComment(line)) backslashEndsPrevLine = true;
// check if this line ends a multi-line #define
// if so, use the #define's cloned beautifier for the line's indentation
// and then remove it from the active beautifier stack and delete it.
if(!backslashEndsPrevLine && isInDefineDefinition && !isInDefine) {
string beautifiedLine;
ASBeautifier* defineBeautifier;
isInDefineDefinition = false;
defineBeautifier = activeBeautifierStack->back();
activeBeautifierStack->pop_back();
beautifiedLine = defineBeautifier->beautify(line);
delete defineBeautifier;
return beautifiedLine;
}
// unless this is a multi-line #define, return this precompiler line as is.
if(!isInDefine && !isInDefineDefinition) return originalLine;
}
// if there exists any worker beautifier in the activeBeautifierStack,
// then use it instead of me to indent the current line.
// variables set by ASFormatter must be updated.
if(!isInDefine && activeBeautifierStack != NULL && !activeBeautifierStack->empty()) {
activeBeautifierStack->back()->inLineNumber = inLineNumber;
activeBeautifierStack->back()->horstmannIndentInStatement = horstmannIndentInStatement;
activeBeautifierStack->back()->nonInStatementBracket = nonInStatementBracket;
activeBeautifierStack->back()->lineCommentNoBeautify = lineCommentNoBeautify;
activeBeautifierStack->back()->isNonInStatementArray = isNonInStatementArray;
activeBeautifierStack->back()->isSharpAccessor = isSharpAccessor;
activeBeautifierStack->back()->isSharpDelegate = isSharpDelegate;
activeBeautifierStack->back()->isInExtern = isInExtern;
activeBeautifierStack->back()->isInBeautifySQL = isInBeautifySQL;
activeBeautifierStack->back()->isInIndentableStruct = isInIndentableStruct;
// must return originalLine not the trimmed line
return activeBeautifierStack->back()->beautify(originalLine);
}
// calculate preliminary indentation based on data from past lines
if(!inStatementIndentStack->empty()) spaceIndentCount = inStatementIndentStack->back();
computePreliminaryIndentation();
iPrelim = headerStack->size();
if(!lineStartsInComment && isCStyle() && isInClass && classIndent && headerStack->size() >= 2 &&
(*headerStack)[headerStack->size() - 2] == &AS_CLASS &&
(*headerStack)[headerStack->size() - 1] == &AS_OPEN_BRACKET && line.length() > 0 && line[0] == '}' &&
bracketBlockStateStack->back() == true)
--indentCount;
else if(!lineStartsInComment && isInSwitch && switchIndent && headerStack->size() >= 2 &&
(*headerStack)[headerStack->size() - 2] == &AS_SWITCH &&
(*headerStack)[headerStack->size() - 1] == &AS_OPEN_BRACKET && line.length() > 0 && line[0] == '}')
--indentCount;
if(isInClassInitializer) {
if(lineStartsInComment || lineOpensComment) {
if(!lineBeginsWithBracket) indentCount--;
} else if(isCStyle() && !isClassAccessModifier(line)) {
isInClassHeaderTab = true;
indentCount += classInitializerIndents;
} else if(blockIndent) {
if(!lineBeginsWithBracket) indentCount++;
}
}
// handle special case of horstmann comment in an indented class statement
if(isInClass && classIndent && isInHorstmannComment && !lineOpensComment && headerStack->size() > 1 &&
(*headerStack)[headerStack->size() - 2] == &AS_CLASS)
--indentCount;
// Flag an indented header in case this line is a one-line block.
// The header in the header stack will be deleted by a one-line block.
bool isInExtraHeaderIndent = false;
if(!headerStack->empty() && line.length() > 0 && line[0] == '{' &&
(headerStack->back() != &AS_OPEN_BRACKET || probationHeader != NULL))
isInExtraHeaderIndent = true;
if(isInConditional) --indentCount;
if(preprocessorCppExternCBracket >= 3) --indentCount;
// parse characters in the current line.
// increment indentCount and spaceIndentCount for the current line
parseCurrentLine(line);
// handle special cases of unindentation:
// unindent a one-line statement in a header indent
if(!lineStartsInComment && !blockIndent && line.length() > 0 && line[0] == '{' && headerStack->size() < iPrelim &&
isInExtraHeaderIndent && (lineOpeningBlocksNum > 0 && lineOpeningBlocksNum <= lineClosingBlocksNum) &&
shouldIndentBrackettedLine)
--indentCount;
/*
* if '{' doesn't follow an immediately previous '{' in the headerStack
* (but rather another header such as "for" or "if", then unindent it
* by one indentation relative to its block.
*/
else if(!lineStartsInComment && !blockIndent && line.length() > 0 && line[0] == '{' &&
!(lineOpeningBlocksNum > 0 && lineOpeningBlocksNum <= lineClosingBlocksNum) &&
(headerStack->size() > 1 && (*headerStack)[headerStack->size() - 2] != &AS_OPEN_BRACKET) &&
shouldIndentBrackettedLine)
--indentCount;
// must check one less in headerStack if more than one header on a line (allow-addins)...
else if(!lineStartsInComment && headerStack->size() > iPrelim + 1 && !blockIndent && line.length() > 0 &&
line[0] == '{' && !(lineOpeningBlocksNum > 0 && lineOpeningBlocksNum <= lineClosingBlocksNum) &&
(headerStack->size() > 2 && (*headerStack)[headerStack->size() - 3] != &AS_OPEN_BRACKET) &&
shouldIndentBrackettedLine)
--indentCount;
// unindent a closing bracket...
else if(!lineStartsInComment && line.length() > 0 && line[0] == '}' && shouldIndentBrackettedLine)
--indentCount;
// correctly indent one-line-blocks...
else if(!lineStartsInComment && line.length() > 0 && lineOpeningBlocksNum > 0 &&
lineOpeningBlocksNum == lineClosingBlocksNum && previousLineProbationTab)
--indentCount;
// correctly indent class continuation lines...
else if(!lineStartsInComment && !lineOpensComment && isInClassHeaderTab && !blockIndent && line.length() > 0 &&
lineOpeningBlocksNum == 0 && lineOpeningBlocksNum == lineClosingBlocksNum &&
(!headerStack->empty() && headerStack->back() == &AS_CLASS))
--indentCount;
if(indentCount < 0) indentCount = 0;
// take care of extra bracket indentatation option...
if(!lineStartsInComment && bracketIndent && shouldIndentBrackettedLine && line.length() > 0 &&
(line[0] == '{' || line[0] == '}'))
indentCount++;
if(isInDefine) {
if(line.length() > 0 && line[0] == '#') {
// the 'define' does not have to be attached to the '#'
string preproc = trim(line.substr(1));
if(preproc.compare(0, 6, "define") == 0) {
if(!inStatementIndentStack->empty() && inStatementIndentStack->back() > 0) {
defineIndentCount = indentCount;
} else {
defineIndentCount = indentCount - 1;
indentCount--;
}
}
}
indentCount -= defineIndentCount;
}
if(indentCount < 0) indentCount = 0;
if(lineCommentNoBeautify || blockCommentNoBeautify || isInQuoteContinuation) indentCount = spaceIndentCount = 0;
// finally, insert indentations into begining of line
if(shouldForceTabIndentation) {
if(tabLength != indentLength) {
// adjust for different tab length
int indentCountOrig = indentCount;
int spaceIndentCountOrig = spaceIndentCount;
indentCount = ((indentCountOrig * indentLength) + spaceIndentCountOrig) / tabLength;
spaceIndentCount = ((indentCountOrig * indentLength) + spaceIndentCountOrig) % tabLength;
} else {
indentCount += spaceIndentCount / indentLength;
spaceIndentCount = spaceIndentCount % indentLength;
}
}
string outBuffer = preLineWS(spaceIndentCount, indentCount) + line;
prevFinalLineSpaceIndentCount = spaceIndentCount;
prevFinalLineIndentCount = indentCount;
if(lastLineHeader != NULL) previousLastLineHeader = lastLineHeader;
return outBuffer;
}
string ASBeautifier::preLineWS(int spaceTabCount_, int tabCount_)
{
string ws;
for(int i = 0; i < tabCount_; i++)
ws += indentString;
while((spaceTabCount_--) > 0)
ws += string(" ");
return ws;
}
bool ASBeautifier::isClassAccessModifier(const string& line) const
{
size_t firstChar = line.find_first_not_of(" \t");
if(firstChar == string::npos) return false;
// bypass a colon
if(line[firstChar] == ':') {
firstChar = line.find_first_not_of(" \t");
if(firstChar == string::npos) return false;
}
if(line.compare(firstChar, 7, "public ") == 0 || line.compare(firstChar, 8, "private ") == 0 ||
line.compare(firstChar, 10, "protected ") == 0)
return true;
return false;
}
/**
* register an in-statement indent.
*/
void ASBeautifier::registerInStatementIndent(const string& line,
int i,
int spaceTabCount_,
int tabIncrementIn,
int minIndent,
bool updateParenStack)
{
int inStatementIndent;
int remainingCharNum = line.length() - i;
int nextNonWSChar = getNextProgramCharDistance(line, i);
// if indent is around the last char in the line, indent instead one indent from the previous indent
if(nextNonWSChar == remainingCharNum) {
int previousIndent = spaceTabCount_;
if(!inStatementIndentStack->empty()) previousIndent = inStatementIndentStack->back();
int currIndent = /*2*/ indentLength + previousIndent;
if(currIndent > maxInStatementIndent && line[i] != '{') currIndent = indentLength * 2 + spaceTabCount_;
inStatementIndentStack->push_back(currIndent);
if(updateParenStack) parenIndentStack->push_back(previousIndent);
return;
}
if(updateParenStack) parenIndentStack->push_back(i + spaceTabCount_ - horstmannIndentInStatement);
int tabIncrement = tabIncrementIn;
// check for following tabs
for(int j = i + 1; j < (i + nextNonWSChar); j++) {
if(line[j] == '\t') tabIncrement += convertTabToSpaces(j, tabIncrement);