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