~ubuntu-branches/ubuntu/saucy/lazarus/saucy

« back to all changes in this revision

Viewing changes to components/codetools/codebeautifier.pas

  • Committer: Package Import Robot
  • Author(s): Paul Gevers, Abou Al Montacir, Bart Martens, Paul Gevers
  • Date: 2013-06-08 14:12:17 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20130608141217-7k0cy9id8ifcnutc
Tags: 1.0.8+dfsg-1
[ Abou Al Montacir ]
* New upstream major release and multiple maintenace release offering many
  fixes and new features marking a new milestone for the Lazarus development
  and its stability level.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_fixes_branch
* LCL changes:
  - LCL is now a normal package.
      + Platform independent parts of the LCL are now in the package LCLBase
      + LCL is automatically recompiled when switching the target platform,
        unless pre-compiled binaries for this target are already installed.
      + No impact on existing projects.
      + Linker options needed by LCL are no more added to projects that do
        not use the LCL package.
  - Minor changes in LCL basic classes behaviour
      + TCustomForm.Create raises an exception if a form resource is not
        found.
      + TNotebook and TPage: a new implementation of these classes was added.
      + TDBNavigator: It is now possible to have focusable buttons by setting
        Options = [navFocusableButtons] and TabStop = True, useful for
        accessibility and for devices with neither mouse nor touch screen.
      + Names of TControlBorderSpacing.GetSideSpace and GetSpace were swapped
        and are now consistent. GetSideSpace = Around + GetSpace.
      + TForm.WindowState=wsFullscreen was added
      + TCanvas.TextFitInfo was added to calculate how many characters will
        fit into a specified Width. Useful for word-wrapping calculations.
      + TControl.GetColorResolvingParent and
        TControl.GetRGBColorResolvingParent were added, simplifying the work
        to obtain the final color of the control while resolving clDefault
        and the ParentColor.
      + LCLIntf.GetTextExtentExPoint now has a good default implementation
        which works in any platform not providing a specific implementation.
        However, Widgetset specific implementation is better, when available.
      + TTabControl was reorganized. Now it has the correct class hierarchy
        and inherits from TCustomTabControl as it should.
  - New unit in the LCL:
      + lazdialogs.pas: adds non-native versions of various native dialogs,
        for example TLazOpenDialog, TLazSaveDialog, TLazSelectDirectoryDialog.
        It is used by widgetsets which either do not have a native dialog, or
        do not wish to use it because it is limited. These dialogs can also be
        used by user applications directly.
      + lazdeviceapis.pas: offers an interface to more hardware devices such
        as the accelerometer, GPS, etc. See LazDeviceAPIs
      + lazcanvas.pas: provides a TFPImageCanvas descendent implementing
        drawing in a LCL-compatible way, but 100% in Pascal.
      + lazregions.pas. LazRegions is a wholly Pascal implementation of
        regions for canvas clipping, event clipping, finding in which control
        of a region tree one an event should reach, for drawing polygons, etc.
      + customdrawncontrols.pas, customdrawndrawers.pas,
        customdrawn_common.pas, customdrawn_android.pas and
        customdrawn_winxp.pas: are the Lazarus Custom Drawn Controls -controls
        which imitate the standard LCL ones, but with the difference that they
        are non-native and support skinning.
  - New APIs added to the LCL to improve support of accessibility software
    such as screen readers.
* IDE changes:
  - Many improvments.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/New_IDE_features_since#v1.0_.282012-08-29.29
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes#IDE_Changes
* Debugger / Editor changes:
  - Added pascal sources and breakpoints to the disassembler
  - Added threads dialog.
* Components changes:
  - TAChart: many fixes and new features
  - CodeTool: support Delphi style generics and new syntax extensions.
  - AggPas: removed to honor free licencing. (Closes: Bug#708695)
[Bart Martens]
* New debian/watch file fixing issues with upstream RC release.
[Abou Al Montacir]
* Avoid changing files in .pc hidden directory, these are used by quilt for
  internal purpose and could lead to surprises during build.
[Paul Gevers]
* Updated get-orig-source target and it compinion script orig-tar.sh so that they
  repack the source file, allowing bug 708695 to be fixed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
135
135
    bbtMainBegin,
136
136
    bbtFreeBegin, // a normal begin
137
137
    bbtRepeat,
 
138
    bbtWhile,
 
139
    bbtWhileDo,   // child of bbtWhile
138
140
    bbtFor,
139
141
    bbtForDo,     // child of bbtFor
 
142
    bbtWith,
 
143
    bbtWithDo,    // child of bbtWith
140
144
    bbtCase,
141
145
    bbtCaseOf,    // child of bbtCase
142
146
    bbtCaseLabel, // child of bbtCaseOf
151
155
    bbtIfBegin,   // child of bbtIfThen or bbtIfElse
152
156
    bbtStatement,
153
157
    bbtStatementRoundBracket,
154
 
    bbtStatementEdgedBracket
 
158
    bbtStatementEdgedBracket,
 
159
    bbtProperty   // global or class property
155
160
    );
156
161
  TFABBlockTypes = set of TFABBlockType;
157
162
 
158
163
const
159
164
  bbtAllIdentifierSections = [bbtTypeSection,bbtConstSection,bbtVarSection,
160
 
       bbtResourceStringSection,bbtLabelSection];
 
165
       bbtResourceStringSection,bbtLabelSection,bbtClassSection];
161
166
  bbtAllProcedures = [bbtProcedure,bbtFunction];
162
167
  bbtAllCodeSections = [bbtInterface,bbtImplementation,bbtInitialization,
163
168
                        bbtFinalization];
164
169
  bbtAllStatementParents = [bbtMainBegin,bbtFreeBegin,bbtProcedureBegin,
165
 
                        bbtRepeat,bbtForDo,
 
170
                        bbtRepeat,bbtWhileDo,bbtForDo,bbtWithDo,
166
171
                        bbtCaseColon,bbtCaseElse,
167
172
                        bbtTry,bbtFinally,bbtExcept,
168
173
                        bbtIfThen,bbtIfElse,bbtIfBegin];
170
175
                      bbtStatement,bbtStatementRoundBracket,bbtStatementEdgedBracket];
171
176
  bbtAllBrackets = [bbtTypeRoundBracket,bbtTypeEdgedBracket,
172
177
                    bbtStatementRoundBracket,bbtStatementEdgedBracket];
173
 
  bbtAllAutoEnd = [bbtStatement,bbtIf,bbtIfThen,bbtIfElse,bbtFor,bbtForDo,
174
 
                  bbtCaseLabel,bbtCaseColon];
 
178
  bbtAllAutoEnd = [bbtStatement,bbtIf,bbtIfThen,bbtIfElse,bbtWhile,bbtWhileDo,
 
179
                  bbtFor,bbtForDo,bbtWith,bbtWithDo,bbtCaseLabel,bbtCaseColon];
175
180
  bbtAllAlignToSibling = [bbtNone]+bbtAllStatements;
176
181
 
177
 
const
178
 
  FABBlockTypeNames: array[TFABBlockType] of string = (
179
 
    'bbtNone',
180
 
    // code sections
181
 
    'bbtInterface',
182
 
    'bbtImplementation',
183
 
    'bbtInitialization',
184
 
    'bbtFinalization',
185
 
    // identifier sections
186
 
    'bbtUsesSection',
187
 
    'bbtTypeSection',
188
 
    'bbtConstSection',
189
 
    'bbtVarSection',
190
 
    'bbtResourceStringSection',
191
 
    'bbtLabelSection',
192
 
    'bbtDefinition',
193
 
    // type blocks
194
 
    'bbtRecord',
195
 
    'bbtClass',
196
 
    'bbtClassInterface',
197
 
    'bbtClassSection',
198
 
    'bbtTypeRoundBracket',
199
 
    'bbtTypeEdgedBracket',
200
 
    // statement blocks
201
 
    'bbtProcedure',
202
 
    'bbtFunction',
203
 
    'bbtProcedureHead',
204
 
    'bbtProcedureParamList',
205
 
    'bbtProcedureModifiers',
206
 
    'bbtProcedureBegin',
207
 
    'bbtMainBegin',
208
 
    'bbtFreeBegin',
209
 
    'bbtRepeat',
210
 
    'bbtFor',
211
 
    'bbtForDo',
212
 
    'bbtCase',
213
 
    'bbtCaseOf',
214
 
    'bbtCaseLabel',
215
 
    'bbtCaseColon',
216
 
    'bbtCaseElse',
217
 
    'bbtTry',
218
 
    'bbtFinally',
219
 
    'bbtExcept',
220
 
    'bbtIf',
221
 
    'bbtIfThen',
222
 
    'bbtIfElse',
223
 
    'bbtIfBegin',
224
 
    'bbtStatement',
225
 
    'bbtStatementRoundBracket',
226
 
    'bbtStatementEdgedBracket'
227
 
    );
228
 
 
229
182
type
230
183
  TOnGetFABExamples = procedure(Sender: TObject; Code: TCodeBuffer;
231
184
                                Step: integer; // starting at 0
234
187
                                ) of object;
235
188
  TOnGetFABNestedComments = procedure(Sender: TObject; Code: TCodeBuffer;
236
189
                                      out NestedComments: boolean) of object;
237
 
  TOnGetFABFile = procedure(Sender: TObject; const ExpandedFilename: string;
 
190
  TOnLoadCTFile = procedure(Sender: TObject; const ExpandedFilename: string;
238
191
                            out Code: TCodeBuffer; var Abort: boolean) of object;
239
192
 
240
193
  TFABIndentationPolicy = record
351
304
    FOnGetExamples: TOnGetFABExamples;
352
305
    FCodePolicies: TAVLTree;// tree of TFABPolicies sorted for Code
353
306
    FOnGetNestedComments: TOnGetFABNestedComments;
354
 
    FOnLoadFile: TOnGetFABFile;
 
307
    FOnLoadFile: TOnLoadCTFile;
355
308
    FUseDefaultIndentForTypes: TFABBlockTypes;
356
309
    procedure ParseSource(const Src: string; StartPos, EndPos: integer;
357
310
      NestedComments: boolean;
398
351
                                              write FOnGetExamples;
399
352
    property OnGetNestedComments: TOnGetFABNestedComments
400
353
                           read FOnGetNestedComments write FOnGetNestedComments;
401
 
    property OnLoadFile: TOnGetFABFile read FOnLoadFile write FOnLoadFile;
 
354
    property OnLoadFile: TOnLoadCTFile read FOnLoadFile write FOnLoadFile;
402
355
    property UseDefaultIndentForTypes: TFABBlockTypes
403
356
                 read FUseDefaultIndentForTypes write FUseDefaultIndentForTypes;
404
357
  end;
405
358
 
 
359
function EnumToStr(BlockType: TFABBlockType): string;
406
360
function CompareFABPoliciesWithCode(Data1, Data2: Pointer): integer;
407
361
function CompareCodeWithFABPolicy(Key, Data: Pointer): integer;
408
362
 
409
363
implementation
410
364
 
 
365
function EnumToStr(BlockType: TFABBlockType): string;
 
366
begin
 
367
  WriteStr(Result, BlockType);
 
368
end;
 
369
 
411
370
function CompareFABPoliciesWithCode(Data1, Data2: Pointer): integer;
412
371
var
413
372
  Policies1: TFABPolicies absolute Data1;
452
411
    ReAllocMem(Stack,SizeOf(TBlock)*Capacity);
453
412
  end;
454
413
  {$IFDEF ShowCodeBeautifier}
455
 
  DebugLn([GetIndentStr(Top*2),'TFABBlockStack.BeginBlock ',FABBlockTypeNames[Typ],' ',StartPos,' at ',PosToStr(StartPos)]);
 
414
  DebugLn([GetIndentStr(Top*2),'TFABBlockStack.BeginBlock ',EnumToStr(Typ),' ',StartPos,' at ',PosToStr(StartPos)]);
456
415
  {$ENDIF}
457
416
  Block:=@Stack[Top];
458
417
  Block^.Typ:=Typ;
470
429
procedure TFABBlockStack.EndBlock(EndPos: integer);
471
430
begin
472
431
  {$IFDEF ShowCodeBeautifier}
473
 
  DebugLn([GetIndentStr(Top*2),'TFABBlockStack.EndBlock ',FABBlockTypeNames[TopType]]);
 
432
  DebugLn([GetIndentStr(Top*2),'TFABBlockStack.EndBlock ',EnumToStr(TopType)]);
474
433
  {$ENDIF}
475
434
  if Top<0 then
476
435
    exit;
508
467
  i: Integer;
509
468
begin
510
469
  for i:=0 to Top do begin
511
 
    debugln([Prefix+GetIndentStr(i*2),FABBlockTypeNames[Stack[i].Typ],
 
470
    debugln([Prefix+GetIndentStr(i*2),EnumToStr(Stack[i].Typ),
512
471
      ' StartPos=',Stack[i].StartPos,
513
472
      ' Indent=',Stack[i].Indent,
514
473
      ' Trailing=',Stack[i].Trailing,
602
561
        if (not InFirstLine) or LearnFromFirstLine then
603
562
          Policies.AddIndent(Block^.Typ,Typ,AtomStart,Indent-BaseBlock^.Indent);
604
563
        {$IFDEF ShowCodeBeautifierLearn}
605
 
        DebugLn([GetIndentStr(Stack.Top*2),'nested indentation learned ',FABBlockTypeNames[Block^.Typ],'/',FABBlockTypeNames[Typ],': ',GetAtomString(@Src[AtomStart],NestedComments),' at ',PosToStr(AtomStart),' Indent=',Indent,'-',BaseBlock^.Indent,'=',Indent-BaseBlock^.Indent]);
606
 
        debugln([GetIndentStr(Stack.Top*2),'  Src=',dbgstr(copy(Src,AtomStart-10,10)),'|',copy(Src,AtomStart,p-AtomStart),' BaseBlock=',FABBlockTypeNames[BaseBlock^.Typ]]);
 
564
        DebugLn([GetIndentStr(Stack.Top*2),'nested indentation learned ',EnumToStr(Block^.Typ),'/',EnumToStr(Typ),': ',GetAtomString(@Src[AtomStart],NestedComments),' at ',PosToStr(AtomStart),' Indent=',Indent,'-',BaseBlock^.Indent,'=',Indent-BaseBlock^.Indent]);
 
565
        debugln([GetIndentStr(Stack.Top*2),'  Src=',dbgstr(copy(Src,AtomStart-10,10)),'|',copy(Src,AtomStart,p-AtomStart),' BaseBlock=',EnumToStr(BaseBlock^.Typ)]);
607
566
        if Typ=bbtCaseLabel then
608
567
          Stack.WriteDebugReport(GetIndentStr(Stack.Top*2));
609
568
        {$ENDIF}
610
569
      end;
611
570
    end;
612
 
    //if not FirstAtomOnNewLine then DebugLn([GetIndentStr(Stack.Top*2),'TRAILING BeginBlock ',FABBlockTypeNames[Typ],' ',GetAtomString(@Src[AtomStart],NestedComments),' at ',PosToStr(AtomStart)]);
 
571
    //if not FirstAtomOnNewLine then DebugLn([GetIndentStr(Stack.Top*2),'TRAILING BeginBlock ',EnumToStr(Typ),' ',GetAtomString(@Src[AtomStart],NestedComments),' at ',PosToStr(AtomStart)]);
613
572
    Stack.BeginBlock(Typ,AtomStart,not FirstAtomOnNewLine,Indent);
614
573
    {$IFDEF ShowCodeBeautifierParser}
615
 
    DebugLn([GetIndentStr(Stack.Top*2),'BeginBlock ',FABBlockTypeNames[Typ],' ',GetAtomString(@Src[AtomStart],NestedComments),' at ',PosToStr(AtomStart)]);
 
574
    DebugLn([GetIndentStr(Stack.Top*2),'BeginBlock ',EnumToStr(Typ),' ',GetAtomString(@Src[AtomStart],NestedComments),' at ',PosToStr(AtomStart)]);
616
575
    {$ENDIF}
617
576
  end;
618
577
 
619
578
  procedure EndBlock;
620
579
  begin
621
580
    {$IFDEF ShowCodeBeautifierParser}
622
 
    DebugLn([GetIndentStr(Stack.Top*2),'EndBlock ',FABBlockTypeNames[Stack.TopType],' ',GetAtomString(@Src[AtomStart],NestedComments),' at ',PosToStr(AtomStart)]);
 
581
    DebugLn([GetIndentStr(Stack.Top*2),'EndBlock ',EnumToStr(Stack.TopType),' ',GetAtomString(@Src[AtomStart],NestedComments),' at ',PosToStr(AtomStart)]);
623
582
    {$ENDIF}
624
583
    AtomEndedBlock:=true;
625
584
    Stack.EndBlock(p);
683
642
 
684
643
  procedure StartProcedure(Typ: TFABBlockType);
685
644
  begin
686
 
    if Stack.TopType<>bbtDefinition then
 
645
    if not (Stack.TopType in [bbtDefinition,bbtClassSection]) then
687
646
      EndIdentifierSectionAndProc;
688
 
    if Stack.TopType in (bbtAllCodeSections+bbtAllProcedures+[bbtNone,bbtDefinition])
 
647
    if Stack.TopType in (bbtAllCodeSections+bbtAllProcedures+[bbtNone,bbtDefinition,bbtClassSection])
689
648
    then begin
690
649
      BeginBlock(Typ);
691
650
      BeginBlock(bbtProcedureHead);
692
651
    end;
693
652
  end;
694
653
 
 
654
  procedure StartProperty;
 
655
  begin
 
656
    if Stack.TopType in [bbtNone, bbtClassSection] then
 
657
      BeginBlock(bbtProperty);
 
658
  end;
 
659
 
695
660
  procedure StartClassSection;
696
661
  begin
697
662
    if (LastAtomStart>0) and (CompareIdentifiers('STRICT',@Src[LastAtomStart])=0)
711
676
    if Stack.TopType=bbtProcedureHead then
712
677
      EndBlock;
713
678
    if (Stack.TopType in bbtAllProcedures) and (not IsProcedureImplementation)
714
 
    then
 
679
    then begin
715
680
      EndBlock;
 
681
      if Stack.TopType=bbtDefinition then
 
682
        EndBlock;
 
683
    end;
716
684
  end;
717
685
 
718
686
  function CheckProcedureModifiers: boolean;
859
827
      case UpChars[r[1]] of
860
828
      'O':
861
829
        if CompareIdentifiers('DO',r)=0 then begin
862
 
          if Stack.TopType=bbtFor then
863
 
            BeginBlock(bbtForDo);
 
830
          case Stack.TopType of
 
831
            bbtWhile: BeginBlock(bbtWhileDo);
 
832
            bbtFor: BeginBlock(bbtForDo);
 
833
            bbtWith: BeginBlock(bbtWithDo);
 
834
          end;
864
835
        end;
865
836
      'E':
866
837
        if CompareIdentifiers('DESTRUCTOR',r)=0 then
1074
1045
            StartClassSection;
1075
1046
        'O': // PRO
1076
1047
          case UpChars[r[3]] of
 
1048
          'P': // PROP
 
1049
            if (CompareIdentifiers('PROPERTY',r)=0) then
 
1050
              StartProperty;
1077
1051
          'T': // PROT
1078
1052
            if (CompareIdentifiers('PROTECTED',r)=0) then
1079
1053
              StartClassSection;
1143
1117
      if CompareIdentifiers('VAR',r)=0 then begin
1144
1118
        StartIdentifierSection(bbtVarSection);
1145
1119
      end;
 
1120
    'W':
 
1121
      case UpChars[r[1]] of
 
1122
      'H': // WH
 
1123
        if CompareIdentifiers('WHILE',r)=0 then begin
 
1124
          if Stack.TopType in bbtAllStatements then
 
1125
            BeginBlock(bbtWhile)
 
1126
        end;
 
1127
      'I': // WI
 
1128
        if CompareIdentifiers('WITH',r)=0 then begin
 
1129
          if Stack.TopType in bbtAllStatements then
 
1130
            BeginBlock(bbtWith)
 
1131
        end;
 
1132
      end;
1146
1133
    ';':
1147
1134
      begin
1148
1135
        // common syntax error: unclosed bracket => ignore it
1149
1136
        while Stack.TopType in [bbtStatementRoundBracket,bbtStatementEdgedBracket] do
1150
1137
          EndBlock;
1151
1138
        case Stack.TopType of
1152
 
        bbtUsesSection,bbtDefinition:
 
1139
        bbtUsesSection,bbtDefinition,bbtProperty:
1153
1140
          EndBlock;
1154
1141
        bbtIfThen,bbtIfElse,bbtStatement,bbtFor,bbtForDo,bbtCaseColon,bbtCaseLabel:
1155
1142
          begin
1166
1153
            EndProcedureHead;
1167
1154
        bbtClassSection,bbtClass:
1168
1155
          begin
1169
 
            if (LastAtomStart>0)
1170
 
            and (LastAtomStart=Stack.Stack[Stack.Top].StartPos) then
1171
 
            begin
1172
 
              if Stack.TopType=bbtClassSection then
1173
 
                EndBlock;
1174
 
              EndBlock;
1175
 
              if Stack.TopType=bbtDefinition then
1176
 
                EndBlock;
1177
 
            end;
 
1156
            if Stack.TopType=bbtClassSection then
 
1157
              EndBlock;
 
1158
            EndBlock;
 
1159
            if Stack.TopType=bbtDefinition then
 
1160
              EndBlock;
1178
1161
          end;
1179
1162
        end;
1180
1163
      end;
1263
1246
        if (Block^.InnerIdent>=0) then begin
1264
1247
          Policies.AddIndent(Block^.Typ,bbtNone,AtomStart,Block^.InnerIdent);
1265
1248
          {$IFDEF ShowCodeBeautifierLearn}
1266
 
          DebugLn([GetIndentStr(Stack.Top*2),'Indentation learned for bbtNone: ',FABBlockTypeNames[Block^.Typ],' Indent=',Block^.InnerIdent,' at ',PosToStr(p)]);
 
1249
          DebugLn([GetIndentStr(Stack.Top*2),'Indentation learned for bbtNone: ',EnumToStr(Block^.Typ),' Indent=',Block^.InnerIdent,' at ',PosToStr(p)]);
1267
1250
          {$ENDIF}
1268
1251
        end;
1269
1252
      end;
1321
1304
    then begin
1322
1305
      {$IFDEF VerboseIndenter}
1323
1306
      DebugLn(['TFullyAutomaticBeautifier.FindPolicyInExamples found in ',
1324
 
        Code.Filename,' ',FABBlockTypeNames[Typ],'/',FABBlockTypeNames[SubTyp]]);
 
1307
        Code.Filename,' ',EnumToStr(Typ),'/',EnumToStr(SubTyp)]);
1325
1308
      {$ENDIF}
1326
1309
      exit;
1327
1310
    end;
1430
1413
  begin
1431
1414
    if StackTopType=bbtDefinition then
1432
1415
      EndBlock;
1433
 
    if StackTopType in bbtAllIdentifierSections then
 
1416
    if StackTopType in (bbtAllIdentifierSections-[bbtClassSection]) then
1434
1417
      EndBlock;
1435
1418
    BeginBlock(bbtProcedure);
1436
1419
  end;
1678
1661
  if (Stack.Top<>Result)  then
1679
1662
    DebugLn(['TFullyAutomaticBeautifier.AdjustByNextAtom block close: Stack.Top=',Stack.Top,' Result=',Result]);
1680
1663
  if TopTypeValid then
1681
 
    DebugLn(['TFullyAutomaticBeautifier.AdjustByNextAtom block open: TopType=',FABBlockTypeNames[TopType]]);
 
1664
    DebugLn(['TFullyAutomaticBeautifier.AdjustByNextAtom block open: TopType=',EnumToStr(TopType)]);
1682
1665
  {$ENDIF}
1683
1666
end;
1684
1667
 
1692
1675
  if Stack<>nil then begin
1693
1676
    for i:=0 to Stack.Top do begin
1694
1677
      Block:=@Stack.Stack[i];
1695
 
      DebugLn([GetIndentStr(i*2),' : Typ=',FABBlockTypeNames[Block^.Typ],' StartPos=',Block^.StartPos,' InnerIdent=',Block^.InnerIdent,' InnerStartPos=',Block^.InnerStartPos]);
 
1678
      DebugLn([GetIndentStr(i*2),' : Typ=',EnumToStr(Block^.Typ),' StartPos=',Block^.StartPos,' InnerIdent=',Block^.InnerIdent,' InnerStartPos=',Block^.InnerStartPos]);
1696
1679
    end;
1697
1680
  end;
1698
1681
end;
1743
1726
    // policy found
1744
1727
    {$IFDEF VerboseIndenter}
1745
1728
    if SubTypeValid then
1746
 
      DebugLn(['TFullyAutomaticBeautifier.GetIndent policy found: Block.Typ=',FABBlockTypeNames[Block.Typ],'/',FABBlockTypeNames[SubType],' BlockIndent=',BlockIndent])
 
1729
      DebugLn(['TFullyAutomaticBeautifier.GetIndent policy found: Block.Typ=',EnumToStr(Block.Typ),'/',EnumToStr(SubType),' BlockIndent=',BlockIndent])
1747
1730
    else
1748
 
      DebugLn(['TFullyAutomaticBeautifier.GetIndent policy found: Block.Typ=',FABBlockTypeNames[Block.Typ],' BlockIndent=',BlockIndent]);
 
1731
      DebugLn(['TFullyAutomaticBeautifier.GetIndent policy found: Block.Typ=',EnumToStr(Block.Typ),' BlockIndent=',BlockIndent]);
1749
1732
    //Policies.WriteDebugReport;
1750
1733
    {$ENDIF}
1751
1734
    Indent.Indent:=GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)
1836
1819
    if (Stack.Stack[StackIndex].Typ in UseDefaultIndentForTypes) then begin
1837
1820
      // use default indent
1838
1821
      {$IFDEF VerboseIndenter}
1839
 
      DebugLn(['TFullyAutomaticBeautifier.GetIndent use default for this type: ',FABBlockTypeNames[Stack.Stack[StackIndex].Typ]]);
 
1822
      DebugLn(['TFullyAutomaticBeautifier.GetIndent use default for this type: ',EnumToStr(Stack.Stack[StackIndex].Typ)]);
1840
1823
      {$ENDIF}
1841
1824
      GetDefaultSrcIndent(Source,CleanPos,NewNestedComments,Indent);
1842
1825
      exit(Indent.IndentValid);
1847
1830
      // use indent of block start
1848
1831
      Block:=Stack.Stack[StackIndex+1];
1849
1832
      {$IFDEF VerboseIndenter}
1850
 
      DebugLn(['TFullyAutomaticBeautifier.GetIndent next token close block: ',FABBlockTypeNames[Stack.TopType],' Block=',dbgstr(copy(Source,Block.StartPos,20))]);
 
1833
      DebugLn(['TFullyAutomaticBeautifier.GetIndent next token close block: ',EnumToStr(Stack.TopType),' Block=',dbgstr(copy(Source,Block.StartPos,20))]);
1851
1834
      {$ENDIF}
1852
1835
      Indent.Indent:=GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth);
1853
1836
      Indent.IndentValid:=true;
1895
1878
    end;
1896
1879
 
1897
1880
    {$IFDEF VerboseIndenter}
1898
 
    DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: context=',FABBlockTypeNames[Block.Typ],'/',FABBlockTypeNames[SubType],' indent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
 
1881
    DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: context=',EnumToStr(Block.Typ),'/',EnumToStr(SubType),' indent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
1899
1882
    {$ENDIF}
1900
1883
    if Policies<>nil then begin
1901
1884
      // check source in front for good match
1918
1901
    // parse examples for good match
1919
1902
    ExamplePolicies:=FindPolicyInExamples(nil,Block.Typ,SubType,true,false);
1920
1903
    {$IFDEF VerboseIndenter}
1921
 
    DebugLn(['TFullyAutomaticBeautifier.GetIndent searched examples for exact match: context=',FABBlockTypeNames[Block.Typ],'/',FABBlockTypeNames[SubType],' contextindent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
 
1904
    DebugLn(['TFullyAutomaticBeautifier.GetIndent searched examples for exact match: context=',EnumToStr(Block.Typ),'/',EnumToStr(SubType),' contextindent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
1922
1905
    {$ENDIF}
1923
1906
    if CheckPolicies(ExamplePolicies,Result,false) then exit;
1924
1907
 
1925
1908
    if Policies<>nil then begin
1926
1909
      // check current source for any match
1927
1910
      {$IFDEF VerboseIndenter}
1928
 
      DebugLn(['TFullyAutomaticBeautifier.GetIndent check current source for any match: context=',FABBlockTypeNames[Block.Typ],'/',FABBlockTypeNames[SubType],' contextindent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
 
1911
      DebugLn(['TFullyAutomaticBeautifier.GetIndent check current source for any match: context=',EnumToStr(Block.Typ),'/',EnumToStr(SubType),' contextindent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
1929
1912
      {$ENDIF}
1930
1913
      if CheckPolicies(Policies,Result,true) then exit;
1931
1914
    end;
1933
1916
    // parse examples for any match
1934
1917
    ExamplePolicies:=FindPolicyInExamples(nil,Block.Typ,SubType,true,true);
1935
1918
    {$IFDEF VerboseIndenter}
1936
 
    DebugLn(['TFullyAutomaticBeautifier.GetIndent searching examples for any match: context=',FABBlockTypeNames[Block.Typ],'/',FABBlockTypeNames[SubType],' contextindent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
 
1919
    DebugLn(['TFullyAutomaticBeautifier.GetIndent searching examples for any match: context=',EnumToStr(Block.Typ),'/',EnumToStr(SubType),' contextindent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
1937
1920
    {$ENDIF}
1938
1921
    if CheckPolicies(ExamplePolicies,Result,true) then exit;
1939
1922
 
1945
1928
  end;
1946
1929
 
1947
1930
  {$IFDEF VerboseIndenter}
1948
 
  DebugLn(['TFullyAutomaticBeautifier.GetIndent no example found : context=',FABBlockTypeNames[Block.Typ],'/',FABBlockTypeNames[SubType],' contextindent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
 
1931
  DebugLn(['TFullyAutomaticBeautifier.GetIndent no example found : context=',EnumToStr(Block.Typ),'/',EnumToStr(SubType),' contextindent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
1949
1932
  {$ENDIF}
1950
1933
  if SubTypeValid then
1951
1934
    GetDefaultIndentPolicy(Block.Typ,SubType,Indent)
1981
1964
    if (BlockIndent<0) then exit;
1982
1965
    // policy found
1983
1966
    {$IFDEF VerboseIndenter}
1984
 
    DebugLn(['TFullyAutomaticBeautifier.GetIndent policy found: Block.Typ=',FABBlockTypeNames[Item^.Block.Typ],' BlockIndent=',BlockIndent]);
 
1967
    DebugLn(['TFullyAutomaticBeautifier.GetIndent policy found: Block.Typ=',EnumToStr(Item^.Block.Typ),' BlockIndent=',BlockIndent]);
1985
1968
    {$ENDIF}
1986
1969
    Item^.Indent.Indent:=GetLineIndentWithTabs(Source,Item^.Block.StartPos,DefaultTabWidth)
1987
1970
                   +BlockIndent;
2087
2070
        end else begin
2088
2071
          Item^.Block:=Stack.Stack[StackIndex];
2089
2072
          {$IFDEF VerboseIndenter}
2090
 
          DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: context=',FABBlockTypeNames[Item^.Block.Typ],'/',FABBlockTypeNames[Item^.SubType],' indent=',GetLineIndentWithTabs(Source,Item^.Block.StartPos,DefaultTabWidth)]);
 
2073
          DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: context=',EnumToStr(Item^.Block.Typ),'/',EnumToStr(Item^.SubType),' indent=',GetLineIndentWithTabs(Source,Item^.Block.StartPos,DefaultTabWidth)]);
2091
2074
          {$ENDIF}
2092
2075
          if CheckPolicies(Policies,Item) then exit(true);
2093
2076
        end;
2302
2285
    Ind^.SrcPos:=SrcPos;
2303
2286
    {$ENDIF}
2304
2287
    {$IFDEF ShowCodeBeautifierLearn}
2305
 
    DebugLn(['TFABPolicies.AddIndent New SubTyp ',FABBlockTypeNames[Typ],'-',FABBlockTypeNames[SubType],': indent=',Indent,' ',CodePosToStr(SrcPos)]);
 
2288
    DebugLn(['TFABPolicies.AddIndent New SubTyp ',EnumToStr(Typ),'-',EnumToStr(SubType),': indent=',Indent,' ',CodePosToStr(SrcPos)]);
2306
2289
    ConsistencyCheck;
2307
2290
    {$ENDIF}
2308
2291
  end else begin
2310
2293
    if Ind^.Indent<>Indent then begin
2311
2294
      Ind^.Indent:=Indent;
2312
2295
      {$IFDEF ShowCodeBeautifierLearn}
2313
 
      DebugLn(['TFABPolicies.AddIndent Changed SubTyp ',FABBlockTypeNames[Typ],'-',FABBlockTypeNames[SubType],': indent=',Indent,' ',CodePosToStr(SrcPos)]);
 
2296
      DebugLn(['TFABPolicies.AddIndent Changed SubTyp ',EnumToStr(Typ),'-',EnumToStr(SubType),': indent=',Indent,' ',CodePosToStr(SrcPos)]);
2314
2297
      {$ENDIF}
2315
2298
    end;
2316
2299
  end;
2324
2307
  for i:=0 to IndentationCount-1 do begin
2325
2308
    if (Indentations[i].Typ<>Typ) or (Indentations[i].Indent<0) then continue;
2326
2309
    {$IFDEF VerboseIndenter}
2327
 
    debugln(['TFABPolicies.GetSmallestIndent ',FABBlockTypeNames[Indentations[i].Typ],'/',FABBlockTypeNames[Indentations[i].SubTyp],' Indent=',Indentations[i].Indent
 
2310
    debugln(['TFABPolicies.GetSmallestIndent ',EnumToStr(Indentations[i].Typ),'/',EnumToStr(Indentations[i].SubTyp),' Indent=',Indentations[i].Indent
2328
2311
      {$IFDEF StoreLearnedPositions}
2329
2312
      ,' SrcPos=',CodePosToStr(Indentations[i].SrcPos)
2330
2313
      {$ENDIF}
2345
2328
  if FindIndentation(Typ,SubType,i) then begin
2346
2329
    Result:=Indentations[i].Indent;
2347
2330
    {$IFDEF VerboseIndenter}
2348
 
    debugln(['TFABPolicies.GetIndent ',FABBlockTypeNames[Typ],'/',FABBlockTypeNames[SubType],' learned at ',CodePosToStr(Indentations[i].SrcPos),' Result=',Result]);
 
2331
    debugln(['TFABPolicies.GetIndent ',EnumToStr(Typ),'/',EnumToStr(SubType),' learned at ',CodePosToStr(Indentations[i].SrcPos),' Result=',Result]);
2349
2332
    {$ENDIF}
2350
2333
  end else if UseNoneIfNotFound and FindIndentation(Typ,bbtNone,i) then begin
2351
2334
    Result:=Indentations[i].Indent;
2352
2335
    {$IFDEF VerboseIndenter}
2353
 
    debugln(['TFABPolicies.GetIndent ',FABBlockTypeNames[Typ],'/',FABBlockTypeNames[bbtNone],' learned at ',CodePosToStr(Indentations[i].SrcPos),' Result=',Result]);
 
2336
    debugln(['TFABPolicies.GetIndent ',EnumToStr(Typ),'/',EnumToStr(bbtNone),' learned at ',CodePosToStr(Indentations[i].SrcPos),' Result=',Result]);
2354
2337
    {$ENDIF}
2355
2338
  end else if UseSmallestIfNotFound then
2356
2339
    Result:=GetSmallestIndent(Typ)
2416
2399
  debugln(['TFABPolicies.WriteDebugReport ']);
2417
2400
  for i:=0 to IndentationCount-1 do begin
2418
2401
    Ind:=@Indentations[i];
2419
 
    debugln(['  ',i,'/',IndentationCount,' ',FABBlockTypeNames[Ind^.Typ],'=',ord(Ind^.Typ),' ',FABBlockTypeNames[Ind^.SubTyp],'=',ord(Ind^.SubTyp),' Indent=',Ind^.Indent]);
 
2402
    debugln(['  ',i,'/',IndentationCount,' ',EnumToStr(Ind^.Typ),'=',ord(Ind^.Typ),' ',EnumToStr(Ind^.SubTyp),'=',ord(Ind^.SubTyp),' Indent=',Ind^.Indent]);
2420
2403
  end;
2421
2404
end;
2422
2405