@@ -35,7 +35,7 @@ fn gen_node<'a>(node: SyntaxElement, context: &mut Context<'a>) -> PrintItems {
35
35
36
36
fn gen_node_with_inner < ' a > ( node : SyntaxElement , context : & mut Context < ' a > , inner_parse : impl FnOnce ( PrintItems , & mut Context < ' a > ) -> PrintItems ) -> PrintItems {
37
37
let mut items = PrintItems :: new ( ) ;
38
- // println !("{:?}", node);
38
+ // eprintln !("{:?}", node);
39
39
40
40
if node. kind ( ) != SyntaxKind :: COMMENT {
41
41
for comment in node. get_comments_on_previous_lines ( ) {
@@ -146,17 +146,24 @@ fn allow_blank_line(previous_kind: Option<SyntaxKind>, current_kind: SyntaxKind)
146
146
147
147
fn gen_array < ' a > ( node : SyntaxNode , context : & mut Context < ' a > ) -> PrintItemsResult {
148
148
let values = node. children ( ) ;
149
- let open_token = get_token_with_kind ( node. clone ( ) , SyntaxKind :: BRACKET_START ) ?;
150
- let close_token = get_token_with_kind ( node. clone ( ) , SyntaxKind :: BRACKET_END ) ?;
149
+ let open_token = get_token_with_kind ( & node, SyntaxKind :: BRACKET_START ) ?;
150
+ let close_token = get_token_with_kind ( & node, SyntaxKind :: BRACKET_END ) ?;
151
151
let is_in_inline_table = node. ancestors ( ) . any ( |a| a. kind ( ) == SyntaxKind :: INLINE_TABLE ) ;
152
- let force_use_new_lines = !is_in_inline_table && has_following_newline ( open_token. clone ( ) ) ;
152
+ let force_use_new_lines = !is_in_inline_table && has_following_newline ( open_token. clone ( ) ) && node . children_with_tokens ( ) . skip ( 3 ) . next ( ) . is_some ( ) ;
153
153
ensure_all_kind ( values. clone ( ) , SyntaxKind :: VALUE ) ?;
154
154
155
155
Ok ( gen_surrounded_by_tokens (
156
156
|context| {
157
+ let nodes = values. into_iter ( ) . map ( |v| v. into ( ) ) . collect :: < Vec < _ > > ( ) ;
158
+ if nodes. is_empty ( ) {
159
+ if force_use_new_lines {
160
+ return Signal :: NewLine . into ( ) ;
161
+ }
162
+ return PrintItems :: new ( ) ;
163
+ }
157
164
gen_comma_separated_values (
158
165
ParseCommaSeparatedValuesOptions {
159
- nodes : values . into_iter ( ) . map ( |v| v . into ( ) ) . collect :: < Vec < _ > > ( ) ,
166
+ nodes,
160
167
prefer_hanging : false ,
161
168
force_use_new_lines,
162
169
allow_blank_lines : true ,
@@ -205,8 +212,8 @@ fn gen_inline_table<'a>(node: SyntaxNode, context: &mut Context<'a>) -> PrintIte
205
212
}
206
213
207
214
fn gen_entry < ' a > ( node : SyntaxNode , context : & mut Context < ' a > ) -> PrintItemsResult {
208
- let key = get_child_with_kind ( node. clone ( ) , SyntaxKind :: KEY ) ?;
209
- let value = get_child_with_kind ( node. clone ( ) , SyntaxKind :: VALUE ) ?;
215
+ let key = get_child_with_kind ( & node, SyntaxKind :: KEY ) ?;
216
+ let value = get_child_with_kind ( & node, SyntaxKind :: VALUE ) ?;
210
217
let mut items = PrintItems :: new ( ) ;
211
218
212
219
items. extend ( gen_node ( key. into ( ) , context) ) ;
@@ -236,7 +243,7 @@ fn gen_children_inline<'a>(node: SyntaxNode, context: &mut Context<'a>) -> Print
236
243
237
244
fn gen_table_header < ' a > ( node : SyntaxNode , context : & mut Context < ' a > ) -> PrintItemsResult {
238
245
// Spec: Naming rules for tables are the same as for keys
239
- let key = get_child_with_kind ( node. clone ( ) , SyntaxKind :: KEY ) ?;
246
+ let key = get_child_with_kind ( & node, SyntaxKind :: KEY ) ?;
240
247
let mut items = PrintItems :: new ( ) ;
241
248
items. push_sc ( sc ! ( "[" ) ) ;
242
249
items. extend ( gen_node ( key. into ( ) , context) ) ;
@@ -246,7 +253,7 @@ fn gen_table_header<'a>(node: SyntaxNode, context: &mut Context<'a>) -> PrintIte
246
253
247
254
fn gen_table_array_header < ' a > ( node : SyntaxNode , context : & mut Context < ' a > ) -> PrintItemsResult {
248
255
// Spec: Naming rules for tables are the same as for keys
249
- let key = get_child_with_kind ( node. clone ( ) , SyntaxKind :: KEY ) ?;
256
+ let key = get_child_with_kind ( & node, SyntaxKind :: KEY ) ?;
250
257
let mut items = PrintItems :: new ( ) ;
251
258
items. push_sc ( sc ! ( "[[" ) ) ;
252
259
items. extend ( gen_node ( key. into ( ) , context) ) ;
@@ -264,7 +271,6 @@ fn gen_surrounded_by_tokens<'a, 'b>(
264
271
opts : ParseSurroundedByTokensParams ,
265
272
context : & mut Context < ' a > ,
266
273
) -> PrintItems {
267
- // parse
268
274
let mut items = PrintItems :: new ( ) ;
269
275
items. extend ( gen_node ( opts. open_token . clone ( ) . into ( ) , context) ) ;
270
276
@@ -533,14 +539,14 @@ fn get_children_with_non_trivia_tokens(node: SyntaxNode) -> impl Iterator<Item =
533
539
} )
534
540
}
535
541
536
- fn get_child_with_kind ( node : SyntaxNode , kind : SyntaxKind ) -> Result < SyntaxNode , ( ) > {
542
+ fn get_child_with_kind ( node : & SyntaxNode , kind : SyntaxKind ) -> Result < SyntaxNode , ( ) > {
537
543
match node. children ( ) . find ( |c| c. kind ( ) == kind) {
538
544
Some ( node) => Ok ( node) ,
539
545
None => Err ( ( ) ) ,
540
546
}
541
547
}
542
548
543
- fn get_token_with_kind ( node : SyntaxNode , kind : SyntaxKind ) -> Result < SyntaxToken , ( ) > {
549
+ fn get_token_with_kind ( node : & SyntaxNode , kind : SyntaxKind ) -> Result < SyntaxToken , ( ) > {
544
550
match node. children_with_tokens ( ) . find ( |c| c. kind ( ) == kind) {
545
551
Some ( NodeOrToken :: Token ( token) ) => Ok ( token) ,
546
552
_ => Err ( ( ) ) ,
0 commit comments