Skip to content

Commit cefa871

Browse files
committed
Use configured extension mapping to select code block language
1 parent dc6b5b6 commit cefa871

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ruff_linter/src/settings/types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,11 @@ impl ExtensionMapping {
500500
let ext = path.extension()?.to_str()?;
501501
self.0.get(ext).copied()
502502
}
503+
504+
/// Return the [`Language`] for a given file extension.
505+
pub fn get_extension(&self, ext: &str) -> Option<Language> {
506+
self.0.get(ext).copied()
507+
}
503508
}
504509

505510
impl From<FxHashMap<String, Language>> for ExtensionMapping {

crates/ruff_markdown/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ ruff_source_file = { workspace = true }
1717
ruff_text_size = { workspace = true }
1818
ruff_workspace = { workspace = true }
1919

20-
insta = { workspace = true }
2120
regex = { workspace = true }
2221

22+
[dev-dependencies]
23+
ruff_linter = { workspace = true }
24+
25+
insta = { workspace = true }
26+
2327
[lints]
2428
workspace = true

crates/ruff_markdown/src/lib.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)]
129133
mod 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

Comments
 (0)