Search in sources :

Example 1 with TreeVisitor

use of org.ballerinalang.langserver.completions.TreeVisitor 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 2 with TreeVisitor

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

the class CompletionTestUtil method getCompletions.

/**
 * Get the completions list.
 *
 * @param documentManager Document manager instance
 * @param pos             {@link TextDocumentPositionParams} position params
 */
public static List<CompletionItem> getCompletions(WorkspaceDocumentManagerImpl documentManager, TextDocumentPositionParams pos) {
    List<CompletionItem> completions;
    TextDocumentServiceContext completionContext = new TextDocumentServiceContext();
    completionContext.put(DocumentServiceKeys.POSITION_KEY, pos);
    completionContext.put(DocumentServiceKeys.FILE_URI_KEY, pos.getTextDocument().getUri());
    BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(completionContext, documentManager, false, CompletionCustomErrorStrategy.class, false).get(0);
    completionContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
    // 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);
    }
    return completions;
}
Also used : TreeVisitor(org.ballerinalang.langserver.completions.TreeVisitor) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) CompletionCustomErrorStrategy(org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) CompletionItem(org.eclipse.lsp4j.CompletionItem)

Aggregations

CompletionCustomErrorStrategy (org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy)2 TreeVisitor (org.ballerinalang.langserver.completions.TreeVisitor)2 CompletionItem (org.eclipse.lsp4j.CompletionItem)2 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)2 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)2 ArrayList (java.util.ArrayList)1 TextDocumentServiceContext (org.ballerinalang.langserver.TextDocumentServiceContext)1 PositionTreeVisitor (org.ballerinalang.langserver.common.position.PositionTreeVisitor)1 SignatureTreeVisitor (org.ballerinalang.langserver.signature.SignatureTreeVisitor)1