From dbb5d95c149fe17f16887b8d74c50bcc65ff72ce Mon Sep 17 00:00:00 2001 From: jannisCode Date: Mon, 14 Oct 2024 15:04:49 +0200 Subject: [PATCH] Multi line error message fixed #2407 When showing the multi line error message, the arrow ^ now points on the right character. https://github.com/eclipse-platform/eclipse.platform.ui/pull/2407 --- .../eclipse/ui/internal/SearchDecoration.java | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/bundles/org.eclipse.ui/src/org/eclipse/ui/internal/SearchDecoration.java b/bundles/org.eclipse.ui/src/org/eclipse/ui/internal/SearchDecoration.java index 47e4e148356..7ccf95b61ad 100644 --- a/bundles/org.eclipse.ui/src/org/eclipse/ui/internal/SearchDecoration.java +++ b/bundles/org.eclipse.ui/src/org/eclipse/ui/internal/SearchDecoration.java @@ -19,6 +19,7 @@ import org.eclipse.jface.fieldassist.ControlDecoration; import org.eclipse.jface.fieldassist.FieldDecorationRegistry; +import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; /** @@ -41,11 +42,10 @@ private SearchDecoration() { * the validation. */ public static boolean validateRegex(String regex, ControlDecoration targetDecoration) { - String errorMessage = getValidationError(regex); + String errorMessage = getValidationError(regex, targetDecoration); if (errorMessage.isEmpty()) { targetDecoration.hide(); return true; - } Image decorationImage = FieldDecorationRegistry.getDefault() @@ -62,20 +62,42 @@ public static boolean validateRegex(String regex, ControlDecoration targetDecora * @return The appropriate error message if the regex is invalid or an empty * string if the regex is valid. */ - private static String getValidationError(String regex) { + private static String getValidationError(String regex, ControlDecoration targetDecoration) { + GC gc = new GC(targetDecoration.getControl()); + try { Pattern.compile(regex); return ""; //$NON-NLS-1$ } catch (PatternSyntaxException e) { - String message = e.getLocalizedMessage(); + String description = e.getDescription(); + int errorIndex = e.getIndex(); + String pattern = e.getPattern(); + + StringBuilder sBuilder = new StringBuilder(); + + sBuilder.append(description); + if (errorIndex == -1) { + return sBuilder.toString(); + } + + sBuilder.append(" at index ").append(errorIndex); //$NON-NLS-1$ + sBuilder.append(System.lineSeparator()); + sBuilder.append(pattern); + sBuilder.append(System.lineSeparator()); - // Only preserve the first line of the original error message. - int i = 0; - while (i < message.length() && "\n\r".indexOf(message.charAt(i)) == -1) { //$NON-NLS-1$ - i++; + String stringToIndexString = pattern.substring(0, errorIndex); + String buildString = ""; //$NON-NLS-1$ + String thinSpace = "\u2009"; //$NON-NLS-1$ + + while (gc.stringExtent(buildString).x < gc.stringExtent(stringToIndexString).x - 2) { + buildString += thinSpace; // $NON-NLS-1$ } + sBuilder.append(buildString); + + sBuilder.append("^"); //$NON-NLS-1$ + gc.dispose(); - return message.substring(0, i); + return sBuilder.toString(); } }