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

do not add newline to all strings #31

Merged
merged 2 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add a newline inside format_single_file not format_string
this is because a standalone string might not be the content of a
file, so we do not want to always add a newline.
  • Loading branch information
amtoine committed Jun 21, 2023
commit 7b293b46d45625cd993de775ed85b3ed4b75d078
3 changes: 1 addition & 2 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ pub(crate) fn format_inner(contents: &[u8], _config: &Config) -> Vec<u8> {

start = span.end + 1;
}
out = add_newline_at_end_of_file(out);

out
}
Expand All @@ -116,7 +115,7 @@ fn insert_newline(mut bytes: Vec<u8>) -> Vec<u8> {
}

/// make sure there is a newline at the end of a buffer
fn add_newline_at_end_of_file(out: Vec<u8>) -> Vec<u8> {
pub(crate) fn add_newline_at_end_of_file(out: Vec<u8>) -> Vec<u8> {
match out.last() {
Some(&b'\n') => out,
_ => insert_newline(out),
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! It does not do anything more than that, which makes it so fast.
use config::Config;
use formatting::format_inner;
use formatting::{add_newline_at_end_of_file, format_inner};
use log::{debug, trace};
use std::fs::File;
use std::io::Write;
Expand All @@ -16,7 +16,7 @@ pub fn format_single_file(file: &PathBuf, config: &Config) {
let contents = std::fs::read(file)
.unwrap_or_else(|_| panic!("something went wrong reading the file {}", file.display()));

let formatted_bytes = format_inner(&contents, config);
let formatted_bytes = add_newline_at_end_of_file(format_inner(&contents, config));

if formatted_bytes == contents {
debug!("File is formatted correctly.");
Expand Down