-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAlignStringsUnit.pas
168 lines (144 loc) · 4.42 KB
/
AlignStringsUnit.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
unit AlignStringsUnit;
interface
uses
Winapi.Windows, System.SysUtils, System.Classes, System.Math, ConstUnit;
type
TAlignStrings = class(TStringList)
private
procedure AlignDeclarations(const StartRow: Integer);
procedure AlignVars(const StartRow: Integer; AlignColumn: Integer = 0);
procedure AlignFunction(const StartRow: Integer; KeyWord: string);
public
procedure AlignFrom(const StartRow: Integer; KeyWord: string);
end;
implementation
{ TAlignStrings }
procedure TAlignStrings.AlignFrom(const StartRow: Integer; KeyWord: string);
begin
if KeyWord = cnstAlignStartWords[0] then AlignDeclarations(StartRow)
else
if KeyWord = cnstAlignStartWords[3] then
AlignVars(StartRow)
else
AlignFunction(StartRow,KeyWord);
end;
procedure TAlignStrings.AlignDeclarations(const StartRow: Integer);
var
iStartPos, iMax, iLen, iLenTotal, i: Integer;
Line, S, Variables: string;
begin
iMax := 0;
iStartPos := Length(cnstAlignStartWords[0]) + 4;
for i := StartRow to Count-1 do
begin
Line := String(WholeWord(AnsiString(Strings[i]),1));
iLen := Line.Length;
if (iLen>0) and (Line[1] = '@') then
iMax := Max(iMax,iLen);
end;
if iMax > 0 then
begin
iLenTotal := 0;
Variables := '';
for i := StartRow to Count-1 do
begin
if i = StartRow then
Strings[i] := StringReplace(Strings[i],cnstAlignStartWords[0],'',[rfIgnoreCase]);
S := Strings[i];
Line := String(WholeWord(AnsiString(S),1));
iLen := Line.Length;
if (iLen>0) and (Line[1] = '@') then
begin
if Pos(Line+',',Variables) = 0 then
begin
Variables := Variables + Line + ',';
S := StringReplace(RemoveComments(S),',','',[]);
S := StringReplace(S,Line,'',[rfIgnoreCase]).Trim;
Strings[i] := Line.PadRight(iMax).PadLeft(iStartPos+iMax-1) + ' ' + S;
iLenTotal := Max(iLenTotal,Strings[i].Length);
end
else
if (Pos('--',Strings[i]) = 0) then
Strings[i] := '--' + Strings[i];
end;
end;
if iLenTotal > 0 then
begin
for i := StartRow to Count-1 do
begin
if i = StartRow then
begin
Strings[i] := ' ' + cnstAlignStartWords[0] + ' ' + Strings[i].TrimLeft;
end;
if (Pos('@',Strings[i]) > 0) and (Pos('--',Strings[i]) = 0) then
Strings[i] := Strings[i].PadRight(iLenTotal) + ',';
end;
for i := Count-1 downto StartRow do
if (Pos('@',Strings[i]) > 0) and (Pos('--',Strings[i]) = 0) then
begin
Strings[i] := StringReplace(Strings[i],',','',[]);
Break;
end;
end;
end;
end;
procedure TAlignStrings.AlignFunction(const StartRow: Integer; KeyWord: string);
var
Words: TStringList;
AlignColumn, Cnt: Integer;
begin
if StartRow+1 < Count then
begin
Words := WholeWords(Strings[StartRow]);
if Assigned(Words) then
try
Cnt := Words.Count;
case Cnt of
2 : AlignColumn := Pos(Words[1],Strings[StartRow])+2;
3..255 : AlignColumn := Pos(Words[2],Strings[StartRow])+2;
else
AlignColumn := 0;
end;
finally
Words.Free;
end;
AlignVars(StartRow+1, AlignColumn);
end;
end;
procedure TAlignStrings.AlignVars(const StartRow: Integer; AlignColumn: Integer = 0);
var
iStartPos, iMax, iLen, i: Integer;
Line, S: string;
begin
iMax := 0;
if AlignColumn = 0 then
iStartPos := Pos(cnstAlignStartWords[3],Strings[StartRow])
else
iStartPos := AlignColumn;
for i := StartRow to Count-1 do
begin
Line := String(WholeWord(AnsiString(Strings[i]),1));
iLen := Line.Length;
if (iLen>0) and (Line[1] = '@') then
iMax := Max(iMax,iLen);
end;
if iMax > 0 then
begin
for i := StartRow to Count-1 do
begin
S := Strings[i];
Line := String(WholeWord(AnsiString(S),1));
iLen := Line.Length;
if (iLen>0) and (Line[1] = '@') then
begin
S := StringReplace(S,Line,'',[rfIgnoreCase]).Trim;
iLen := Pos('=',S);
if iLen = 1 then
Strings[i] := Line.PadRight(iMax).PadLeft(iStartPos+iMax-1) + ' = ' + Copy(S,iLen+1,MaxInt).Trim
else
Strings[i] := Line.PadRight(iMax).PadLeft(iStartPos+iMax-1) + ' ' + S;
end;
end;
end;
end;
end.