diff options
author | Jens Baumgart | 2011-12-01 00:10:25 +0000 |
---|---|---|
committer | Matthias Sohn | 2011-12-01 00:10:25 +0000 |
commit | abd4106f8de2f72d32c81d6e76355aa20ca4e175 (patch) | |
tree | af976249adbcbb2ef1d835af5acfb451e2d78de9 | |
parent | b1a55ea6562d8beb30764ac2f8dceaf631ea3d38 (diff) | |
download | egit-abd4106f8de2f72d32c81d6e76355aa20ca4e175.tar.gz egit-abd4106f8de2f72d32c81d6e76355aa20ca4e175.tar.xz egit-abd4106f8de2f72d32c81d6e76355aa20ca4e175.zip |
SpellcheckableMessageArea: remove platform specific line endings
SpellcheckableMessageArea.getCommitMessage called
getTextWidget().getLineDelimiter() to determine the platform line
ending. This was wrong because getLineDelimiter() returns the first
line delimiter found in the text control.
The new implementation of SpellcheckableMessageArea.getCommitMessage
guarantees that the returned string only contains \n line endings.
Bug: 361504
Change-Id: If1dbab98745d3178163bad4a66ca3f1a15f5b456
Signed-off-by: Jens Baumgart <jens.baumgart@sap.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
3 files changed, 84 insertions, 2 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/UtilsTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/UtilsTest.java new file mode 100644 index 0000000000..4269fe2f82 --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/UtilsTest.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (C) 2011, Jens Baumgart <jens.baumgart@sap.com> + * + * 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 + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.egit.core.test; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.egit.core.internal.Utils; +import org.junit.Test; + +public class UtilsTest { + + @Test + public void testLineEndingNormalization() { + String str = ""; + String result = Utils.normalizeLineEndings(str); + assertEquals(result, ""); + + str = "Line 1"; + result = Utils.normalizeLineEndings(str); + assertEquals("Line 1", result); + + str = "Line 1\r\nLine 2"; + result = Utils.normalizeLineEndings(str); + assertEquals("Line 1\nLine 2", result); + + str = "Line 1\r\nLine 2\r"; + result = Utils.normalizeLineEndings(str); + assertEquals("Line 1\nLine 2\n", result); + + str = "Line 1\r\nLine 2\nLine 3\rLine 4\r\nLine 5\rLine 6\r\n Line 7\r"; + result = Utils.normalizeLineEndings(str); + assertEquals( + "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\n Line 7\n", + result); + } + +} diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/Utils.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/Utils.java index 7a41ebd1a3..bca96ecd23 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/Utils.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/Utils.java @@ -16,6 +16,10 @@ import org.eclipse.jgit.lib.ObjectId; */ public class Utils { + private static final String EMPTY_STRING = ""; //$NON-NLS-1$ + private static final char CR_CHAR = '\r'; + private static final char LF_CHAR = '\n'; + /** * @param id * @return a shortened ObjectId (first 6 digits) @@ -24,4 +28,39 @@ public class Utils { return id.abbreviate(6).name(); } + /** + * The method replaces all platform specific line endings + * with <code>\n</code> + * @param s + * @return String with normalized line endings + */ + public static String normalizeLineEndings(String s) { + if (s == null) + return null; + if (s.length() == 0) + return EMPTY_STRING; + StringBuilder result = new StringBuilder(); + int length = s.length(); + int i = 0; + while (i < length) { + if (s.charAt(i) == CR_CHAR) { + if (i + 1 < length) { + if (s.charAt(i + 1) == LF_CHAR) { + // CRLF -> LF + result.append(LF_CHAR); + i += 1; + } else { + // CR not followed by LF + result.append(LF_CHAR); + } + } else { + // CR at end of string + result.append(LF_CHAR); + } + } else + result.append(s.charAt(i)); + i++; + } + return result.toString(); + } } diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/SpellcheckableMessageArea.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/SpellcheckableMessageArea.java index 14e5efa12e..8564018186 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/SpellcheckableMessageArea.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/SpellcheckableMessageArea.java @@ -20,6 +20,7 @@ import java.util.List; import java.util.Map; import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.egit.core.internal.Utils; import org.eclipse.egit.ui.Activator; import org.eclipse.egit.ui.UIPreferences; import org.eclipse.egit.ui.UIText; @@ -564,8 +565,7 @@ public class SpellcheckableMessageArea extends Composite { */ public String getCommitMessage() { String text = getText(); - text = text.replaceAll(getTextWidget().getLineDelimiter(), "\n"); //$NON-NLS-1$ - return text; + return Utils.normalizeLineEndings(text); } /** |