Skip to content

Commit

Permalink
Rename ConfigProcessor to ConfigurationTransformer (#7536)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Sep 20, 2023
1 parent b19eec9 commit 83daddb
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 53 deletions.
20 changes: 11 additions & 9 deletions crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ruff_linter::settings::types::{
};
use ruff_linter::{RuleSelector, RuleSelectorParser};
use ruff_workspace::configuration::{Configuration, RuleSelection};
use ruff_workspace::resolver::ConfigProcessor;
use ruff_workspace::resolver::ConfigurationTransformer;

#[derive(Debug, Parser)]
#[command(
Expand Down Expand Up @@ -452,7 +452,7 @@ impl From<&LogLevelArgs> for LogLevel {
impl CheckCommand {
/// Partition the CLI into command-line arguments and configuration
/// overrides.
pub fn partition(self) -> (CheckArguments, Overrides) {
pub fn partition(self) -> (CheckArguments, CliOverrides) {
(
CheckArguments {
add_noqa: self.add_noqa,
Expand All @@ -472,7 +472,7 @@ impl CheckCommand {
stdin_filename: self.stdin_filename,
watch: self.watch,
},
Overrides {
CliOverrides {
dummy_variable_rgx: self.dummy_variable_rgx,
exclude: self.exclude,
extend_exclude: self.extend_exclude,
Expand Down Expand Up @@ -508,7 +508,7 @@ impl CheckCommand {
impl FormatCommand {
/// Partition the CLI into command-line arguments and configuration
/// overrides.
pub fn partition(self) -> (FormatArguments, Overrides) {
pub fn partition(self) -> (FormatArguments, CliOverrides) {
(
FormatArguments {
check: self.check,
Expand All @@ -517,7 +517,7 @@ impl FormatCommand {
isolated: self.isolated,
stdin_filename: self.stdin_filename,
},
Overrides {
CliOverrides {
line_length: self.line_length,
respect_gitignore: resolve_bool_arg(
self.respect_gitignore,
Expand All @@ -526,7 +526,7 @@ impl FormatCommand {
preview: resolve_bool_arg(self.preview, self.no_preview).map(PreviewMode::from),
force_exclude: resolve_bool_arg(self.force_exclude, self.no_force_exclude),
// Unsupported on the formatter CLI, but required on `Overrides`.
..Overrides::default()
..CliOverrides::default()
},
)
}
Expand Down Expand Up @@ -577,7 +577,7 @@ pub struct FormatArguments {
/// CLI settings that function as configuration overrides.
#[derive(Clone, Default)]
#[allow(clippy::struct_excessive_bools)]
pub struct Overrides {
pub struct CliOverrides {
pub dummy_variable_rgx: Option<Regex>,
pub exclude: Option<Vec<FilePattern>>,
pub extend_exclude: Option<Vec<FilePattern>>,
Expand All @@ -604,8 +604,8 @@ pub struct Overrides {
pub show_fixes: Option<bool>,
}

impl ConfigProcessor for Overrides {
fn process_config(&self, config: &mut Configuration) {
impl ConfigurationTransformer for CliOverrides {
fn transform(&self, mut config: Configuration) -> Configuration {
if let Some(cache_dir) = &self.cache_dir {
config.cache_dir = Some(cache_dir.clone());
}
Expand Down Expand Up @@ -671,6 +671,8 @@ impl ConfigProcessor for Overrides {
if let Some(target_version) = &self.target_version {
config.target_version = Some(*target_version);
}

config
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_cli/src/commands/add_noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use ruff_linter::warn_user_once;
use ruff_python_ast::{PySourceType, SourceType};
use ruff_workspace::resolver::{python_files_in_path, PyprojectConfig};

use crate::args::Overrides;
use crate::args::CliOverrides;
use crate::diagnostics::LintSource;

/// Add `noqa` directives to a collection of files.
pub(crate) fn add_noqa(
files: &[PathBuf],
pyproject_config: &PyprojectConfig,
overrides: &Overrides,
overrides: &CliOverrides,
) -> Result<usize> {
// Collect all the files to check.
let start = Instant::now();
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use ruff_source_file::SourceFileBuilder;
use ruff_text_size::{TextRange, TextSize};
use ruff_workspace::resolver::{python_files_in_path, PyprojectConfig, PyprojectDiscoveryStrategy};

use crate::args::Overrides;
use crate::args::CliOverrides;
use crate::cache::{self, Cache};
use crate::diagnostics::Diagnostics;
use crate::panic::catch_unwind;
Expand All @@ -32,7 +32,7 @@ use crate::panic::catch_unwind;
pub(crate) fn check(
files: &[PathBuf],
pyproject_config: &PyprojectConfig,
overrides: &Overrides,
overrides: &CliOverrides,
cache: flags::Cache,
noqa: flags::Noqa,
autofix: flags::FixMode,
Expand Down Expand Up @@ -241,7 +241,7 @@ mod test {
use ruff_linter::settings::{flags, Settings};
use ruff_workspace::resolver::{PyprojectConfig, PyprojectDiscoveryStrategy};

use crate::args::Overrides;
use crate::args::CliOverrides;

use super::check;

Expand Down Expand Up @@ -277,7 +277,7 @@ mod test {
// Notebooks are not included by default
&[tempdir.path().to_path_buf(), notebook],
&pyproject_config,
&Overrides::default(),
&CliOverrides::default(),
flags::Cache::Disabled,
flags::Noqa::Disabled,
flags::FixMode::Generate,
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_cli/src/commands/check_stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use ruff_linter::packaging;
use ruff_linter::settings::flags;
use ruff_workspace::resolver::{python_file_at_path, PyprojectConfig};

use crate::args::Overrides;
use crate::args::CliOverrides;
use crate::diagnostics::{lint_stdin, Diagnostics};
use crate::stdin::read_from_stdin;

/// Run the linter over a single file, read from `stdin`.
pub(crate) fn check_stdin(
filename: Option<&Path>,
pyproject_config: &PyprojectConfig,
overrides: &Overrides,
overrides: &CliOverrides,
noqa: flags::Noqa,
autofix: flags::FixMode,
) -> Result<Diagnostics> {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use ruff_python_formatter::{format_module, FormatModuleError, PyFormatOptions};
use ruff_source_file::{find_newline, LineEnding};
use ruff_workspace::resolver::python_files_in_path;

use crate::args::{FormatArguments, Overrides};
use crate::args::{CliOverrides, FormatArguments};
use crate::panic::{catch_unwind, PanicError};
use crate::resolve::resolve;
use crate::ExitStatus;
Expand All @@ -38,7 +38,7 @@ pub(crate) enum FormatMode {
/// Format a set of files, and return the exit status.
pub(crate) fn format(
cli: &FormatArguments,
overrides: &Overrides,
overrides: &CliOverrides,
log_level: LogLevel,
) -> Result<ExitStatus> {
let pyproject_config = resolve(
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_cli/src/commands/format_stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use ruff_linter::settings::types::PreviewMode;
use ruff_python_formatter::{format_module, PyFormatOptions};
use ruff_workspace::resolver::python_file_at_path;

use crate::args::{FormatArguments, Overrides};
use crate::args::{CliOverrides, FormatArguments};
use crate::commands::format::{FormatCommandError, FormatCommandResult, FormatMode};
use crate::resolve::resolve;
use crate::stdin::read_from_stdin;
use crate::ExitStatus;

/// Run the formatter over a single file, read from `stdin`.
pub(crate) fn format_stdin(cli: &FormatArguments, overrides: &Overrides) -> Result<ExitStatus> {
pub(crate) fn format_stdin(cli: &FormatArguments, overrides: &CliOverrides) -> Result<ExitStatus> {
let pyproject_config = resolve(
cli.isolated,
cli.config.as_deref(),
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_cli/src/commands/show_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use itertools::Itertools;
use ruff_linter::warn_user_once;
use ruff_workspace::resolver::{python_files_in_path, PyprojectConfig};

use crate::args::Overrides;
use crate::args::CliOverrides;

/// Show the list of files to be checked based on current settings.
pub(crate) fn show_files(
files: &[PathBuf],
pyproject_config: &PyprojectConfig,
overrides: &Overrides,
overrides: &CliOverrides,
writer: &mut impl Write,
) -> Result<()> {
// Collect all files in the hierarchy.
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_cli/src/commands/show_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use itertools::Itertools;

use ruff_workspace::resolver::{python_files_in_path, PyprojectConfig};

use crate::args::Overrides;
use crate::args::CliOverrides;

/// Print the user-facing configuration settings.
pub(crate) fn show_settings(
files: &[PathBuf],
pyproject_config: &PyprojectConfig,
overrides: &Overrides,
overrides: &CliOverrides,
writer: &mut impl Write,
) -> Result<()> {
// Collect all files in the hierarchy.
Expand Down
13 changes: 6 additions & 7 deletions crates/ruff_cli/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ use path_absolutize::path_dedot;
use ruff_workspace::configuration::Configuration;
use ruff_workspace::pyproject;
use ruff_workspace::resolver::{
resolve_root_settings, ConfigProcessor, PyprojectConfig, PyprojectDiscoveryStrategy, Relativity,
resolve_root_settings, ConfigurationTransformer, PyprojectConfig, PyprojectDiscoveryStrategy,
Relativity,
};

use crate::args::Overrides;
use crate::args::CliOverrides;

/// Resolve the relevant settings strategy and defaults for the current
/// invocation.
pub fn resolve(
isolated: bool,
config: Option<&Path>,
overrides: &Overrides,
overrides: &CliOverrides,
stdin_filename: Option<&Path>,
) -> Result<PyprojectConfig> {
// First priority: if we're running in isolated mode, use the default settings.
if isolated {
let mut config = Configuration::default();
overrides.process_config(&mut config);
let config = overrides.transform(Configuration::default());
let settings = config.into_settings(&path_dedot::CWD)?;
debug!("Isolated mode, not reading any pyproject.toml");
return Ok(PyprojectConfig::new(
Expand Down Expand Up @@ -91,8 +91,7 @@ pub fn resolve(
// "closest" `pyproject.toml` file for every Python file later on, so these act
// as the "default" settings.)
debug!("Using Ruff default settings");
let mut config = Configuration::default();
overrides.process_config(&mut config);
let config = overrides.transform(Configuration::default());
let settings = config.into_settings(&path_dedot::CWD)?;
Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
Expand Down

0 comments on commit 83daddb

Please sign in to comment.