diff --git a/components/codetools/codebeautifier.pas b/components/codetools/codebeautifier.pas index 068741cd87..8cff7a9003 100644 --- a/components/codetools/codebeautifier.pas +++ b/components/codetools/codebeautifier.pas @@ -189,6 +189,10 @@ type FOnGetExamples: TOnGetFABExamples; FCodePolicies: TAVLTree;// tree of TFABPolicies sorted for Code FOnGetNestedComments: TOnGetFABNestedComments; + procedure ParseSource(const Src: string; StartPos, EndPos: integer; + NestedComments: boolean; + Stack: TFABBlockStack; Policies: TFABPolicies; + out LastAtomStart, LastAtomEnd: integer); procedure ParseSource(const Src: string; StartPos, EndPos: integer; NestedComments: boolean; Stack: TFABBlockStack; Policies: TFABPolicies); @@ -360,7 +364,7 @@ end; procedure TFullyAutomaticBeautifier.ParseSource(const Src: string; StartPos, EndPos: integer; NestedComments: boolean; Stack: TFABBlockStack; - Policies: TFABPolicies); + Policies: TFABPolicies; out LastAtomStart, LastAtomEnd: integer); var p: Integer; AtomStart: integer; @@ -490,12 +494,19 @@ var r: PChar; Block: PBlock; begin + LastAtomStart:=0; + LastAtomEnd:=0; p:=StartPos; repeat ReadRawNextPascalAtom(Src,p,AtomStart,NestedComments); DebugLn(['TFullyAutomaticBeautifier.ParseSource Atom=',copy(Src,AtomStart,p-AtomStart)]); - if p>=EndPos then break; - + if p>=EndPos then begin + if (AtomStart<=p) and (p>EndPos) then begin + LastAtomStart:=p; + LastAtomEnd:=p; + end; + break; + end; // check if first block inner found FirstAtomOnNewLine:=false; if (Stack.Top>=0) then begin @@ -787,6 +798,16 @@ begin until false; end; +procedure TFullyAutomaticBeautifier.ParseSource(const Src: string; StartPos, + EndPos: integer; NestedComments: boolean; Stack: TFABBlockStack; + Policies: TFABPolicies); +var + LastAtomStart, LastAtomEnd: integer; +begin + ParseSource(Src,StartPos,EndPos,NestedComments,Stack,Policies, + LastAtomStart,LastAtomEnd); +end; + function TFullyAutomaticBeautifier.FindPolicyInExamples(StartCode: TCodeBuffer; Typ: TFABBlockType): TFABPolicies; var @@ -900,6 +921,7 @@ var var Stack: TFABBlockStack; Policies: TFABPolicies; + LastAtomStart, LastAtomEnd: integer; begin Result:=false; FillByte(Indent,SizeOf(Indent),0); @@ -911,13 +933,20 @@ begin Stack:=TFABBlockStack.Create; try // parse source in front - ParseSource(Source,1,CleanPos,NewNestedComments,Stack,Policies); - if Stack.Top<0 then begin + ParseSource(Source,1,CleanPos,NewNestedComments,Stack,Policies, + LastAtomStart,LastAtomEnd); + if (Stack.Top<0) then begin // no context DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: no context']); GetDefaultIndent(Source,CleanPos,NewNestedComments,Indent); exit; end; + if (LastAtomStart>0) and (CleanPos>LastAtomStart) then begin + // in comment or atom + DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: position in middle of atom, e.g. comment']); + GetDefaultIndent(Source,CleanPos,NewNestedComments,Indent); + exit; + end; Block:=Stack.Stack[Stack.Top]; DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: context=',FABBlockTypeNames[Block.Typ],' blockindent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]); diff --git a/components/codetools/stdcodetools.pas b/components/codetools/stdcodetools.pas index 132952c46d..02e28b73b0 100644 --- a/components/codetools/stdcodetools.pas +++ b/components/codetools/stdcodetools.pas @@ -5179,7 +5179,6 @@ type var CleanCursorPos: integer; StartNode: TCodeTreeNode; - NewIndent: TFABIndentationPolicy; procedure InitStack(out Stack: TBlockStack); begin