-
Notifications
You must be signed in to change notification settings - Fork 35
Add Custom Error Messages #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,4 +16,6 @@ | |
| public @interface Refinement { | ||
|
|
||
| public String value(); | ||
|
|
||
| public String msg() default ""; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,9 +7,9 @@ | |
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Annotation to create state transitions in a method. The annotation has two arguments: from : the | ||
| * Annotation to create state transitions in a method. The annotation has three arguments: from : the | ||
| * state in which the object needs to be for the method to be invoked correctly to : the state in | ||
| * which the object will be after the execution of the method | ||
| * which the object will be after the execution of the method msg : optional custom error message to display when refinement is violated | ||
| * e.g. @StateRefinement(from="open(this)", to="closed(this)") | ||
| * | ||
| * @author catarina gamboa | ||
|
|
@@ -21,4 +21,6 @@ | |
| public String from() default ""; | ||
|
|
||
| public String to() default ""; | ||
|
|
||
| public String msg() default ""; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets add documentation here to and say that this msg should be for when the |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,15 +11,17 @@ public class LJDiagnostic extends RuntimeException { | |
| private final String title; | ||
| private final String message; | ||
| private final String accentColor; | ||
| private final String customMessage; | ||
| private String file; | ||
| private ErrorPosition position; | ||
|
|
||
| public LJDiagnostic(String title, String message, SourcePosition pos, String accentColor) { | ||
| public LJDiagnostic(String title, String message, SourcePosition pos, String accentColor, String customMessage) { | ||
| this.title = title; | ||
| this.message = message; | ||
| this.file = (pos != null && pos.getFile() != null) ? pos.getFile().getPath() : null; | ||
| this.position = ErrorPosition.fromSpoonPosition(pos); | ||
| this.accentColor = accentColor; | ||
| this.customMessage = customMessage; | ||
| } | ||
|
|
||
| public String getTitle() { | ||
|
|
@@ -30,6 +32,10 @@ public String getMessage() { | |
| return message; | ||
| } | ||
|
|
||
| public String getCustomMessage() { | ||
| return customMessage; | ||
| } | ||
|
|
||
| public String getDetails() { | ||
| return ""; // to be overridden by subclasses | ||
| } | ||
|
|
@@ -98,25 +104,33 @@ public String getSnippet() { | |
|
|
||
| // calculate padding for line numbers | ||
| int padding = String.valueOf(endLine).length(); | ||
| String pipe = " | "; | ||
|
||
|
|
||
| for (int i = startLine; i <= endLine; i++) { | ||
| String lineNumStr = String.format("%" + padding + "d", i); | ||
| String line = lines.get(i - 1); | ||
|
|
||
| // add line | ||
| sb.append(Colors.GREY).append(lineNumStr).append(" | ").append(line).append(Colors.RESET).append("\n"); | ||
| sb.append(Colors.GREY).append(lineNumStr).append(pipe).append(line).append(Colors.RESET).append("\n"); | ||
|
|
||
| // add error markers on the line(s) with the error | ||
| if (i >= position.lineStart() && i <= position.lineEnd()) { | ||
| int colStart = (i == position.lineStart()) ? position.colStart() : 1; | ||
| int colEnd = (i == position.lineEnd()) ? position.colEnd() : line.length(); | ||
|
|
||
| if (colStart > 0 && colEnd > 0) { | ||
| // line number padding + " | " + column offset | ||
| String indent = " ".repeat(padding) + Colors.GREY + " | " + Colors.RESET | ||
| // line number padding + pipe + column offset | ||
| String indent = " ".repeat(padding) + Colors.GREY + pipe + Colors.RESET | ||
| + " ".repeat(colStart - 1); | ||
| String markers = accentColor + "^".repeat(Math.max(1, colEnd - colStart + 1)) + Colors.RESET; | ||
| sb.append(indent).append(markers).append("\n"); | ||
| String markers = accentColor + "^".repeat(Math.max(1, colEnd - colStart + 1)); | ||
| sb.append(indent).append(markers); | ||
|
|
||
| // custom message | ||
| if (customMessage != null && !customMessage.isBlank()) { | ||
| String offset = " ".repeat(padding + colEnd + pipe.length() + 1); | ||
| sb.append(" " + customMessage.replace("\n", "\n" + offset)); | ||
| } | ||
| sb.append(Colors.RESET).append("\n"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add documentation to all these fields in a follow up pr