Skip to content

Commit

Permalink
Add option to disable color for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RadCod3 committed Mar 3, 2024
1 parent 43cba09 commit 6c5edf1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public void execute(Project project) {
// HashMap for documents based on filename
Map<String, Document> documentMap = new HashMap<>();
int terminalWidth = AnnotateDiagnostics.getTerminalWidth();
boolean colorEnabled = terminalWidth != 0;

Package currentPackage = project.currentPackage();
currentPackage.moduleIds().forEach(moduleId -> {
Expand All @@ -243,9 +244,10 @@ public void execute(Project project) {
if (diagnosticSet.add(d.toString())) {
Document document = documentMap.get(d.location().lineRange().fileName());
if (document != null) {
err.println(AnnotateDiagnostics.renderDiagnostic(d, document, terminalWidth));
err.println(AnnotateDiagnostics.renderDiagnostic(d, document,
terminalWidth == 0 ? 999 : terminalWidth, colorEnabled));
} else {
err.println(AnnotateDiagnostics.renderDiagnostic(d));
err.println(AnnotateDiagnostics.renderDiagnostic(d, colorEnabled));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,44 +47,47 @@ public class AnnotateDiagnostics {
private static final int MISSING_TOKEN_KEYWORD_CODE_THRESHOLD = 400;
private static final int INVALID_TOKEN_CODE = 600;

public static Ansi renderDiagnostic(Diagnostic diagnostic, Document document, int terminalWidth) {
public static Ansi renderDiagnostic(Diagnostic diagnostic, Document document, int terminalWidth,
boolean colorEnabled) {

String diagnosticCode = diagnostic.diagnosticInfo().code();
if (diagnostic instanceof PackageDiagnostic && diagnosticCode.startsWith(COMPILER_ERROR_PREFIX)) {
int diagnosticCodeNumber = Integer.parseInt(diagnosticCode.substring(3));
if (diagnosticCodeNumber < SYNTAX_ERROR_CODE_THRESHOLD) {
PackageDiagnostic packageDiagnostic = (PackageDiagnostic) diagnostic;
return Ansi.ansi().render(diagnosticToString(diagnostic) + NEW_LINE + getSyntaxDiagnosticAnnotation(
document, packageDiagnostic, diagnosticCodeNumber, terminalWidth));
return Ansi.ansi()
.render(diagnosticToString(diagnostic, colorEnabled) + NEW_LINE + getSyntaxDiagnosticAnnotation(
document, packageDiagnostic, diagnosticCodeNumber, terminalWidth, colorEnabled));
}
}
DiagnosticAnnotation diagnosticAnnotation = getDiagnosticLineFromSyntaxAPI(
document, diagnostic.location(), diagnostic.diagnosticInfo().severity(), terminalWidth);
return Ansi.ansi().render(diagnosticToString(diagnostic) + NEW_LINE + diagnosticAnnotation);
document, diagnostic.location(), diagnostic.diagnosticInfo().severity(), terminalWidth, colorEnabled);
return Ansi.ansi().render(diagnosticToString(diagnostic, colorEnabled) + NEW_LINE + diagnosticAnnotation);

}

public static int getTerminalWidth() {
return AnsiConsole.getTerminalWidth();
}

public static Ansi renderDiagnostic(Diagnostic diagnostic) {
return Ansi.ansi().render(diagnosticToString(diagnostic));
public static Ansi renderDiagnostic(Diagnostic diagnostic, boolean colorEnabled) {
return Ansi.ansi().render(diagnosticToString(diagnostic, colorEnabled));
}

private static String diagnosticToString(Diagnostic diagnostic) {
private static String diagnosticToString(Diagnostic diagnostic, boolean colorEnabled) {
DiagnosticSeverity severity = diagnostic.diagnosticInfo().severity();
String severityString = severity.toString();
String color = SEVERITY_COLORS.get(severity);
String message = diagnostic.toString().substring(severityString.length());
String code = diagnostic.diagnosticInfo().code();
String formatString = getColoredString("%s", color) + "%s (%s)";
String formatString = getColoredString("%s", color, colorEnabled) + "%s (%s)";

return String.format(formatString, severityString, message, code);
}

private static DiagnosticAnnotation getDiagnosticLineFromSyntaxAPI(Document document, Location location,
DiagnosticSeverity severity, int terminalWidth) {
DiagnosticSeverity severity, int terminalWidth,
boolean colorEnabled) {
TextDocument textDocument = document.textDocument();
int startOffset = location.lineRange().startLine().offset();
int endOffset = location.lineRange().endLine().offset();
Expand All @@ -102,12 +105,13 @@ private static DiagnosticAnnotation getDiagnosticLineFromSyntaxAPI(Document docu
startLine + 1,
severity,
DiagnosticAnnotation.DiagnosticAnnotationType.REGULAR,
terminalWidth);
terminalWidth, colorEnabled);
}

private static DiagnosticAnnotation getSyntaxDiagnosticAnnotation(Document document,
PackageDiagnostic packageDiagnostic,
int diagnosticCode, int terminalWidth) {
int diagnosticCode, int terminalWidth,
boolean colorEnabled) {
TextDocument textDocument = document.textDocument();
Location location = packageDiagnostic.location();
int startLine = location.lineRange().startLine().line();
Expand All @@ -120,7 +124,7 @@ private static DiagnosticAnnotation getSyntaxDiagnosticAnnotation(Document docum
if (diagnosticCode < MISSING_TOKEN_KEYWORD_CODE_THRESHOLD) {
StringDiagnosticProperty strProperty = (StringDiagnosticProperty) packageDiagnostic.properties().get(0);
String lineString = textDocument.line(startLine).text();
String missingTokenString = getColoredString(strProperty.value(), color);
String missingTokenString = getColoredString(strProperty.value(), color, colorEnabled);
if (startOffset < lineString.length() && lineString.charAt(startOffset) != ' ') {
missingTokenString = missingTokenString + " ";
}
Expand All @@ -142,16 +146,16 @@ private static DiagnosticAnnotation getSyntaxDiagnosticAnnotation(Document docum
startLine + 1,
DiagnosticSeverity.ERROR,
DiagnosticAnnotation.DiagnosticAnnotationType.MISSING,
terminalWidth);
terminalWidth, colorEnabled);
}

if (diagnosticCode == INVALID_TOKEN_CODE) {
List<String> lines = getLines(textDocument, startLine, endLine);
if (lines.size() > 1) {
String annotatedLine1 = lines.get(0).substring(0, startOffset) +
getColoredString(lines.get(0).substring(startOffset), color);
getColoredString(lines.get(0).substring(startOffset), color, colorEnabled);
String annotatedLine2 =
getColoredString(lines.get(lines.size() - 1).substring(0, endOffset), color) +
getColoredString(lines.get(lines.size() - 1).substring(0, endOffset), color, colorEnabled) +
lines.get(lines.size() - 1).substring(endOffset);
lines.set(0, annotatedLine1);
lines.set(lines.size() - 1, annotatedLine2);
Expand All @@ -164,11 +168,12 @@ private static DiagnosticAnnotation getSyntaxDiagnosticAnnotation(Document docum
startLine + 1,
DiagnosticSeverity.ERROR,
DiagnosticAnnotation.DiagnosticAnnotationType.INVALID,
terminalWidth);
terminalWidth, colorEnabled);
}
String line = lines.get(0);
String annotatedLine = line.substring(0, startOffset) +
getColoredString(line.substring(startOffset, endOffset), color) + line.substring(endOffset);
getColoredString(line.substring(startOffset, endOffset), color, colorEnabled) +
line.substring(endOffset);
lines.set(0, annotatedLine);
return new DiagnosticAnnotation(
lines,
Expand All @@ -179,9 +184,10 @@ private static DiagnosticAnnotation getSyntaxDiagnosticAnnotation(Document docum
startLine + 1,
DiagnosticSeverity.ERROR,
DiagnosticAnnotation.DiagnosticAnnotationType.INVALID,
terminalWidth);
terminalWidth, colorEnabled);
}
return getDiagnosticLineFromSyntaxAPI(document, location, DiagnosticSeverity.ERROR, terminalWidth);
return getDiagnosticLineFromSyntaxAPI(document, location, DiagnosticSeverity.ERROR, terminalWidth,
colorEnabled);
}

private static List<String> getLines(TextDocument textDocument, int start, int end) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ public enum DiagnosticAnnotationType {
private final int terminalWidth;
private final DiagnosticSeverity severity;
private final DiagnosticAnnotationType type;
private final boolean colorEnabled;

public DiagnosticAnnotation(List<String> lines, int start, int length, boolean isMultiline, int endOffset,
int startLineNumber, DiagnosticSeverity severity, DiagnosticAnnotationType type,
int terminalWidth) {
int terminalWidth, boolean colorEnabled) {
this.start = start + 3 * countTabChars(lines.get(0), start);
lines.set(0, replaceTabs(lines.get(0), start));
this.lines = lines;
Expand All @@ -70,6 +71,7 @@ public DiagnosticAnnotation(List<String> lines, int start, int length, boolean i
this.severity = severity;
this.type = type;
this.terminalWidth = terminalWidth;
this.colorEnabled = colorEnabled;
}

public String toString() {
Expand All @@ -78,10 +80,11 @@ public String toString() {
String padding = " ".repeat(digitsNum + 1);
int maxLength = terminalWidth - digitsNum - 3;
TruncateResult result = truncate(lines.get(0), maxLength, start, length);
return padding + "| " + NEW_LINE
return padding + "|" + NEW_LINE
+ String.format("%" + digitsNum + "d ", startLineNumber) + "| " + result.line + NEW_LINE
+ padding + "| " +
getUnderline(result.diagnosticStart, result.diagnosticLength, this.severity, this.type) + NEW_LINE;
getUnderline(result.diagnosticStart, result.diagnosticLength, this.severity, this.type,
colorEnabled) + NEW_LINE;

}

Expand All @@ -104,39 +107,44 @@ public String toString() {
endOffset + 3 * tabsInLastLine);

if (lines.size() == 2) {
return padding + "| " + NEW_LINE
return padding + "|" + NEW_LINE
+ String.format("%" + endDigitsNum + "d ", startLineNumber) + "| " + result1.line + NEW_LINE
+ padding + "| " +
getUnderline(result1.diagnosticStart, result1.diagnosticLength, this.severity, this.type) + NEW_LINE
getUnderline(result1.diagnosticStart, result1.diagnosticLength, this.severity, this.type,
colorEnabled) + NEW_LINE
+ String.format("%" + endDigitsNum + "d ", startLineNumber + 1) + "| " + result2.line + NEW_LINE
+ padding + "| " + getUnderline(0, result2.diagnosticLength, this.severity, this.type) + NEW_LINE
+ padding + "| " + NEW_LINE;
+ padding + "| " +
getUnderline(0, result2.diagnosticLength, this.severity, this.type, colorEnabled) + NEW_LINE
+ padding + "|" + NEW_LINE;
}
String padding2 = " ".repeat(Math.min(terminalWidth, maxLineLength) / 2);
return padding + "| " + NEW_LINE
return padding + "|" + NEW_LINE
+ String.format("%" + endDigitsNum + "d ", startLineNumber) + "| " + result1.line + NEW_LINE
+ paddingWithColon + "| " +
getUnderline(result1.diagnosticStart, result1.diagnosticLength, this.severity, this.type) + NEW_LINE
getUnderline(result1.diagnosticStart, result1.diagnosticLength, this.severity, this.type,
colorEnabled) + NEW_LINE
+ paddingWithColon + "| " + padding2 + ":" + NEW_LINE
+ paddingWithColon + "| " + padding2 + ":" + NEW_LINE
+ String.format("%" + endDigitsNum + "d ", startLineNumber + lines.size() - 1) + "| "
+ result2.line + NEW_LINE
+ padding + "| " + getUnderline(0, result2.diagnosticLength, this.severity, this.type) + NEW_LINE
+ padding + "| " + NEW_LINE;
+ padding + "| " + getUnderline(0, result2.diagnosticLength, this.severity, this.type, colorEnabled) +
NEW_LINE
+ padding + "|" + NEW_LINE;

}

public static String getColoredString(String message, String color) {
return JANSI_ANNOTATOR + color + " " + message + JANSI_RESET;
public static String getColoredString(String message, String color, boolean colorEnabled) {
return colorEnabled ? JANSI_ANNOTATOR + color + " " + message + JANSI_RESET : message;
}

private static String getUnderline(int offset, int length, DiagnosticSeverity severity,
DiagnosticAnnotationType type) {
DiagnosticAnnotationType type, boolean colorEnabled) {
String symbol = "^";
if (type == DiagnosticAnnotationType.MISSING) {
symbol = "+";
}
return " ".repeat(offset) + getColoredString(symbol.repeat(length), SEVERITY_COLORS.get(severity));
return " ".repeat(offset) +
getColoredString(symbol.repeat(length), SEVERITY_COLORS.get(severity), colorEnabled);
}

private static int countTabChars(String line, int end) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Compiling source
file_create.bal
WARNING [:(1:1,1:1)] Skipped adding the generated source file with prefix "dummyfunc". Source file generation is not supported with standalone bal files
WARNING [file_create.bal:(6:4,6:58)] unused variable 'isSuccess'
WARNING [:(1:1,1:1)] Skipped adding the generated source file with prefix "dummyfunc". Source file generation is not supported with standalone bal files (BCE5401)
WARNING [file_create.bal:(6:4,6:58)] unused variable 'isSuccess' (BCE20403)
|
6 | boolean|error isSuccess = createNewFileInternal(file);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


Running executable

Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Compiling source
file_create.bal
WARNING [:(1:1,1:1)] Skipped adding the generated source file with prefix "dummyfunc". Source file generation is not supported with standalone bal files
WARNING [file_create.bal:(6:4,6:58)] unused variable 'isSuccess'
WARNING [:(1:1,1:1)] Skipped adding the generated source file with prefix "dummyfunc". Source file generation is not supported with standalone bal files (BCE5401)
WARNING [file_create.bal:(6:4,6:58)] unused variable 'isSuccess' (BCE20403)
|
6 | boolean|error isSuccess = createNewFileInternal(file);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


Running executable

0 comments on commit 6c5edf1

Please sign in to comment.