Skip to content

Commit 3eb0f05

Browse files
committed
feat: soft wrap import items
1 parent 1ceee64 commit 3eb0f05

16 files changed

+107
-205
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ impl<'a> PrettyPrinter<'a> {
108108
.print_doc(ListStyle {
109109
omit_delim_flat: true,
110110
omit_delim_empty: true,
111+
soft_break: true,
111112
..Default::default()
112113
})
113114
}

crates/typstyle-core/src/pretty/layout/list.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ use crate::{
77

88
pub struct ListStylist<'a> {
99
printer: &'a PrettyPrinter<'a>,
10+
1011
can_attach: bool,
1112
free_comments: Vec<ArenaDoc<'a>>,
1213
peek_hash: bool,
1314
items: Vec<Item<'a>>,
1415
real_item_count: usize,
1516
has_comment: bool,
1617
has_line_comment: bool,
18+
1719
fold_style: FoldStyle,
1820
disallow_front_comment: bool,
1921
disallow_comment_detach: bool,
@@ -56,6 +58,8 @@ pub struct ListStyle {
5658
pub omit_delim_empty: bool,
5759
/// Whether not to indent the items.
5860
pub no_indent: bool,
61+
/// Whether use soft linebreaks to wrap items.
62+
pub soft_break: bool,
5963
}
6064

6165
impl Default for ListStyle {
@@ -71,6 +75,7 @@ impl Default for ListStyle {
7175
omit_delim_flat: false,
7276
omit_delim_empty: false,
7377
no_indent: false,
78+
soft_break: false,
7479
}
7580
}
7681
}
@@ -79,13 +84,15 @@ impl<'a> ListStylist<'a> {
7984
pub fn new(printer: &'a PrettyPrinter<'a>) -> Self {
8085
Self {
8186
printer,
87+
8288
can_attach: false,
8389
free_comments: Default::default(),
8490
peek_hash: false,
8591
items: Default::default(),
8692
real_item_count: 0,
8793
has_comment: false,
8894
has_line_comment: false,
95+
8996
fold_style: FoldStyle::Fit,
9097
disallow_front_comment: false,
9198
disallow_comment_detach: false,
@@ -455,6 +462,13 @@ impl<'a> ListStylist<'a> {
455462
enclose_fitted(inner)
456463
}
457464
FoldStyle::Fit | FoldStyle::Compact => {
465+
let ln = if sty.soft_break && !self.has_line_comment {
466+
arena.softline()
467+
} else {
468+
arena.line()
469+
};
470+
let ln_last = arena.line_();
471+
458472
let mut inner = if sty.tight_delim {
459473
arena.nil()
460474
} else {
@@ -498,11 +512,11 @@ impl<'a> ListStylist<'a> {
498512
follow
499513
};
500514
let ln = if !is_last_real {
501-
arena.line()
515+
ln.clone()
502516
} else if sty.tight_delim {
503517
arena.nil()
504518
} else {
505-
arena.line_()
519+
ln_last.clone()
506520
};
507521
inner += body + follow + ln;
508522
}

tests/fixtures/packages/snap/codly.typ-40.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ input_file: tests/fixtures/packages/codly.typ
1212
*/
1313

1414
#import "args.typ": (
15-
__codly-args,
16-
__codly-load,
15+
__codly-args, __codly-load,
1716
__codly-save,
1817
)
1918

tests/fixtures/unit/code/args/snap/with-newline.typ-40.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ source: tests/src/unit.rs
33
input_file: tests/fixtures/unit/code/args/with-newline.typ
44
---
55
#import "@preview/fletcher:0.5.7" as fletcher: (
6-
diagram,
7-
edge,
8-
node,
6+
diagram, edge, node,
97
)
108
#set page(
119
width: auto,
Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,11 @@
11
---
22
source: tests/src/unit.rs
33
input_file: tests/fixtures/unit/code/import-items.typ
4-
snapshot_kind: text
54
---
65
#import "test.typ": (
7-
aaa,
8-
bbb as cccccccccc,
9-
ddd as eeeeeeeeeee,
10-
fff as g,
11-
hhh as iiii,
12-
jjj as k,
13-
lll as m,
14-
nnn as o,
15-
ppp as q,
16-
rrr as s,
17-
ttt as u,
18-
vvv as w,
19-
xxx as y,
20-
zzz as aa,
21-
bbb as cc,
22-
ddd as ee,
23-
fff as gg,
24-
hhh as ii,
25-
jjj as kk,
26-
lll as mm,
27-
nnn as oo,
28-
ppp as qq,
29-
rrr as ss,
30-
ttt as uu,
31-
vvv as ww,
32-
xxx as yy,
33-
zzz as aaa,
34-
bbb as ccc,
35-
ddd as eee,
36-
fff as ggg,
37-
hhh as iii,
6+
aaa, bbb as cccccccccc, ddd as eeeeeeeeeee, fff as g, hhh as iiii, jjj as k, lll as m, nnn as o, ppp as q, rrr as s,
7+
ttt as u, vvv as w, xxx as y, zzz as aa, bbb as cc, ddd as ee, fff as gg, hhh as ii, jjj as kk, lll as mm, nnn as oo,
8+
ppp as qq, rrr as ss, ttt as uu, vvv as ww, xxx as yy, zzz as aaa, bbb as ccc, ddd as eee, fff as ggg, hhh as iii,
389
jjj as k,
3910
)
4011
#import "test.typ": a.b.c.d, eee
Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,19 @@
11
---
22
source: tests/src/unit.rs
33
input_file: tests/fixtures/unit/code/import-items.typ
4-
snapshot_kind: text
54
---
65
#import "test.typ": (
7-
aaa,
8-
bbb as cccccccccc,
9-
ddd as eeeeeeeeeee,
10-
fff as g,
11-
hhh as iiii,
12-
jjj as k,
13-
lll as m,
14-
nnn as o,
15-
ppp as q,
16-
rrr as s,
17-
ttt as u,
18-
vvv as w,
19-
xxx as y,
20-
zzz as aa,
21-
bbb as cc,
22-
ddd as ee,
23-
fff as gg,
24-
hhh as ii,
25-
jjj as kk,
26-
lll as mm,
27-
nnn as oo,
28-
ppp as qq,
29-
rrr as ss,
30-
ttt as uu,
31-
vvv as ww,
32-
xxx as yy,
33-
zzz as aaa,
34-
bbb as ccc,
35-
ddd as eee,
36-
fff as ggg,
37-
hhh as iii,
6+
aaa, bbb as cccccccccc,
7+
ddd as eeeeeeeeeee, fff as g,
8+
hhh as iiii, jjj as k, lll as m,
9+
nnn as o, ppp as q, rrr as s,
10+
ttt as u, vvv as w, xxx as y,
11+
zzz as aa, bbb as cc, ddd as ee,
12+
fff as gg, hhh as ii, jjj as kk,
13+
lll as mm, nnn as oo, ppp as qq,
14+
rrr as ss, ttt as uu, vvv as ww,
15+
xxx as yy, zzz as aaa, bbb as ccc,
16+
ddd as eee, fff as ggg, hhh as iii,
3817
jjj as k,
3918
)
4019
#import "test.typ": a.b.c.d, eee
Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,12 @@
11
---
22
source: tests/src/unit.rs
33
input_file: tests/fixtures/unit/code/import-items.typ
4-
snapshot_kind: text
54
---
65
#import "test.typ": (
7-
aaa,
8-
bbb as cccccccccc,
9-
ddd as eeeeeeeeeee,
10-
fff as g,
11-
hhh as iiii,
12-
jjj as k,
13-
lll as m,
14-
nnn as o,
15-
ppp as q,
16-
rrr as s,
17-
ttt as u,
18-
vvv as w,
19-
xxx as y,
20-
zzz as aa,
21-
bbb as cc,
22-
ddd as ee,
23-
fff as gg,
24-
hhh as ii,
25-
jjj as kk,
26-
lll as mm,
27-
nnn as oo,
28-
ppp as qq,
29-
rrr as ss,
30-
ttt as uu,
31-
vvv as ww,
32-
xxx as yy,
33-
zzz as aaa,
34-
bbb as ccc,
35-
ddd as eee,
36-
fff as ggg,
37-
hhh as iii,
38-
jjj as k,
6+
aaa, bbb as cccccccccc, ddd as eeeeeeeeeee, fff as g, hhh as iiii, jjj as k,
7+
lll as m, nnn as o, ppp as q, rrr as s, ttt as u, vvv as w, xxx as y,
8+
zzz as aa, bbb as cc, ddd as ee, fff as gg, hhh as ii, jjj as kk, lll as mm,
9+
nnn as oo, ppp as qq, rrr as ss, ttt as uu, vvv as ww, xxx as yy, zzz as aaa,
10+
bbb as ccc, ddd as eee, fff as ggg, hhh as iii, jjj as k,
3911
)
4012
#import "test.typ": a.b.c.d, eee

tests/fixtures/unit/code/snap/import-reorder.typ-120.snap

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,8 @@ input_file: tests/fixtures/unit/code/import-reorder.typ
66

77
// Single import with mixed styles in random order
88
#import "module.typ": (
9-
a.b.c,
10-
alpha,
11-
beta,
12-
func as renamed,
13-
gamma,
14-
h.i.j,
15-
j.k.l as util,
16-
m.n,
17-
p.q as custom,
18-
widget as tool,
19-
x.y as short,
20-
xyz,
21-
z.y.x,
9+
a.b.c, alpha, beta, func as renamed, gamma, h.i.j, j.k.l as util, m.n, p.q as custom, widget as tool, x.y as short,
10+
xyz, z.y.x,
2211
)
2312

2413
// Multiple imports from same module, should ideally be merged and reordered

tests/fixtures/unit/code/snap/import-reorder.typ-40.snap

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,27 @@ input_file: tests/fixtures/unit/code/import-reorder.typ
66

77
// Single import with mixed styles in random order
88
#import "module.typ": (
9-
a.b.c,
10-
alpha,
11-
beta,
12-
func as renamed,
13-
gamma,
14-
h.i.j,
15-
j.k.l as util,
16-
m.n,
17-
p.q as custom,
18-
widget as tool,
19-
x.y as short,
20-
xyz,
21-
z.y.x,
9+
a.b.c, alpha, beta, func as renamed,
10+
gamma, h.i.j, j.k.l as util, m.n,
11+
p.q as custom, widget as tool,
12+
x.y as short, xyz, z.y.x,
2213
)
2314

2415
// Multiple imports from same module, should ideally be merged and reordered
2516
#import "common.typ": format
2617
#import "common.typ": (
27-
layout as l,
28-
render,
18+
layout as l, render,
2919
)
3020
#import "common.typ": (
31-
style.color,
32-
transform as t,
21+
style.color, transform as t,
3322
)
3423
#import "common.typ": (
35-
data.chart as graph,
36-
data.table,
24+
data.chart as graph, data.table,
3725
)
3826

3927
// Imports with special names and deep nesting
4028
#import "special.typ": (
41-
_hidden,
42-
_private.func as _f,
29+
_hidden, _private.func as _f,
4330
a.b.c.d.e.f,
4431
very.deep.nested.module.function as fn,
4532
x-y-z as x_y_z,
@@ -63,30 +50,22 @@ input_file: tests/fixtures/unit/code/import-reorder.typ
6350
// This comment should prevent reordering
6451
)
6552
#import "commented.typ": (
66-
ddd,
67-
/* Another comment type */ ccc,
53+
ddd, /* Another comment type */ ccc,
6854
)
6955

7056
// Case 2: Import with duplicate item names - should not reorder
7157
// Same name imported twice
7258
#import "duplicates.typ": (
73-
helper,
74-
widget,
75-
helper,
76-
formatter,
59+
helper, widget, helper, formatter,
7760
)
7861

7962
// Same name imported directly and via alias
8063
#import "alias-duplicates.typ": (
81-
config,
82-
settings as config,
83-
template,
64+
config, settings as config, template,
8465
)
8566

8667
// Two aliases with the same target name
8768
#import "same-alias.typ": (
88-
render,
89-
format as util,
90-
transform as util,
91-
aliases,
69+
render, format as util,
70+
transform as util, aliases,
9271
)

tests/fixtures/unit/code/snap/import-reorder.typ-80.snap

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,8 @@ input_file: tests/fixtures/unit/code/import-reorder.typ
66

77
// Single import with mixed styles in random order
88
#import "module.typ": (
9-
a.b.c,
10-
alpha,
11-
beta,
12-
func as renamed,
13-
gamma,
14-
h.i.j,
15-
j.k.l as util,
16-
m.n,
17-
p.q as custom,
18-
widget as tool,
19-
x.y as short,
20-
xyz,
21-
z.y.x,
9+
a.b.c, alpha, beta, func as renamed, gamma, h.i.j, j.k.l as util, m.n,
10+
p.q as custom, widget as tool, x.y as short, xyz, z.y.x,
2211
)
2312

2413
// Multiple imports from same module, should ideally be merged and reordered
@@ -29,11 +18,8 @@ input_file: tests/fixtures/unit/code/import-reorder.typ
2918

3019
// Imports with special names and deep nesting
3120
#import "special.typ": (
32-
_hidden,
33-
_private.func as _f,
34-
a.b.c.d.e.f,
35-
very.deep.nested.module.function as fn,
36-
x-y-z as x_y_z,
21+
_hidden, _private.func as _f, a.b.c.d.e.f,
22+
very.deep.nested.module.function as fn, x-y-z as x_y_z,
3723
)
3824

3925
// Conflicting names that should be preserved

0 commit comments

Comments
 (0)