Skip to content

Commit

Permalink
Correctly format files with dos line endings (#81)
Browse files Browse the repository at this point in the history
* Correctly format files with dos line endings

* Refactor and naming improvements

* Avoid panic on empty file
  • Loading branch information
nikhilraojl authored Sep 23, 2023
1 parent 9aa7caa commit 0f9c511
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
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,

Check failure on line 171 in formatter/src/formatter/mac.rs

View workflow job for this annotation

GitHub Actions / Test Suite

struct `PrinterSettings` has no field named `crlf_line_endings`

Check failure on line 171 in formatter/src/formatter/mac.rs

View workflow job for this annotation

GitHub Actions / Check

struct `PrinterSettings` has no field named `crlf_line_endings`

Check failure on line 171 in formatter/src/formatter/mac.rs

View workflow job for this annotation

GitHub Actions / Clippy

struct `leptosfmt_pretty_printer::PrinterSettings` has no field named `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,

Check failure on line 57 in formatter/src/formatter/mod.rs

View workflow job for this annotation

GitHub Actions / Test Suite

struct `PrinterSettings` has no field named `crlf_line_endings`

Check failure on line 57 in formatter/src/formatter/mod.rs

View workflow job for this annotation

GitHub Actions / Check

struct `PrinterSettings` has no field named `crlf_line_endings`

Check failure on line 57 in formatter/src/formatter/mod.rs

View workflow job for this annotation

GitHub Actions / Clippy

struct `leptosfmt_pretty_printer::PrinterSettings` has no field named `crlf_line_endings`
}
}
}
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

0 comments on commit 0f9c511

Please sign in to comment.