@@ -88,7 +88,11 @@ pub fn format_code_blocks(
8888 let end = code_line. start ( ) ;
8989 let unformatted_code = dedent ( & source[ TextRange :: new ( start, end) ] ) ;
9090
91- let py_source_type = PySourceType :: from_extension ( & language) ;
91+ // map code block to source type, accounting for configured extension mappings
92+ let py_source_type = match settings. extension . get_extension ( & language) {
93+ None => PySourceType :: from_extension ( & language) ,
94+ Some ( language) => PySourceType :: from ( language) ,
95+ } ;
9296 let options =
9397 settings. to_format_options ( py_source_type, & unformatted_code, path) ;
9498
@@ -128,6 +132,7 @@ pub fn format_code_blocks(
128132#[ cfg( test) ]
129133mod tests {
130134 use insta:: assert_snapshot;
135+ use ruff_linter:: settings:: types:: { ExtensionMapping , ExtensionPair , Language } ;
131136 use ruff_workspace:: FormatterSettings ;
132137
133138 use crate :: { MarkdownResult , format_code_blocks} ;
@@ -380,4 +385,27 @@ print( 'hello' )
380385 & FormatterSettings :: default ( )
381386 ) , @"Unchanged" ) ;
382387 }
388+
389+ #[ test]
390+ fn format_code_blocks_extension_mapping ( ) {
391+ // format "py" mapped as "pyi" instead
392+ let code = r#"
393+ ```py
394+ def foo(): ...
395+ def bar(): ...
396+ ```
397+ "# ;
398+ let mapping = ExtensionMapping :: from_iter ( [ ExtensionPair {
399+ extension : "py" . to_string ( ) ,
400+ language : Language :: Pyi ,
401+ } ] ) ;
402+ assert_snapshot ! ( format_code_blocks(
403+ code,
404+ None ,
405+ & FormatterSettings {
406+ extension: mapping,
407+ ..Default :: default ( )
408+ }
409+ ) , @"Unchanged" ) ;
410+ }
383411}
0 commit comments