diff --git a/formatter/src/formatter/mac.rs b/formatter/src/formatter/mac.rs index 7bf6bf2..969cd65 100644 --- a/formatter/src/formatter/mac.rs +++ b/formatter/src/formatter/mac.rs @@ -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}; @@ -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); diff --git a/formatter/src/formatter/mod.rs b/formatter/src/formatter/mod.rs index ef016bb..28182cf 100644 --- a/formatter/src/formatter/mod.rs +++ b/formatter/src/formatter/mod.rs @@ -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, } } } diff --git a/printer/src/algorithm.rs b/printer/src/algorithm.rs index dc9a54f..704fab5 100644 --- a/printer/src/algorithm.rs +++ b/printer/src/algorithm.rs @@ -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 { @@ -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();