use of org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit in project ballerina by ballerina-lang.
the class TextDocumentFormatUtil method getAST.
/**
* Get the AST for the current text document's content.
*
* @param params Document Formatting parameters
* @param documentManager Workspace document manager instance
* @param context Document formatting context
* @return {@link JsonObject} AST as a Json Object
*/
public static JsonObject getAST(DocumentFormattingParams params, WorkspaceDocumentManager documentManager, TextDocumentServiceContext context) {
String documentUri = params.getTextDocument().getUri();
String[] uriParts = documentUri.split(Pattern.quote(File.separator));
String fileName = uriParts[uriParts.length - 1];
final BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(context, documentManager, true, LSCustomErrorStrategy.class, false).get(0);
context.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
final List<Diagnostic> diagnostics = new ArrayList<>();
JsonArray errors = new JsonArray();
JsonObject result = new JsonObject();
result.add("errors", errors);
Gson gson = new Gson();
JsonElement diagnosticsJson = gson.toJsonTree(diagnostics);
result.add("diagnostics", diagnosticsJson);
BLangCompilationUnit compilationUnit = bLangPackage.getCompilationUnits().stream().filter(compUnit -> fileName.equals(compUnit.getName())).findFirst().orElseGet(null);
JsonElement modelElement = CommonUtil.generateJSON(compilationUnit, new HashMap<>());
result.add("model", modelElement);
return result;
}
use of org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit in project ballerina by ballerina-lang.
the class SwaggerConverterUtils method getAlias.
/**
* Gets the alias for a given package from a bLang file root node.
* @param topCompilationUnit The root node.
* @param packageName The package name.
* @return The alias.
*/
private static String getAlias(BLangCompilationUnit topCompilationUnit, String packageName) {
for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) {
if (topLevelNode instanceof BLangImportPackage) {
BLangImportPackage importPackage = (BLangImportPackage) topLevelNode;
String packagePath = importPackage.getPackageName().stream().map(BLangIdentifier::getValue).collect(Collectors.joining("."));
if (packageName.equals(packagePath)) {
return importPackage.getAlias().getValue();
}
}
}
return null;
}
use of org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit in project ballerina by ballerina-lang.
the class SignatureTreeVisitor method visit.
@Override
public void visit(BLangPackage pkgNode) {
SymbolEnv pkgEnv = symTable.pkgEnvMap.get(pkgNode.symbol);
// Then visit each top-level element sorted using the compilation unit
String fileName = documentServiceContext.get(DocumentServiceKeys.FILE_NAME_KEY);
BLangCompilationUnit compilationUnit = pkgNode.getCompilationUnits().stream().filter(bLangCompilationUnit -> bLangCompilationUnit.getName().equals(fileName)).findFirst().orElse(null);
List<TopLevelNode> topLevelNodes = compilationUnit.getTopLevelNodes();
if (!topLevelNodes.isEmpty()) {
topLevelNodes.forEach(topLevelNode -> acceptNode((BLangNode) topLevelNode, pkgEnv));
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit in project ballerina by ballerina-lang.
the class BallerinaTextDocumentService method documentSymbol.
@Override
public CompletableFuture<List<? extends SymbolInformation>> documentSymbol(DocumentSymbolParams params) {
String uri = params.getTextDocument().getUri();
List<SymbolInformation> symbols = new ArrayList<>();
TextDocumentServiceContext symbolsContext = new TextDocumentServiceContext();
symbolsContext.put(DocumentServiceKeys.FILE_URI_KEY, uri);
symbolsContext.put(DocumentServiceKeys.SYMBOL_LIST_KEY, symbols);
BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(symbolsContext, documentManager, false, LSCustomErrorStrategy.class, false).get(0);
symbolsContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
Optional<BLangCompilationUnit> documentCUnit = bLangPackage.getCompilationUnits().stream().filter(cUnit -> (uri.endsWith(cUnit.getName()))).findFirst();
documentCUnit.ifPresent(cUnit -> {
SymbolFindingVisitor visitor = new SymbolFindingVisitor(symbolsContext);
cUnit.accept(visitor);
});
return CompletableFuture.supplyAsync(() -> symbols);
}
use of org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit in project ballerina by ballerina-lang.
the class CommonUtil method getCurrentPackageByFileName.
/**
* Get current package by given file name.
*
* @param packages list of packages to be searched
* @param fileUri string file URI
* @return {@link BLangPackage} current package
*/
public static BLangPackage getCurrentPackageByFileName(List<BLangPackage> packages, String fileUri) {
LSDocument document = new LSDocument(fileUri);
Path filePath = getPath(document);
Path fileNamePath = filePath.getFileName();
BLangPackage currentPackage = null;
try {
found: for (BLangPackage bLangPackage : packages) {
for (BLangCompilationUnit compilationUnit : bLangPackage.getCompilationUnits()) {
if (compilationUnit.name.equals(fileNamePath.getFileName().toString())) {
currentPackage = bLangPackage;
break found;
}
}
}
} catch (NullPointerException e) {
currentPackage = packages.get(0);
}
return currentPackage;
}
Aggregations