diff --git a/crates/ruff_cli/tests/format.rs b/crates/ruff_cli/tests/format.rs index 22ba8adf0bd8b..5fc8fc94fe3de 100644 --- a/crates/ruff_cli/tests/format.rs +++ b/crates/ruff_cli/tests/format.rs @@ -94,6 +94,35 @@ if condition: Ok(()) } +#[test] +fn mixed_line_endings() -> Result<()> { + let tempdir = TempDir::new()?; + + fs::write( + tempdir.path().join("main.py"), + "from test import say_hy\n\nif __name__ == \"__main__\":\n say_hy(\"dear Ruff contributor\")\n", + )?; + + fs::write( + tempdir.path().join("test.py"), + "def say_hy(name: str):\r\n print(f\"Hy {name}\")\r\n", + )?; + + assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) + .current_dir(tempdir.path()) + .args(["format", "--diff", "--isolated"]) + .arg("."), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + warning: `ruff format` is not yet stable, and subject to change in future versions. + 2 files left unchanged + "###); + Ok(()) +} + #[test] fn exclude() -> Result<()> { let tempdir = TempDir::new()?; diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index 19e53af7ac389..b60c86c9aed8f 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -2623,16 +2623,16 @@ pub struct FormatOptions { /// The character Ruff uses at the end of a line. /// + /// * `auto`: The newline style is detected automatically on a file per file basis. Files with mixed line endings will be converted to the first detected line ending. Defaults to `\n` for files that contain no line endings. /// * `lf`: Line endings will be converted to `\n`. The default line ending on Unix. /// * `cr-lf`: Line endings will be converted to `\r\n`. The default line ending on Windows. - /// * `auto`: The newline style is detected automatically on a file per file basis. Files with mixed line endings will be converted to the first detected line ending. Defaults to `\n` for files that contain no line endings. /// * `native`: Line endings will be converted to `\n` on Unix and `\r\n` on Windows. #[option( - default = r#"lf"#, - value_type = r#""lf" | "cr-lf" | "auto" | "native""#, + default = r#"auto"#, + value_type = r#""auto" | "lf" | "cr-lf" | "native""#, example = r#" - # Automatically detect the line ending on a file per file basis. - line-ending = "auto" + # Use `\n` line endings for all files + line-ending = "lf" "# )] pub line_ending: Option, diff --git a/crates/ruff_workspace/src/settings.rs b/crates/ruff_workspace/src/settings.rs index 71bf524950568..982732e487317 100644 --- a/crates/ruff_workspace/src/settings.rs +++ b/crates/ruff_workspace/src/settings.rs @@ -168,7 +168,7 @@ impl Default for FormatterSettings { exclude: FilePatternSet::default(), preview: PreviewMode::Disabled, line_width: default_options.line_width(), - line_ending: LineEnding::Lf, + line_ending: LineEnding::Auto, indent_style: default_options.indent_style(), indent_width: default_options.indent_width(), quote_style: default_options.quote_style(), @@ -183,18 +183,18 @@ impl Default for FormatterSettings { #[serde(rename_all = "kebab-case")] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub enum LineEnding { - /// Line endings will be converted to `\n` as is common on Unix. + /// The newline style is detected automatically on a file per file basis. + /// Files with mixed line endings will be converted to the first detected line ending. + /// Defaults to [`LineEnding::Lf`] for a files that contain no line endings. #[default] + Auto, + + /// Line endings will be converted to `\n` as is common on Unix. Lf, /// Line endings will be converted to `\r\n` as is common on Windows. CrLf, - /// The newline style is detected automatically on a file per file basis. - /// Files with mixed line endings will be converted to the first detected line ending. - /// Defaults to [`LineEnding::Lf`] for a files that contain no line endings. - Auto, - /// Line endings will be converted to `\n` on Unix and `\r\n` on Windows. Native, } diff --git a/ruff.schema.json b/ruff.schema.json index e4ad0ae65cd86..1c95b90e7163a 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -1253,7 +1253,7 @@ ] }, "line-ending": { - "description": "The character Ruff uses at the end of a line.\n\n* `lf`: Line endings will be converted to `\\n`. The default line ending on Unix. * `cr-lf`: Line endings will be converted to `\\r\\n`. The default line ending on Windows. * `auto`: The newline style is detected automatically on a file per file basis. Files with mixed line endings will be converted to the first detected line ending. Defaults to `\\n` for files that contain no line endings. * `native`: Line endings will be converted to `\\n` on Unix and `\\r\\n` on Windows.", + "description": "The character Ruff uses at the end of a line.\n\n* `auto`: The newline style is detected automatically on a file per file basis. Files with mixed line endings will be converted to the first detected line ending. Defaults to `\\n` for files that contain no line endings. * `lf`: Line endings will be converted to `\\n`. The default line ending on Unix. * `cr-lf`: Line endings will be converted to `\\r\\n`. The default line ending on Windows. * `native`: Line endings will be converted to `\\n` on Unix and `\\r\\n` on Windows.", "anyOf": [ { "$ref": "#/definitions/LineEnding" @@ -1565,24 +1565,24 @@ "LineEnding": { "oneOf": [ { - "description": "Line endings will be converted to `\\n` as is common on Unix.", + "description": "The newline style is detected automatically on a file per file basis. Files with mixed line endings will be converted to the first detected line ending. Defaults to [`LineEnding::Lf`] for a files that contain no line endings.", "type": "string", "enum": [ - "lf" + "auto" ] }, { - "description": "Line endings will be converted to `\\r\\n` as is common on Windows.", + "description": "Line endings will be converted to `\\n` as is common on Unix.", "type": "string", "enum": [ - "cr-lf" + "lf" ] }, { - "description": "The newline style is detected automatically on a file per file basis. Files with mixed line endings will be converted to the first detected line ending. Defaults to [`LineEnding::Lf`] for a files that contain no line endings.", + "description": "Line endings will be converted to `\\r\\n` as is common on Windows.", "type": "string", "enum": [ - "auto" + "cr-lf" ] }, {