Search in sources :

Example 6 with Diagnostic

use of org.ballerinalang.util.diagnostic.Diagnostic in project ballerina by ballerina-lang.

the class BAssertUtil method validateErrorMessageOnly.

/**
 * Validate if at least one in the given list of texts is contained in the error message.
 *
 * @param result          Result from compilation
 * @param errorIndex      Index of the error in the result
 * @param expectedPartsOfErrMsg  Array of expected parts of error message
 */
public static void validateErrorMessageOnly(CompileResult result, int errorIndex, String[] expectedPartsOfErrMsg) {
    Diagnostic diag = result.getDiagnostics()[errorIndex];
    Assert.assertEquals(diag.getKind(), Diagnostic.Kind.ERROR, "incorrect diagnostic type");
    boolean contains = false;
    for (String part : expectedPartsOfErrMsg) {
        if (diag.getMessage().contains(part)) {
            contains = true;
            break;
        }
    }
    Assert.assertTrue(contains, "None of given strings is contained in the error message '" + diag.getMessage() + '\'');
}
Also used : Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic)

Example 7 with Diagnostic

use of org.ballerinalang.util.diagnostic.Diagnostic in project ballerina by ballerina-lang.

the class BAssertUtil method validateWarning.

/**
 * Assert a warning.
 *
 * @param result           Result from compilation
 * @param warningIndex     Index of the warning in the result
 * @param expectedWarnMsg  Expected warning message
 * @param expectedWarnLine Expected line number of the warning
 * @param expectedWarnCol  Expected column number of the warning
 */
public static void validateWarning(CompileResult result, int warningIndex, String expectedWarnMsg, int expectedWarnLine, int expectedWarnCol) {
    Diagnostic diag = result.getDiagnostics()[warningIndex];
    Assert.assertEquals(diag.getKind(), Diagnostic.Kind.WARNING, "incorrect diagnostic type");
    Assert.assertEquals(diag.getMessage(), expectedWarnMsg, "incorrect warning message:");
    Assert.assertEquals(diag.getPosition().getStartLine(), expectedWarnLine, "incorrect line number:");
    Assert.assertEquals(diag.getPosition().getStartColumn(), expectedWarnCol, "incorrect column position:");
}
Also used : Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic)

Example 8 with Diagnostic

use of org.ballerinalang.util.diagnostic.Diagnostic in project ballerina by ballerina-lang.

the class BCompileUtil method getDiagnostics.

/**
 * Used by IntelliJ IDEA plugin to provide semantic analyzing capability.
 *
 * @param classLoader a {@link ClassLoader} to be set as thread context class loader. This is used by {@link
 *                    java.util.ServiceLoader}. Otherwise semantic analyzing capability providing wont work since it
 *                    cant find core package.
 * @param sourceRoot  source root of a project
 * @param fileName    either the file name (if in project root) or the package name
 * @return list of diagnostics
 */
public static List<Diagnostic> getDiagnostics(ClassLoader classLoader, String sourceRoot, String fileName) {
    Thread.currentThread().setContextClassLoader(classLoader);
    CompilerContext context = new CompilerContext();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, sourceRoot);
    options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString());
    options.put(PRESERVE_WHITESPACE, "false");
    CompileResult comResult = new CompileResult();
    // catch errors
    DiagnosticListener listener = comResult::addDiagnostic;
    context.put(DiagnosticListener.class, listener);
    // compile
    Compiler compiler = Compiler.getInstance(context);
    BLangPackage entryPackageNode = compiler.compile(fileName);
    CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(entryPackageNode);
    if (programFile != null) {
        comResult.setProgFile(LauncherUtils.getExecutableProgram(programFile));
    }
    Diagnostic[] diagnostics = comResult.getDiagnostics();
    return Arrays.stream(diagnostics).collect(Collectors.toList());
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener)

Example 9 with Diagnostic

use of org.ballerinalang.util.diagnostic.Diagnostic in project ballerina by ballerina-lang.

the class BallerinaExternalAnnotator method apply.

/**
 * Called 3rd to actually annotate the editor window.
 */
@Override
public void apply(@NotNull PsiFile file, List<Diagnostic> diagnostics, @NotNull AnnotationHolder holder) {
    String packageName = getPackageName(file);
    String fileName = file.getVirtualFile().getName();
    try {
        for (Diagnostic diagnostic : diagnostics) {
            // Validate the package name.
            if (packageName != null && !diagnostic.getSource().getPackageName().equals(packageName)) {
                continue;
            }
            // Validate the file name since diagnostics are sent for all files in the package.
            if (!fileName.equals(diagnostic.getSource().getCompilationUnitName())) {
                continue;
            }
            Diagnostic.DiagnosticPosition position = diagnostic.getPosition();
            // If the start line or start column is less than 0, it will throw an exception.
            if (position.getStartLine() <= 0 || position.getStartColumn() <= 0) {
                continue;
            }
            // Get the logical start postion. This is used to get the offset.
            LogicalPosition startPosition = new LogicalPosition(position.getStartLine() - 1, position.getStartColumn() - 1);
            int startOffset = editor.logicalPositionToOffset(startPosition);
            // Get the element at the offset.
            PsiElement elementAtOffset = file.findElementAt(startOffset);
            // If the element at the offset is a whitespace, highlight the next element.
            if (elementAtOffset instanceof PsiWhiteSpace) {
                elementAtOffset = PsiTreeUtil.nextVisibleLeaf(elementAtOffset);
            }
            // Get the text range to be highlighted.
            TextRange textRange;
            if (elementAtOffset == null) {
                int endColumn = position.getStartColumn() == position.getEndColumn() ? position.getEndColumn() + 1 : position.getEndColumn();
                if (position.getEndLine() <= 0) {
                    continue;
                }
                LogicalPosition endPosition = new LogicalPosition(position.getEndLine() - 1, endColumn);
                int endOffset = editor.logicalPositionToOffset(endPosition);
                textRange = new TextRange(startOffset, endOffset);
            } else {
                int endOffset = elementAtOffset.getTextOffset() + elementAtOffset.getTextLength();
                textRange = new TextRange(elementAtOffset.getTextOffset(), endOffset);
            }
            // Highlight the range according to the diagnostic kind.
            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
                holder.createErrorAnnotation(textRange, diagnostic.getMessage());
            } else if (diagnostic.getKind() == Diagnostic.Kind.WARNING) {
                holder.createWarningAnnotation(textRange, diagnostic.getMessage());
            } else if (diagnostic.getKind() == Diagnostic.Kind.NOTE) {
                holder.createInfoAnnotation(textRange, diagnostic.getMessage());
            }
        }
    } catch (ClassCastException e) {
        LOGGER.debug(e.getMessage(), e);
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 10 with Diagnostic

use of org.ballerinalang.util.diagnostic.Diagnostic in project ballerina by ballerina-lang.

the class BAssertUtil method validateError.

/**
 * Assert an error (without error message).
 *
 * @param result          Result from compilation
 * @param errorIndex      Index of the error in the result
 * @param expectedErrLine Expected line number of the error
 * @param expectedErrCol  Expected column number of the error
 */
public static void validateError(CompileResult result, int errorIndex, int expectedErrLine, int expectedErrCol) {
    Diagnostic diag = result.getDiagnostics()[errorIndex];
    Assert.assertEquals(diag.getKind(), Diagnostic.Kind.ERROR, "incorrect diagnostic type");
    Assert.assertEquals(diag.getPosition().getStartLine(), expectedErrLine, "incorrect line number:");
    Assert.assertEquals(diag.getPosition().getStartColumn(), expectedErrCol, "incorrect column position:");
}
Also used : Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic)

Aggregations

Diagnostic (org.ballerinalang.util.diagnostic.Diagnostic)14 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)3 Gson (com.google.gson.Gson)2 JsonArray (com.google.gson.JsonArray)2 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 BallerinaFile (org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile)2 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)2 Debugger (org.ballerinalang.util.debugger.Debugger)2 Compiler (org.wso2.ballerinalang.compiler.Compiler)2 BLangCompilationUnit (org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)2 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)2 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 IOException (java.io.IOException)1 CompilerPlugin (org.ballerinalang.compiler.plugins.CompilerPlugin)1