@@ -10,23 +10,35 @@ use std::path::Path;
10
10
use taplo:: syntax:: SyntaxNode ;
11
11
12
12
pub fn format_text ( file_path : & Path , text : & str , config : & Configuration ) -> Result < Option < String > > {
13
- let node = parse_and_process_node ( file_path, text, config) ?;
14
-
15
- let result = dprint_core:: formatting:: format ( || generate ( node, text, config) , config_to_print_options ( text, config) ) ;
13
+ let result = format_text_inner ( file_path, text, config) ?;
16
14
if result == text {
17
15
Ok ( None )
18
16
} else {
19
17
Ok ( Some ( result) )
20
18
}
21
19
}
22
20
21
+ fn format_text_inner ( file_path : & Path , text : & str , config : & Configuration ) -> Result < String > {
22
+ let text = strip_bom ( text) ;
23
+ let node = parse_and_process_node ( file_path, text, config) ?;
24
+
25
+ Ok ( dprint_core:: formatting:: format (
26
+ || generate ( node, text, config) ,
27
+ config_to_print_options ( text, config) ,
28
+ ) )
29
+ }
30
+
23
31
#[ cfg( feature = "tracing" ) ]
24
32
pub fn trace_file ( file_path : & Path , text : & str , config : & Configuration ) -> dprint_core:: formatting:: TracingResult {
25
33
let node = parse_and_process_node ( file_path, text, config) . unwrap ( ) ;
26
34
27
35
dprint_core:: formatting:: trace_printing ( || generate ( node, text, config) , config_to_print_options ( text, config) )
28
36
}
29
37
38
+ fn strip_bom ( text : & str ) -> & str {
39
+ text. strip_prefix ( "\u{FEFF} " ) . unwrap_or ( text)
40
+ }
41
+
30
42
fn parse_and_process_node ( file_path : & Path , text : & str , config : & Configuration ) -> Result < SyntaxNode > {
31
43
let node = parse_taplo ( text) ?;
32
44
@@ -58,3 +70,14 @@ fn config_to_print_options(text: &str, config: &Configuration) -> PrintOptions {
58
70
new_line_text : resolve_new_line_kind ( text, config. new_line_kind ) ,
59
71
}
60
72
}
73
+
74
+ #[ cfg( test) ]
75
+ mod test {
76
+ #[ test]
77
+ fn strips_bom ( ) {
78
+ let config = crate :: configuration:: ConfigurationBuilder :: new ( ) . build ( ) ;
79
+ let file_text = crate :: format_text:: format_text ( & std:: path:: PathBuf :: from ( "file.toml" ) , "\u{FEFF} # 1\n # 2\n " , & config) . unwrap ( ) ;
80
+
81
+ assert_eq ! ( file_text. unwrap( ) , "# 1\n # 2\n " ) ;
82
+ }
83
+ }
0 commit comments