Search in sources :

Example 36 with PACKAGE

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE 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 37 with PACKAGE

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE 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 38 with PACKAGE

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE 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)

Example 39 with PACKAGE

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.

the class LSPackageCache method findPackage.

/**
 * Find the package by Package ID.
 * @param compilerContext       compiler context
 * @param pkgId                 Package Id to lookup
 * @return {@link BLangPackage} BLang Package resolved
 */
public BLangPackage findPackage(CompilerContext compilerContext, PackageID pkgId) {
    if (containsPackage(pkgId.getName().getValue())) {
        return packageMap.get(pkgId.getName().getValue());
    } else {
        BLangPackage bLangPackage = LSPackageLoader.getPackageById(compilerContext, pkgId);
        addPackage(bLangPackage);
        return bLangPackage;
    }
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage)

Example 40 with PACKAGE

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.

the class PushUtils method pushPackages.

/**
 * Push/Uploads packages to the central repository.
 *
 * @param packageName   path of the package folder to be pushed
 * @param installToRepo if it should be pushed to central or home
 */
public static void pushPackages(String packageName, String installToRepo) {
    String accessToken = getAccessTokenOfCLI();
    Manifest manifest = readManifestConfigurations();
    if (manifest.getName() == null && manifest.getVersion() == null) {
        throw new BLangCompilerException("An org-name and package version is required when pushing. " + "This is not specified in Ballerina.toml inside the project");
    }
    String orgName = manifest.getName();
    String version = manifest.getVersion();
    PackageID packageID = new PackageID(new Name(orgName), new Name(packageName), new Name(version));
    Path prjDirPath = Paths.get(".").toAbsolutePath().normalize().resolve(ProjectDirConstants.DOT_BALLERINA_DIR_NAME);
    // Get package path from project directory path
    Path pkgPathFromPrjtDir = Paths.get(prjDirPath.toString(), "repo", Names.ANON_ORG.getValue(), packageName, Names.DEFAULT_VERSION.getValue(), packageName + ".zip");
    if (installToRepo == null) {
        if (accessToken == null) {
            // TODO: get bal home location dynamically
            throw new BLangCompilerException("Access token is missing in ~/ballerina_home/Settings.toml file.\n" + "Please visit https://central.ballerina.io/cli-token");
        }
        // Push package to central
        String resourcePath = resolvePkgPathInRemoteRepo(packageID);
        URI balxPath = URI.create(String.valueOf(PushUtils.class.getClassLoader().getResource("ballerina.push.balx")));
        String msg = orgName + "/" + packageName + ":" + version + " [project repo -> central]";
        ExecutorUtils.execute(balxPath, accessToken, resourcePath, pkgPathFromPrjtDir.toString(), msg);
    } else {
        if (!installToRepo.equals("home")) {
            throw new BLangCompilerException("Unknown repository provided to push the package");
        }
        Path balHomeDir = HomeRepoUtils.createAndGetHomeReposPath();
        Path targetDirectoryPath = Paths.get(balHomeDir.toString(), "repo", orgName, packageName, version, packageName + ".zip");
        if (Files.exists(targetDirectoryPath)) {
            throw new BLangCompilerException("Ballerina package exists in the home repository");
        } else {
            try {
                Files.createDirectories(targetDirectoryPath);
                Files.copy(pkgPathFromPrjtDir, targetDirectoryPath, StandardCopyOption.REPLACE_EXISTING);
                outStream.println(orgName + "/" + packageName + ":" + version + " [project repo -> home repo]");
            } catch (IOException e) {
                throw new BLangCompilerException("Error occurred when creating directories in the home repository");
            }
        }
    }
}
Also used : Path(java.nio.file.Path) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) PackageID(org.ballerinalang.model.elements.PackageID) IOException(java.io.IOException) Manifest(org.ballerinalang.toml.model.Manifest) URI(java.net.URI) Name(org.wso2.ballerinalang.compiler.util.Name)

Aggregations

BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)49 ArrayList (java.util.ArrayList)34 Test (org.testng.annotations.Test)29 Page (org.ballerinalang.docgen.model.Page)18 File (java.io.File)16 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)16 IOException (java.io.IOException)15 Path (java.nio.file.Path)13 List (java.util.List)13 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)11 PackageID (org.ballerinalang.model.elements.PackageID)10 Compiler (org.wso2.ballerinalang.compiler.Compiler)10 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)10 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)10 Collectors (java.util.stream.Collectors)9 Arrays (java.util.Arrays)8 CompilerPhase (org.ballerinalang.compiler.CompilerPhase)7 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)6 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)6 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)6