improved invert assignment tool for comments from Andrew Haines

git-svn-id: trunk@6639 -
This commit is contained in:
mattias 2005-01-19 08:17:03 +00:00
parent 03115ff293
commit 9775b31e86

View File

@ -46,26 +46,54 @@ begin
Result :=Length(Aline) - Length(TrimLeft(ALine)); Result :=Length(Aline) - Length(TrimLeft(ALine));
end; end;
procedure DivideLines(Lines: TStrings; var AList: TStrings; var BList: TStrings); procedure DivideLines(Lines: TStrings; var PreList, AList, BList, PostList: TStrings);
var var
X: Integer; X: Integer;
ALine: String; ALine: String;
fPos: Integer; EqPos: Integer;
SemiPos: Integer;
WordBeforeEqPos: Integer;
TrueFalse: String; TrueFalse: String;
function FindWordBeforeEquals(ALine: String): Integer;
var
X: Integer;
fPos: Integer;
begin
Result := 0;
fPos := Pos(':=', ALine);
if fPos > 0 then begin
ALine := Trim(Copy(ALine,1,fPos-1));
for X := Length(ALine) downto 1 do begin
if ALine[X] = ' ' then begin
Result := X+1;
Exit;
end;
end;
end;
end;
begin begin
AList.Clear; AList.Clear;
BList.Clear; BList.Clear;
for X := 0 to Lines.Count-1 do begin for X := 0 to Lines.Count-1 do begin
ALine := Trim(Lines.Strings[X]); ALine := Trim(Lines.Strings[X]);
fPos := Pos(':=', ALine); EqPos := Pos(':=', ALine);
if (fPos > 0) and (Pos(';',ALine) > 0) then begin SemiPos := Pos(';', ALine);
AList.Add(Trim(Copy(ALine, 1, fPos-1))); WordBeforeEqPos := FindWordBeforeEquals(ALine);
BList.Add(Trim(Copy(ALine, fPos+2, (Pos(';', ALine)-3)-(fPos-1))));
if (EqPos > 0) and (SemiPos > 0) then begin
Alist.Add(Trim(Copy(ALine, WordBeforeEqPos+Ord(WordBeforeEqPos=0), EqPos - (WordBeforeEqPos+Ord(WordBeforeEqPos=0)))));
BList.Add(Trim(Copy(ALine, EqPos + 2, (SemiPos-1) -(EqPos+1))));
PreList.Add(Trim(Copy(ALine,1, WordBeforeEqPos-1)));
PostList.Add(Trim(Copy(ALine, SemiPos, Length(ALine)-(SemiPos-1))));
if Length(PreList.Strings[X]) > 0 then
PreList.Strings[X] := PreList.Strings[X] + ' ';
end end
else begin // not a valid line else begin // not a valid line
PreList.Add('');
AList.Add(ALine); AList.Add(ALine);
Blist.Add(''); Blist.Add('');
PostList.Add('');
end; end;
// Check if is being assigned true or false // Check if is being assigned true or false
if CompareText(BList.Strings[X], 'True') = 0 then begin if CompareText(BList.Strings[X], 'True') = 0 then begin
@ -82,7 +110,7 @@ begin
end; end;
function InvertLine(VarA, VarB: String; LineStart, EqualPosition: Integer): String; function InvertLine(PreVar, VarA, VarB, PostVar: String; LineStart, EqualPosition: Integer): String;
var var
fLength: Integer; fLength: Integer;
X: Integer; X: Integer;
@ -99,7 +127,7 @@ begin
Exit; Exit;
end; end;
Result := Result + VarB; Result := Result + PreVar + VarB;
fLength := Length(Trim(Result)); fLength := Length(Trim(Result));
if fLength < EqualPosition then begin if fLength < EqualPosition then begin
@ -107,7 +135,7 @@ begin
Result := Result + ' '; Result := Result + ' ';
end; end;
end; end;
Result := Result + ' := ' + VarA + ';'; Result := Result + ' := ' + VarA + PostVar;
end; end;
function IsAWholeLine(ALine: String): Boolean; function IsAWholeLine(ALine: String): Boolean;
@ -136,6 +164,7 @@ begin
or (Pos('else', ALine) > 0) or (Pos('else', ALine) > 0)
or (Pos('and', ALine) > 0) or (Pos('and', ALine) > 0)
or (Pos('or', ALine) > 0) or (Pos('or', ALine) > 0)
or (Pos('//', ALine) > 0)
then Result := True; then Result := True;
end; end;
@ -146,15 +175,22 @@ end;
function InvertAssignment(ALines:TStrings):TStrings; function InvertAssignment(ALines:TStrings):TStrings;
var var
Lines: TStringList; Lines: TStringList;
PreList,
AList, AList,
BList: TStringList; BList,
Indents: ^Integer; PostList: TStringList;
Indents: PInteger;
X, Y: Integer; X, Y: Integer;
EqPos: Integer; EqPos: Integer;
ALine: String; ALine: String;
begin begin
if ALines.Count = 0 then begin
Result := ALines;
Exit;
end;
Lines := TStringList.Create; Lines := TStringList.Create;
if ALines.Count>0 then begin
GetMem(Indents,SizeOf(Integer)*ALines.Count); GetMem(Indents,SizeOf(Integer)*ALines.Count);
// Put a line on multiple lines, on one line // Put a line on multiple lines, on one line
@ -174,12 +210,13 @@ begin
end; end;
ALines.Clear; ALines.Clear;
end;
PreList := TStringList.Create;
AList := TStringList.Create; AList := TStringList.Create;
BList := TStringList.Create; BList := TStringList.Create;
PostList := TStringList.Create;
DivideLines(Lines, AList, BList); DivideLines(Lines, PreList, AList, BList, PostList);
Lines.Free; Lines.Free;
//Find where the ':=' should be //Find where the ':=' should be
@ -190,13 +227,20 @@ begin
end; end;
for X := 0 to AList.Count-1 do begin for X := 0 to AList.Count-1 do begin
ALines.Add(InvertLine(Alist.Strings[X], BList.Strings[X], Indents[X], EqPos)); ALines.Add(InvertLine(PreList.Strings[X],
Alist.Strings[X],
BList.Strings[X],
PostList.Strings[X],
Indents[X],
EqPos));
end; end;
PreList.Free;
AList.Free; AList.Free;
BList.Free; BList.Free;
PostList.Free;
ReAllocMem(Indents,0);
Result := ALines; Result := ALines;
ReAllocMem(Indents,0);
// TODO: How do you stop this from adding a new line at the end of the last item // TODO: How do you stop this from adding a new line at the end of the last item
end; end;
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////