@@ -11,6 +11,20 @@ use jsonc_parser::ParseOptions;
1111pub fn format_text (
1212 input_text : & str ,
1313 format_with_host : impl FnMut ( & Path , String ) -> Result < Option < String > > ,
14+ ) -> Result < Option < String > > {
15+ let had_bom = input_text. starts_with ( "\u{FEFF} " ) ;
16+ let input_text = if had_bom { & input_text[ 3 ..] } else { input_text } ;
17+ let result = format_inner ( input_text, format_with_host) ?;
18+ if result. is_none ( ) && had_bom {
19+ Ok ( Some ( input_text. to_string ( ) ) )
20+ } else {
21+ Ok ( result)
22+ }
23+ }
24+
25+ fn format_inner (
26+ input_text : & str ,
27+ format_with_host : impl FnMut ( & Path , String ) -> Result < Option < String > > ,
1428) -> Result < Option < String > > {
1529 let parse_result = jsonc_parser:: parse_to_ast (
1630 input_text,
@@ -254,4 +268,48 @@ mod test {
254268 assert_eq ! ( get_indent_text( "\n hello" , 1 ) , "" ) ;
255269 assert_eq ! ( get_indent_text( "\n hello" , 2 ) , "" ) ;
256270 }
271+
272+ #[ test]
273+ fn formats_with_bom ( ) {
274+ // no changes to code other than bom
275+ {
276+ let input_text = "\u{FEFF} {\" cells\" :[{\" cell_type\" :\" code\" ,\" source\" :\" let x = 5;\" }]}" ;
277+ let formatted_text = format_text ( input_text, |_, text| Ok ( Some ( text) ) ) . unwrap ( ) . unwrap ( ) ;
278+ assert_eq ! (
279+ formatted_text,
280+ "{\" cells\" :[{\" cell_type\" :\" code\" ,\" source\" :\" let x = 5;\" }]}"
281+ ) ;
282+ }
283+ // other changes as well
284+ let input_text = "\u{FEFF} {
285+ \" cells\" :[{
286+ \" cell_type\" :\" code\" ,
287+ \" metadata\" : {
288+ \" vscode\" : {
289+ \" languageId\" : \" typescript\"
290+ }
291+ },
292+ \" source\" : \" let x = 5;\"
293+ }]
294+ }
295+ " ;
296+ let formatted_text = format_text ( input_text, |_, text| Ok ( Some ( format ! ( "{}_formatted" , text) ) ) )
297+ . unwrap ( )
298+ . unwrap ( ) ;
299+ assert_eq ! (
300+ formatted_text,
301+ "{
302+ \" cells\" :[{
303+ \" cell_type\" :\" code\" ,
304+ \" metadata\" : {
305+ \" vscode\" : {
306+ \" languageId\" : \" typescript\"
307+ }
308+ },
309+ \" source\" : \" let x = 5;_formatted\"
310+ }]
311+ }
312+ "
313+ ) ;
314+ }
257315}
0 commit comments