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

Correctly format files with dos line endings #81

Merged
merged 3 commits into from
Sep 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions formatter/src/formatter/mac.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crop::Rope;
use leptosfmt_pretty_printer::Printer;
use leptosfmt_pretty_printer::{Printer, PrinterSettings};
use proc_macro2::{token_stream, Span, TokenStream, TokenTree};
use rstml::node::Node;
use syn::{spanned::Spanned, Macro};
Expand Down Expand Up @@ -155,16 +155,28 @@ pub fn format_macro(
settings: &FormatterSettings,
source: Option<&Rope>,
) -> String {
let mut printer = Printer::new(settings.into());
let mut printer: Printer;
let mut formatter = match source {
Some(source) => {
let whitespace = crate::collect_comments::extract_whitespace_and_comments(
source,
mac.mac.tokens.clone(),
);
let crlf_line_endings = source
.raw_lines()
.nth(0)
.map(|raw_line| raw_line.to_string().ends_with("\r\n"))
.unwrap_or_default();
printer = Printer::new(PrinterSettings {
crlf_line_endings,
..settings.into()
});
Formatter::with_source(*settings, &mut printer, source, whitespace)
}
None => Formatter::new(*settings, &mut printer),
None => {
printer = Printer::new(settings.into());
Formatter::new(*settings, &mut printer)
}
};

formatter.view_macro(mac);
Expand Down
1 change: 1 addition & 0 deletions formatter/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl From<&FormatterSettings> for PrinterSettings {
margin: value.max_width as isize,
indent: value.tab_spaces as isize,
min_space: 60,
crlf_line_endings: false,
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions printer/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub struct PrinterSettings {
pub indent: isize,
// Every line is allowed at least this much space, even if highly indented.
pub min_space: isize,
// Print CRLF line ending instead of LF
pub crlf_line_endings: bool,
}

pub struct Printer {
Expand Down Expand Up @@ -332,6 +334,9 @@ impl Printer {
self.print_indent();
self.out.push(pre_break);
}
if self.settings.crlf_line_endings {
self.out.push('\r');
}
self.out.push('\n');
let indent = self.indent as isize + token.offset;
self.pending_indentation = usize::try_from(indent).unwrap();
Expand Down