Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change line-ending default to auto #8057

Merged
merged 1 commit into from Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions crates/ruff_cli/tests/format.rs
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
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
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.