Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/ruff_linter/src/settings/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,11 @@ impl ExtensionMapping {
let ext = path.extension()?.to_str()?;
self.0.get(ext).copied()
}

/// Return the [`Language`] for a given file extension.
pub fn get_extension(&self, ext: &str) -> Option<Language> {
self.0.get(ext).copied()
}
}

impl From<FxHashMap<String, Language>> for ExtensionMapping {
Expand Down
6 changes: 5 additions & 1 deletion crates/ruff_markdown/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ ruff_source_file = { workspace = true }
ruff_text_size = { workspace = true }
ruff_workspace = { workspace = true }

insta = { workspace = true }
regex = { workspace = true }

[dev-dependencies]
ruff_linter = { workspace = true }

insta = { workspace = true }

[lints]
workspace = true
29 changes: 28 additions & 1 deletion crates/ruff_markdown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ pub fn format_code_blocks(
let end = code_line.start();
let unformatted_code = dedent(&source[TextRange::new(start, end)]);

let py_source_type = PySourceType::from_extension(&language);
let py_source_type = match settings.extension.get_extension(&language) {
None => PySourceType::from_extension(&language),
Some(language) => PySourceType::from(language),
};
let options =
settings.to_format_options(py_source_type, &unformatted_code, path);

Expand Down Expand Up @@ -132,6 +135,7 @@ pub fn format_code_blocks(
#[cfg(test)]
mod tests {
use insta::assert_snapshot;
use ruff_linter::settings::types::{ExtensionMapping, ExtensionPair, Language};
use ruff_workspace::FormatterSettings;

use crate::{MarkdownResult, format_code_blocks};
Expand Down Expand Up @@ -384,4 +388,27 @@ print( 'hello' )
&FormatterSettings::default()
), @"Unchanged");
}

#[test]
fn format_code_blocks_extension_mapping() {
// format "py" mapped as "pyi" instead
let code = r#"
```py
def foo(): ...
def bar(): ...
```
"#;
let mapping = ExtensionMapping::from_iter([ExtensionPair {
extension: "py".to_string(),
language: Language::Pyi,
}]);
assert_snapshot!(format_code_blocks(
code,
None,
&FormatterSettings {
extension: mapping,
..Default::default()
}
), @"Unchanged");
}
}
Loading