Skip to content

Commit

Permalink
feat: Automatically detect CRLF or LF 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

[skip ci] Update CHANGELOG.md

[skip ci] Update CHANGELOG.md
  • Loading branch information
nikhilraojl authored and bram209 committed Sep 25, 2023
1 parent 9aa7caa commit 36ed995
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file.

## [unreleased]
## [0.1.14] - 2023-09-06

### Bug Fixes

Expand Down
10 changes: 2 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ license = "MIT OR Apache-2.0"
[workspace.dependencies]
leptosfmt-formatter = { path = "./formatter", version = "0.1.14" }
leptosfmt-pretty-printer = { version = "0.1.6" }

[patch.crates-io]
leptosfmt-pretty-printer = { path = "./printer" }
4 changes: 2 additions & 2 deletions cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ protect_breaking_commits = false
# filter out the commits that are not matched by commit parsers
filter_commits = true
# glob pattern for matching git tags
tag_pattern = "[0-9]*"
tag_pattern = "[0-9]*.[0-9]*.[0-9]*"
# regex for skipping tags
skip_tags = "0.1.0"
# regex for ignoring tags
ignore_tags = ""
# ignore_tags = ""
# sort the tags topologically
topo_order = false
# sort the commits inside sections by oldest/newest order
Expand Down
10 changes: 5 additions & 5 deletions formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
name = "leptosfmt-formatter"
version = { workspace = true }
edition = "2021"
authors = [ "Bram Hoendervangers" ]
authors = ["Bram Hoendervangers"]
license = { workspace = true }
repository = "https://github.com/bram209/leptosfmt"
description = "view macro formatter for the Leptos web framework"

[dependencies]
leptosfmt-pretty-printer.workspace = true
rstml = "0.10.6"
syn = { version = "2.0.18", features = [ "visit", "full", "extra-traits" ] }
leptosfmt-prettyplease = { features = [ "verbatim" ], version = "0.2.11" }
proc-macro2 = { version = "1.0.52", features = [ "span-locations" ] }
syn = { version = "2.0.18", features = ["visit", "full", "extra-traits"] }
leptosfmt-prettyplease = { features = ["verbatim"], version = "0.2.11" }
proc-macro2 = { version = "1.0.52", features = ["span-locations"] }
thiserror = "1.0.40"
crop = "0.3.0"
serde = { version = "1.0.163", features = [ "derive" ] }
serde = { version = "1.0.163", features = ["derive"] }
quote = "1.0.26"

[dev-dependencies]
Expand Down
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

0 comments on commit 36ed995

Please sign in to comment.