1
+ use std:: borrow:: Cow ;
2
+
1
3
use crate :: indentation:: Indentation ;
2
4
use crate :: inline_block:: InlineBlock ;
3
5
use crate :: params:: Params ;
4
6
use crate :: tokenizer:: { Token , TokenKind } ;
5
7
use crate :: { FormatOptions , QueryParams } ;
6
- use itertools:: Itertools ;
7
- use std:: borrow:: Cow ;
8
8
9
9
pub ( crate ) fn format ( tokens : & [ Token < ' _ > ] , params : & QueryParams , options : FormatOptions ) -> String {
10
10
let mut formatter = Formatter :: new ( tokens, params, options) ;
@@ -223,25 +223,25 @@ impl<'a> Formatter<'a> {
223
223
}
224
224
225
225
fn indent_comment ( & self , token : & str ) -> String {
226
- token
227
- . split ( '\n' )
228
- . enumerate ( )
229
- . map ( | ( i , line) | {
230
- if i == 0 {
231
- return line . to_string ( ) ;
232
- }
233
- if !line . starts_with ( |c| c == ' ' || c == '\t' ) {
234
- return line . to_string ( ) ;
235
- }
236
- format ! (
237
- "{} {}" ,
238
- self . indentation . get_indent ( ) ,
239
- line. chars ( )
240
- . skip_while ( | & c| c == ' ' || c == '\t' )
241
- . collect :: < String > ( )
242
- )
243
- } )
244
- . join ( " \n " )
226
+ let mut combined = String :: with_capacity ( token. len ( ) + 4 ) ;
227
+ for ( i , line ) in token . split ( '\n' ) . enumerate ( ) {
228
+ if i == 0 {
229
+ combined . push_str ( line)
230
+ } else if line . starts_with ( [ ' ' , '\t' ] ) {
231
+ let indent = self . indentation . get_indent ( ) ;
232
+ let start_trimmed = line . trim_start_matches ( [ ' ' , '\t' ] ) ;
233
+ combined . reserve ( indent . len ( ) + start_trimmed . len ( ) + 2 ) ;
234
+ combined . push ( '\n' ) ;
235
+ combined . push_str ( & indent ) ;
236
+ combined . push ( ' ' ) ;
237
+ combined . push_str ( start_trimmed ) ;
238
+ } else {
239
+ combined . reserve ( line. len ( ) + 1 ) ;
240
+ combined . push ( '\n' ) ;
241
+ combined . push_str ( line ) ;
242
+ }
243
+ }
244
+ combined
245
245
}
246
246
247
247
fn format_reserved_word < ' t > ( & self , token : & ' t str ) -> Cow < ' t , str > {
@@ -252,12 +252,16 @@ impl<'a> Formatter<'a> {
252
252
}
253
253
}
254
254
255
- // Replace any sequence of whitespace characters with single space
255
+ /// Replace any sequence of whitespace characters with single space
256
256
fn equalize_whitespace ( & self , token : & str ) -> String {
257
- token
258
- . split ( char:: is_whitespace)
259
- . filter ( |s| !s. is_empty ( ) )
260
- . join ( " " )
257
+ let mut combined = String :: with_capacity ( token. len ( ) ) ;
258
+ for s in token. split ( char:: is_whitespace) . filter ( |s| !s. is_empty ( ) ) {
259
+ if !combined. is_empty ( ) {
260
+ combined. push ( ' ' ) ;
261
+ }
262
+ combined. push_str ( s) ;
263
+ }
264
+ combined
261
265
}
262
266
263
267
fn previous_token ( & self ) -> Option < & Token < ' _ > > {
0 commit comments