From efe641d53aaef19dd4ce8fb7e67d901ea99bc30e Mon Sep 17 00:00:00 2001
From: Anton Leherbauer <anton.leherbauer@windriver.com>
Date: Wed, 13 Oct 2010 09:23:47 +0000
Subject: [PATCH] Bug 327311 - Typing 'Enter' in a multi-line comment might
 copy too much from the previous line

---
 .../eclipse/cdt/ui/tests/text/CAutoIndentTest.java |  9 +++++++++
 .../DefaultMultilineCommentAutoEditStrategy.java   | 14 +++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java
index 96d11fb1b90..edaef1b7b94 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java
@@ -523,6 +523,15 @@ public class CAutoIndentTest extends AbstractAutoEditTest {
 		assertEquals("if (i > 0)\n    {\n    \n    }", tester.fDoc.get());
 	}
 
+	public void testCopyCommentPrefix_Bug327311() throws Exception {
+		AutoEditTester tester = createAutoEditTester();
+		tester.type("/*\n"); //$NON-NLS-1$
+		assertEquals(" * ", tester.getLine());
+		tester.backspace(); // delete space
+		tester.type("\tDemonstrate\n"); //$NON-NLS-1$
+		assertEquals(" *\t", tester.getLine());
+	}
+
 	private void assertNoError() {
 		if (!fStatusLog.isEmpty()) {
 			fail(fStatusLog.get(0).toString());
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/text/doctools/DefaultMultilineCommentAutoEditStrategy.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/text/doctools/DefaultMultilineCommentAutoEditStrategy.java
index 959eea84270..82ce3ae92f3 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/text/doctools/DefaultMultilineCommentAutoEditStrategy.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/text/doctools/DefaultMultilineCommentAutoEditStrategy.java
@@ -376,11 +376,11 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
 	}
 	
 	/**
-	 * Returns the range of the java-doc prefix on the given line in
+	 * Returns the range of the comment prefix on the given line in
 	 * <code>document</code>. The prefix greedily matches the following regex
-	 * pattern: <code>\w*\*\w*</code>, that is, any number of whitespace
+	 * pattern: <code>\s*\*\S*\s*</code>, that is, any number of whitespace
 	 * characters, followed by an asterisk ('*'), followed by any number of
-	 * whitespace characters.
+	 * non-whitespace characters, followed by any number of whitespace characters.
 	 *
 	 * @param document the document to which <code>line</code> refers
 	 * @param line the line from which to extract the prefix range
@@ -394,14 +394,18 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
 		int indentEnd= findEndOfWhiteSpaceAt(document, lineOffset, lineEnd);
 		if (indentEnd < lineEnd && document.getChar(indentEnd) == '*') {
 			indentEnd++;
-			while (indentEnd < lineEnd && document.getChar(indentEnd) != ' ')
+			while (indentEnd < lineEnd && !isWhitespace(document.getChar(indentEnd)))
 				indentEnd++;
-			while (indentEnd < lineEnd && document.getChar(indentEnd) == ' ')
+			while (indentEnd < lineEnd && isWhitespace(document.getChar(indentEnd)))
 				indentEnd++;
 		}
 		return new Region(lineOffset, indentEnd - lineOffset);
 	}
 	
+	private static boolean isWhitespace(char ch) {
+		return ch == ' ' || ch == '\t';
+	}
+
 	/**
 	 * Returns whether the text ends with one of the specified IDocument object's
 	 * legal line delimiters.