Skip to content

Commit

Permalink
Move usage of jansi to AnnotateDiagnostics class
Browse files Browse the repository at this point in the history
  • Loading branch information
RadCod3 committed Mar 3, 2024
1 parent d340ce7 commit 43cba09
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.ballerina.cli.task;

import io.ballerina.cli.utils.AnnotateDiagnostics;
import io.ballerina.cli.utils.BuildTime;
import io.ballerina.projects.CodeGeneratorResult;
import io.ballerina.projects.CodeModifierResult;
Expand All @@ -42,7 +43,6 @@
import io.ballerina.tools.diagnostics.DiagnosticInfo;
import io.ballerina.tools.diagnostics.DiagnosticSeverity;
import org.ballerinalang.central.client.CentralClientConstants;
import org.jline.jansi.AnsiConsole;
import org.wso2.ballerinalang.util.RepoUtils;

import java.io.PrintStream;
Expand All @@ -55,10 +55,8 @@
import java.util.Set;

import static io.ballerina.cli.launcher.LauncherUtils.createLauncherException;
import static io.ballerina.cli.utils.AnnotateDiagnostics.renderDiagnostic;
import static io.ballerina.projects.util.ProjectConstants.DOT;
import static io.ballerina.projects.util.ProjectConstants.TOOL_DIAGNOSTIC_CODE_PREFIX;
import static org.jline.jansi.Ansi.ansi;

/**
* Task for compiling a package.
Expand Down Expand Up @@ -223,7 +221,7 @@ public void execute(Project project) {
Set<String> diagnosticSet = new HashSet<>();
// HashMap for documents based on filename
Map<String, Document> documentMap = new HashMap<>();
int terminalWidth = AnsiConsole.getTerminalWidth();
int terminalWidth = AnnotateDiagnostics.getTerminalWidth();

Package currentPackage = project.currentPackage();
currentPackage.moduleIds().forEach(moduleId -> {
Expand All @@ -236,7 +234,6 @@ public void execute(Project project) {
documentMap.put(getDocumentPath(document.module().moduleName(), document.name()), document);
});
});
AnsiConsole.systemInstall();
// Report package compilation and backend diagnostics
diagnostics.addAll(jBallerinaBackend.diagnosticResult().diagnostics(false));
diagnostics.forEach(d -> {
Expand All @@ -246,15 +243,13 @@ public void execute(Project project) {
if (diagnosticSet.add(d.toString())) {
Document document = documentMap.get(d.location().lineRange().fileName());
if (document != null) {
err.println(
ansi().render(renderDiagnostic(d, document, terminalWidth)));
err.println(AnnotateDiagnostics.renderDiagnostic(d, document, terminalWidth));
} else {
err.println(ansi().render(renderDiagnostic(d)));
err.println(AnnotateDiagnostics.renderDiagnostic(d));
}
}
}
});
AnsiConsole.systemUninstall();
// Report build tool execution diagnostics
if (project.getToolContextMap() != null) {
for (ToolContext tool : project.getToolContextMap().values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import io.ballerina.tools.diagnostics.DiagnosticSeverity;
import io.ballerina.tools.diagnostics.Location;
import io.ballerina.tools.text.TextDocument;
import org.jline.jansi.Ansi;
import org.jline.jansi.AnsiConsole;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -45,25 +47,29 @@ public class AnnotateDiagnostics {
private static final int MISSING_TOKEN_KEYWORD_CODE_THRESHOLD = 400;
private static final int INVALID_TOKEN_CODE = 600;

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

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 diagnosticToString(diagnostic) + NEW_LINE + getSyntaxDiagnosticAnnotation(
document, packageDiagnostic, diagnosticCodeNumber, terminalWidth);
return Ansi.ansi().render(diagnosticToString(diagnostic) + NEW_LINE + getSyntaxDiagnosticAnnotation(
document, packageDiagnostic, diagnosticCodeNumber, terminalWidth));
}
}
DiagnosticAnnotation diagnosticAnnotation = getDiagnosticLineFromSyntaxAPI(
document, diagnostic.location(), diagnostic.diagnosticInfo().severity(), terminalWidth);
return diagnosticToString(diagnostic) + NEW_LINE + diagnosticAnnotation;
return Ansi.ansi().render(diagnosticToString(diagnostic) + NEW_LINE + diagnosticAnnotation);

}

public static String renderDiagnostic(Diagnostic diagnostic) {
return diagnosticToString(diagnostic);
public static int getTerminalWidth() {
return AnsiConsole.getTerminalWidth();
}

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

private static String diagnosticToString(Diagnostic diagnostic) {
Expand Down

0 comments on commit 43cba09

Please sign in to comment.