@@ -11,15 +11,18 @@ public class LJDiagnostic extends RuntimeException {
1111 private final String title ;
1212 private final String message ;
1313 private final String accentColor ;
14+ private final String customMessage ;
1415 private String file ;
1516 private ErrorPosition position ;
17+ private static final String PIPE = " | " ;
1618
17- public LJDiagnostic (String title , String message , SourcePosition pos , String accentColor ) {
19+ public LJDiagnostic (String title , String message , SourcePosition pos , String accentColor , String customMessage ) {
1820 this .title = title ;
1921 this .message = message ;
2022 this .file = (pos != null && pos .getFile () != null ) ? pos .getFile ().getPath () : null ;
2123 this .position = ErrorPosition .fromSpoonPosition (pos );
2224 this .accentColor = accentColor ;
25+ this .customMessage = customMessage ;
2326 }
2427
2528 public String getTitle () {
@@ -30,6 +33,10 @@ public String getMessage() {
3033 return message ;
3134 }
3235
36+ public String getCustomMessage () {
37+ return customMessage ;
38+ }
39+
3340 public String getDetails () {
3441 return "" ; // to be overridden by subclasses
3542 }
@@ -104,19 +111,26 @@ public String getSnippet() {
104111 String line = lines .get (i - 1 );
105112
106113 // add line
107- sb .append (Colors .GREY ).append (lineNumStr ).append (" | " ).append (line ).append (Colors .RESET ).append ("\n " );
114+ sb .append (Colors .GREY ).append (lineNumStr ).append (PIPE ).append (line ).append (Colors .RESET ).append ("\n " );
108115
109116 // add error markers on the line(s) with the error
110117 if (i >= position .lineStart () && i <= position .lineEnd ()) {
111118 int colStart = (i == position .lineStart ()) ? position .colStart () : 1 ;
112119 int colEnd = (i == position .lineEnd ()) ? position .colEnd () : line .length ();
113120
114121 if (colStart > 0 && colEnd > 0 ) {
115- // line number padding + " | " + column offset
116- String indent = " " .repeat (padding ) + Colors .GREY + " | " + Colors .RESET
122+ // line number padding + pipe + column offset
123+ String indent = " " .repeat (padding ) + Colors .GREY + PIPE + Colors .RESET
117124 + " " .repeat (colStart - 1 );
118- String markers = accentColor + "^" .repeat (Math .max (1 , colEnd - colStart + 1 )) + Colors .RESET ;
119- sb .append (indent ).append (markers ).append ("\n " );
125+ String markers = accentColor + "^" .repeat (Math .max (1 , colEnd - colStart + 1 ));
126+ sb .append (indent ).append (markers );
127+
128+ // custom message
129+ if (customMessage != null && !customMessage .isBlank ()) {
130+ String offset = " " .repeat (padding + colEnd + PIPE .length () + 1 );
131+ sb .append (" " + customMessage .replace ("\n " , "\n " + offset ));
132+ }
133+ sb .append (Colors .RESET ).append ("\n " );
120134 }
121135 }
122136 }
0 commit comments