Search in sources :

Example 1 with PackageRepository

use of org.ballerinalang.repository.PackageRepository 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)

Example 2 with PackageRepository

use of org.ballerinalang.repository.PackageRepository in project ballerina by ballerina-lang.

the class TextDocumentServiceUtil method getBLangPackage.

/**
 * Get the BLangPackage for a given program.
 *
 * @param context             Language Server Context
 * @param docManager          Document manager
 * @param preserveWhitespace  Enable preserve whitespace
 * @param customErrorStrategy custom error strategy class
 * @param compileFullProject  compile full project from the source root
 * @return {@link BLangPackage} BLang Package
 */
public static List<BLangPackage> getBLangPackage(LanguageServerContext context, WorkspaceDocumentManager docManager, boolean preserveWhitespace, Class customErrorStrategy, boolean compileFullProject) {
    String uri = context.get(DocumentServiceKeys.FILE_URI_KEY);
    LSDocument document = new LSDocument(uri);
    Path filePath = CommonUtil.getPath(document);
    Path fileNamePath = filePath.getFileName();
    String fileName = "";
    if (fileNamePath != null) {
        fileName = fileNamePath.toString();
    }
    String sourceRoot = TextDocumentServiceUtil.getSourceRoot(filePath);
    String pkgName = TextDocumentServiceUtil.getPackageNameForGivenFile(sourceRoot, filePath.toString());
    LSDocument sourceDocument = new LSDocument();
    sourceDocument.setUri(uri);
    sourceDocument.setSourceRoot(sourceRoot);
    PackageRepository packageRepository = new WorkspacePackageRepository(sourceRoot, docManager);
    List<BLangPackage> packages = new ArrayList<>();
    if (compileFullProject) {
        if (!sourceRoot.isEmpty()) {
            File projectDir = new File(sourceRoot);
            File[] files = projectDir.listFiles();
            if (files != null) {
                for (File file : files) {
                    if ((file.isDirectory() && !file.getName().startsWith(".")) || (!file.isDirectory() && file.getName().endsWith(".bal"))) {
                        Compiler compiler = getCompiler(context, fileName, packageRepository, sourceDocument, preserveWhitespace, customErrorStrategy, docManager);
                        packages.add(compiler.compile(file.getName()));
                    }
                }
            }
        }
    } else {
        Compiler compiler = getCompiler(context, fileName, packageRepository, sourceDocument, preserveWhitespace, customErrorStrategy, docManager);
        if ("".equals(pkgName)) {
            packages.add(compiler.compile(fileName));
        } else {
            packages.add(compiler.compile(pkgName));
        }
    }
    return packages;
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) LSDocument(org.ballerinalang.langserver.common.LSDocument) ArrayList(java.util.ArrayList) PackageRepository(org.ballerinalang.repository.PackageRepository) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) File(java.io.File) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository)

Example 3 with PackageRepository

use of org.ballerinalang.repository.PackageRepository in project ballerina by ballerina-lang.

the class BallerinaTextDocumentService method compileAndSendDiagnostics.

private void compileAndSendDiagnostics(String content, LSDocument document, Path path) {
    String sourceRoot = TextDocumentServiceUtil.getSourceRoot(path);
    String pkgName = TextDocumentServiceUtil.getPackageNameForGivenFile(sourceRoot, path.toString());
    LSDocument sourceDocument = new LSDocument();
    sourceDocument.setUri(document.getURIString());
    sourceDocument.setSourceRoot(sourceRoot);
    PackageRepository packageRepository = new WorkspacePackageRepository(sourceRoot, documentManager);
    CompilerContext context = TextDocumentServiceUtil.prepareCompilerContext(packageRepository, sourceDocument, false, documentManager);
    List<org.ballerinalang.util.diagnostic.Diagnostic> balDiagnostics = new ArrayList<>();
    CollectDiagnosticListener diagnosticListener = new CollectDiagnosticListener(balDiagnostics);
    context.put(DiagnosticListener.class, diagnosticListener);
    Compiler compiler = Compiler.getInstance(context);
    if ("".equals(pkgName)) {
        Path filePath = path.getFileName();
        if (filePath != null) {
            compiler.compile(filePath.toString());
        }
    } else {
        compiler.compile(pkgName);
    }
    publishDiagnostics(balDiagnostics, path);
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) PackageRepository(org.ballerinalang.repository.PackageRepository) MarkedString(org.eclipse.lsp4j.MarkedString) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) LSDocument(org.ballerinalang.langserver.common.LSDocument)

Aggregations

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