Skip to content

Commit

Permalink
better checking of auto-split-margins re: unbreakable lines
Browse files Browse the repository at this point in the history
  • Loading branch information
pmjones committed May 7, 2024
1 parent e3e3330 commit 46e8ad7
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,12 @@ public function autoAddMargins() : void
{
$tmp = clone $this;
$output = '';
$tmp->append($output);

if ($tmp->fitsOnSingleLine($output)) {
return;
if ($tmp->lines || strpos(trim($output), $this->eol)) {
$this->addMarginAbove();
$this->addMarginBelow();
}

$this->addMarginAbove();
$this->addMarginBelow();
}

public function append(string &$output) : void
Expand Down
1 change: 1 addition & 0 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,7 @@ protected function escapeString(
$hex = dechex(ord($matches[0]));
return '\x' . str_pad($hex, 2, '0', STR_PAD_LEFT);
};

return preg_replace_callback($regex, $callback, $escaped);
}

Expand Down
12 changes: 11 additions & 1 deletion src/Styler.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@ protected function sClassPropertyEnd(P\ClassProperty $end) : void

protected function sClosure(P\Closure $p) : void
{
if (! $this->nesting->in(P\Args::class)) {
$this->line->addMarginAbove();
}

$this->line[] = $p->static ? 'static function ' : 'function ';
}

Expand Down Expand Up @@ -624,6 +628,10 @@ protected function sClosureBodyEnd(P\Body $p) : void
$this->line->allowMarginAbove(false);
$this->outdent();
$this->line[] = '}';

if (! $this->nesting->in(P\Args::class)) {
$this->line->addMarginBelow();
}
}

protected function sClosureBodyEmpty(P\BodyEmpty $p) : void
Expand Down Expand Up @@ -906,6 +914,7 @@ protected function sGoto(P\Goto_ $p) : void

protected function sHaltCompiler(P\HaltCompiler $p) : void
{
$this->line->allowMarginAbove(false);
$this->line[] = '__halt_compiler();';
}

Expand Down Expand Up @@ -1054,13 +1063,14 @@ protected function sInfixEnd(P\Infix $p) : void

protected function sInlineHtml(P\InlineHtml $p) : void
{
$this->line->allowMarginAbove(false);
$this->line[] = '?>' . ($p->newline ? $this->eol : '');
}
protected function sInlineHtmlEnd(P\InlineHtml $p) : void
{
$this->line[] = '<?php';
$this->newline();
$this->newlineWithoutMargins();
}

protected function sInstanceOp(P\InstanceOp $p) : void
Expand Down
7 changes: 7 additions & 0 deletions tests/Examples/auto-blank-lines.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
/**
*
* Short line
* Also a short line
* A very very very very very very very very very very very very very very very very very very long line
*
*/
if (true) {
$foo1->language
->evaluate(
Expand Down
5 changes: 5 additions & 0 deletions tests/Examples/comments-inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ function foo()

// set matcher callbacks
$this->htmlAttrMatcher = /** @param array<array-key, string> $matches */

function (array $matches) : string {
return $this->htmlAttrMatcher($matches);
};

$this->jsMatcher = /** @param array<array-key, string> $matches */

function (array $matches) : string {
return $this->jsMatcher($matches);
};

$this->cssMatcher = /** @param array<array-key, string> $matches */

function (array $matches) : string {
return $this->cssMatcher($matches);
};
4 changes: 3 additions & 1 deletion tests/Examples/other-expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
print "hello";
print (1 + 2) * 3;
(print "hello") && false;
function () {

$foo = function () {
yield from $foo;
};

@suppressError();
4 changes: 4 additions & 0 deletions tests/Examples/quoted-strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
$foo = "this\nthat";
$foo = "zim\"zim\"zim";
$foo = 'zim\'zim\'zim';

$foo = 'foo
bar
baz';

$foo = "foo
bar
baz";

$foo = "\nfoo\nbar";
1 change: 0 additions & 1 deletion tests/LineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public function testOffsetUnset() : void
public function testNoSuchSplit() : void
{
$this->line[] = 'fake fake fake fake fake fake fake fake fake fake fake fake fake fake fake fake fake fake ';

$this->line[] = new Split(0, 'fake', 'fake', null);
$output = '';
$this->expectException(Exception::class);
Expand Down

0 comments on commit 46e8ad7

Please sign in to comment.