mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-06-22 09:28:23 +02:00
codetools: indentation: added default policy
git-svn-id: trunk@20637 -
This commit is contained in:
parent
ab475f9133
commit
35d2816a9c
@ -162,6 +162,16 @@ type
|
|||||||
end;
|
end;
|
||||||
PBlock = ^TBlock;
|
PBlock = ^TBlock;
|
||||||
|
|
||||||
|
const
|
||||||
|
CleanBlock: TBlock = (
|
||||||
|
Typ: bbtNone;
|
||||||
|
StartPos: -1;
|
||||||
|
InnerStartPos: -1;
|
||||||
|
InnerIdent: -1
|
||||||
|
);
|
||||||
|
|
||||||
|
type
|
||||||
|
|
||||||
{ TFABBlockStack }
|
{ TFABBlockStack }
|
||||||
|
|
||||||
TFABBlockStack = class
|
TFABBlockStack = class
|
||||||
@ -197,7 +207,7 @@ type
|
|||||||
NestedComments: boolean;
|
NestedComments: boolean;
|
||||||
Stack: TFABBlockStack; Policies: TFABPolicies);
|
Stack: TFABBlockStack; Policies: TFABPolicies);
|
||||||
function FindPolicyInExamples(StartCode: TCodeBuffer;
|
function FindPolicyInExamples(StartCode: TCodeBuffer;
|
||||||
Typ: TFABBlockType): TFABPolicies;
|
ParentTyp, Typ: TFABBlockType): TFABPolicies;
|
||||||
function GetNestedCommentsForCode(Code: TCodeBuffer): boolean;
|
function GetNestedCommentsForCode(Code: TCodeBuffer): boolean;
|
||||||
procedure WriteDebugReport(Msg: string; Stack: TFABBlockStack);
|
procedure WriteDebugReport(Msg: string; Stack: TFABBlockStack);
|
||||||
public
|
public
|
||||||
@ -208,9 +218,11 @@ type
|
|||||||
function GetIndent(const Source: string; CleanPos: integer;
|
function GetIndent(const Source: string; CleanPos: integer;
|
||||||
NewNestedComments: boolean;
|
NewNestedComments: boolean;
|
||||||
out Indent: TFABIndentationPolicy): boolean;
|
out Indent: TFABIndentationPolicy): boolean;
|
||||||
procedure GetDefaultIndent(const Source: string; CleanPos: integer;
|
procedure GetDefaultSrcIndent(const Source: string; CleanPos: integer;
|
||||||
NewNestedComments: boolean;
|
NewNestedComments: boolean;
|
||||||
out Indent: TFABIndentationPolicy);
|
out Indent: TFABIndentationPolicy);
|
||||||
|
procedure GetDefaultIndentPolicy(Typ, SubTyp: TFABBlockType;
|
||||||
|
out Indent: TFABIndentationPolicy);
|
||||||
{ ToDo:
|
{ ToDo:
|
||||||
- indent on paste (position + new source)
|
- indent on paste (position + new source)
|
||||||
- indent auto generated code (several snippets)
|
- indent auto generated code (several snippets)
|
||||||
@ -813,7 +825,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
function TFullyAutomaticBeautifier.FindPolicyInExamples(StartCode: TCodeBuffer;
|
function TFullyAutomaticBeautifier.FindPolicyInExamples(StartCode: TCodeBuffer;
|
||||||
Typ: TFABBlockType): TFABPolicies;
|
ParentTyp, Typ: TFABBlockType): TFABPolicies;
|
||||||
var
|
var
|
||||||
CodeBuffers: TFPList;
|
CodeBuffers: TFPList;
|
||||||
i: Integer;
|
i: Integer;
|
||||||
@ -941,6 +953,7 @@ var
|
|||||||
Stack: TFABBlockStack;
|
Stack: TFABBlockStack;
|
||||||
Policies: TFABPolicies;
|
Policies: TFABPolicies;
|
||||||
LastAtomStart, LastAtomEnd: integer;
|
LastAtomStart, LastAtomEnd: integer;
|
||||||
|
ParentBlock: TBlock;
|
||||||
begin
|
begin
|
||||||
Result:=false;
|
Result:=false;
|
||||||
FillByte(Indent,SizeOf(Indent),0);
|
FillByte(Indent,SizeOf(Indent),0);
|
||||||
@ -948,6 +961,8 @@ begin
|
|||||||
CleanPos:=FindStartOfAtom(Source,CleanPos);
|
CleanPos:=FindStartOfAtom(Source,CleanPos);
|
||||||
if CleanPos<1 then exit;
|
if CleanPos<1 then exit;
|
||||||
|
|
||||||
|
Block:=CleanBlock;
|
||||||
|
ParentBlock:=CleanBlock;
|
||||||
Policies:=TFABPolicies.Create;
|
Policies:=TFABPolicies.Create;
|
||||||
Stack:=TFABBlockStack.Create;
|
Stack:=TFABBlockStack.Create;
|
||||||
try
|
try
|
||||||
@ -959,23 +974,26 @@ begin
|
|||||||
if (Stack.Top<0) then begin
|
if (Stack.Top<0) then begin
|
||||||
// no context
|
// no context
|
||||||
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: no context']);
|
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: no context']);
|
||||||
GetDefaultIndent(Source,CleanPos,NewNestedComments,Indent);
|
GetDefaultSrcIndent(Source,CleanPos,NewNestedComments,Indent);
|
||||||
exit;
|
exit;
|
||||||
end;
|
end;
|
||||||
if (LastAtomStart>0) and (CleanPos>LastAtomStart) then begin
|
if (LastAtomStart>0) and (CleanPos>LastAtomStart) then begin
|
||||||
// in comment or atom
|
// in comment or atom
|
||||||
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: position in middle of atom, e.g. comment']);
|
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: position in middle of atom, e.g. comment']);
|
||||||
GetDefaultIndent(Source,CleanPos,NewNestedComments,Indent);
|
GetDefaultSrcIndent(Source,CleanPos,NewNestedComments,Indent);
|
||||||
exit;
|
exit;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Block:=Stack.Stack[Stack.Top];
|
Block:=Stack.Stack[Stack.Top];
|
||||||
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: context=',FABBlockTypeNames[Block.Typ],' blockindent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
|
|
||||||
|
|
||||||
|
if Stack.Top>0 then
|
||||||
|
ParentBlock:=Stack.Stack[Stack.Top-1];
|
||||||
|
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed code in front: context=',FABBlockTypeNames[ParentBlock.Typ],'/',FABBlockTypeNames[Block.Typ],' blockindent=',GetLineIndentWithTabs(Source,Block.StartPos,DefaultTabWidth)]);
|
||||||
if CheckPolicies(Policies,Result) then exit;
|
if CheckPolicies(Policies,Result) then exit;
|
||||||
|
|
||||||
// parse source behind
|
// parse source behind
|
||||||
ParseSource(Source,CleanPos,length(Source)+1,NewNestedComments,Stack,Policies);
|
ParseSource(Source,CleanPos,length(Source)+1,NewNestedComments,Stack,Policies);
|
||||||
|
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed source behind']);
|
||||||
if CheckPolicies(Policies,Result) then exit;
|
if CheckPolicies(Policies,Result) then exit;
|
||||||
|
|
||||||
finally
|
finally
|
||||||
@ -984,11 +1002,14 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
// parse examples
|
// parse examples
|
||||||
Policies:=FindPolicyInExamples(nil,Block.Typ);
|
Policies:=FindPolicyInExamples(nil,ParentBlock.Typ,Block.Typ);
|
||||||
|
DebugLn(['TFullyAutomaticBeautifier.GetIndent parsed examples']);
|
||||||
if CheckPolicies(Policies,Result) then exit;
|
if CheckPolicies(Policies,Result) then exit;
|
||||||
|
|
||||||
|
//GetDefaultIndentPolicy(ParentBlock.Typ,Block.Typ);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TFullyAutomaticBeautifier.GetDefaultIndent(const Source: string;
|
procedure TFullyAutomaticBeautifier.GetDefaultSrcIndent(const Source: string;
|
||||||
CleanPos: integer; NewNestedComments: boolean; out
|
CleanPos: integer; NewNestedComments: boolean; out
|
||||||
Indent: TFABIndentationPolicy);
|
Indent: TFABIndentationPolicy);
|
||||||
// return indent of last non empty line
|
// return indent of last non empty line
|
||||||
@ -1024,6 +1045,62 @@ begin
|
|||||||
// only empty lines in front
|
// only empty lines in front
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TFullyAutomaticBeautifier.GetDefaultIndentPolicy(Typ,
|
||||||
|
SubTyp: TFABBlockType; out Indent: TFABIndentationPolicy);
|
||||||
|
begin
|
||||||
|
Indent.IndentValid:=false;
|
||||||
|
Indent.Indent:=0;
|
||||||
|
case Typ of
|
||||||
|
bbtInterface,
|
||||||
|
bbtImplementation,
|
||||||
|
bbtInitialization,
|
||||||
|
bbtFinalization,
|
||||||
|
bbtClass,
|
||||||
|
bbtClassInterface,
|
||||||
|
bbtProcedure,
|
||||||
|
bbtFunction,
|
||||||
|
bbtCase,
|
||||||
|
bbtCaseOf,
|
||||||
|
bbtIf:
|
||||||
|
begin
|
||||||
|
Indent.Indent:=0;
|
||||||
|
Indent.IndentValid:=true;
|
||||||
|
end;
|
||||||
|
bbtUsesSection,
|
||||||
|
bbtTypeSection,
|
||||||
|
bbtConstSection,
|
||||||
|
bbtVarSection,
|
||||||
|
bbtResourceStringSection,
|
||||||
|
bbtLabelSection,
|
||||||
|
bbtRecord,
|
||||||
|
bbtClassSection,
|
||||||
|
bbtMainBegin,
|
||||||
|
bbtCommentaryBegin,
|
||||||
|
bbtRepeat,
|
||||||
|
bbtProcedureBegin,
|
||||||
|
bbtCaseColon,
|
||||||
|
bbtCaseBegin,
|
||||||
|
bbtCaseElse,
|
||||||
|
bbtTry,
|
||||||
|
bbtFinally,
|
||||||
|
bbtExcept,
|
||||||
|
bbtIfBegin:
|
||||||
|
begin
|
||||||
|
Indent.Indent:=2;
|
||||||
|
Indent.IndentValid:=true;
|
||||||
|
end;
|
||||||
|
bbtIfThen,
|
||||||
|
bbtIfElse:
|
||||||
|
if SubTyp=bbtIfBegin then begin
|
||||||
|
Indent.Indent:=0;
|
||||||
|
Indent.IndentValid:=true;
|
||||||
|
end else begin
|
||||||
|
Indent.Indent:=2;
|
||||||
|
Indent.IndentValid:=true;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
{ TFABPolicies }
|
{ TFABPolicies }
|
||||||
|
|
||||||
constructor TFABPolicies.Create;
|
constructor TFABPolicies.Create;
|
||||||
|
Loading…
Reference in New Issue
Block a user