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