Skip to content

Commit

Permalink
Multi line error message fixed #2407
Browse files Browse the repository at this point in the history
When showing the multi line error message, the arrow ^ now points on the
right character.

#2407
  • Loading branch information
jannisCode committed Oct 23, 2024
1 parent 3ffdfe3 commit 856d61d
Showing 1 changed file with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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()
Expand All @@ -62,20 +62,40 @@ 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();

// 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++;
StringBuilder sBuilder = new StringBuilder();

sBuilder.append(description);
if (errorIndex != -1) {
sBuilder.append(" at index ").append(errorIndex); //$NON-NLS-1$
}
sBuilder.append(System.lineSeparator());
sBuilder.append(pattern);
sBuilder.append(System.lineSeparator());

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();
}
}

Expand Down

0 comments on commit 856d61d

Please sign in to comment.