@@ -7,7 +7,7 @@ use super::helpers::*;
7
7
use crate :: configuration:: Configuration ;
8
8
9
9
pub fn parse_items ( file : & Dockerfile , text : & str , config : & Configuration ) -> PrintItems {
10
- let mut context = Context :: new ( text, config) ;
10
+ let mut context = Context :: new ( text, file , config) ;
11
11
let mut items = PrintItems :: new ( ) ;
12
12
let top_level_nodes = context. parse_nodes_with_comments ( 0 , text. len ( ) , file. instructions . iter ( ) . map ( |i| i. into ( ) ) ) ;
13
13
@@ -158,23 +158,61 @@ fn parse_label<'a>(node: &'a Label, context: &mut Context<'a>) -> PrintItems {
158
158
}
159
159
160
160
fn parse_multi_line_items < ' a > ( nodes : Vec < Node < ' a > > , indent_width : u32 , context : & mut Context < ' a > ) -> PrintItems {
161
- let mut items = PrintItems :: new ( ) ;
162
161
let count = nodes. len ( ) ;
163
- for ( i, node) in nodes. into_iter ( ) . enumerate ( ) {
164
- let is_comment = node. is_comment ( ) ;
165
- let mut node_items = parse_node ( node, context) ;
166
- if i < count - 1 && !is_comment {
167
- node_items. push_str ( " \\ " ) ;
168
- node_items. push_signal ( Signal :: NewLine ) ;
169
- }
170
-
171
- if i > 0 {
172
- items. extend ( parser_helpers:: with_indent_times ( node_items, indent_width) ) ;
173
- } else {
174
- items. extend ( node_items) ;
175
- }
176
- }
177
- items
162
+ let nodes_with_line_index = nodes
163
+ . into_iter ( )
164
+ . map ( |node| {
165
+ let ( line_index, _) = node. span ( ) . relative_span ( context. dockerfile ) ;
166
+ ( node, line_index)
167
+ } )
168
+ . collect :: < Vec < _ > > ( ) ;
169
+ let force_use_new_lines = nodes_with_line_index. len ( ) > 1 && nodes_with_line_index[ 0 ] . 1 < nodes_with_line_index[ 1 ] . 1 ;
170
+
171
+ parser_helpers:: parse_separated_values (
172
+ |is_multiline| {
173
+ nodes_with_line_index
174
+ . into_iter ( )
175
+ . enumerate ( )
176
+ . map ( |( i, ( node, line_index) ) | {
177
+ let is_comment = node. is_comment ( ) ;
178
+ let mut node_items = parse_node ( node, context) ;
179
+ if i < count - 1 && !is_comment {
180
+ node_items. push_condition ( conditions:: if_true ( "endLineText" , is_multiline. create_resolver ( ) , {
181
+ let mut items = PrintItems :: new ( ) ;
182
+ items. push_str ( " \\ " ) ;
183
+ items
184
+ } ) ) ;
185
+ }
186
+
187
+ parser_helpers:: ParsedValue {
188
+ items : if i > 0 {
189
+ parser_helpers:: with_indent_times ( node_items, indent_width)
190
+ } else {
191
+ node_items
192
+ } ,
193
+ lines_span : Some ( parser_helpers:: LinesSpan {
194
+ start_line : line_index,
195
+ end_line : line_index,
196
+ } ) ,
197
+ allow_inline_multi_line : false ,
198
+ allow_inline_single_line : false ,
199
+ }
200
+ } )
201
+ . collect ( )
202
+ } ,
203
+ parser_helpers:: ParseSeparatedValuesOptions {
204
+ prefer_hanging : false ,
205
+ force_use_new_lines,
206
+ allow_blank_lines : false ,
207
+ single_line_space_at_start : false ,
208
+ single_line_space_at_end : false ,
209
+ single_line_separator : Signal :: SpaceOrNewLine . into ( ) ,
210
+ indent_width : 0 as u8 ,
211
+ multi_line_options : parser_helpers:: MultiLineOptions :: same_line_no_indent ( ) ,
212
+ force_possible_newline_at_start : false ,
213
+ } ,
214
+ )
215
+ . items
178
216
}
179
217
180
218
fn parse_misc_instruction < ' a > ( node : & ' a MiscInstruction , context : & mut Context < ' a > ) -> PrintItems {
0 commit comments