Skip to content

Commit

Permalink
Trim trailing new lines from file
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Dec 29, 2024
1 parent a6c2f67 commit 4ec3101
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub fn format_file(
}

new_text = remove_trailing_spaces(&new_text);
new_text = remove_trailing_blank_lines(&new_text);
record_file_log(logs, Info, file, "Formatting complete.");
new_text
}
Expand Down
7 changes: 7 additions & 0 deletions src/subs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ pub fn remove_trailing_spaces(text: &str) -> String {
RE_TRAIL.replace_all(text, LINE_END).to_string()
}

/// Remove trailing blank lines from file
pub fn remove_trailing_blank_lines(text: &str) -> String {
let mut new_text = text.trim_end().to_string();
new_text.push_str(LINE_END);
new_text
}

/// Check if line contains content which be split onto a new line
pub fn needs_split(line: &str, pattern: &Pattern) -> bool {
// Check if we should format this line and if we've matched an environment.
Expand Down
7 changes: 6 additions & 1 deletion tests/source/short_document.tex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
multiple lines
\item Item three
\begin{itemize}
\item Subitem one of item two
\item Subitem one of item two % this line has trailing spaces
\item Subitem two of item two
\end{itemize}
\item Item four % trailing comment % with [( brackets
Expand Down Expand Up @@ -56,3 +56,8 @@
and is broken over three lines}.

\end{document}

% This file ends with trailing newlines



1 change: 1 addition & 0 deletions tests/target/empty.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 3 additions & 1 deletion tests/target/short_document.tex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
multiple lines
\item Item three
\begin{itemize}
\item Subitem one of item two
\item Subitem one of item two % this line has trailing spaces
\item Subitem two of item two
\end{itemize}
\item Item four % trailing comment % with [( brackets
Expand Down Expand Up @@ -56,3 +56,5 @@
and is broken over three lines}.

\end{document}

% This file ends with trailing newlines

0 comments on commit 4ec3101

Please sign in to comment.