Skip to content

Commit b32cf11

Browse files
committed
Use configured extension mapping to select code block language
1 parent ce5f7b6 commit b32cf11

4 files changed

Lines changed: 39 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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ pub fn format_code_blocks(
9393
let end = code_line.start();
9494
let unformatted_code = dedent(&source[TextRange::new(start, end)]);
9595

96-
let py_source_type = PySourceType::from_extension(&language);
96+
let py_source_type = match settings.extension.get_extension(&language) {
97+
None => PySourceType::from_extension(&language),
98+
Some(language) => PySourceType::from(language),
99+
};
97100
let options =
98101
settings.to_format_options(py_source_type, &unformatted_code, path);
99102

@@ -132,6 +135,7 @@ pub fn format_code_blocks(
132135
#[cfg(test)]
133136
mod tests {
134137
use insta::assert_snapshot;
138+
use ruff_linter::settings::types::{ExtensionMapping, ExtensionPair, Language};
135139
use ruff_workspace::FormatterSettings;
136140

137141
use crate::{MarkdownResult, format_code_blocks};
@@ -384,4 +388,27 @@ print( 'hello' )
384388
&FormatterSettings::default()
385389
), @"Unchanged");
386390
}
391+
392+
#[test]
393+
fn format_code_blocks_extension_mapping() {
394+
// format "py" mapped as "pyi" instead
395+
let code = r#"
396+
```py
397+
def foo(): ...
398+
def bar(): ...
399+
```
400+
"#;
401+
let mapping = ExtensionMapping::from_iter([ExtensionPair {
402+
extension: "py".to_string(),
403+
language: Language::Pyi,
404+
}]);
405+
assert_snapshot!(format_code_blocks(
406+
code,
407+
None,
408+
&FormatterSettings {
409+
extension: mapping,
410+
..Default::default()
411+
}
412+
), @"Unchanged");
413+
}
387414
}

0 commit comments

Comments
 (0)