mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-02 09:52:18 +00:00
Formatter: Add SourceType to context to enable special formatting for stub files (#6331)
**Summary** This adds the information whether we're in a .py python source file or in a .pyi stub file to enable people working on #5822 and related issues. I'm not completely happy with `Default` for something that depends on the input. **Test Plan** None, this is currently unused, i'm leaving this to first implementation of stub file specific formatting. --------- Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
fe97a2a302
commit
1031bb6550
12 changed files with 137 additions and 97 deletions
|
@ -376,10 +376,10 @@ fn format_dev_project(
|
|||
|
||||
// TODO(konstin): The assumptions between this script (one repo) and ruff (pass in a bunch of
|
||||
// files) mismatch.
|
||||
let options = BlackOptions::from_file(&files[0])?.to_py_format_options();
|
||||
let black_options = BlackOptions::from_file(&files[0])?;
|
||||
debug!(
|
||||
parent: None,
|
||||
"Options for {}: {options:?}",
|
||||
"Options for {}: {black_options:?}",
|
||||
files[0].display()
|
||||
);
|
||||
|
||||
|
@ -398,7 +398,7 @@ fn format_dev_project(
|
|||
paths
|
||||
.into_par_iter()
|
||||
.map(|dir_entry| {
|
||||
let result = format_dir_entry(dir_entry, stability_check, write, &options);
|
||||
let result = format_dir_entry(dir_entry, stability_check, write, &black_options);
|
||||
pb_span.pb_inc(1);
|
||||
result
|
||||
})
|
||||
|
@ -447,7 +447,7 @@ fn format_dir_entry(
|
|||
dir_entry: Result<DirEntry, ignore::Error>,
|
||||
stability_check: bool,
|
||||
write: bool,
|
||||
options: &PyFormatOptions,
|
||||
options: &BlackOptions,
|
||||
) -> anyhow::Result<(Result<Statistics, CheckFileError>, PathBuf), Error> {
|
||||
let dir_entry = match dir_entry.context("Iterating the files in the repository failed") {
|
||||
Ok(dir_entry) => dir_entry,
|
||||
|
@ -460,27 +460,27 @@ fn format_dir_entry(
|
|||
}
|
||||
|
||||
let file = dir_entry.path().to_path_buf();
|
||||
let options = options.to_py_format_options(&file);
|
||||
// Handle panics (mostly in `debug_assert!`)
|
||||
let result =
|
||||
match catch_unwind(|| format_dev_file(&file, stability_check, write, options.clone())) {
|
||||
Ok(result) => result,
|
||||
Err(panic) => {
|
||||
if let Some(message) = panic.downcast_ref::<String>() {
|
||||
Err(CheckFileError::Panic {
|
||||
message: message.clone(),
|
||||
})
|
||||
} else if let Some(&message) = panic.downcast_ref::<&str>() {
|
||||
Err(CheckFileError::Panic {
|
||||
message: message.to_string(),
|
||||
})
|
||||
} else {
|
||||
Err(CheckFileError::Panic {
|
||||
// This should not happen, but it can
|
||||
message: "(Panic didn't set a string message)".to_string(),
|
||||
})
|
||||
}
|
||||
let result = match catch_unwind(|| format_dev_file(&file, stability_check, write, options)) {
|
||||
Ok(result) => result,
|
||||
Err(panic) => {
|
||||
if let Some(message) = panic.downcast_ref::<String>() {
|
||||
Err(CheckFileError::Panic {
|
||||
message: message.clone(),
|
||||
})
|
||||
} else if let Some(&message) = panic.downcast_ref::<&str>() {
|
||||
Err(CheckFileError::Panic {
|
||||
message: message.to_string(),
|
||||
})
|
||||
} else {
|
||||
Err(CheckFileError::Panic {
|
||||
// This should not happen, but it can
|
||||
message: "(Panic didn't set a string message)".to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
Ok((result, file))
|
||||
}
|
||||
|
||||
|
@ -833,9 +833,8 @@ impl BlackOptions {
|
|||
Self::from_toml(&fs::read_to_string(&path)?, repo)
|
||||
}
|
||||
|
||||
fn to_py_format_options(&self) -> PyFormatOptions {
|
||||
let mut options = PyFormatOptions::default();
|
||||
options
|
||||
fn to_py_format_options(&self, file: &Path) -> PyFormatOptions {
|
||||
PyFormatOptions::from_extension(file)
|
||||
.with_line_width(
|
||||
LineWidth::try_from(self.line_length).expect("Invalid line length limit"),
|
||||
)
|
||||
|
@ -843,8 +842,7 @@ impl BlackOptions {
|
|||
MagicTrailingComma::Ignore
|
||||
} else {
|
||||
MagicTrailingComma::Respect
|
||||
});
|
||||
options
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -868,7 +866,7 @@ mod tests {
|
|||
"};
|
||||
let options = BlackOptions::from_toml(toml, Path::new("pyproject.toml"))
|
||||
.unwrap()
|
||||
.to_py_format_options();
|
||||
.to_py_format_options(Path::new("code_inline.py"));
|
||||
assert_eq!(options.line_width(), LineWidth::try_from(119).unwrap());
|
||||
assert!(matches!(
|
||||
options.magic_trailing_comma(),
|
||||
|
@ -887,7 +885,7 @@ mod tests {
|
|||
"#};
|
||||
let options = BlackOptions::from_toml(toml, Path::new("pyproject.toml"))
|
||||
.unwrap()
|
||||
.to_py_format_options();
|
||||
.to_py_format_options(Path::new("code_inline.py"));
|
||||
assert_eq!(options.line_width(), LineWidth::try_from(130).unwrap());
|
||||
assert!(matches!(
|
||||
options.magic_trailing_comma(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue