From b2377c0c8b279f11c968321a186881ad3339b8c2 Mon Sep 17 00:00:00 2001 From: mattias Date: Fri, 22 May 2009 14:19:54 +0000 Subject: [PATCH] codetools: started fully automatic indentation git-svn-id: trunk@20124 - --- components/codetools/allcodetoolunits.pp | 2 +- components/codetools/codebeautifier.pas | 138 ++++++++++++++++++++++- 2 files changed, 138 insertions(+), 2 deletions(-) diff --git a/components/codetools/allcodetoolunits.pp b/components/codetools/allcodetoolunits.pp index 969bceadb2..2b8e7ab11f 100644 --- a/components/codetools/allcodetoolunits.pp +++ b/components/codetools/allcodetoolunits.pp @@ -24,7 +24,7 @@ uses CodeCache, KeywordFuncLists, SourceLog, ExprEval, DefineTemplates, FileProcs, CodeToolsStrConsts, DirectoryCacher, CCodeParserTool, H2PasTool, MultiKeyWordListTool, ResourceCodeTool, CodeToolsStructs, CacheCodeTools, - PPUParser, PPUGraph, CodeIndex, FindOverloads, + CodeBeautifier, PPUParser, PPUGraph, CodeIndex, FindOverloads, // fast xml units, changes not merged in current fpc Laz_DOM, Laz_XMLCfg, Laz_XMLRead, Laz_XMLWrite, Laz_XMLStreaming; diff --git a/components/codetools/codebeautifier.pas b/components/codetools/codebeautifier.pas index 8efb601045..a213d69f80 100644 --- a/components/codetools/codebeautifier.pas +++ b/components/codetools/codebeautifier.pas @@ -53,7 +53,7 @@ unit CodeBeautifier; interface uses - Classes, SysUtils; + Classes, SysUtils, CodeCache, BasicCodeTools; type TBeautifySplit =( @@ -75,8 +75,144 @@ type wpLowerCaseFirstLetterUp ); + TFABContext = ( + cbcNone, + cbcRepeat, + cbcUntil + ); + + TOnGetFABExamples = procedure(Sender: TObject; Code: TCodeBuffer; + out CodeBuffers: TFPList) of object; + + TFABIndentation = record + Indent: integer; + UseTabs: boolean; + InsertEmptyLines: integer; + end; + + { TFullyAutomaticBeautifier } + + TFullyAutomaticBeautifier = class + private + FOnGetExamples: TOnGetFABExamples; + FAtomStarts: PInteger; + FAtomCapacity: integer; + FAtomCount: integer; + procedure ParseSource(const Source: string; NewNestedComments: boolean); + function IndexOfAtomInFront(CleanPos: integer): integer; + public + Src: string; + SrcLen: integer; + NestedComments: boolean; + constructor Create; + destructor Destroy; override; + procedure Clear; + function GetIndent(const Source: string; CleanPos: integer; + NewNestedComments: boolean; + out Indent: TFABIndentation): boolean; + property OnGetExamples: TOnGetFABExamples read FOnGetExamples write FOnGetExamples; + end; implementation +{ TFullyAutomaticBeautifier } + +procedure TFullyAutomaticBeautifier.ParseSource(const Source: string; + NewNestedComments: boolean); +var + AtomStart: integer; + MinAtomCapacity: Integer; + p: Integer; +begin + Src:=Source; + SrcLen:=length(Src); + NestedComments:=NewNestedComments; + FAtomCount:=0; + MinAtomCapacity:=SrcLen div 4; + if MinAtomCapacity<1024 then + MinAtomCapacity:=1024; + if FAtomCapacitySrcLen then break; + FAtomStarts[FAtomCount]:=AtomStart; + inc(FAtomCount); + if FAtomCount>FAtomCapacity then begin + FAtomCapacity:=FAtomCapacity*2; + ReAllocMem(FAtomStarts,FAtomCapacity*SizeOf(integer)); + end; + until false; +end; + +function TFullyAutomaticBeautifier.IndexOfAtomInFront(CleanPos: integer + ): integer; +// returns index in FAtomStarts of atom in front +// if CleanPos is start of an atom the atom in front is returned +// default: -1 +var + l: Integer; + r: LongInt; + m: Integer; + p: LongInt; +begin + l:=0; + r:=FAtomCount-1; + while l<=r do begin + m:=(l+r) shr 1; + p:=FAtomStarts[m]; + if p>CleanPos then + r:=m-1 + else if p