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);
});
}
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;
}
Aggregations