Search in sources :

Example 1 with CollectDiagnosticListener

use of org.ballerinalang.langserver.CollectDiagnosticListener in project ballerina by ballerina-lang.

the class ParserUtils method compile.

/**
 * Compile a Ballerina file.
 *
 * @param content       file content
 * @param path          file path
 * @param compilerPhase {CompilerPhase} set phase for the compiler.
 * @return
 */
public static BallerinaFile compile(String content, Path path, CompilerPhase compilerPhase) {
    if (documentManager.isFileOpen(path)) {
        documentManager.updateFile(path, content);
    } else {
        documentManager.openFile(path, content);
    }
    String sourceRoot = TextDocumentServiceUtil.getSourceRoot(path);
    String pkgName = TextDocumentServiceUtil.getPackageNameForGivenFile(sourceRoot, path.toString());
    LSDocument sourceDocument = new LSDocument();
    sourceDocument.setUri(path.toUri().toString());
    sourceDocument.setSourceRoot(sourceRoot);
    PackageRepository packageRepository = new WorkspacePackageRepository(sourceRoot, documentManager);
    CompilerContext context = TextDocumentServiceUtil.prepareCompilerContext(packageRepository, sourceDocument, true, documentManager, CompilerPhase.DEFINE);
    List<org.ballerinalang.util.diagnostic.Diagnostic> balDiagnostics = new ArrayList<>();
    CollectDiagnosticListener diagnosticListener = new CollectDiagnosticListener(balDiagnostics);
    context.put(DiagnosticListener.class, diagnosticListener);
    BLangPackage bLangPackage = null;
    try {
        Compiler compiler = Compiler.getInstance(context);
        if ("".equals(pkgName)) {
            Path filePath = path.getFileName();
            if (filePath != null) {
                bLangPackage = compiler.compile(filePath.toString());
            }
        } else {
            bLangPackage = compiler.compile(pkgName);
        }
    } catch (Exception e) {
    // Ignore.
    }
    BallerinaFile bfile = new BallerinaFile();
    bfile.setBLangPackage(bLangPackage);
    bfile.setDiagnostics(balDiagnostics);
    return bfile;
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) BallerinaFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) BDiagnostic(org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnostic) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) PackageRepository(org.ballerinalang.repository.PackageRepository) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) IOException(java.io.IOException) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) LSDocument(org.ballerinalang.langserver.common.LSDocument) CollectDiagnosticListener(org.ballerinalang.langserver.CollectDiagnosticListener)

Aggregations

IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 BallerinaFile (org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile)1 CollectDiagnosticListener (org.ballerinalang.langserver.CollectDiagnosticListener)1 LSDocument (org.ballerinalang.langserver.common.LSDocument)1 WorkspacePackageRepository (org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository)1 PackageRepository (org.ballerinalang.repository.PackageRepository)1 Diagnostic (org.ballerinalang.util.diagnostic.Diagnostic)1 Compiler (org.wso2.ballerinalang.compiler.Compiler)1 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)1 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)1 BDiagnostic (org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnostic)1