Search in sources :

Example 1 with PositionTreeVisitor

use of org.ballerinalang.langserver.common.position.PositionTreeVisitor 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 2 with PositionTreeVisitor

use of org.ballerinalang.langserver.common.position.PositionTreeVisitor 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 3 with PositionTreeVisitor

use of org.ballerinalang.langserver.common.position.PositionTreeVisitor in project ballerina by ballerina-lang.

the class BallerinaTextDocumentService method definition.

@Override
public CompletableFuture<List<? extends Location>> definition(TextDocumentPositionParams position) {
    return CompletableFuture.supplyAsync(() -> {
        TextDocumentServiceContext definitionContext = new TextDocumentServiceContext();
        definitionContext.put(DocumentServiceKeys.FILE_URI_KEY, position.getTextDocument().getUri());
        definitionContext.put(DocumentServiceKeys.POSITION_KEY, position);
        BLangPackage currentBLangPackage = TextDocumentServiceUtil.getBLangPackage(definitionContext, documentManager, false, LSCustomErrorStrategy.class, false).get(0);
        definitionContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, currentBLangPackage.symbol.getName().getValue());
        lSPackageCache.addPackage(currentBLangPackage);
        List<Location> contents;
        try {
            PositionTreeVisitor positionTreeVisitor = new PositionTreeVisitor(definitionContext);
            currentBLangPackage.accept(positionTreeVisitor);
            contents = DefinitionUtil.getDefinitionPosition(definitionContext, lSPackageCache);
        } catch (Exception e) {
            contents = new ArrayList<>();
        }
        return contents;
    });
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) ArrayList(java.util.ArrayList) PositionTreeVisitor(org.ballerinalang.langserver.common.position.PositionTreeVisitor) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy) Location(org.eclipse.lsp4j.Location)

Example 4 with PositionTreeVisitor

use of org.ballerinalang.langserver.common.position.PositionTreeVisitor in project ballerina by ballerina-lang.

the class HoverUtil method getHoverContent.

/**
 * get current hover content.
 *
 * @param hoverContext        text document context for the hover provider.
 * @param currentBLangPackage package which currently user working on.
 * @return return Hover object.
 */
public static Hover getHoverContent(TextDocumentServiceContext hoverContext, BLangPackage currentBLangPackage, LSPackageCache packageContext) {
    PositionTreeVisitor positionTreeVisitor = new PositionTreeVisitor(hoverContext);
    currentBLangPackage.accept(positionTreeVisitor);
    Hover hover;
    // If the cursor is on a node of the current package go inside, else check builtin and native packages.
    if (hoverContext.get(NodeContextKeys.PACKAGE_OF_NODE_KEY) != null) {
        hover = getHoverInformation(packageContext.findPackage(hoverContext.get(DocumentServiceKeys.COMPILER_CONTEXT_KEY), hoverContext.get(NodeContextKeys.PACKAGE_OF_NODE_KEY)), hoverContext);
    } else {
        hover = new Hover();
        List<Either<String, MarkedString>> contents = new ArrayList<>();
        contents.add(Either.forLeft(""));
        hover.setContents(contents);
    }
    return hover;
}
Also used : Hover(org.eclipse.lsp4j.Hover) ArrayList(java.util.ArrayList) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) PositionTreeVisitor(org.ballerinalang.langserver.common.position.PositionTreeVisitor)

Aggregations

ArrayList (java.util.ArrayList)4 PositionTreeVisitor (org.ballerinalang.langserver.common.position.PositionTreeVisitor)4 Location (org.eclipse.lsp4j.Location)3 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)3 LSCustomErrorStrategy (org.ballerinalang.langserver.common.LSCustomErrorStrategy)1 Hover (org.eclipse.lsp4j.Hover)1 MarkedString (org.eclipse.lsp4j.MarkedString)1 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)1 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)1 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)1