Skip to content

Commit

Permalink
Fix checkstyle fail and add review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
RadCod3 committed Mar 1, 2024
1 parent 4fa5982 commit 7e60e7e
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@

package io.ballerina.cli.task;

import io.ballerina.projects.*;
import io.ballerina.cli.utils.AnnotateDiagnostics;
import io.ballerina.cli.utils.BuildTime;
import io.ballerina.projects.CodeGeneratorResult;
import io.ballerina.projects.CodeModifierResult;
import io.ballerina.projects.Document;
import io.ballerina.projects.JBallerinaBackend;
import io.ballerina.projects.JvmTarget;
import io.ballerina.projects.ModuleName;
import io.ballerina.projects.Package;
import io.ballerina.projects.PackageCompilation;
import io.ballerina.projects.PackageResolution;
import io.ballerina.projects.Project;
Expand All @@ -41,17 +42,22 @@
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;
import java.util.*;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
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 org.jline.jansi.AnsiConsole;

import static org.jline.jansi.Ansi.ansi;

/**
Expand Down Expand Up @@ -214,18 +220,19 @@ public void execute(Project project) {
BuildTime.getInstance().codeGenDuration = System.currentTimeMillis() - start;
}
// HashSet to keep track of the diagnostics to avoid duplicate diagnostics
HashSet<String> diagnosticSet = new HashSet<>();
Set<String> diagnosticSet = new HashSet<>();
// HashMap for documents based on filename
HashMap<String, Document> documentMap = new HashMap<>();
Map<String, Document> documentMap = new HashMap<>();
int terminalWidth = AnsiConsole.getTerminalWidth();

project.currentPackage().moduleIds().forEach(moduleId -> {
project.currentPackage().module(moduleId).documentIds().forEach(documentId -> {
Document document = project.currentPackage().module(moduleId).document(documentId);
Package currentPackage = project.currentPackage();
currentPackage.moduleIds().forEach(moduleId -> {
currentPackage.module(moduleId).documentIds().forEach(documentId -> {
Document document = currentPackage.module(moduleId).document(documentId);
documentMap.put(getDocumentPath(document.module().moduleName(), document.name()), document);
});
project.currentPackage().module(moduleId).testDocumentIds().forEach(documentId -> {
Document document = project.currentPackage().module(moduleId).document(documentId);
currentPackage.module(moduleId).testDocumentIds().forEach(documentId -> {
Document document = currentPackage.module(moduleId).document(documentId);
documentMap.put(getDocumentPath(document.module().moduleName(), document.name()), document);
});
});
Expand All @@ -240,9 +247,9 @@ public void execute(Project project) {
Document document = documentMap.get(d.location().lineRange().fileName());
if (document != null) {
err.println(
ansi().render(AnnotateDiagnostics.renderDiagnostic(d, document, terminalWidth)));
ansi().render(renderDiagnostic(d, document, terminalWidth)));
} else {
err.println(ansi().render(AnnotateDiagnostics.renderDiagnostic(d)));
err.println(ansi().render(renderDiagnostic(d)));
}
}
}
Expand Down Expand Up @@ -273,7 +280,7 @@ private String getDocumentPath(ModuleName moduleName, String documentName) {
if (moduleName.isDefaultModuleName()) {
return documentName;
}
return "modules" + "/" + moduleName.moduleNamePart() + "/" + documentName;
return Paths.get("modules", moduleName.moduleNamePart(), documentName).toString();
}

private boolean isPackCmdForATemplatePkg(Project project) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.cli.utils;

Expand All @@ -25,8 +27,11 @@
import io.ballerina.tools.text.TextDocument;

import java.util.ArrayList;
import java.util.List;

import static io.ballerina.cli.utils.DiagnosticAnnotation.NEW_LINE;
import static io.ballerina.cli.utils.DiagnosticAnnotation.SEVERITY_COLORS;
import static io.ballerina.cli.utils.DiagnosticAnnotation.getColoredString;

/**
* This class is used to generate diagnostic annotations from diagnostics.
Expand All @@ -35,20 +40,25 @@
*/
public class AnnotateDiagnostics {

private static final String COMPILER_ERROR_PREFIX = "BCE";
private static final int SYNTAX_ERROR_CODE_THRESHOLD = 1000;
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) {

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

}

Expand All @@ -62,8 +72,9 @@ private static String diagnosticToString(Diagnostic diagnostic) {
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)";

return String.format("@|%s %s|@%s (%s)", color, severityString, message, code);
return String.format(formatString, severityString, message, code);
}

private static DiagnosticAnnotation getDiagnosticLineFromSyntaxAPI(Document document, Location location,
Expand All @@ -73,23 +84,15 @@ private static DiagnosticAnnotation getDiagnosticLineFromSyntaxAPI(Document docu
int endOffset = location.lineRange().endLine().offset();
int startLine = location.lineRange().startLine().line();
int endLine = location.lineRange().endLine().line();
boolean isMultiline = startLine != endLine;
int length = isMultiline ? textDocument.line(startLine).length() - startOffset : endOffset - startOffset;

if (startLine != endLine) {
return new DiagnosticAnnotation(
getLines(textDocument, startLine, endLine),
startOffset,
textDocument.line(startLine).length() - startOffset,
endOffset,
startLine + 1,
severity,
DiagnosticAnnotation.DiagnosticAnnotationType.REGULAR,
terminalWidth);
}
int length = endOffset - startOffset;
return new DiagnosticAnnotation(
getLines(textDocument, startLine, endLine),
startOffset,
length == 0 ? 1 : length,
isMultiline,
endOffset,
startLine + 1,
severity,
DiagnosticAnnotation.DiagnosticAnnotationType.REGULAR,
Expand All @@ -106,11 +109,12 @@ private static DiagnosticAnnotation getSyntaxDiagnosticAnnotation(Document docum
int padding = 0;
int endLine = location.lineRange().endLine().line();
int endOffset = location.lineRange().endLine().offset();
// missing tokens and keywords
if (diagnosticCode < 400) {
String color = SEVERITY_COLORS.get(DiagnosticSeverity.ERROR);

if (diagnosticCode < MISSING_TOKEN_KEYWORD_CODE_THRESHOLD) {
StringDiagnosticProperty strProperty = (StringDiagnosticProperty) packageDiagnostic.properties().get(0);
String lineString = textDocument.line(startLine).text();
String missingTokenString = "@|red " + strProperty.value() + "|@";
String missingTokenString = getColoredString(strProperty.value(), color);
if (startOffset < lineString.length() && lineString.charAt(startOffset) != ' ') {
missingTokenString = missingTokenString + " ";
}
Expand All @@ -121,45 +125,51 @@ private static DiagnosticAnnotation getSyntaxDiagnosticAnnotation(Document docum

String lineWithMissingToken = lineString.substring(0, startOffset) + missingTokenString +
lineString.substring(startOffset);
ArrayList<String> lines = new ArrayList<>();
List<String> lines = new ArrayList<>();
lines.add(lineWithMissingToken);
return new DiagnosticAnnotation(
lines,
padding + startOffset,
strProperty.value().length(),
false,
0,
startLine + 1,
DiagnosticSeverity.ERROR,
DiagnosticAnnotation.DiagnosticAnnotationType.MISSING,
terminalWidth);
}
// Invalid Token
if (diagnosticCode == 600) {
ArrayList<String> lines = getLines(textDocument, startLine, endLine);

if (diagnosticCode == INVALID_TOKEN_CODE) {
List<String> lines = getLines(textDocument, startLine, endLine);
if (lines.size() > 1) {
String annotatedLine1 = lines.get(0).substring(0, startOffset) + "@|red " +
lines.get(0).substring(startOffset) + "|@";
String annotatedLine2 = "@|red " + lines.get(lines.size() - 1).substring(0, endOffset) + "|@" +
lines.get(lines.size() - 1).substring(endOffset);
String annotatedLine1 = lines.get(0).substring(0, startOffset) +
getColoredString(lines.get(0).substring(startOffset), color);
String annotatedLine2 =
getColoredString(lines.get(lines.size() - 1).substring(0, endOffset), color) +
lines.get(lines.size() - 1).substring(endOffset);
lines.set(0, annotatedLine1);
lines.set(lines.size() - 1, annotatedLine2);
return new DiagnosticAnnotation(
lines,
startOffset,
textDocument.line(startLine).length() - location.lineRange().startLine().offset(),
true,
endOffset,
startLine + 1,
DiagnosticSeverity.ERROR,
DiagnosticAnnotation.DiagnosticAnnotationType.INVALID,
terminalWidth);
}
String line = lines.get(0);
String annotatedLine = line.substring(0, startOffset) + "@|red " +
line.substring(startOffset, endOffset) + "|@" + line.substring(endOffset);
String annotatedLine = line.substring(0, startOffset) +
getColoredString(line.substring(startOffset, endOffset), color) + line.substring(endOffset);
lines.set(0, annotatedLine);
return new DiagnosticAnnotation(
lines,
startOffset,
endOffset - startOffset,
false,
0,
startLine + 1,
DiagnosticSeverity.ERROR,
DiagnosticAnnotation.DiagnosticAnnotationType.INVALID,
Expand All @@ -168,13 +178,12 @@ private static DiagnosticAnnotation getSyntaxDiagnosticAnnotation(Document docum
return getDiagnosticLineFromSyntaxAPI(document, location, DiagnosticSeverity.ERROR, terminalWidth);
}

private static ArrayList<String> getLines(TextDocument textDocument, int start, int end) {
ArrayList<String> lines = new ArrayList<>();
private static List<String> getLines(TextDocument textDocument, int start, int end) {
List<String> lines = new ArrayList<>();
for (int i = start; i <= end; i++) {
lines.add(textDocument.line(i).text());
}
return lines;
}

}

}
Loading

0 comments on commit 7e60e7e

Please sign in to comment.