Skip to content

Commit c5dacd2

Browse files
committed
changed the error message so that the ^ is always pointing to the right
character
1 parent 0a3c1fd commit c5dacd2

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

bundles/org.eclipse.ui/src/org/eclipse/ui/internal/SearchDecoration.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
package org.eclipse.ui.internal;
1616

17+
1718
import java.util.regex.Pattern;
1819
import java.util.regex.PatternSyntaxException;
1920

2021
import org.eclipse.jface.fieldassist.ControlDecoration;
2122
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
23+
import org.eclipse.swt.graphics.GC;
2224
import org.eclipse.swt.graphics.Image;
2325

2426
/**
@@ -41,7 +43,9 @@ private SearchDecoration() {
4143
* the validation.
4244
*/
4345
public static boolean validateRegex(String regex, ControlDecoration targetDecoration) {
44-
String errorMessage = getValidationError(regex);
46+
GC gc = new GC(targetDecoration.getControl());
47+
48+
String errorMessage = getValidationError(regex, gc);
4549
if (errorMessage.isEmpty()) {
4650
targetDecoration.hide();
4751
return true;
@@ -53,6 +57,7 @@ public static boolean validateRegex(String regex, ControlDecoration targetDecora
5357
targetDecoration.setImage(decorationImage);
5458
targetDecoration.setDescriptionText(errorMessage);
5559
targetDecoration.show();
60+
gc.dispose();
5661
return false;
5762
}
5863

@@ -62,20 +67,33 @@ public static boolean validateRegex(String regex, ControlDecoration targetDecora
6267
* @return The appropriate error message if the regex is invalid or an empty
6368
* string if the regex is valid.
6469
*/
65-
private static String getValidationError(String regex) {
70+
private static String getValidationError(String regex, GC gc) {
6671
try {
6772
Pattern.compile(regex);
6873
return ""; //$NON-NLS-1$
6974
} catch (PatternSyntaxException e) {
70-
String message = e.getLocalizedMessage();
75+
String description = e.getDescription();
76+
int errorIndex = e.getIndex();
77+
String pattern = e.getPattern();
78+
79+
StringBuilder sBuilder = new StringBuilder();
80+
81+
sBuilder.append(description + " at index " + errorIndex); //$NON-NLS-1$
82+
sBuilder.append(System.lineSeparator());
83+
sBuilder.append(pattern);
84+
sBuilder.append(System.lineSeparator());
85+
86+
String stringToIndexString = pattern.substring(0, errorIndex);
87+
String buildString = ""; //$NON-NLS-1$
88+
String thinSpace = "\u2009"; //$NON-NLS-1$
7189

72-
// Only preserve the first line of the original error message.
73-
int i = 0;
74-
while (i < message.length() && "\n\r".indexOf(message.charAt(i)) == -1) { //$NON-NLS-1$
75-
i++;
90+
while (gc.stringExtent(buildString).x < gc.stringExtent(stringToIndexString).x - 1) {
91+
buildString += thinSpace; // $NON-NLS-1$
7692
}
93+
sBuilder.append(buildString);
7794

78-
return message.substring(0, i);
95+
sBuilder.append("^"); //$NON-NLS-1$
96+
return sBuilder.toString();
7997
}
8098
}
8199

0 commit comments

Comments
 (0)