Skip to content

Commit 24a2f56

Browse files
committed
feat: do not collapse spaces on both sides of content blocks
1 parent 5225fb8 commit 24a2f56

23 files changed

+252
-67
lines changed

crates/typstyle-core/src/pretty/markup.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,20 @@ impl<'a> PrettyPrinter<'a> {
222222
self.arena.line_()
223223
}
224224
}
225-
Boundary::SpaceOrBreak | Boundary::WeakSpaceOrBreak => {
225+
Boundary::SpaceOrBreak(n) | Boundary::WeakSpaceOrBreak(n) => {
226226
if is_symmetric && !ctx.break_suppressed || !prefer_tight {
227-
self.arena.line()
227+
if self.config.wrap_text {
228+
self.arena.line()
229+
} else {
230+
self.arena.hardline().flat_alt(self.arena.spaces(n))
231+
}
228232
} else if scope.can_trim() {
229233
// the space can be safely eaten
230234
self.arena.nil()
231-
} else {
235+
} else if self.config.wrap_text {
232236
self.arena.space()
237+
} else {
238+
self.arena.spaces(n)
233239
}
234240
}
235241
Boundary::Break | Boundary::WeakBreak => self.arena.hardline(),
@@ -387,27 +393,27 @@ enum Boundary {
387393
/// Can add a space or linebreak when multiline.
388394
NilOrBreak,
389395
/// Can turn to a linebreak.
390-
SpaceOrBreak,
396+
SpaceOrBreak(usize),
391397
/// Always breaks.
392398
Break,
393399
/// Can turn to a linebreak if not in document scope.
394-
WeakSpaceOrBreak,
400+
WeakSpaceOrBreak(usize),
395401
/// Always breaks if not in document scope.
396402
WeakBreak,
397403
}
398404

399405
impl Boundary {
400-
fn from_space(space: &str) -> Self {
406+
pub fn from_space(space: &str) -> Self {
401407
if space.has_linebreak() {
402408
Self::Break
403409
} else {
404-
Self::SpaceOrBreak
410+
Self::SpaceOrBreak(space.len())
405411
}
406412
}
407413

408-
fn strip_space(self) -> Self {
414+
pub fn strip_space(self) -> Self {
409415
match self {
410-
Self::SpaceOrBreak => Self::NilOrBreak,
416+
Self::SpaceOrBreak(_) => Self::NilOrBreak,
411417
_ => self,
412418
}
413419
}
@@ -487,7 +493,7 @@ fn collect_markup_repr(markup: Markup<'_>) -> MarkupRepr {
487493
repr.start_bound = Boundary::NilOrBreak;
488494
}
489495
Some(it) if it.kind() == SyntaxKind::Space => {
490-
repr.start_bound = Boundary::WeakSpaceOrBreak;
496+
repr.start_bound = Boundary::WeakSpaceOrBreak(1);
491497
}
492498
None if !first_line.nodes.is_empty() => repr.start_bound = Boundary::WeakBreak,
493499
_ => {}
@@ -501,7 +507,7 @@ fn collect_markup_repr(markup: Markup<'_>) -> MarkupRepr {
501507
repr.end_bound = Boundary::NilOrBreak;
502508
}
503509
Some(it) if it.kind() == SyntaxKind::Space => {
504-
repr.end_bound = Boundary::WeakSpaceOrBreak;
510+
repr.end_bound = Boundary::WeakSpaceOrBreak(1);
505511
}
506512
None if !last_line.nodes.is_empty() => repr.end_bound = Boundary::WeakBreak,
507513
_ => {}

tests/fixtures/unit/code/snap/if-chain.typ-120.snap

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
---
22
source: tests/src/unit.rs
33
input_file: tests/fixtures/unit/code/if-chain.typ
4-
snapshot_kind: text
54
---
65
#if a < b { true } else if c < d { (false,) } else if 1 + 2 == 3 { (true, false) } else { none }
76

8-
#if a < b { true } else if c < d [ false ] else if 1 + 2 == 3 [*strong*] else { none }
7+
#if a < b { true } else if c < d [ false ] else if 1 + 2 == 3 [*strong*] else { none }
98

109
#if a < b { true } else if c < d [
1110
false
@@ -15,13 +14,13 @@ snapshot_kind: text
1514

1615
#if a < b {
1716
true
18-
} else if c < d [ false ] else if 1 + 2 == 3 [
17+
} else if c < d [ false ] else if 1 + 2 == 3 [
1918
*strong*] else { none }
2019

2120
#{
2221
if a < b { true } else if c < d { (false,) } else if 1 + 2 == 3 { (true, false) } else { none }
2322

24-
if a < b { true } else if c < d [ false ] else if 1 + 2 == 3 [*strong*] else { none }
23+
if a < b { true } else if c < d [ false ] else if 1 + 2 == 3 [*strong*] else { none }
2524

2625
if a < b { true } else if c < d [
2726
false
@@ -31,14 +30,14 @@ snapshot_kind: text
3130

3231
if a < b {
3332
true
34-
} else if c < d [ false ] else if 1 + 2 == 3 [
33+
} else if c < d [ false ] else if 1 + 2 == 3 [
3534
*strong*] else { none }
3635
}
3736

3837
#if a < b {
3938
true
4039
false
41-
} else if c < d [ false ] else if 1 + 2 == 3 [*strong*] else {
40+
} else if c < d [ false ] else if 1 + 2 == 3 [*strong*] else {
4241
a = 3
4342
none
4443
}

tests/fixtures/unit/code/snap/if-chain.typ-40.snap

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: tests/src/unit.rs
33
input_file: tests/fixtures/unit/code/if-chain.typ
4-
snapshot_kind: text
54
---
65
#if a < b { true } else if c < d {
76
(false,)
@@ -23,7 +22,7 @@ snapshot_kind: text
2322

2423
#if a < b {
2524
true
26-
} else if c < d [ false ] else if (
25+
} else if c < d [ false ] else if (
2726
1 + 2 == 3
2827
) [
2928
*strong*] else { none }
@@ -49,7 +48,7 @@ snapshot_kind: text
4948

5049
if a < b {
5150
true
52-
} else if c < d [ false ] else if (
51+
} else if c < d [ false ] else if (
5352
1 + 2 == 3
5453
) [
5554
*strong*] else { none }
@@ -58,7 +57,7 @@ snapshot_kind: text
5857
#if a < b {
5958
true
6059
false
61-
} else if c < d [ false ] else if (
60+
} else if c < d [ false ] else if (
6261
1 + 2 == 3
6362
) [*strong*] else {
6463
a = 3

tests/fixtures/unit/code/snap/if-chain.typ-80.snap

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
---
22
source: tests/src/unit.rs
33
input_file: tests/fixtures/unit/code/if-chain.typ
4-
snapshot_kind: text
54
---
65
#if a < b { true } else if c < d { (false,) } else if 1 + 2 == 3 {
76
(true, false)
87
} else { none }
98

10-
#if a < b { true } else if c < d [ false ] else if 1 + 2 == 3 [*strong*] else {
9+
#if a < b { true } else if c < d [ false ] else if 1 + 2 == 3 [*strong*] else {
1110
none
1211
}
1312

@@ -19,17 +18,17 @@ snapshot_kind: text
1918

2019
#if a < b {
2120
true
22-
} else if c < d [ false ] else if 1 + 2 == 3 [
21+
} else if c < d [ false ] else if 1 + 2 == 3 [
2322
*strong*] else { none }
2423

2524
#{
2625
if a < b { true } else if c < d { (false,) } else if 1 + 2 == 3 {
2726
(true, false)
2827
} else { none }
2928

30-
if a < b { true } else if c < d [ false ] else if 1 + 2 == 3 [*strong*] else {
31-
none
32-
}
29+
if a < b { true } else if c < d [ false ] else if (
30+
1 + 2 == 3
31+
) [*strong*] else { none }
3332

3433
if a < b { true } else if c < d [
3534
false
@@ -39,14 +38,14 @@ snapshot_kind: text
3938

4039
if a < b {
4140
true
42-
} else if c < d [ false ] else if 1 + 2 == 3 [
41+
} else if c < d [ false ] else if 1 + 2 == 3 [
4342
*strong*] else { none }
4443
}
4544

4645
#if a < b {
4746
true
4847
false
49-
} else if c < d [ false ] else if 1 + 2 == 3 [*strong*] else {
48+
} else if c < d [ false ] else if 1 + 2 == 3 [*strong*] else {
5049
a = 3
5150
none
5251
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
source: tests/src/unit.rs
3+
input_file: tests/fixtures/unit/markup/reflow/spaces.typ
4+
---
5+
/// typstyle: wrap_text
6+
7+
Nested
8+
#strong[
9+
*
10+
bold
11+
*
12+
*inside
13+
*
14+
*strong*]
15+
with
16+
spaces
17+
\
18+
and
19+
line
20+
breaks
21+
\
22+
across
23+
multiple
24+
lines.
25+
26+
#quote[
27+
Quoted
28+
text
29+
with
30+
spaces
31+
and
32+
multiple
33+
lines
34+
]
35+
36+
#align(
37+
center,
38+
)[
39+
Centered
40+
text
41+
with
42+
spaces
43+
]
44+
and
45+
#stack[
46+
Stacked
47+
content
48+
with
49+
preserved
50+
spaces
51+
]
52+
53+
#table(
54+
columns: 2,
55+
[
56+
Cell
57+
with
58+
spaces],
59+
[Another
60+
cell
61+
with
62+
spaces
63+
],
64+
65+
[More
66+
content],
67+
[
68+
Even
69+
more
70+
spaced
71+
content
72+
],
73+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
source: tests/src/unit.rs
3+
input_file: tests/fixtures/unit/markup/reflow/spaces.typ
4+
---
5+
/// typstyle: wrap_text
6+
7+
Nested #strong[ * bold * *inside * *strong*] with spaces \
8+
and line breaks \
9+
across multiple lines.
10+
11+
#quote[
12+
Quoted text with spaces and multiple lines
13+
]
14+
15+
#align(center)[
16+
Centered text with spaces
17+
] and #stack[
18+
Stacked content with preserved spaces
19+
]
20+
21+
#table(
22+
columns: 2,
23+
[ Cell with spaces], [Another cell with spaces ],
24+
[More content], [ Even more spaced content ],
25+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
source: tests/src/unit.rs
3+
input_file: tests/fixtures/unit/markup/reflow/spaces.typ
4+
---
5+
/// typstyle: wrap_text
6+
7+
Nested #strong[
8+
*
9+
bold
10+
* *inside
11+
* *strong*] with spaces \
12+
and line breaks \
13+
across multiple lines.
14+
15+
#quote[
16+
Quoted text with spaces and multiple
17+
lines
18+
]
19+
20+
#align(center)[
21+
Centered text with spaces
22+
] and #stack[
23+
Stacked content with preserved spaces
24+
]
25+
26+
#table(
27+
columns: 2,
28+
[ Cell with spaces],
29+
[Another cell with spaces ],
30+
31+
[More content],
32+
[ Even more spaced content ],
33+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
source: tests/src/unit.rs
3+
input_file: tests/fixtures/unit/markup/reflow/spaces.typ
4+
---
5+
/// typstyle: wrap_text
6+
7+
Nested #strong[ * bold * *inside * *strong*] with spaces \
8+
and line breaks \
9+
across multiple lines.
10+
11+
#quote[
12+
Quoted text with spaces and multiple lines
13+
]
14+
15+
#align(center)[
16+
Centered text with spaces
17+
] and #stack[
18+
Stacked content with preserved spaces
19+
]
20+
21+
#table(
22+
columns: 2,
23+
[ Cell with spaces], [Another cell with spaces ],
24+
[More content], [ Even more spaced content ],
25+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// typstyle: wrap_text
2+
3+
Nested #strong[ * bold * *inside * *strong*] with spaces \
4+
and line breaks \
5+
across multiple lines.
6+
7+
#quote[ Quoted text with spaces
8+
and multiple lines
9+
]
10+
11+
#align(center)[
12+
Centered text with spaces
13+
] and #stack[
14+
Stacked content
15+
with preserved spaces
16+
]
17+
18+
#table(
19+
columns: 2,
20+
[ Cell with spaces], [Another cell with spaces ],
21+
[More content], [ Even more spaced content ]
22+
)

0 commit comments

Comments
 (0)