mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-14 14:19:22 +02:00
codetools: indentations: considering tabs
git-svn-id: trunk@20297 -
This commit is contained in:
parent
bb721d48bf
commit
8ac8d4e9f9
@ -69,6 +69,8 @@ function FindMainUnitHint(const ASource: string; out Filename: string): boolean;
|
|||||||
procedure GetLineStartEndAtPosition(const Source:string; Position:integer;
|
procedure GetLineStartEndAtPosition(const Source:string; Position:integer;
|
||||||
var LineStart,LineEnd:integer);
|
var LineStart,LineEnd:integer);
|
||||||
function GetLineIndent(const Source: string; Position: integer): integer;
|
function GetLineIndent(const Source: string; Position: integer): integer;
|
||||||
|
function GetLineIndentWithTabs(const Source: string; Position: integer;
|
||||||
|
TabWidth: integer): integer;
|
||||||
function GetPosInLine(const Source: string; Position: integer): integer;
|
function GetPosInLine(const Source: string; Position: integer): integer;
|
||||||
function GetBlockMinIndent(const Source: string;
|
function GetBlockMinIndent(const Source: string;
|
||||||
StartPos, EndPos: integer): integer;
|
StartPos, EndPos: integer): integer;
|
||||||
@ -2248,10 +2250,8 @@ begin
|
|||||||
if (LineStart<0) then LineStart:=1;
|
if (LineStart<0) then LineStart:=1;
|
||||||
if (LineStart>length(Source)+1) then LineStart:=length(Source)+1;
|
if (LineStart>length(Source)+1) then LineStart:=length(Source)+1;
|
||||||
// search beginning of line
|
// search beginning of line
|
||||||
repeat
|
while (LineStart>1) and not (Source[LineStart-1] in [#10,#13]) do
|
||||||
dec(LineStart);
|
dec(LineStart);
|
||||||
until (LineStart<1) or (Source[LineStart] in [#10,#13]);
|
|
||||||
inc(LineStart);
|
|
||||||
// search code
|
// search code
|
||||||
Result:=LineStart;
|
Result:=LineStart;
|
||||||
while (Result<=length(Source)) and (Source[Result]=' ') do inc(Result);
|
while (Result<=length(Source)) and (Source[Result]=' ') do inc(Result);
|
||||||
@ -3724,6 +3724,34 @@ begin
|
|||||||
until false;
|
until false;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function GetLineIndentWithTabs(const Source: string; Position: integer;
|
||||||
|
TabWidth: integer): integer;
|
||||||
|
var p: integer;
|
||||||
|
begin
|
||||||
|
Result:=0;
|
||||||
|
p:=Position;
|
||||||
|
if p=0 then exit;
|
||||||
|
if (p<0) then p:=1;
|
||||||
|
if (p>length(Source)+1) then p:=length(Source)+1;
|
||||||
|
// search beginning of line
|
||||||
|
while (p>1) and not (Source[p-1] in [#10,#13]) do
|
||||||
|
dec(p);
|
||||||
|
// search code
|
||||||
|
Result:=0;
|
||||||
|
while (p<=length(Source)) do begin
|
||||||
|
case Source[p] of
|
||||||
|
' ': inc(Result);
|
||||||
|
#9:
|
||||||
|
begin
|
||||||
|
Result:=Result+TabWidth;
|
||||||
|
Result:=Result-(Result mod TabWidth);
|
||||||
|
end;
|
||||||
|
else break;
|
||||||
|
end;
|
||||||
|
inc(p);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
function GetPosInLine(const Source: string; Position: integer): integer;
|
function GetPosInLine(const Source: string; Position: integer): integer;
|
||||||
begin
|
begin
|
||||||
Result:=0;
|
Result:=0;
|
||||||
|
@ -124,10 +124,8 @@ type
|
|||||||
out CodeBuffers: TFPList) of object;
|
out CodeBuffers: TFPList) of object;
|
||||||
|
|
||||||
TFABIndentationPolicy = record
|
TFABIndentationPolicy = record
|
||||||
IndentBefore: integer;
|
Indent: integer;
|
||||||
IndentBeforeValid: boolean;
|
IndentValid: boolean;
|
||||||
IndentAfter: integer;
|
|
||||||
IndentAfterValid: boolean;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TFABPolicies }
|
{ TFABPolicies }
|
||||||
@ -150,6 +148,7 @@ type
|
|||||||
NestedComments: boolean; Policies: TFABPolicies);
|
NestedComments: boolean; Policies: TFABPolicies);
|
||||||
procedure FindPolicies(Types: TFABBlockTypes; Policies: TFABPolicies);
|
procedure FindPolicies(Types: TFABBlockTypes; Policies: TFABPolicies);
|
||||||
public
|
public
|
||||||
|
DefaultTabWidth: integer;
|
||||||
constructor Create;
|
constructor Create;
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
procedure Clear;
|
procedure Clear;
|
||||||
@ -344,23 +343,10 @@ var
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
procedure EndBlock;
|
procedure EndBlock;
|
||||||
var
|
|
||||||
Block: PBlock;
|
|
||||||
begin
|
begin
|
||||||
{$IFDEF ShowCodeBeautifierParser}
|
{$IFDEF ShowCodeBeautifierParser}
|
||||||
DebugLn([GetIndentStr(Stack.Top*2),'EndBlock ',FABBlockTypeNames[Stack.TopType],' ',GetAtomString(@Src[AtomStart],NestedComments),' at ',PosToStr(p)]);
|
DebugLn([GetIndentStr(Stack.Top*2),'EndBlock ',FABBlockTypeNames[Stack.TopType],' ',GetAtomString(@Src[AtomStart],NestedComments),' at ',PosToStr(p)]);
|
||||||
{$ENDIF}
|
{$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;
|
Stack.EndBlock;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -438,19 +424,22 @@ begin
|
|||||||
if (Stack.Top>=0) then begin
|
if (Stack.Top>=0) then begin
|
||||||
Block:=@Stack.Stack[Stack.Top];
|
Block:=@Stack.Stack[Stack.Top];
|
||||||
if (Policies<>nil)
|
if (Policies<>nil)
|
||||||
and (not Policies.Indentations[Block^.Typ].IndentAfterValid) then begin
|
and (not Policies.Indentations[Block^.Typ].IndentValid) then begin
|
||||||
// set block InnerIdent
|
// set block InnerIdent
|
||||||
if (Block^.InnerIdent<0)
|
if (Block^.InnerIdent<0)
|
||||||
and (not PositionsInSameLine(Src,Block^.StartPos,AtomStart)) then begin
|
and (not PositionsInSameLine(Src,Block^.StartPos,AtomStart)) then begin
|
||||||
Block^.InnerIdent:=GetLineIndent(Src,AtomStart);
|
Block^.InnerIdent:=GetLineIndentWithTabs(Src,AtomStart,DefaultTabWidth);
|
||||||
if Block^.Typ in [bbtIfThen,bbtIfElse] then
|
if Block^.Typ in [bbtIfThen,bbtIfElse] then
|
||||||
Indent:=Block^.InnerIdent-GetLineIndent(Src,Stack.Stack[Stack.Top-1].StartPos)
|
Indent:=Block^.InnerIdent
|
||||||
|
-GetLineIndentWithTabs(Src,Stack.Stack[Stack.Top-1].StartPos,
|
||||||
|
DefaultTabWidth)
|
||||||
else
|
else
|
||||||
Indent:=Block^.InnerIdent-GetLineIndent(Src,Block^.StartPos);
|
Indent:=Block^.InnerIdent
|
||||||
Policies.Indentations[Block^.Typ].IndentAfter:=Indent;
|
-GetLineIndentWithTabs(Src,Block^.StartPos,DefaultTabWidth);
|
||||||
Policies.Indentations[Block^.Typ].IndentAfterValid:=true;
|
Policies.Indentations[Block^.Typ].Indent:=Indent;
|
||||||
|
Policies.Indentations[Block^.Typ].IndentValid:=true;
|
||||||
{$IFDEF ShowCodeBeautifierParser}
|
{$IFDEF ShowCodeBeautifierParser}
|
||||||
DebugLn([GetIndentStr(Stack.Top*2),'Indentation learned: ',FABBlockTypeNames[Block^.Typ],' IndentAfter=',Policies.Indentations[Block^.Typ].IndentAfter]);
|
DebugLn([GetIndentStr(Stack.Top*2),'Indentation learned: ',FABBlockTypeNames[Block^.Typ],' Indent=',Policies.Indentations[Block^.Typ].Indent]);
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
@ -736,7 +725,7 @@ end;
|
|||||||
|
|
||||||
constructor TFullyAutomaticBeautifier.Create;
|
constructor TFullyAutomaticBeautifier.Create;
|
||||||
begin
|
begin
|
||||||
|
DefaultTabWidth:=4;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TFullyAutomaticBeautifier.Destroy;
|
destructor TFullyAutomaticBeautifier.Destroy;
|
||||||
|
@ -51,7 +51,7 @@ begin
|
|||||||
end else begin
|
end else begin
|
||||||
Filename:=ExpandFileNameUTF8('scanexamples/indentation.pas');
|
Filename:=ExpandFileNameUTF8('scanexamples/indentation.pas');
|
||||||
X:=3;
|
X:=3;
|
||||||
Y:=73;
|
Y:=74;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// load the example unit
|
// load the example unit
|
||||||
@ -67,8 +67,7 @@ begin
|
|||||||
exit;
|
exit;
|
||||||
end;
|
end;
|
||||||
if FAB.GetIndent(Code.Source,p,true,Indentation) then begin
|
if FAB.GetIndent(Code.Source,p,true,Indentation) then begin
|
||||||
writeln('IndentAfter=',Indentation.IndentAfter);
|
writeln('Indent=',Indentation.Indent);
|
||||||
writeln('IndentBefore=',Indentation.IndentBefore);
|
|
||||||
end else begin
|
end else begin
|
||||||
writeln('Error: GetIndent failed');
|
writeln('Error: GetIndent failed');
|
||||||
end;
|
end;
|
||||||
|
Loading…
Reference in New Issue
Block a user