Search in sources :

Example 21 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class ParserUtils method getAllPackages.

/**
 * Get All Native Packages.
 *
 * @return {@link Map} Package name, package functions and connectors
 */
public static Map<String, ModelPackage> getAllPackages() {
    final Map<String, ModelPackage> modelPackage = new HashMap<>();
    // TODO: remove once the packerina api for package listing is available
    final String[] packageNames = { "net.http", "net.http.authadaptor", "net.http.endpoints", "net.http.mock", "net.http.swagger", "net.uri", "mime", "net.websub", "net.websub.hub", "net.grpc", "auth", "auth.authz", "auth.authz.permissionstore", "auth.basic", "auth.jwtAuth", "auth.userstore", "auth.utils", "caching", "collections", "config", "data.sql", "file", "internal", "io", "jwt", "jwt.signature", "log", "math", "os", "reflect", "runtime", "security.crypto", "task", "time", "transactions.coordinator", "user", "util" };
    try {
        List<BLangPackage> builtInPackages = LSPackageLoader.getBuiltinPackages();
        for (BLangPackage bLangPackage : builtInPackages) {
            loadPackageMap(bLangPackage.packageID.getName().getValue(), bLangPackage, modelPackage);
        }
        CompilerContext context = CommonUtil.prepareTempCompilerContext();
        for (String packageName : packageNames) {
            PackageID packageID = new PackageID(new Name("ballerina"), new Name(packageName), new Name("0.0.0"));
            BLangPackage bLangPackage = LSPackageLoader.getPackageById(context, packageID);
            loadPackageMap(bLangPackage.packageID.getName().getValue(), bLangPackage, modelPackage);
        }
    } catch (Exception e) {
        // Above catch is to fail safe composer front end due to core errors.
        logger.warn("Error while loading packages");
    }
    return modelPackage;
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) HashMap(java.util.HashMap) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) PackageID(org.ballerinalang.model.elements.PackageID) IOException(java.io.IOException) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 22 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class BallerinaTextDocumentService method references.

@Override
public CompletableFuture<List<? extends Location>> references(ReferenceParams params) {
    return CompletableFuture.supplyAsync(() -> {
        TextDocumentServiceContext referenceContext = new TextDocumentServiceContext();
        referenceContext.put(DocumentServiceKeys.FILE_URI_KEY, params.getTextDocument().getUri());
        referenceContext.put(DocumentServiceKeys.POSITION_KEY, params);
        List<Location> contents = new ArrayList<>();
        try {
            List<BLangPackage> bLangPackages = TextDocumentServiceUtil.getBLangPackage(referenceContext, documentManager, false, LSCustomErrorStrategy.class, true);
            // Get the current package.
            BLangPackage currentBLangPackage = CommonUtil.getCurrentPackageByFileName(bLangPackages, params.getTextDocument().getUri());
            referenceContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, currentBLangPackage.symbol.getName().getValue());
            // Calculate position for the current package.
            PositionTreeVisitor positionTreeVisitor = new PositionTreeVisitor(referenceContext);
            currentBLangPackage.accept(positionTreeVisitor);
            // Run reference visitor for all the packages in project folder.
            for (BLangPackage bLangPackage : bLangPackages) {
                referenceContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
                lSPackageCache.addPackage(bLangPackage);
                referenceContext.put(NodeContextKeys.REFERENCE_NODES_KEY, contents);
                contents = ReferenceUtil.getReferences(referenceContext, bLangPackage);
            }
        } catch (Exception e) {
        // Ignore
        }
        return contents;
    });
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) ArrayList(java.util.ArrayList) PositionTreeVisitor(org.ballerinalang.langserver.common.position.PositionTreeVisitor) Location(org.eclipse.lsp4j.Location)

Example 23 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class BallerinaTextDocumentService method signatureHelp.

@Override
public CompletableFuture<SignatureHelp> signatureHelp(TextDocumentPositionParams position) {
    return CompletableFuture.supplyAsync(() -> {
        String uri = position.getTextDocument().getUri();
        String fileContent = this.documentManager.getFileContent(Paths.get(URI.create(uri)));
        TextDocumentServiceContext signatureContext = new TextDocumentServiceContext();
        SignatureHelpUtil.captureCallableItemInfo(position.getPosition(), fileContent, signatureContext);
        signatureContext.put(DocumentServiceKeys.POSITION_KEY, position);
        signatureContext.put(DocumentServiceKeys.FILE_URI_KEY, uri);
        SignatureHelp signatureHelp;
        try {
            BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(signatureContext, documentManager, false, LSCustomErrorStrategy.class, false).get(0);
            signatureContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
            SignatureTreeVisitor signatureTreeVisitor = new SignatureTreeVisitor(signatureContext);
            bLangPackage.accept(signatureTreeVisitor);
            signatureContext.put(DocumentServiceKeys.LS_PACKAGE_CACHE_KEY, lSPackageCache);
            signatureHelp = SignatureHelpUtil.getFunctionSignatureHelp(signatureContext);
        } catch (Exception e) {
            signatureHelp = new SignatureHelp();
        }
        return signatureHelp;
    });
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) MarkedString(org.eclipse.lsp4j.MarkedString) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy) SignatureTreeVisitor(org.ballerinalang.langserver.signature.SignatureTreeVisitor)

Example 24 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class BallerinaTextDocumentService method completion.

@Override
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion(TextDocumentPositionParams position) {
    return CompletableFuture.supplyAsync(() -> {
        List<CompletionItem> completions;
        TextDocumentServiceContext completionContext = new TextDocumentServiceContext();
        completionContext.put(DocumentServiceKeys.POSITION_KEY, position);
        completionContext.put(DocumentServiceKeys.FILE_URI_KEY, position.getTextDocument().getUri());
        completionContext.put(DocumentServiceKeys.LS_PACKAGE_CACHE_KEY, lSPackageCache);
        try {
            BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(completionContext, documentManager, false, CompletionCustomErrorStrategy.class, false).get(0);
            completionContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
            completionContext.put(DocumentServiceKeys.CURRENT_BLANG_PACKAGE_CONTEXT_KEY, bLangPackage);
            // Visit the package to resolve the symbols
            TreeVisitor treeVisitor = new TreeVisitor(completionContext);
            bLangPackage.accept(treeVisitor);
            BLangNode symbolEnvNode = completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY);
            if (symbolEnvNode == null) {
                completions = CompletionItemResolver.getResolverByClass(TopLevelResolver.class).resolveItems(completionContext);
            } else {
                completions = CompletionItemResolver.getResolverByClass(symbolEnvNode.getClass()).resolveItems(completionContext);
            }
        } catch (Exception | AssertionError e) {
            completions = new ArrayList<>();
        }
        return Either.forLeft(completions);
    });
}
Also used : TreeVisitor(org.ballerinalang.langserver.completions.TreeVisitor) SignatureTreeVisitor(org.ballerinalang.langserver.signature.SignatureTreeVisitor) PositionTreeVisitor(org.ballerinalang.langserver.common.position.PositionTreeVisitor) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) CompletionCustomErrorStrategy(org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy) CompletionItem(org.eclipse.lsp4j.CompletionItem) ArrayList(java.util.ArrayList)

Example 25 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class BallerinaTextDocumentService method rename.

@Override
public CompletableFuture<WorkspaceEdit> rename(RenameParams params) {
    return CompletableFuture.supplyAsync(() -> {
        WorkspaceEdit workspaceEdit = new WorkspaceEdit();
        TextDocumentServiceContext renameContext = new TextDocumentServiceContext();
        renameContext.put(DocumentServiceKeys.FILE_URI_KEY, params.getTextDocument().getUri());
        renameContext.put(DocumentServiceKeys.POSITION_KEY, new TextDocumentPositionParams(params.getTextDocument(), params.getPosition()));
        List<Location> contents = new ArrayList<>();
        try {
            List<BLangPackage> bLangPackages = TextDocumentServiceUtil.getBLangPackage(renameContext, documentManager, false, LSCustomErrorStrategy.class, true);
            // Get the current package.
            BLangPackage currentBLangPackage = CommonUtil.getCurrentPackageByFileName(bLangPackages, params.getTextDocument().getUri());
            renameContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, currentBLangPackage.symbol.getName().getValue());
            renameContext.put(NodeContextKeys.REFERENCE_NODES_KEY, contents);
            // Run the position calculator for the current package.
            PositionTreeVisitor positionTreeVisitor = new PositionTreeVisitor(renameContext);
            currentBLangPackage.accept(positionTreeVisitor);
            String replaceableSymbolName = renameContext.get(NodeContextKeys.NAME_OF_NODE_KEY);
            // Run reference visitor and rename util for project folder.
            for (BLangPackage bLangPackage : bLangPackages) {
                renameContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
                lSPackageCache.addPackage(bLangPackage);
                contents = ReferenceUtil.getReferences(renameContext, bLangPackage);
            }
            workspaceEdit.setDocumentChanges(RenameUtil.getRenameTextEdits(contents, documentManager, params.getNewName(), replaceableSymbolName));
        } catch (Exception e) {
        // Ignore exception and will return the empty workspace edits list
        }
        return workspaceEdit;
    });
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) ArrayList(java.util.ArrayList) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) MarkedString(org.eclipse.lsp4j.MarkedString) PositionTreeVisitor(org.ballerinalang.langserver.common.position.PositionTreeVisitor) Location(org.eclipse.lsp4j.Location)

Aggregations

BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)78 Test (org.testng.annotations.Test)29 ArrayList (java.util.ArrayList)28 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)19 Page (org.ballerinalang.docgen.model.Page)18 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)15 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)15 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)14 IOException (java.io.IOException)13 List (java.util.List)13 Path (java.nio.file.Path)12 Compiler (org.wso2.ballerinalang.compiler.Compiler)12 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)12 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)11 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)10 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)10 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)9 BLangAction (org.wso2.ballerinalang.compiler.tree.BLangAction)9 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)9