From 3663a18bc813a527cf773e7ff551caa17ccbce04 Mon Sep 17 00:00:00 2001 From: John Dallaway Date: Fri, 22 Aug 2014 18:50:55 +0100 Subject: [PATCH] Bug 442186: Code formatter handling of CRLF after single line comment Change-Id: I1a17ec992fd881851e076c732629ac912effc2f1 Signed-off-by: John Dallaway Reviewed-on: https://git.eclipse.org/r/32024 Tested-by: Hudson CI Reviewed-by: Sergey Prigogin Tested-by: Sergey Prigogin --- .../cdt/internal/formatter/scanner/SimpleScanner.java | 11 +++++++++-- .../eclipse/cdt/ui/tests/text/CodeFormatterTest.java | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/scanner/SimpleScanner.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/scanner/SimpleScanner.java index 448d0f2d5c2..3d3b60cc821 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/scanner/SimpleScanner.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/scanner/SimpleScanner.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2011 IBM Corporation and others. + * Copyright (c) 2004, 2014 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -9,6 +9,7 @@ * IBM Corporation - initial implementation * Anton Leherbauer - adding tokens for preprocessing directives * Markus Schorn - classification of preprocessing directives. + * John Dallaway - handle CRLF after single line comment (bug 442186) *******************************************************************************/ package org.eclipse.cdt.internal.formatter.scanner; @@ -831,7 +832,13 @@ public class SimpleScanner { private void matchSinglelineComment(boolean includeNewline) { int c = getChar(); while (c != '\n' && c != EOFCHAR) { - c = getChar(); + int next = getChar(); + if (c == '\r' && next == '\n' && !includeNewline) { + // exclude CRLF line ending + ungetChar(next); + break; + } + c = next; } if (c == EOFCHAR || !includeNewline) { ungetChar(c); diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java index 971db070a25..b6cac775e7a 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java @@ -2999,4 +2999,10 @@ public class CodeFormatterTest extends BaseUITestCase { public void testFunctionMacroInInitializerExpression() throws Exception { assertFormatterResult(); } + + public void testCrLfAfterSingleLineComment_Bug442186() throws Exception { + String before = "#define TESTING1 //\r\n#define TESTING2 // CR \r in comment\r\n"; + String expected = before; + assertFormatterResult(before, expected); + } }