From f3f511af1b0937cd64f739976cdcf485d0e3e2e0 Mon Sep 17 00:00:00 2001 From: Shohei Wada Date: Mon, 24 Dec 2018 06:12:28 +0900 Subject: [PATCH 1/3] Fix #2973 in Windows CRLF env. --- src/comment.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/comment.rs b/src/comment.rs index 6f229414e0d6..89f9258fd7a8 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -1304,7 +1304,13 @@ impl<'a> Iterator for LineClasses<'a> { None => FullCodeCharKind::Normal, }; - while let Some((kind, c)) = self.base.next() { + while let Some((kind, mut c)) = self.base.next() { + // If \r\n newline appears, consume one more character. + // Then do the same process with the single \n case. + if c == '\r' && self.base.peek().map_or(false, |(_, c2)| *c2 == '\n') { + self.base.next(); + c = '\n'; + } if c == '\n' { self.kind = match (start_class, kind) { (FullCodeCharKind::Normal, FullCodeCharKind::InString) => { From 6a316e3ac7d48e01c6eb6987b74e2c0e040bf206 Mon Sep 17 00:00:00 2001 From: Shohei Wada Date: Mon, 24 Dec 2018 06:18:00 +0900 Subject: [PATCH 2/3] Add test cases. --- tests/source/issue-3265.rs | 14 ++++++++++++++ tests/target/issue-3265.rs | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/source/issue-3265.rs create mode 100644 tests/target/issue-3265.rs diff --git a/tests/source/issue-3265.rs b/tests/source/issue-3265.rs new file mode 100644 index 000000000000..e927cf2be466 --- /dev/null +++ b/tests/source/issue-3265.rs @@ -0,0 +1,14 @@ +// rustfmt-newline_style: Windows +#[cfg(test)] +mod test { + summary_test! { + tokenize_recipe_interpolation_eol, + "foo: # some comment + {{hello}} +", + "foo: \ + {{hello}} \ +{{ahah}}", + "N:#$>^{N}$<.", + } +} diff --git a/tests/target/issue-3265.rs b/tests/target/issue-3265.rs new file mode 100644 index 000000000000..7db1dbd8b732 --- /dev/null +++ b/tests/target/issue-3265.rs @@ -0,0 +1,14 @@ +// rustfmt-newline_style: Windows +#[cfg(test)] +mod test { + summary_test! { + tokenize_recipe_interpolation_eol, + "foo: # some comment + {{hello}} +", + "foo: \ + {{hello}} \ + {{ahah}}", + "N:#$>^{N}$<.", + } +} From e37f468b218f11096173538774c77c483006efdd Mon Sep 17 00:00:00 2001 From: Shohei Wada Date: Mon, 24 Dec 2018 07:25:15 +0900 Subject: [PATCH 3/3] Change \r detection much simpler. --- src/comment.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/comment.rs b/src/comment.rs index 89f9258fd7a8..635ee9a85f29 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -1304,13 +1304,7 @@ impl<'a> Iterator for LineClasses<'a> { None => FullCodeCharKind::Normal, }; - while let Some((kind, mut c)) = self.base.next() { - // If \r\n newline appears, consume one more character. - // Then do the same process with the single \n case. - if c == '\r' && self.base.peek().map_or(false, |(_, c2)| *c2 == '\n') { - self.base.next(); - c = '\n'; - } + while let Some((kind, c)) = self.base.next() { if c == '\n' { self.kind = match (start_class, kind) { (FullCodeCharKind::Normal, FullCodeCharKind::InString) => { @@ -1327,6 +1321,11 @@ impl<'a> Iterator for LineClasses<'a> { } } + // Workaround for CRLF newline. + if line.ends_with('\r') { + line.pop(); + } + Some((self.kind, line)) } }