Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wolf2019-11-02 07:35:48 -0400
committerThomas Wolf2019-11-02 07:50:10 -0400
commit3a2231f15dffe5ec99beb86e30d06bb1cbc639e5 (patch)
tree1a95a1d16f570a7688f721370d3885c0e0220e91
parent09354fca29aefb20f03c9cde775905878b2b165f (diff)
downloadegit-3a2231f15dffe5ec99beb86e30d06bb1cbc639e5.tar.gz
egit-3a2231f15dffe5ec99beb86e30d06bb1cbc639e5.tar.xz
egit-3a2231f15dffe5ec99beb86e30d06bb1cbc639e5.zip
Add utility methods for handling multi-line text
Change-Id: I27cab234003214042027319b890114939eb62f75 Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/internal/Utils.java53
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java10
2 files changed, 53 insertions, 10 deletions
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 afbfab3dc..b00c0e282 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (C) 2010, 2015 Jens Baumgart <jens.baumgart@sap.com> and others.
+ * Copyright (C) 2010, 2019 Jens Baumgart <jens.baumgart@sap.com> and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -16,6 +16,8 @@ import static org.eclipse.core.runtime.Status.OK_STATUS;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Collection;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.egit.core.Activator;
@@ -26,16 +28,59 @@ import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.lib.Repository;
/**
- * Utility class
- *
+ * General utilities, mostly related to strings.
*/
-public class Utils {
+public final class Utils {
private static final int SHORT_OBJECT_ID_LENGTH = 7;
private static final String EMPTY_STRING = ""; //$NON-NLS-1$
private static final char CR_CHAR = '\r';
private static final char LF_CHAR = '\n';
+ private static final Pattern VERTICAL_SPACES = Pattern.compile("\\v+"); //$NON-NLS-1$
+
+ private Utils() {
+ // No instantiation
+ }
+
+ /**
+ * Reduce a multi-line text to the first line only.
+ *
+ * @param text
+ * to reduce
+ * @return the first line of {@text}, may be empty.
+ */
+ public static String firstLine(String text) {
+ Matcher m = VERTICAL_SPACES.matcher(text);
+ if (m.find()) {
+ return text.substring(0, m.start());
+ }
+ return text;
+ }
+
+ /**
+ * Returns whether the given text contains any vertical whitespace.
+ *
+ * @param text
+ * to check
+ * @return whether {@code text} contains vertical whitespace
+ */
+ public static boolean isMultiLine(String text) {
+ return VERTICAL_SPACES.matcher(text).find();
+ }
+
+ /**
+ * Converts a multi-line text to a single line by replacing all sequences of
+ * vertical whitespace by a single blank.
+ *
+ * @param text
+ * to convert
+ * @return the converted text
+ */
+ public static String toSingleLine(String text) {
+ return VERTICAL_SPACES.matcher(text).replaceAll(" "); //$NON-NLS-1$
+ }
+
/**
* @param id
* @return a shortened ObjectId (first {@value #SHORT_OBJECT_ID_LENGTH}
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java
index 88c3cea20..a9c926071 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java
@@ -46,6 +46,7 @@ import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.egit.core.internal.Utils;
import org.eclipse.egit.core.internal.gerrit.GerritUtil;
import org.eclipse.egit.core.op.CreateLocalBranchOperation;
import org.eclipse.egit.core.op.TagOperation;
@@ -287,12 +288,9 @@ public class FetchGerritChangePage extends WizardPage {
refText = new Text(main, SWT.SINGLE | SWT.BORDER);
GridDataFactory.fillDefaults().grab(true, false).applyTo(refText);
contentProposer = addRefContentProposalToText(refText);
- refText.addVerifyListener(event -> {
- event.text = event.text
- // C.f. https://bugs.eclipse.org/bugs/show_bug.cgi?id=273470
- .replaceAll("\\v", " ") //$NON-NLS-1$ //$NON-NLS-2$
- .trim();
- });
+ // C.f. https://bugs.eclipse.org/bugs/show_bug.cgi?id=273470
+ refText.addVerifyListener(
+ event -> event.text = Utils.toSingleLine(event.text).trim());
final Group checkoutGroup = new Group(main, SWT.SHADOW_ETCHED_IN);
checkoutGroup.setLayout(new GridLayout(3, false));

Back to the top