From 3c38e445f7605407c4ecbe2231836e3311a1ef24 Mon Sep 17 00:00:00 2001 From: mattias Date: Fri, 29 May 2009 10:19:25 +0000 Subject: [PATCH] codetools: added class section to indentation parser git-svn-id: trunk@20294 - --- components/codetools/codebeautifier.pas | 51 +++++++++++++++++++- components/codetools/examples/autoindent.pas | 7 ++- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/components/codetools/codebeautifier.pas b/components/codetools/codebeautifier.pas index 34834b73a8..06e654d954 100644 --- a/components/codetools/codebeautifier.pas +++ b/components/codetools/codebeautifier.pas @@ -87,6 +87,7 @@ type bbtRecord, bbtClass, bbtClassInterface, + bbtClassSection, // public, private, protected, published // statement blocks bbtProcedure, // procedure, constructor, destructor bbtFunction, @@ -183,6 +184,7 @@ const 'bbtRecord', 'bbtClass', 'bbtClassInterface', + 'bbtClassSection', // statement blocks 'bbtProcedure', 'bbtFunction', @@ -342,10 +344,23 @@ var end; procedure EndBlock; + var + Block: PBlock; begin {$IFDEF ShowCodeBeautifierParser} DebugLn([GetIndentStr(Stack.Top*2),'EndBlock ',FABBlockTypeNames[Stack.TopType],' ',GetAtomString(@Src[AtomStart],NestedComments),' at ',PosToStr(p)]); {$ENDIF} + Block:=@Stack.Stack[Stack.Top]; + if (Policies<>nil) + and (not Policies.Indentations[Block^.Typ].IndentBeforeValid) then begin + with Policies.Indentations[Block^.Typ] do begin + IndentBefore:=-IndentAfter; + IndentBeforeValid:=IndentAfterValid; + {$IFDEF ShowCodeBeautifierParser} + DebugLn([GetIndentStr(Stack.Top*2),'Indentation learned: ',FABBlockTypeNames[Block^.Typ],' IndentBefore=',IndentBefore]); + {$ENDIF} + end; + end; Stack.EndBlock; end; @@ -399,6 +414,14 @@ var BeginBlock(Typ); end; + procedure StartClassSection; + begin + if Stack.TopType=bbtClassSection then + EndBlock; + if Stack.TopType=bbtClass then + BeginBlock(bbtClassSection); + end; + var r: PChar; Block: PBlock; @@ -426,6 +449,9 @@ begin Indent:=Block^.InnerIdent-GetLineIndent(Src,Block^.StartPos); Policies.Indentations[Block^.Typ].IndentAfter:=Indent; Policies.Indentations[Block^.Typ].IndentAfterValid:=true; + {$IFDEF ShowCodeBeautifierParser} + DebugLn([GetIndentStr(Stack.Top*2),'Indentation learned: ',FABBlockTypeNames[Block^.Typ],' IndentAfter=',Policies.Indentations[Block^.Typ].IndentAfter]); + {$ENDIF} end; end; end; @@ -486,6 +512,8 @@ begin if CompareIdentifiers('END',r)=0 then begin // if statements can be closed by end without semicolon while Stack.TopType in [bbtIf,bbtIfThen,bbtIfElse] do EndBlock; + if Stack.TopType=bbtClassSection then + EndBlock; case Stack.TopType of bbtMainBegin,bbtCommentaryBegin, @@ -591,8 +619,27 @@ begin end; end; 'P': - if CompareIdentifiers('PROCEDURE',r)=0 then - StartProcedure(bbtProcedure); + case UpChars[r[1]] of + 'R': // PR + case UpChars[r[2]] of + 'I': // PRI + if (CompareIdentifiers('PRIVATE',r)=0) then + StartClassSection; + 'O': // PRO + case UpChars[r[3]] of + 'T': // PROT + if (CompareIdentifiers('PROTECTED',r)=0) then + StartClassSection; + 'C': // PROC + if CompareIdentifiers('PROCEDURE',r)=0 then + StartProcedure(bbtProcedure); + end; + end; + 'U': // PU + if (CompareIdentifiers('PUBLIC',r)=0) + or (CompareIdentifiers('PUBLISHED',r)=0) then + StartClassSection; + end; 'R': case UpChars[r[1]] of 'E': // RE diff --git a/components/codetools/examples/autoindent.pas b/components/codetools/examples/autoindent.pas index 4337786c26..dacb3b1af1 100644 --- a/components/codetools/examples/autoindent.pas +++ b/components/codetools/examples/autoindent.pas @@ -38,7 +38,7 @@ var Y: LongInt; X: LongInt; p: integer; - Indentation: TFABIndentation; + Indentation: TFABIndentationPolicy; begin if Paramcount>0 then begin if Paramcount<>3 then begin @@ -67,9 +67,8 @@ begin exit; end; if FAB.GetIndent(Code.Source,p,true,Indentation) then begin - writeln('Indent=',Indentation.Indent); - writeln('UseTabs=',Indentation.UseTabs); - writeln('InsertEmptyLines=',Indentation.InsertEmptyLines); + writeln('IndentAfter=',Indentation.IndentAfter); + writeln('IndentBefore=',Indentation.IndentBefore); end else begin writeln('Error: GetIndent failed'); end;