@@ -13,27 +13,26 @@ pub(crate) enum Step {
13
13
14
14
#[ derive( Clone ) ]
15
15
pub ( crate ) struct BuildCtx {
16
- pub force_wide : bool ,
17
- pub indentation : usize ,
18
- pub pos_new : crate :: position :: Position ,
19
- pub pos_old : crate :: position:: Position ,
20
- pub path : String ,
21
- pub vertical : bool ,
16
+ pub force_wide : bool ,
17
+ pub force_wide_success : bool ,
18
+ pub indentation : usize ,
19
+ pub pos_old : crate :: position:: Position ,
20
+ pub path : String ,
21
+ pub vertical : bool ,
22
22
}
23
23
24
24
impl BuildCtx {
25
25
pub fn new (
26
26
force_wide : bool ,
27
27
path : String ,
28
- pos_new : crate :: position:: Position ,
29
28
pos_old : crate :: position:: Position ,
30
29
vertical : bool ,
31
30
) -> BuildCtx {
32
31
BuildCtx {
33
32
force_wide,
33
+ force_wide_success : true ,
34
34
indentation : 0 ,
35
35
path,
36
- pos_new,
37
36
pos_old,
38
37
vertical,
39
38
}
@@ -51,7 +50,6 @@ pub(crate) fn build(
51
50
force_wide,
52
51
path,
53
52
crate :: position:: Position :: default ( ) ,
54
- crate :: position:: Position :: default ( ) ,
55
53
vertical,
56
54
) ;
57
55
@@ -61,8 +59,8 @@ pub(crate) fn build(
61
59
& crate :: builder:: Step :: Format ( element) ,
62
60
) ;
63
61
64
- if build_ctx . force_wide && build_ctx . pos_new . line > 1 {
65
- None
62
+ if force_wide {
63
+ if build_ctx . force_wide_success { Some ( builder . finish ( ) ) } else { None }
66
64
} else {
67
65
Some ( builder. finish ( ) )
68
66
}
@@ -74,7 +72,7 @@ fn build_step(
74
72
75
73
step : & crate :: builder:: Step ,
76
74
) {
77
- if build_ctx. force_wide && build_ctx. pos_new . line > 1 {
75
+ if build_ctx. force_wide && ! build_ctx. force_wide_success {
78
76
return ;
79
77
}
80
78
@@ -102,7 +100,6 @@ fn build_step(
102
100
103
101
add_token (
104
102
builder,
105
- build_ctx,
106
103
rnix:: SyntaxKind :: TOKEN_COMMENT ,
107
104
& lines. join ( "\n " ) ,
108
105
) ;
@@ -120,45 +117,34 @@ fn build_step(
120
117
build_ctx. indentation += 1 ;
121
118
}
122
119
crate :: builder:: Step :: NewLine => {
123
- add_token (
124
- builder,
125
- build_ctx,
126
- rnix:: SyntaxKind :: TOKEN_WHITESPACE ,
127
- "\n " ,
128
- ) ;
120
+ build_ctx. force_wide_success = false ;
121
+
122
+ add_token ( builder, rnix:: SyntaxKind :: TOKEN_WHITESPACE , "\n " ) ;
129
123
}
130
124
crate :: builder:: Step :: Pad => {
131
125
if build_ctx. indentation > 0 {
132
126
add_token (
133
127
builder,
134
- build_ctx,
135
128
rnix:: SyntaxKind :: TOKEN_WHITESPACE ,
136
129
& format ! ( "{0:<1$}" , "" , 2 * build_ctx. indentation) ,
137
130
) ;
138
131
}
139
132
}
140
133
crate :: builder:: Step :: Token ( kind, text) => {
141
- add_token ( builder, build_ctx , * kind, text) ;
134
+ add_token ( builder, * kind, text) ;
142
135
}
143
136
crate :: builder:: Step :: Whitespace => {
144
- add_token (
145
- builder,
146
- build_ctx,
147
- rnix:: SyntaxKind :: TOKEN_WHITESPACE ,
148
- " " ,
149
- ) ;
137
+ add_token ( builder, rnix:: SyntaxKind :: TOKEN_WHITESPACE , " " ) ;
150
138
}
151
139
}
152
140
}
153
141
154
142
fn add_token (
155
143
builder : & mut rowan:: GreenNodeBuilder ,
156
- build_ctx : & mut BuildCtx ,
157
144
kind : rnix:: SyntaxKind ,
158
145
text : & str ,
159
146
) {
160
147
builder. token ( rowan:: SyntaxKind ( kind as u16 ) , text) ;
161
- build_ctx. pos_new . update ( text) ;
162
148
}
163
149
164
150
fn format (
@@ -254,7 +240,7 @@ fn format(
254
240
}
255
241
rnix:: SyntaxElement :: Token ( token) => {
256
242
let text = token. text ( ) ;
257
- add_token ( builder, build_ctx , kind, text) ;
243
+ add_token ( builder, kind, text) ;
258
244
build_ctx. pos_old . update ( text) ;
259
245
}
260
246
}
@@ -283,13 +269,7 @@ pub(crate) fn fits_in_single_line(
283
269
build_ctx : & crate :: builder:: BuildCtx ,
284
270
element : rnix:: SyntaxElement ,
285
271
) -> bool {
286
- let line = build_ctx. pos_new . line ;
287
- let maybe_green_node = build ( element, true , build_ctx. path . clone ( ) , false ) ;
288
-
289
- match maybe_green_node {
290
- Some ( _) => build_ctx. pos_new . line == line,
291
- None => false ,
292
- }
272
+ build ( element, true , build_ctx. path . clone ( ) , false ) . is_some ( )
293
273
}
294
274
295
275
pub ( crate ) fn make_isolated_token (
0 commit comments