Skip to content

Commit

Permalink
Normalize all line endings to Unix on the way in (#78)
Browse files Browse the repository at this point in the history
* Normalize line endings to Unix everywhere on the way in

* Turn multiline raw string test back on

* Explicitly add a multiline string test

* Add CRLF formatter test
  • Loading branch information
DavisVaughan authored Dec 6, 2024
1 parent 007008f commit 5d5a2db
Show file tree
Hide file tree
Showing 23 changed files with 302 additions and 176 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
* text=auto eol=lf

# Windows specific test files where we need CRLF endings
crates/air_r_formatter/tests/specs/r/crlf/*.R text eol=crlf
25 changes: 19 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ air_r_formatter = { path = "./crates/air_r_formatter" }
air_r_parser = { path = "./crates/air_r_parser" }
air_r_syntax = { path = "./crates/air_r_syntax" }
biome_ungrammar = { path = "./crates/biome_ungrammar" }
line_ending = { path = "./crates/line_ending" }
lsp = { path = "./crates/lsp" }
lsp_test = { path = "./crates/lsp_test" }
tests_macros = { path = "./crates/tests_macros" }
Expand Down Expand Up @@ -76,7 +77,6 @@ checked_conversions = "warn"
cloned_instead_of_copied = "warn"
copy_iterator = "warn"
dbg_macro = "warn"
doc_link_with_quotes = "warn"
empty_enum = "warn"
expl_impl_clone_on_copy = "warn"
explicit_into_iter_loop = "warn"
Expand Down
2 changes: 2 additions & 0 deletions crates/air/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ rust-version.workspace = true
air_r_formatter = { workspace = true }
air_r_parser = { workspace = true }
anyhow = { workspace = true }
biome_formatter = { workspace = true }
clap = { workspace = true }
line_ending = { workspace = true }
lsp = { workspace = true }
tokio = "1.41.1"

Expand Down
21 changes: 18 additions & 3 deletions crates/air/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::PathBuf;

use air_r_formatter::context::RFormatOptions;
use air_r_parser::RParserOptions;
use line_ending::LineEnding;

use crate::args::FormatCommand;
use crate::ExitStatus;
Expand Down Expand Up @@ -29,16 +30,30 @@ fn _format_dir(path: &PathBuf) -> anyhow::Result<ExitStatus> {
}

fn format_file(path: &PathBuf) -> anyhow::Result<ExitStatus> {
let text = std::fs::read_to_string(path)?;
let contents = std::fs::read_to_string(path)?;

let line_ending = line_ending::infer(&contents);

// Normalize to Unix line endings
let contents = match line_ending {
LineEnding::Lf => contents,
LineEnding::Crlf => line_ending::normalize(contents),
};

let parser_options = RParserOptions::default();
let parsed = air_r_parser::parse(text.as_str(), parser_options);
let parsed = air_r_parser::parse(contents.as_str(), parser_options);

if parsed.has_errors() {
return Ok(ExitStatus::Error);
}

let formatter_options = RFormatOptions::default();
// TODO: Respect user specified `LineEnding` option too, not just inferred line endings
let line_ending = match line_ending {
LineEnding::Lf => biome_formatter::LineEnding::Lf,
LineEnding::Crlf => biome_formatter::LineEnding::Crlf,
};

let formatter_options = RFormatOptions::default().with_line_ending(line_ending);
let formatted = air_r_formatter::format_node(formatter_options, &parsed.syntax())?;
let result = formatted.print()?;
let code = result.as_code();
Expand Down
41 changes: 21 additions & 20 deletions crates/air_formatter_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
[package]
authors.workspace = true
categories.workspace = true
description = "Air's formatter test shared infrastructure"
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
name = "air_formatter_test"
publish = false
repository.workspace = true
authors.workspace = true
categories.workspace = true
description = "Air's formatter test shared infrastructure"
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
name = "air_formatter_test"
publish = false
repository.workspace = true
rust-version.workspace = true
version = "0.0.0"
version = "0.0.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
biome_console = { workspace = true }
biome_diagnostics = { workspace = true }
biome_formatter = { workspace = true }
biome_parser = { workspace = true }
biome_rowan = { workspace = true }
insta = { workspace = true, features = ["glob"] }
serde = { workspace = true, features = ["derive"] }
similar = "2.6.0"
similar-asserts = "1.6.0"
biome_console = { workspace = true }
biome_diagnostics = { workspace = true }
biome_formatter = { workspace = true }
biome_parser = { workspace = true }
biome_rowan = { workspace = true }
insta = { workspace = true, features = ["glob"] }
line_ending = { workspace = true }
serde = { workspace = true, features = ["derive"] }
similar = "2.6.0"
similar-asserts = "1.6.0"

[lints]
workspace = true
3 changes: 3 additions & 0 deletions crates/air_formatter_test/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ impl<'a> SpecTestFile<'a> {

let input_code = std::fs::read_to_string(input_file).unwrap();

// Normalize to Unix line endings
let input_code = line_ending::normalize(input_code);

// For the whole file, not a specific range right now
let range_start_index = None;
let range_end_index = None;
Expand Down
1 change: 1 addition & 0 deletions crates/air_r_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tracing = { workspace = true }
air_formatter_test = { workspace = true }
air_r_parser = { workspace = true }
biome_parser = { workspace = true }
line_ending = { workspace = true }
tests_macros = { workspace = true }

[lints]
Expand Down
2 changes: 2 additions & 0 deletions crates/air_r_formatter/tests/specs/r/crlf/string_value.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"multiline
string"
30 changes: 30 additions & 0 deletions crates/air_r_formatter/tests/specs/r/crlf/string_value.R.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/air_formatter_test/src/snapshot_builder.rs
info: r/crlf/string_value.R
---
# Input

```R
"multiline
string"
```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
-----

```R
"multiline
string"
```
7 changes: 4 additions & 3 deletions crates/air_r_formatter/tests/specs/r/value/string_value.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'hi there!'
'\''
"'"
# TODO: https://github.com/posit-dev/air/issues/74
# r"("some raw string
# business")"
"multiline
string"
r"("some raw string
business")"
14 changes: 8 additions & 6 deletions crates/air_r_formatter/tests/specs/r/value/string_value.R.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ info: r/value/string_value.R
'hi there!'
'\''
"'"
# TODO: https://github.com/posit-dev/air/issues/74
# r"("some raw string
# business")"
"multiline
string"
r"("some raw string
business")"
```

Expand All @@ -36,7 +37,8 @@ Line width: 80
'hi there!'
'\''
"'"
# TODO: https://github.com/posit-dev/air/issues/74
# r"("some raw string
# business")"
"multiline
string"
r"("some raw string
business")"
```
45 changes: 23 additions & 22 deletions crates/air_r_parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
[package]
authors.workspace = true
categories.workspace = true
description = "Air R parser"
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
name = "air_r_parser"
publish = false
repository.workspace = true
authors.workspace = true
categories.workspace = true
description = "Air R parser"
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
name = "air_r_parser"
publish = false
repository.workspace = true
rust-version.workspace = true
version = "0.0.0"
version = "0.0.0"

[dependencies]
air_r_factory = { workspace = true }
air_r_syntax = { workspace = true }
biome_parser = { workspace = true }
biome_rowan = { workspace = true }
air_r_factory = { workspace = true }
air_r_syntax = { workspace = true }
biome_parser = { workspace = true }
biome_rowan = { workspace = true }
biome_unicode_table = { workspace = true }
serde = { workspace = true, features = ["derive"] }
tracing = { workspace = true }
tree-sitter = { workspace = true }
tree-sitter-r = { workspace = true }
serde = { workspace = true, features = ["derive"] }
tracing = { workspace = true }
tree-sitter = { workspace = true }
tree-sitter-r = { workspace = true }

[dev-dependencies]
biome_console = { workspace = true }
biome_console = { workspace = true }
biome_diagnostics = { workspace = true }
insta = { workspace = true }
tests_macros = { workspace = true }
insta = { workspace = true }
line_ending = { workspace = true }
tests_macros = { workspace = true }

# cargo-workspaces metadata
[package.metadata.workspaces]
Expand Down
2 changes: 2 additions & 0 deletions crates/air_r_parser/tests/snapshots/ok/value/string_value.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
'hi there!'
'\''
"'"
"multiline
string"
r"("some raw string
business")"
Loading

0 comments on commit 5d5a2db

Please sign in to comment.