Skip to content

Commit dbe2c23

Browse files
authored
Fix mixed indents in block strings, attempt 2 (#78)
1 parent 90a0d3f commit dbe2c23

File tree

5 files changed

+10
-2
lines changed

5 files changed

+10
-2
lines changed

src/Neon/Encoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function valueToNode(mixed $val, bool $blockMode = false): Node
6464
return $node;
6565

6666
} elseif (is_string($val) && Lexer::requiresDelimiters($val)) {
67-
return new Node\StringNode($val);
67+
return new Node\StringNode($val, $this->indentation);
6868

6969
} else {
7070
return new Node\LiteralNode($val);

src/Neon/Node/StringNode.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ final class StringNode extends Node
2323

2424
public function __construct(
2525
public string $value,
26+
public string $indentation = "\t",
2627
) {
2728
}
2829

@@ -89,7 +90,7 @@ public function toString(): string
8990
$delim = "'''";
9091
}
9192

92-
$s = preg_replace('#^(?=.)#m', "\t", $s);
93+
$s = preg_replace('#^(?=.)#m', $this->indentation, $s);
9394
return $delim . "\n" . $s . "\n" . $delim;
9495
}
9596
}

tests/Neon/Encoder.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,8 @@ Assert::same(
180180
"\"\"\"\n\tspecial\\r\n\tchars\n\"\"\"",
181181
Neon::encode("special\r\nchars", true),
182182
);
183+
184+
Assert::same(
185+
"inner:\n msg: '''\n string\n with newline\n '''\n\n",
186+
Neon::encode(['inner' => ['msg' => "string\nwith newline"]], true, ' '),
187+
);

tests/Neon/fixtures/Encoder.nodes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ Nette\Neon\Node\InlineArrayNode
306306
| | | value: string
307307
| | | | 'hello\n
308308
| | | | world'
309+
| | | indentation: '\t '
309310
| | | startTokenPos: null
310311
| | | endTokenPos: null
311312
| | | start: null

tests/Neon/fixtures/Parser.nodes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,7 @@ Nette\Neon\Node\BlockArrayNode
993993
| | | value: string
994994
| | | | 'one\n
995995
| | | | two'
996+
| | | indentation: '\t '
996997
| | | startTokenPos: unset
997998
| | | endTokenPos: unset
998999
| | | start: Nette\Neon\Position

0 commit comments

Comments
 (0)