forked from hhvm/hack-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseCodeBuilder.php
More file actions
314 lines (279 loc) · 8.85 KB
/
BaseCodeBuilder.php
File metadata and controls
314 lines (279 loc) · 8.85 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?hh // strict
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
abstract class BaseCodeBuilder implements ICodeBuilder {
const string DELIMITER = "\t";
private StrBuffer $code;
private bool $isNewLine = true;
private int $indentationLevel = 0;
private bool $isInsideFunction = false;
private bool $wasGetCodeCalled = false;
final public function __construct(private IHackCodegenConfig $config) {
$this->code = new StrBuffer();
}
final public function newLine(): this {
$this->code->append("\n");
$this->isNewLine = true;
return $this;
}
/**
* If the cursor is not in a new line, it will insert a line break.
*/
final public function ensureNewLine(): this {
if (!$this->isNewLine) {
$this->newLine();
}
return $this;
}
/**
* Ensures that the cursor is in a new line right after an empty line.
*/
final public function ensureEmptyLine(): this {
return $this->ensureNewLine()->newLine();
}
/**
* Add the code to the buffer (sprintf style may be used).
* It automatically deals with indentation. The code may contain line breaks.
* If code is null, nothing will be added
*/
/* HH_FIXME[4033] variadic params with type constraints are not supported */
final public function add(?string $code, ...$args): this {
return $this->addv($code, $args);
}
final protected function addv(?string $code, array<mixed> $args): this {
if ($code === null) {
return $this;
}
if (count($args)) {
$code = vsprintf($code, $args);
}
// break into lines and add one by one to handle indentation
$lines = explode("\n", $code);
$last_line = array_pop($lines);
foreach ($lines as $line) {
$this->addLine($line);
}
if (trim($last_line) === '') {
return $this;
}
// if we're in a new line, insert indentation
if ($this->isNewLine) {
if ($this->indentationLevel !== 0) {
$n = $this->config->getSpacesPerIndentation() * $this->indentationLevel;
$this->code->append(str_repeat(' ', $n));
}
$this->isNewLine = false;
}
$this->code->append($last_line);
return $this;
}
/**
* If the condition evaluates to true, the code will be added to the buffer.
*/
final public function addIf(bool $condition, string $code, ...): this {
if ($condition) {
$this->addv($code, array_slice(func_get_args(), 2));
}
return $this;
}
final protected function addLineImpl(
?string $code,
array<mixed> $args,
): this {
return $this->addv($code, $args)->newLine();
}
/**
* If the condition evaluates to true, the code will be added to the buffer
* with a new line.
*/
final public function addLineIf(bool $condition, string $code, ...): this {
if ($condition) {
$this->addLineImpl($code, array_slice(func_get_args(), 2));
}
return $this;
}
/**
* Add the code to the buffer followed by a new line.
* If code is null, nothing will be added
*/
/* HH_FIXME[4033] variadic params with type constraints are not supported */
final public function addLine(?string $code, ...$args): this {
return $this->addLineImpl($code, $args);
}
/**
* Add each element of the vector as a new line
*/
final public function addLines(ConstVector<string> $lines): this {
foreach ($lines as $line) {
$this->addLine($line);
}
return $this;
}
/**
* Indicate that the code that will follow will be inside a function, so that
* the indentation is taken into account for the max line length.
*/
final public function setIsInsideFunction(): this {
$this->isInsideFunction = true;
return $this;
}
/**
* Let's the user suggest linebreaks in the code string provided, marked by
* the delimiter. The max length is calculated based on the current
* indentation level.
* If the code string exceeds the max length
* - Preferentially uses the delimiter to break the line
* - If some part is too big to fit, it lets it be.
* If the code string or a part doesn't exceed the max length
* - Replaces the delimiter with space.
*
* @param delimiter e.g "\t" The function respects all other chars except
* the delimiter which it always replaces
* either with " " or "\n".
*/
final public function addWithSuggestedLineBreaks(
?string $code,
...
): this {
if ($code === null) {
return $this;
}
// If there's more than 1 line, add them 1 by 1
$lines = Str::explode("\n", $code);
if ($lines->count() > 1) {
// The last line shouldn't have a finishing end line,
// so add it manually
$last_line = $lines->pop();
$this->addLinesWithSuggestedLineBreaks($lines);
return $this->addWithSuggestedLineBreaks($last_line);
}
$args = array_slice(func_get_args(), 1);
if (count($args)) {
$code = vsprintf($code, $args);
}
// Subtracting two to allow space for indenting and/or opening braces.
$max_length = $this->getMaxCodeLength() - 2;
if ($this->isInsideFunction) {
// Please note that this method is called both from inside a function or
// stuff like class/func declaration.
$max_length = $max_length - 2;
}
$lines_with_sugg_breaks = explode(self::DELIMITER, $code);
$final_lines = Vector {};
foreach ($lines_with_sugg_breaks as $line) {
if (!$line) {
// Continue if the line is empty
continue;
}
invariant(is_string($line), 'For Hack');
if ($final_lines->isEmpty()) {
$final_lines->add($line);
} else {
$last_line = $final_lines->pop();
$composite_line = $last_line . ' ' . $line;
if (strlen($composite_line) > $max_length) {
$final_lines->add($last_line)->add($line);
} else {
// Concatenate the line to the last line
$final_lines->add($composite_line);
}
}
}
return $this->add(implode("\n ", $final_lines->toArray()));
}
/**
* Similar to addWithSuggestedLineBreaks but allows to add more than one
* line at a time. See that method for more information.
*/
final public function addLinesWithSuggestedLineBreaks(
Vector<string> $lines,
): this {
foreach ($lines as $line) {
$this->addWithSuggestedLineBreaks($line)->newLine();
}
return $this;
}
final protected function setIndentationLevel(int $level): void {
$this->indentationLevel = $level;
}
final protected static function checkIfLineIsTooLong(
string $code,
int $max_length,
): bool {
$line_too_long = false;
foreach (explode("\n", $code) as $line) {
if (strlen($line) > $max_length) {
$line_too_long = true;
break;
}
}
return $line_too_long;
}
/**
* Returns the maximum length of code based on the current identation level.
*/
final protected function getMaxCodeLength(): int {
return $this->config->getMaxLineLength() -
$this->config->getSpacesPerIndentation() * $this->indentationLevel;
}
final public function indent(): this {
$this->indentationLevel++;
return $this;
}
final public function unindent(): this {
invariant(
$this->indentationLevel >= 1,
'Indentation level cannot go below zero.',
);
$this->indentationLevel--;
return $this;
}
/**
* Get the code that was inserted in the buffer.
*/
final public function getCode(): string {
invariant(
!$this->wasGetCodeCalled,
"You may only call getCode() once on a given HackBuilder object.",
);
$this->wasGetCodeCalled = true;
return $this->code->detach();
}
/**
* clone() doesn't work as they end up sharing the same String Buffer, sharing
* all the history (code already added to it).
* So, if code is detached from the clone, it gets detached from original
* builder as well.
* This doesn't bother about the history, just copies the settings.
*/
final protected function getClone(): this {
$builder = new static($this->config);
if ($this->isInsideFunction) {
$builder->setIsInsideFunction();
}
$builder->setIndentationLevel($this->indentationLevel);
return $builder;
}
/**
* Add to the buffer the begin of a manual section with the specified id.
*/
public function beginManualSection(string $id): this {
return $this
->ensureNewLine()
->addLine(PartiallyGeneratedCode::getBeginManualSection($id));
}
/**
* Add to the buffer the end of a manual section.
*/
public function endManualSection(): this {
return $this
->ensureNewLine()
->addLine(PartiallyGeneratedCode::getEndManualSection());
}
}