mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-22 01:59:12 +02:00
tests: GuessIndentSize
git-svn-id: trunk@40793 -
This commit is contained in:
parent
09f8f9e4f4
commit
925530b425
@ -91,8 +91,8 @@ function FindFirstNonSpaceCharInLine(const Source: string;
|
|||||||
Position: integer): integer;
|
Position: integer): integer;
|
||||||
function IsFirstNonSpaceCharInLine(const Source: string;
|
function IsFirstNonSpaceCharInLine(const Source: string;
|
||||||
Position: integer): boolean;
|
Position: integer): boolean;
|
||||||
procedure GuessIndentSize(const Source: string; TabWidth: integer;
|
procedure GuessIndentSize(const Source: string;
|
||||||
var IndentSize: integer; MaxLineCount: integer = 10000);
|
var IndentSize: integer; TabWidth: integer = 2; MaxLineCount: integer = 10000);
|
||||||
|
|
||||||
// identifiers
|
// identifiers
|
||||||
procedure GetIdentStartEndAtPosition(const Source:string; Position:integer;
|
procedure GetIdentStartEndAtPosition(const Source:string; Position:integer;
|
||||||
@ -2633,8 +2633,8 @@ begin
|
|||||||
Result:=(Position=1) or (Source[Position-1] in [#10,#13]);
|
Result:=(Position=1) or (Source[Position-1] in [#10,#13]);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure GuessIndentSize(const Source: string; TabWidth: integer;
|
procedure GuessIndentSize(const Source: string; var IndentSize: integer;
|
||||||
var IndentSize: integer; MaxLineCount: integer = 10000);
|
TabWidth: integer; MaxLineCount: integer = 10000);
|
||||||
{ check all line indents and return the most common.
|
{ check all line indents and return the most common.
|
||||||
Stop after MaxLineCount lines. Ignore empty lines.
|
Stop after MaxLineCount lines. Ignore empty lines.
|
||||||
}
|
}
|
||||||
@ -2660,8 +2660,8 @@ var
|
|||||||
var
|
var
|
||||||
LineNumber: Integer;
|
LineNumber: Integer;
|
||||||
p: PChar;
|
p: PChar;
|
||||||
CurIndent: Integer;
|
CurLineIndent: Integer;
|
||||||
LastIndent: Integer;
|
LastLineIndent: Integer;
|
||||||
begin
|
begin
|
||||||
LineNumber:=0;
|
LineNumber:=0;
|
||||||
if Source='' then exit;
|
if Source='' then exit;
|
||||||
@ -2671,26 +2671,27 @@ begin
|
|||||||
FillByte(IndentCounts[0],SizeOf(SizeInt)*MaxIndentSize,0);
|
FillByte(IndentCounts[0],SizeOf(SizeInt)*MaxIndentSize,0);
|
||||||
BestCount:=0;
|
BestCount:=0;
|
||||||
p:=PChar(Source);
|
p:=PChar(Source);
|
||||||
LastIndent:=0;
|
LastLineIndent:=0;
|
||||||
repeat
|
repeat
|
||||||
inc(LineNumber);
|
inc(LineNumber);
|
||||||
// read indent
|
// read indent
|
||||||
CurIndent:=0;
|
CurLineIndent:=0;
|
||||||
repeat
|
repeat
|
||||||
case p^ of
|
case p^ of
|
||||||
' ': inc(CurIndent);
|
' ': inc(CurLineIndent);
|
||||||
#9:
|
#9:
|
||||||
begin
|
begin
|
||||||
CurIndent+=TabWidth;
|
CurLineIndent+=TabWidth;
|
||||||
CurIndent-=(CurIndent mod TabWidth);
|
CurLineIndent-=(CurLineIndent mod TabWidth);
|
||||||
end;
|
end;
|
||||||
else break;
|
else break;
|
||||||
end;
|
end;
|
||||||
|
inc(p);
|
||||||
until false;
|
until false;
|
||||||
if not (p^ in [#0,#10,#13]) then begin
|
if not (p^ in [#0,#10,#13]) then begin
|
||||||
// not an empty line
|
// not an empty line
|
||||||
AddIndent(CurIndent-LastIndent);
|
AddIndent(CurLineIndent-LastLineIndent);
|
||||||
LastIndent:=CurIndent;
|
LastLineIndent:=CurLineIndent;
|
||||||
end;
|
end;
|
||||||
// skip to next line
|
// skip to next line
|
||||||
repeat
|
repeat
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
./runtests --format=plain --suite=TestBasicFindCommentEnd
|
./runtests --format=plain --suite=TestBasicFindCommentEnd
|
||||||
./runtests --format=plain --suite=TestBasicFindNextComment
|
./runtests --format=plain --suite=TestBasicFindNextComment
|
||||||
./runtests --format=plain --suite=TestCompareTextIgnoringSpace
|
./runtests --format=plain --suite=TestCompareTextIgnoringSpace
|
||||||
|
./runtests --format=plain --suite=TestGuessIndentSize
|
||||||
}
|
}
|
||||||
unit TestBasicCodetools;
|
unit TestBasicCodetools;
|
||||||
|
|
||||||
@ -27,6 +28,7 @@ type
|
|||||||
procedure TestBasicFindCommentEnd;
|
procedure TestBasicFindCommentEnd;
|
||||||
procedure TestBasicFindNextComment;
|
procedure TestBasicFindNextComment;
|
||||||
procedure TestCompareTextIgnoringSpace;
|
procedure TestCompareTextIgnoringSpace;
|
||||||
|
procedure TestGuessIndentSize;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
@ -164,6 +166,28 @@ begin
|
|||||||
'procedure TCustomSynEdit.LineCountChanged(Sender: TSynEditStrings; AIndex, ACount: Integer);',0);
|
'procedure TCustomSynEdit.LineCountChanged(Sender: TSynEditStrings; AIndex, ACount: Integer);',0);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TTestBasicCodeTools.TestGuessIndentSize;
|
||||||
|
|
||||||
|
procedure t(Src: string; ExpectedIndent: integer; DefaultIndent: integer = 2; TabWidth: integer = 2);
|
||||||
|
var
|
||||||
|
ActualIndent: Integer;
|
||||||
|
begin
|
||||||
|
ActualIndent:=DefaultIndent;
|
||||||
|
GuessIndentSize(Src,ActualIndent,TabWidth);
|
||||||
|
if ActualIndent=ExpectedIndent then exit;
|
||||||
|
AssertEquals('"'+DbgStr(Src)+'"',ExpectedIndent,ActualIndent);
|
||||||
|
end;
|
||||||
|
|
||||||
|
begin
|
||||||
|
t('',2,2);
|
||||||
|
t(' a',1);
|
||||||
|
t(' a',2);
|
||||||
|
t(' a',3);
|
||||||
|
t(' a'#10'b',2);
|
||||||
|
t(' a'#10#13' b',2);
|
||||||
|
t(' a'#10' b'#10' c',2);
|
||||||
|
end;
|
||||||
|
|
||||||
initialization
|
initialization
|
||||||
AddToCodetoolsTestSuite(TTestBasicCodeTools);
|
AddToCodetoolsTestSuite(TTestBasicCodeTools);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user