1919
2020import org .eclipse .jface .fieldassist .ControlDecoration ;
2121import org .eclipse .jface .fieldassist .FieldDecorationRegistry ;
22+ import org .eclipse .swt .graphics .GC ;
2223import org .eclipse .swt .graphics .Image ;
2324
2425/**
@@ -41,11 +42,10 @@ private SearchDecoration() {
4142 * the validation.
4243 */
4344 public static boolean validateRegex (String regex , ControlDecoration targetDecoration ) {
44- String errorMessage = getValidationError (regex );
45+ String errorMessage = getValidationError (regex , targetDecoration );
4546 if (errorMessage .isEmpty ()) {
4647 targetDecoration .hide ();
4748 return true ;
48-
4949 }
5050
5151 Image decorationImage = FieldDecorationRegistry .getDefault ()
@@ -62,20 +62,42 @@ public static boolean validateRegex(String regex, ControlDecoration targetDecora
6262 * @return The appropriate error message if the regex is invalid or an empty
6363 * string if the regex is valid.
6464 */
65- private static String getValidationError (String regex ) {
65+ private static String getValidationError (String regex , ControlDecoration targetDecoration ) {
66+ GC gc = new GC (targetDecoration .getControl ());
67+
6668 try {
6769 Pattern .compile (regex );
6870 return "" ; //$NON-NLS-1$
6971 } catch (PatternSyntaxException e ) {
70- String message = e .getLocalizedMessage ();
72+ String description = e .getDescription ();
73+ int errorIndex = e .getIndex ();
74+ String pattern = e .getPattern ();
75+
76+ StringBuilder sBuilder = new StringBuilder ();
77+
78+ sBuilder .append (description );
79+ if (errorIndex == -1 ) {
80+ return sBuilder .toString ();
81+ }
82+
83+ sBuilder .append (" at index " ).append (errorIndex ); //$NON-NLS-1$
84+ sBuilder .append (System .lineSeparator ());
85+ sBuilder .append (pattern );
86+ sBuilder .append (System .lineSeparator ());
7187
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 ++;
88+ String stringToIndexString = pattern .substring (0 , errorIndex );
89+ String buildString = "" ; //$NON-NLS-1$
90+ String thinSpace = "\u2009 " ; //$NON-NLS-1$
91+
92+ while (gc .stringExtent (buildString ).x < gc .stringExtent (stringToIndexString ).x - 2 ) {
93+ buildString += thinSpace ; // $NON-NLS-1$
7694 }
95+ sBuilder .append (buildString );
96+
97+ sBuilder .append ("^" ); //$NON-NLS-1$
98+ gc .dispose ();
7799
78- return message . substring ( 0 , i );
100+ return sBuilder . toString ( );
79101 }
80102 }
81103
0 commit comments