Skip to content

Commit

Permalink
Change line-ending default to auto (#8057)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Oct 19, 2023
1 parent a00c445 commit 962472d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
29 changes: 29 additions & 0 deletions crates/ruff_cli/tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
10 changes: 5 additions & 5 deletions crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LineEnding>,
Expand Down
14 changes: 7 additions & 7 deletions crates/ruff_workspace/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
}
14 changes: 7 additions & 7 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 962472d

Please sign in to comment.