mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-11-05 06:19:41 +01:00
codetools: auto indent: when next token close block use indent of block start
git-svn-id: trunk@22502 -
This commit is contained in:
parent
d52294f627
commit
ac5b7f25b3
@ -58,13 +58,13 @@
|
|||||||
until
|
until
|
||||||
Closing the corresponding block, not all blocks:
|
Closing the corresponding block, not all blocks:
|
||||||
if expr then
|
if expr then
|
||||||
begin|
|
if expr then begin|
|
||||||
|end
|
|end
|
||||||
|
|
||||||
Examples for beautification styles: see scanexamples/indentation.pas
|
Examples for beautification styles: see scanexamples/indentation.pas
|
||||||
|
|
||||||
ToDo:
|
ToDo:
|
||||||
* ecLineBreak:
|
* LineBreak:
|
||||||
- indent last line after pressing return key:
|
- indent last line after pressing return key:
|
||||||
if true then
|
if true then
|
||||||
exit;|
|
exit;|
|
||||||
@ -275,7 +275,8 @@ type
|
|||||||
CleanPos: integer;
|
CleanPos: integer;
|
||||||
Indent: TFABIndentationPolicy;
|
Indent: TFABIndentationPolicy;
|
||||||
Block: TBlock;
|
Block: TBlock;
|
||||||
ParentBlock: TBlock;
|
SubType: TFABBlockType;
|
||||||
|
SubTypeValid: boolean;
|
||||||
end;
|
end;
|
||||||
PFABPositionIndent = ^TFABPositionIndent;
|
PFABPositionIndent = ^TFABPositionIndent;
|
||||||
|
|
||||||
@ -332,7 +333,7 @@ type
|
|||||||
Stack: TFABBlockStack; Policies: TFABPolicies;
|
Stack: TFABBlockStack; Policies: TFABPolicies;
|
||||||
LearnFromFirstLine: boolean = true);
|
LearnFromFirstLine: boolean = true);
|
||||||
function FindPolicyInExamples(StartCode: TCodeBuffer;
|
function FindPolicyInExamples(StartCode: TCodeBuffer;
|
||||||
ParentTyp, Typ: TFABBlockType): TFABPolicies;
|
Typ, SubTyp: TFABBlockType): TFABPolicies;
|
||||||
function GetNestedCommentsForCode(Code: TCodeBuffer): boolean;
|
function GetNestedCommentsForCode(Code: TCodeBuffer): boolean;
|
||||||
function FindStackPosForBlockCloseAtPos(const Source: string;
|
function FindStackPosForBlockCloseAtPos(const Source: string;
|
||||||
CleanPos: integer; NestedComments: boolean;
|
CleanPos: integer; NestedComments: boolean;
|
||||||
@ -1098,7 +1099,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
function TFullyAutomaticBeautifier.FindPolicyInExamples(StartCode: TCodeBuffer;
|
function TFullyAutomaticBeautifier.FindPolicyInExamples(StartCode: TCodeBuffer;
|
||||||
ParentTyp, Typ: TFABBlockType): TFABPolicies;
|
Typ, SubTyp: TFABBlockType): TFABPolicies;
|
||||||
|
|
||||||
function CheckCode(Code: TCodeBuffer; out Policies: TFABPolicies): boolean;
|
function CheckCode(Code: TCodeBuffer; out Policies: TFABPolicies): boolean;
|
||||||
// result=false : abort
|
// result=false : abort
|
||||||
@ -1132,7 +1133,7 @@ function TFullyAutomaticBeautifier.FindPolicyInExamples(StartCode: TCodeBuffer;
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
// search policy
|
// search policy
|
||||||
if Policies.GetSmallestIndent(Typ)>=0 then begin
|
if Policies.GetIndent(Typ,SubTyp,false)>=0 then begin
|
||||||
exit;
|
exit;
|
||||||
end;
|
end;
|
||||||
Policies:=nil;
|
Policies:=nil;
|
||||||
@ -1467,8 +1468,8 @@ function TFullyAutomaticBeautifier.GetIndent(const Source: string;
|
|||||||
ContextLearn: boolean; const InsertText: string): boolean;
|
ContextLearn: boolean; const InsertText: string): boolean;
|
||||||
var
|
var
|
||||||
Block: TBlock;
|
Block: TBlock;
|
||||||
TopType: TFABBlockType;
|
SubType: TFABBlockType;
|
||||||
TopTypeValid: Boolean;
|
SubTypeValid: Boolean;
|
||||||
|
|
||||||
function CheckPolicies(Policies: TFABPolicies; var Found: boolean): boolean;
|
function CheckPolicies(Policies: TFABPolicies; var Found: boolean): boolean;
|
||||||
// returns true to stop searching
|
// returns true to stop searching
|
||||||
@ -1478,8 +1479,8 @@ var
|
|||||||
Result:=false;
|
Result:=false;
|
||||||
Found:=false;
|
Found:=false;
|
||||||
if (Policies=nil) then exit;
|
if (Policies=nil) then exit;
|
||||||
if TopTypeValid then
|
if SubTypeValid then
|
||||||
BlockIndent:=Policies.GetIndent(Block.Typ,TopType,true)
|
BlockIndent:=Policies.GetIndent(Block.Typ,SubType,true)
|
||||||
else
|
else
|
||||||
BlockIndent:=Policies.GetSmallestIndent(Block.Typ);
|
BlockIndent:=Policies.GetSmallestIndent(Block.Typ);
|
||||||
if (BlockIndent<0) then exit;
|
if (BlockIndent<0) then exit;
|
||||||
@ -1498,7 +1499,6 @@ var
|
|||||||
Stack: TFABBlockStack;
|
Stack: TFABBlockStack;
|
||||||
Policies: TFABPolicies;
|
Policies: TFABPolicies;
|
||||||
LastAtomStart, LastAtomEnd: integer;
|
LastAtomStart, LastAtomEnd: integer;
|
||||||
ParentBlock: TBlock;
|
|
||||||
StackIndex: LongInt;
|
StackIndex: LongInt;
|
||||||
begin
|
begin
|
||||||
Result:=false;
|
Result:=false;
|
||||||
@ -1513,7 +1513,6 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
Block:=CleanBlock;
|
Block:=CleanBlock;
|
||||||
ParentBlock:=CleanBlock;
|
|
||||||
Policies:=nil;
|
Policies:=nil;
|
||||||
Stack:=TFABBlockStack.Create;
|
Stack:=TFABBlockStack.Create;
|
||||||
try
|
try
|
||||||
@ -1543,15 +1542,15 @@ begin
|
|||||||
if LastAtomStart>0 then CleanPos:=LastAtomStart;
|
if LastAtomStart>0 then CleanPos:=LastAtomStart;
|
||||||
|
|
||||||
StackIndex:=Stack.Top;
|
StackIndex:=Stack.Top;
|
||||||
TopType:=bbtNone;
|
SubType:=bbtNone;
|
||||||
TopTypeValid:=false;
|
SubTypeValid:=false;
|
||||||
if UseLineStart then begin
|
if UseLineStart then begin
|
||||||
if InsertText='' then begin
|
if InsertText='' then begin
|
||||||
StackIndex:=FindStackPosForBlockCloseAtPos(Source,CleanPos,
|
StackIndex:=FindStackPosForBlockCloseAtPos(Source,CleanPos,
|
||||||
NewNestedComments,Stack,TopType,TopTypeValid);
|
NewNestedComments,Stack,SubType,SubTypeValid);
|
||||||
end else begin
|
end else begin
|
||||||
StackIndex:=FindStackPosForBlockCloseAtPos(InsertText,1,
|
StackIndex:=FindStackPosForBlockCloseAtPos(InsertText,1,
|
||||||
NewNestedComments,Stack,TopType,TopTypeValid);
|
NewNestedComments,Stack,SubType,SubTypeValid);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
if (StackIndex<0) then begin
|
if (StackIndex<0) then begin
|
||||||
@ -1564,14 +1563,19 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
Block:=Stack.Stack[StackIndex];
|
Block:=Stack.Stack[StackIndex];
|
||||||
|
if (StackIndex<Stack.Top) then begin
|
||||||
|
// block(s) closed by next token
|
||||||
|
// use indent of block start
|
||||||
|
Indent.Indent:=GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth);
|
||||||
|
Indent.IndentValid:=true;
|
||||||
|
exit(true);
|
||||||
|
end;
|
||||||
|
|
||||||
if StackIndex>0 then
|
|
||||||
ParentBlock:=Stack.Stack[StackIndex-1];
|
|
||||||
{$IFDEF VerboseIndenter}
|
{$IFDEF VerboseIndenter}
|
||||||
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: context=',FABBlockTypeNames[ParentBlock.Typ],'/',FABBlockTypeNames[Block.Typ],' indent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
|
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: context=',FABBlockTypeNames[Block.Typ],'/',FABBlockTypeNames[SubType],' indent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
if CheckPolicies(Policies,Result) then exit;
|
if CheckPolicies(Policies,Result) then exit;
|
||||||
TopTypeValid:=false;
|
SubTypeValid:=false;
|
||||||
|
|
||||||
if ContextLearn then begin
|
if ContextLearn then begin
|
||||||
// parse source behind
|
// parse source behind
|
||||||
@ -1590,7 +1594,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
// parse examples
|
// parse examples
|
||||||
Policies:=FindPolicyInExamples(nil,ParentBlock.Typ,Block.Typ);
|
Policies:=FindPolicyInExamples(nil,Block.Typ,SubType);
|
||||||
{$IFDEF VerboseIndenter}
|
{$IFDEF VerboseIndenter}
|
||||||
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed examples']);
|
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed examples']);
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
@ -1613,7 +1617,10 @@ var
|
|||||||
begin
|
begin
|
||||||
Result:=false;
|
Result:=false;
|
||||||
if (Policies=nil) then exit;
|
if (Policies=nil) then exit;
|
||||||
BlockIndent:=Policies.GetSmallestIndent(Item^.Block.Typ);
|
if Item^.SubTypeValid then
|
||||||
|
BlockIndent:=Policies.GetIndent(Item^.Block.Typ,Item^.SubType,true)
|
||||||
|
else
|
||||||
|
BlockIndent:=Policies.GetSmallestIndent(Item^.Block.Typ);
|
||||||
if (BlockIndent<0) then exit;
|
if (BlockIndent<0) then exit;
|
||||||
// policy found
|
// policy found
|
||||||
{$IFDEF VerboseIndenter}
|
{$IFDEF VerboseIndenter}
|
||||||
@ -1633,8 +1640,6 @@ var
|
|||||||
Stack: TFABBlockStack;
|
Stack: TFABBlockStack;
|
||||||
StackIndex: LongInt;
|
StackIndex: LongInt;
|
||||||
Policies: TFABPolicies;
|
Policies: TFABPolicies;
|
||||||
TopType: TFABBlockType;
|
|
||||||
TopTypeValid: Boolean;
|
|
||||||
begin
|
begin
|
||||||
Result:=false;
|
Result:=false;
|
||||||
if (Positions=nil) or (Positions.Count=0) then exit;
|
if (Positions=nil) or (Positions.Count=0) then exit;
|
||||||
@ -1648,7 +1653,8 @@ begin
|
|||||||
and (Item^.CleanPos<=Positions.Items[ItemIndex-1].CleanPos) then
|
and (Item^.CleanPos<=Positions.Items[ItemIndex-1].CleanPos) then
|
||||||
exit;
|
exit;
|
||||||
Item^.Block:=CleanBlock;
|
Item^.Block:=CleanBlock;
|
||||||
Item^.ParentBlock:=CleanBlock;
|
Item^.SubType:=bbtNone;
|
||||||
|
Item^.SubTypeValid:=false;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
if UseLineStart then begin
|
if UseLineStart then begin
|
||||||
@ -1699,11 +1705,11 @@ begin
|
|||||||
if not Item^.Indent.IndentValid then begin
|
if not Item^.Indent.IndentValid then begin
|
||||||
if LastAtomStart>0 then Item^.CleanPos:=LastAtomStart;
|
if LastAtomStart>0 then Item^.CleanPos:=LastAtomStart;
|
||||||
|
|
||||||
TopType:=bbtNone;
|
Item^.SubType:=bbtNone;
|
||||||
TopTypeValid:=false;
|
Item^.SubTypeValid:=false;
|
||||||
if UseLineStart then
|
if UseLineStart then
|
||||||
StackIndex:=FindStackPosForBlockCloseAtPos(Source,Item^.CleanPos,
|
StackIndex:=FindStackPosForBlockCloseAtPos(Source,Item^.CleanPos,
|
||||||
NewNestedComments,Stack,TopType,TopTypeValid);
|
NewNestedComments,Stack,Item^.SubType,Item^.SubTypeValid);
|
||||||
if (StackIndex<0) then begin
|
if (StackIndex<0) then begin
|
||||||
// no context
|
// no context
|
||||||
{$IFDEF VerboseIndenter}
|
{$IFDEF VerboseIndenter}
|
||||||
@ -1723,11 +1729,8 @@ begin
|
|||||||
if Needed=0 then exit(true);
|
if Needed=0 then exit(true);
|
||||||
end else begin
|
end else begin
|
||||||
Item^.Block:=Stack.Stack[StackIndex];
|
Item^.Block:=Stack.Stack[StackIndex];
|
||||||
|
|
||||||
if StackIndex>0 then
|
|
||||||
Item^.ParentBlock:=Stack.Stack[StackIndex-1];
|
|
||||||
{$IFDEF VerboseIndenter}
|
{$IFDEF VerboseIndenter}
|
||||||
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: context=',FABBlockTypeNames[Item^.ParentBlock.Typ],'/',FABBlockTypeNames[Item^.Block.Typ],' indent=',GetLineIndentWithTabs(Source,Item^.Block.StartPos,DefaultTabWidth)]);
|
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: context=',FABBlockTypeNames[Item^.Block.Typ],'/',FABBlockTypeNames[Item^.SubType],' indent=',GetLineIndentWithTabs(Source,Item^.Block.StartPos,DefaultTabWidth)]);
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
if CheckPolicies(Policies,Item) then exit(true);
|
if CheckPolicies(Policies,Item) then exit(true);
|
||||||
end;
|
end;
|
||||||
@ -1758,7 +1761,7 @@ begin
|
|||||||
for ItemIndex:=0 to Positions.Count-1 do begin
|
for ItemIndex:=0 to Positions.Count-1 do begin
|
||||||
Item:=@Positions.Items[ItemIndex];
|
Item:=@Positions.Items[ItemIndex];
|
||||||
if (not Item^.Indent.IndentValid) and (Item^.Block.Typ<>bbtNone) then begin
|
if (not Item^.Indent.IndentValid) and (Item^.Block.Typ<>bbtNone) then begin
|
||||||
Policies:=FindPolicyInExamples(nil,Item^.ParentBlock.Typ,Item^.Block.Typ);
|
Policies:=FindPolicyInExamples(nil,Item^.Block.Typ,Item^.SubType);
|
||||||
{$IFDEF VerboseIndenter}
|
{$IFDEF VerboseIndenter}
|
||||||
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed examples']);
|
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed examples']);
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user