Skip to content
Open
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
25 changes: 20 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ impl DiagnosticsConfig {
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
pub struct FormattingConfig {
/// Explicitly disable all formatting
pub enabled: Option<bool>,
/// Command (path or name) to run php-cs-fixer.
///
/// - `None` (default) — check `require-dev` in `composer.json`;
Expand Down Expand Up @@ -379,12 +381,9 @@ impl FormattingConfig {
self.timeout.unwrap_or(10_000)
}

/// Whether formatting is entirely disabled (every tool explicitly
/// set to an empty string).
/// Whether formatting is explicitly disabled
pub fn is_disabled(&self) -> bool {
crate::formatting::Tool::ALL
.into_iter()
.all(|tool| tool.configured(self) == Some(""))
self.enabled == Some(false)
}
}

Expand Down Expand Up @@ -1542,6 +1541,22 @@ paths = ["database/schema", "extra/schema.sql"]
assert_eq!(config.formatting.php_cs_fixer.as_deref(), Some(""));
assert_eq!(config.formatting.phpcbf.as_deref(), Some(""));
assert_eq!(config.formatting.pint.as_deref(), Some(""));
assert!(!config.formatting.is_disabled());
}

#[test]
fn formatting_disabled_disables_tool() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(CONFIG_FILE_NAME);
std::fs::write(
&path,
"[formatting]\nenabled = false\nphp-cs-fixer = \"\"\nphpcbf = \"\"\npint = \"\"\n",
)
.unwrap();
let config = load_config(dir.path()).unwrap();
assert_eq!(config.formatting.php_cs_fixer.as_deref(), Some(""));
assert_eq!(config.formatting.phpcbf.as_deref(), Some(""));
assert_eq!(config.formatting.pint.as_deref(), Some(""));
assert!(config.formatting.is_disabled());
}

Expand Down
5 changes: 5 additions & 0 deletions src/formatting/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ fn malformed_mago_toml_returns_error_not_panic() {
#[test]
fn strategy_both_disabled() {
let config = FormattingConfig {
enabled: Some(true),
pint: Some(String::new()),
php_cs_fixer: Some(String::new()),
phpcbf: Some(String::new()),
Expand All @@ -144,6 +145,7 @@ fn strategy_both_disabled() {
#[test]
fn strategy_explicit_commands() {
let config = FormattingConfig {
enabled: Some(true),
pint: None,
php_cs_fixer: Some("/usr/bin/php-cs-fixer".to_string()),
phpcbf: Some("/usr/bin/phpcbf".to_string()),
Expand All @@ -167,6 +169,7 @@ fn strategy_explicit_commands() {
#[test]
fn strategy_one_explicit_one_disabled() {
let config = FormattingConfig {
enabled: Some(true),
pint: None,
php_cs_fixer: Some("/usr/bin/php-cs-fixer".to_string()),
phpcbf: Some(String::new()),
Expand Down Expand Up @@ -468,6 +471,7 @@ fn strategy_explicit_overrides_require_dev() {

// User explicitly set a different path.
let config = FormattingConfig {
enabled: Some(true),
pint: None,
php_cs_fixer: Some("/opt/php-cs-fixer".to_string()),
phpcbf: Some(String::new()),
Expand Down Expand Up @@ -725,6 +729,7 @@ fn execute_builtin_reformats_messy_class() {
fn execute_disabled_returns_none() {
let content = "<?php\necho 'hello';\n";
let config = FormattingConfig {
enabled: Some(true),
pint: None,
php_cs_fixer: Some(String::new()),
phpcbf: Some(String::new()),
Expand Down
Loading