@@ -10,17 +10,17 @@ use tower_lsp_server::{
1010 lsp_types:: { Position , Range , TextEdit , Uri } ,
1111} ;
1212
13- use crate :: FORMAT_CONFIG_FILE ;
14-
13+ use crate :: formatter :: options :: FormatOptions as LSPFormatOptions ;
14+ use crate :: { FORMAT_CONFIG_FILE , utils :: normalize_path } ;
1515pub struct ServerFormatter {
1616 options : FormatOptions ,
1717}
1818
1919impl ServerFormatter {
20- pub fn new ( root_uri : & Uri ) -> Self {
20+ pub fn new ( root_uri : & Uri , options : & LSPFormatOptions ) -> Self {
2121 let root_path = root_uri. to_file_path ( ) . unwrap ( ) ;
2222
23- Self { options : Self :: get_format_options ( & root_path) }
23+ Self { options : Self :: get_format_options ( & root_path, options . config_path . as_ref ( ) ) }
2424 }
2525
2626 pub fn run_single ( & self , uri : & Uri , content : Option < String > ) -> Option < Vec < TextEdit > > {
@@ -75,9 +75,9 @@ impl ServerFormatter {
7575 ) ] )
7676 }
7777
78- fn get_format_options ( root_path : & Path ) -> FormatOptions {
79- let config_path = FORMAT_CONFIG_FILE ;
80- let config = root_path. join ( config_path) ; // normalize_path when supporting `oxc.fmt.configPath`
78+ fn get_format_options ( root_path : & Path , config_path : Option < & String > ) -> FormatOptions {
79+ let config_path = config_path . map_or ( FORMAT_CONFIG_FILE , |v| v ) ;
80+ let config = normalize_path ( root_path. join ( config_path) ) ;
8181 let oxfmtrc = if config. try_exists ( ) . is_ok_and ( |exists| exists) {
8282 if let Ok ( oxfmtrc) = Oxfmtrc :: from_file ( & config) {
8383 oxfmtrc
@@ -232,13 +232,31 @@ mod tests {
232232
233233 #[ test]
234234 fn test_formatter ( ) {
235- Tester :: new ( "fixtures/formatter/basic" , Some ( FormatOptions { experimental : true } ) )
236- . format_and_snapshot_single_file ( "basic.ts" ) ;
235+ Tester :: new (
236+ "fixtures/formatter/basic" ,
237+ Some ( FormatOptions { experimental : true , ..Default :: default ( ) } ) ,
238+ )
239+ . format_and_snapshot_single_file ( "basic.ts" ) ;
237240 }
238241
239242 #[ test]
240243 fn test_root_config_detection ( ) {
241- Tester :: new ( "fixtures/formatter/root_config" , Some ( FormatOptions { experimental : true } ) )
242- . format_and_snapshot_single_file ( "semicolons-as-needed.ts" ) ;
244+ Tester :: new (
245+ "fixtures/formatter/root_config" ,
246+ Some ( FormatOptions { experimental : true , ..Default :: default ( ) } ) ,
247+ )
248+ . format_and_snapshot_single_file ( "semicolons-as-needed.ts" ) ;
249+ }
250+
251+ #[ test]
252+ fn test_custom_config_path ( ) {
253+ Tester :: new (
254+ "fixtures/formatter/custom_config_path" ,
255+ Some ( FormatOptions {
256+ experimental : true ,
257+ config_path : Some ( "./format.json" . to_string ( ) ) ,
258+ } ) ,
259+ )
260+ . format_and_snapshot_single_file ( "semicolons-as-needed.ts" ) ;
243261 }
244262}
0 commit comments