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,40 @@ 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 ();
7175
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 ++;
76+ StringBuilder sBuilder = new StringBuilder ();
77+
78+ sBuilder .append (description );
79+ if (errorIndex != -1 ) {
80+ sBuilder .append (" at index " ).append (errorIndex ); //$NON-NLS-1$
7681 }
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$
89+
90+ while (gc .stringExtent (buildString ).x < gc .stringExtent (stringToIndexString ).x - 2 ) {
91+ buildString += thinSpace ; // $NON-NLS-1$
92+ }
93+ sBuilder .append (buildString );
94+
95+ sBuilder .append ("^" ); //$NON-NLS-1$
96+ gc .dispose ();
7797
78- return message . substring ( 0 , i );
98+ return sBuilder . toString ( );
7999 }
80100 }
81101
0 commit comments