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
60 changes: 60 additions & 0 deletions crates/ruff/tests/cli/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2512,3 +2512,63 @@ fn markdown_formatting_stdin() -> Result<()> {
"#);
Ok(())
}

#[test]
fn format_mapped_extension_files() -> Result<()> {
let test = CliTest::with_files([
(
"pyproject.toml",
r#"
[tool.ruff]
extension = {foo="python", bar="markdown"}
"#,
),
(
"test.foo",
r"
print( 'hello' )
",
),
(
"test.bar",
r"
Text string

```py
print( 'hello' )
```
",
),
])?;

assert_cmd_snapshot!(
test.format_command()
.args(["format", "--preview", "--check", "."]),
@r#"
success: false
exit_code: 2
----- stdout -----
io: [TMP]/format: No such file or directory (os error 2)
--> format:1:1

unformatted: File would be reformatted
--> test.bar:1:1
2 | Text string
3 |
4 | ```py
- print( 'hello' )
5 + print("hello")
6 | ```

unformatted: File would be reformatted
--> test.foo:1:1
-
- print( 'hello' )
1 + print("hello")

2 files would be reformatted

----- stderr -----
"#);
Ok(())
}
11 changes: 10 additions & 1 deletion crates/ruff_linter/src/settings/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl Deref for GlobPath {
#[derive(Debug, Clone, CacheKey, PartialEq, PartialOrd, Eq, Ord)]
pub enum FilePattern {
Builtin(&'static str),
Config(String),
User(String, GlobPath),
}

Expand All @@ -209,6 +210,9 @@ impl FilePattern {
FilePattern::Builtin(pattern) => {
builder.add(Glob::from_str(pattern)?);
}
FilePattern::Config(pattern) => {
builder.add(Glob::new(&pattern)?);
}
FilePattern::User(pattern, absolute) => {
// Add the absolute path.
builder.add(Glob::new(&absolute.to_string_lossy())?);
Expand All @@ -230,7 +234,7 @@ impl Display for FilePattern {
"{:?}",
match self {
Self::Builtin(pattern) => pattern,
Self::User(pattern, _) => pattern.as_str(),
Self::User(pattern, _) | Self::Config(pattern) => pattern.as_str(),
}
)
}
Expand Down Expand Up @@ -498,6 +502,11 @@ impl From<ExtensionPair> for (String, Language) {
pub struct ExtensionMapping(FxHashMap<String, Language>);

impl ExtensionMapping {
/// Return the file extensions in the mapping.
pub fn extensions(&self) -> impl Iterator<Item = &String> {
self.0.keys()
}

/// Return the [`Language`] for the given file.
pub fn get(&self, path: &Path) -> Option<Language> {
let ext = path.extension()?.to_str()?;
Expand Down
16 changes: 13 additions & 3 deletions crates/ruff_workspace/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,19 @@ impl Configuration {
PreviewMode::Disabled => FilePatternSet::try_from_iter(
self.include.unwrap_or_else(|| INCLUDE.to_vec()),
)?,
PreviewMode::Enabled => FilePatternSet::try_from_iter(
self.include.unwrap_or_else(|| INCLUDE_PREVIEW.to_vec()),
)?,
PreviewMode::Enabled => {
FilePatternSet::try_from_iter(self.include.unwrap_or_else(|| {
let mut patterns = INCLUDE_PREVIEW.to_vec();
if let Some(extension_map) = &self.extension {
patterns.extend(
extension_map
.extensions()
.map(|ext| FilePattern::Config(format!("*.{ext}"))),
);
}
patterns
}))?
}
},
respect_gitignore: self.respect_gitignore.unwrap_or(true),
project_root: project_root.to_path_buf(),
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ pub struct Options {
/// by the `--extension` command-line flag).
///
/// Supported file types include `python`, `pyi`, `ipynb`, and `markdown`.
///
/// Any file extensions listed here will be automatically added to the
/// default `include` list as a `*.{ext}` glob, so that they are linted
/// and formatted without needing any additional configuration settings.
#[option(
default = "{}",
value_type = "dict[str, Language]",
Expand Down
2 changes: 1 addition & 1 deletion ruff.schema.json

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

Loading