Add --extension support to the formatter (#9483)

## Summary

We added `--extension` to `ruff check`, but it's equally applicable to
`ruff format`.

Closes https://github.com/astral-sh/ruff/issues/9482.

Resolves https://github.com/astral-sh/ruff/discussions/9481.

## Test Plan

`cargo test`
This commit is contained in:
Charlie Marsh 2024-01-12 13:53:25 -05:00 committed by GitHub
parent d16c4a2d25
commit 3261d16e61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 221 additions and 51 deletions

View file

@ -117,6 +117,7 @@ pub struct Configuration {
pub output_format: Option<SerializationFormat>,
pub preview: Option<PreviewMode>,
pub required_version: Option<Version>,
pub extension: Option<ExtensionMapping>,
pub show_fixes: Option<bool>,
pub show_source: Option<bool>,
@ -174,6 +175,7 @@ impl Configuration {
let formatter = FormatterSettings {
exclude: FilePatternSet::try_from_iter(format.exclude.unwrap_or_default())?,
extension: self.extension.clone().unwrap_or_default(),
preview: format_preview,
target_version: match target_version {
PythonVersion::Py37 => ruff_python_formatter::PythonVersion::Py37,
@ -241,7 +243,7 @@ impl Configuration {
linter: LinterSettings {
rules: lint.as_rule_table(lint_preview),
exclude: FilePatternSet::try_from_iter(lint.exclude.unwrap_or_default())?,
extension: lint.extension.unwrap_or_default(),
extension: self.extension.unwrap_or_default(),
preview: lint_preview,
target_version,
project_root: project_root.to_path_buf(),
@ -496,6 +498,9 @@ impl Configuration {
.map(|src| resolve_src(&src, project_root))
.transpose()?,
target_version: options.target_version,
// `--extension` is a hidden command-line argument that isn't supported in configuration
// files at present.
extension: None,
lint: LintConfiguration::from_options(lint, project_root)?,
format: FormatConfiguration::from_options(
@ -538,6 +543,7 @@ impl Configuration {
src: self.src.or(config.src),
target_version: self.target_version.or(config.target_version),
preview: self.preview.or(config.preview),
extension: self.extension.or(config.extension),
lint: self.lint.combine(config.lint),
format: self.format.combine(config.format),
@ -549,7 +555,6 @@ impl Configuration {
pub struct LintConfiguration {
pub exclude: Option<Vec<FilePattern>>,
pub preview: Option<PreviewMode>,
pub extension: Option<ExtensionMapping>,
// Rule selection
pub extend_per_file_ignores: Vec<PerFileIgnore>,
@ -616,9 +621,6 @@ impl LintConfiguration {
.chain(options.common.extend_unfixable.into_iter().flatten())
.collect();
Ok(LintConfiguration {
// `--extension` is a hidden command-line argument that isn't supported in configuration
// files at present.
extension: None,
exclude: options.exclude.map(|paths| {
paths
.into_iter()
@ -954,7 +956,6 @@ impl LintConfiguration {
Self {
exclude: self.exclude.or(config.exclude),
preview: self.preview.or(config.preview),
extension: self.extension.or(config.extension),
rule_selections: config
.rule_selections
.into_iter()
@ -1031,6 +1032,7 @@ impl LintConfiguration {
pub struct FormatConfiguration {
pub exclude: Option<Vec<FilePattern>>,
pub preview: Option<PreviewMode>,
pub extension: Option<ExtensionMapping>,
pub indent_style: Option<IndentStyle>,
pub quote_style: Option<QuoteStyle>,
@ -1044,6 +1046,9 @@ impl FormatConfiguration {
#[allow(clippy::needless_pass_by_value)]
pub fn from_options(options: FormatOptions, project_root: &Path) -> Result<Self> {
Ok(Self {
// `--extension` is a hidden command-line argument that isn't supported in configuration
// files at present.
extension: None,
exclude: options.exclude.map(|paths| {
paths
.into_iter()
@ -1077,18 +1082,19 @@ impl FormatConfiguration {
#[must_use]
#[allow(clippy::needless_pass_by_value)]
pub fn combine(self, other: Self) -> Self {
pub fn combine(self, config: Self) -> Self {
Self {
exclude: self.exclude.or(other.exclude),
preview: self.preview.or(other.preview),
indent_style: self.indent_style.or(other.indent_style),
quote_style: self.quote_style.or(other.quote_style),
magic_trailing_comma: self.magic_trailing_comma.or(other.magic_trailing_comma),
line_ending: self.line_ending.or(other.line_ending),
docstring_code_format: self.docstring_code_format.or(other.docstring_code_format),
exclude: self.exclude.or(config.exclude),
preview: self.preview.or(config.preview),
extension: self.extension.or(config.extension),
indent_style: self.indent_style.or(config.indent_style),
quote_style: self.quote_style.or(config.quote_style),
magic_trailing_comma: self.magic_trailing_comma.or(config.magic_trailing_comma),
line_ending: self.line_ending.or(config.line_ending),
docstring_code_format: self.docstring_code_format.or(config.docstring_code_format),
docstring_code_line_width: self
.docstring_code_line_width
.or(other.docstring_code_line_width),
.or(config.docstring_code_line_width),
}
}
}

View file

@ -1,7 +1,9 @@
use path_absolutize::path_dedot;
use ruff_cache::cache_dir;
use ruff_formatter::{FormatOptions, IndentStyle, IndentWidth, LineWidth};
use ruff_linter::settings::types::{FilePattern, FilePatternSet, SerializationFormat, UnsafeFixes};
use ruff_linter::settings::types::{
ExtensionMapping, FilePattern, FilePatternSet, SerializationFormat, UnsafeFixes,
};
use ruff_linter::settings::LinterSettings;
use ruff_macros::CacheKey;
use ruff_python_ast::PySourceType;
@ -116,6 +118,7 @@ impl FileResolverSettings {
#[derive(CacheKey, Clone, Debug)]
pub struct FormatterSettings {
pub exclude: FilePatternSet,
pub extension: ExtensionMapping,
pub preview: PreviewMode,
pub target_version: ruff_python_formatter::PythonVersion,
@ -177,6 +180,7 @@ impl Default for FormatterSettings {
Self {
exclude: FilePatternSet::default(),
extension: ExtensionMapping::default(),
target_version: default_options.target_version(),
preview: PreviewMode::Disabled,
line_width: default_options.line_width(),