Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A statically typed programming language.

```zy
const std = import("std")
std.debug.print("Hello World!")
std.debug.print("Hello, 世界")
```

## License
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-world.zy
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const std = import("std")
std.debug.print("Hello World!")
std.debug.print("Hello, 世界")
12 changes: 9 additions & 3 deletions src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,14 @@ fn fmt_diff(source: &str, formatted: &str) -> String {
}
}
}
let trimmed_len = out.trim_end().len();
out.truncate(trimmed_len);
if out.is_empty() && source != formatted {
// Only difference is trailing newline — show as an added empty line
let ln = source.lines().count() + 1;
out.push_str(&crate::colors::green(&format!("{} | +", ln)));
} else {
let trimmed_len = out.trim_end().len();
out.truncate(trimmed_len);
}
out
}

Expand Down Expand Up @@ -196,7 +202,7 @@ fn check_and_report(
let mut changed = false;
if parse_errors.is_empty() {
let formatted = crate::fmt::format_program(&ast, &blanks);
if formatted.trim() != source.trim() {
if formatted != source {
if fix {
std::fs::write(path, &formatted)
.unwrap_or_else(|e| panic!("Failed to write '{}': {}", path, e));
Expand Down
3 changes: 3 additions & 0 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,5 +362,8 @@ fn precedence(op: &BinOp) -> u8 {
pub fn format_program(program: &crate::parser::Program, blanks: &[bool]) -> String {
let mut f = Formatter::new();
f.format_program(program, blanks);
if !f.buf.ends_with('\n') {
f.buf.push('\n');
}
f.buf
}
21 changes: 21 additions & 0 deletions src/tests/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,27 @@ fn test_fmt_modulo() {
idempotent(&out);
}

#[test]
fn test_fmt_trailing_newline() {
let out = fmt("const x = 1");
assert!(
out.ends_with('\n'),
"expected trailing newline, got: {:?}",
out
);
}

#[test]
fn test_fmt_trailing_newline_not_doubled() {
let out = fmt("const x = 1\n");
assert!(out.ends_with('\n'), "expected trailing newline");
assert!(
!out.ends_with("\n\n"),
"trailing newline doubled: {:?}",
out
);
}

#[test]
fn test_fmt_fixtures_idempotent() {
let fixtures = std::fs::read_dir("src/tests/fixtures")
Expand Down
Loading