From f077b3c589f1b7da28de957cfa2659264d6f2110 Mon Sep 17 00:00:00 2001 From: jlnikhilrao Date: Wed, 20 Sep 2023 19:16:02 +0530 Subject: [PATCH 1/3] Correctly format files with dos line endings --- formatter/src/formatter/mac.rs | 7 +++++++ printer/src/algorithm.rs | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/formatter/src/formatter/mac.rs b/formatter/src/formatter/mac.rs index 7bf6bf2..f938f2d 100644 --- a/formatter/src/formatter/mac.rs +++ b/formatter/src/formatter/mac.rs @@ -162,6 +162,13 @@ pub fn format_macro( source, mac.mac.tokens.clone(), ); + let dos_endings = source + .raw_lines() + .nth(0) + .unwrap() + .to_string() + .ends_with("\r\n"); + printer.set_dos(dos_endings); Formatter::with_source(*settings, &mut printer, source, whitespace) } None => Formatter::new(*settings, &mut printer), diff --git a/printer/src/algorithm.rs b/printer/src/algorithm.rs index dc9a54f..c48614b 100644 --- a/printer/src/algorithm.rs +++ b/printer/src/algorithm.rs @@ -78,6 +78,8 @@ pub struct Printer { indent: usize, // Buffered indentation to avoid writing trailing whitespace pending_indentation: usize, + // To correctly print with dos line endings(CRLF) + dos_line_endings: bool, } #[derive(Clone)] @@ -99,9 +101,14 @@ impl Printer { print_stack: Vec::new(), indent: 0, pending_indentation: 0, + dos_line_endings: false, } } + pub fn set_dos(&mut self, value: bool) { + self.dos_line_endings = value; + } + pub fn eof(mut self) -> String { if !self.scan_stack.is_empty() { self.check_stack(0); @@ -332,6 +339,9 @@ impl Printer { self.print_indent(); self.out.push(pre_break); } + if self.dos_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(); From 49b907c29c44a52258c4ada8c18921b7a6699d48 Mon Sep 17 00:00:00 2001 From: jlnikhilrao Date: Thu, 21 Sep 2023 11:09:07 +0530 Subject: [PATCH 2/3] Refactor and naming improvements --- formatter/src/formatter/mac.rs | 16 +++++++++++----- formatter/src/formatter/mod.rs | 1 + printer/src/algorithm.rs | 11 +++-------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/formatter/src/formatter/mac.rs b/formatter/src/formatter/mac.rs index f938f2d..78f6ef0 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,23 +155,29 @@ 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 dos_endings = source + let crlf_line_endings = source .raw_lines() .nth(0) .unwrap() .to_string() .ends_with("\r\n"); - printer.set_dos(dos_endings); + 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 c48614b..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 { @@ -78,8 +80,6 @@ pub struct Printer { indent: usize, // Buffered indentation to avoid writing trailing whitespace pending_indentation: usize, - // To correctly print with dos line endings(CRLF) - dos_line_endings: bool, } #[derive(Clone)] @@ -101,14 +101,9 @@ impl Printer { print_stack: Vec::new(), indent: 0, pending_indentation: 0, - dos_line_endings: false, } } - pub fn set_dos(&mut self, value: bool) { - self.dos_line_endings = value; - } - pub fn eof(mut self) -> String { if !self.scan_stack.is_empty() { self.check_stack(0); @@ -339,7 +334,7 @@ impl Printer { self.print_indent(); self.out.push(pre_break); } - if self.dos_line_endings { + if self.settings.crlf_line_endings { self.out.push('\r'); } self.out.push('\n'); From e4529e62677cf2e0cb89b1410d6af9b09d8ef116 Mon Sep 17 00:00:00 2001 From: jlnikhilrao Date: Thu, 21 Sep 2023 13:49:34 +0530 Subject: [PATCH 3/3] Avoid panic on empty file --- formatter/src/formatter/mac.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/formatter/src/formatter/mac.rs b/formatter/src/formatter/mac.rs index 78f6ef0..969cd65 100644 --- a/formatter/src/formatter/mac.rs +++ b/formatter/src/formatter/mac.rs @@ -165,9 +165,8 @@ pub fn format_macro( let crlf_line_endings = source .raw_lines() .nth(0) - .unwrap() - .to_string() - .ends_with("\r\n"); + .map(|raw_line| raw_line.to_string().ends_with("\r\n")) + .unwrap_or_default(); printer = Printer::new(PrinterSettings { crlf_line_endings, ..settings.into()