-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGDK.ToolsAPI.UsesBuilder.pas
206 lines (162 loc) · 5.01 KB
/
GDK.ToolsAPI.UsesBuilder.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
unit GDK.ToolsAPI.UsesBuilder;
interface
uses
GDK.ToolsAPI.UsesManager,
System.SysUtils;
type
IUsesToWriteResult = interface
['{270998A3-D930-459D-8926-75C6673D28A5}']
{$REGION 'Getters and setters'}
function GetText: string;
procedure SetText(const Value: string);
function GetPosition: Integer;
procedure SetPosition(const Value: Integer);
{$ENDREGION}
function CanWrite: Boolean;
property Text: string read GetText write SetText;
property Position: Integer read GetPosition write SetPosition;
end;
IToolsAPIUsesBuilder = interface
['{CA44B1AA-BBC0-4C9B-A73C-FEAEC60138B9}']
function InInterface: IToolsAPIUsesBuilder;
function InImplementation: IToolsAPIUsesBuilder;
function WithSource(const Source: string): IToolsAPIUsesBuilder;
function Build(const UnitNames: TArray<string>): IUsesToWriteResult;
end;
TToolsAPIUsesBuilder = class(TInterfacedObject, IToolsAPIUsesBuilder)
const
UnitNames_Separator = ', ';
private
FUsesManager: IToolsApiUsesManager;
FInImplementation: Boolean;
FUnitNamesToWrite: TArray<string>;
FPosition: Integer;
FIsEmptyUses: Boolean;
procedure DoFindPositionToAdd(const UnitName: string);
procedure HandleOnPositionFound(const Position: Integer; const IsEmptyUses: Boolean);
procedure AddUnitNameToWrite(const UnitName: string);
function ComposeUsesToWrite: IUsesToWriteResult;
procedure ResetFields;
public
class function Use: IToolsAPIUsesBuilder;
constructor Create(const UsesManager: IToolsApiUsesManager);
function InInterface: IToolsAPIUsesBuilder;
function InImplementation: IToolsAPIUsesBuilder;
function WithSource(const Source: string): IToolsAPIUsesBuilder;
function Build(const UnitNames: TArray<string>): IUsesToWriteResult;
end;
TUsesToWriteResult = class(TInterfacedObject, IUsesToWriteResult)
strict private
FPosition: Integer;
FText: string;
public
{$REGION 'Getters and setters'}
function GetText: string;
procedure SetText(const Value: string);
function GetPosition: Integer;
procedure SetPosition(const Value: Integer);
{$ENDREGION}
function CanWrite: Boolean;
property Text: string read GetText write SetText;
property Position: Integer read GetPosition write SetPosition;
end;
implementation
{ TToolsAPIUsesBuilder }
class function TToolsAPIUsesBuilder.Use: IToolsAPIUsesBuilder;
begin
Result := TToolsAPIUsesBuilder.Create(TToolsApiUsesManager.Use);
end;
constructor TToolsAPIUsesBuilder.Create(const UsesManager: IToolsApiUsesManager);
begin
inherited Create;
FUsesManager := UsesManager;
ResetFields;
end;
function TToolsAPIUsesBuilder.InImplementation: IToolsAPIUsesBuilder;
begin
FInImplementation := True;
Result := Self;
end;
function TToolsAPIUsesBuilder.InInterface: IToolsAPIUsesBuilder;
begin
FInImplementation := False;
Result := Self;
end;
function TToolsAPIUsesBuilder.WithSource(const Source: string): IToolsAPIUsesBuilder;
begin
FUsesManager.WithSource(Source);
Result := Self;
end;
function TToolsAPIUsesBuilder.Build(const UnitNames: TArray<string>): IUsesToWriteResult;
begin
try
for var UnitName in UnitNames do
DoFindPositionToAdd(UnitName);
Result := ComposeUsesToWrite;
finally
ResetFields;
end;
end;
procedure TToolsAPIUsesBuilder.DoFindPositionToAdd(const UnitName: string);
begin
FUsesManager.FindPositionToAdd(UnitName, FInImplementation,
procedure(const Position: Integer; const IsEmptyUses: Boolean)
begin
HandleOnPositionFound(Position, IsEmptyUses);
AddUnitNameToWrite(UnitName);
end
);
end;
procedure TToolsAPIUsesBuilder.HandleOnPositionFound(const Position: Integer; const IsEmptyUses: Boolean);
begin
FIsEmptyUses := FIsEmptyUses or IsEmptyUses;
FPosition := Position;
end;
procedure TToolsAPIUsesBuilder.AddUnitNameToWrite(const UnitName: string);
begin
FUnitNamesToWrite := FUnitNamesToWrite + [UnitName];
end;
function TToolsAPIUsesBuilder.ComposeUsesToWrite: IUsesToWriteResult;
begin
Result := TUsesToWriteResult.Create;
Result.Position := FPosition;
if FIsEmptyUses then
begin
Result.Text := sLineBreak +
sLineBreak + 'uses' +
sLineBreak + ' ' + string.Join(UnitNames_Separator, FUnitNamesToWrite) + ';'
end
else
begin
Result.Text := UnitNames_Separator + string.Join(UnitNames_Separator, FUnitNamesToWrite);
end;
end;
procedure TToolsAPIUsesBuilder.ResetFields;
begin
FUnitNamesToWrite := [];
FIsEmptyUses := False;
FPosition := -1;
end;
{$REGION 'TUsesToWriteResult'}
function TUsesToWriteResult.CanWrite: Boolean;
begin
Result := (FPosition >= 0) and (not FText.IsEmpty);
end;
function TUsesToWriteResult.GetPosition: Integer;
begin
Result := FPosition;
end;
function TUsesToWriteResult.GetText: string;
begin
Result := FText;
end;
procedure TUsesToWriteResult.SetPosition(const Value: Integer);
begin
FPosition := Value;
end;
procedure TUsesToWriteResult.SetText(const Value: string);
begin
FText := Value;
end;
{$ENDREGION}
end.