Search in sources :

Example 6 with BLangCompilationUnit

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;
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) JsonArray(com.google.gson.JsonArray) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) JsonElement(com.google.gson.JsonElement) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)

Example 7 with BLangCompilationUnit

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;
}
Also used : BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 8 with BLangCompilationUnit

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));
    }
}
Also used : BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 9 with BLangCompilationUnit

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);
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) JsonObject(com.google.gson.JsonObject) RenameUtil(org.ballerinalang.langserver.rename.RenameUtil) HoverUtil(org.ballerinalang.langserver.hover.util.HoverUtil) DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) NodeContextKeys(org.ballerinalang.langserver.common.constants.NodeContextKeys) DidSaveTextDocumentParams(org.eclipse.lsp4j.DidSaveTextDocumentParams) TreeVisitor(org.ballerinalang.langserver.completions.TreeVisitor) LSDocument(org.ballerinalang.langserver.common.LSDocument) PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams) CodeLens(org.eclipse.lsp4j.CodeLens) Map(java.util.Map) Location(org.eclipse.lsp4j.Location) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) URI(java.net.URI) DidCloseTextDocumentParams(org.eclipse.lsp4j.DidCloseTextDocumentParams) RenameParams(org.eclipse.lsp4j.RenameParams) Path(java.nio.file.Path) SignatureTreeVisitor(org.ballerinalang.langserver.signature.SignatureTreeVisitor) ReferenceUtil(org.ballerinalang.langserver.references.util.ReferenceUtil) TextDocumentService(org.eclipse.lsp4j.services.TextDocumentService) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener) MarkedString(org.eclipse.lsp4j.MarkedString) DocumentOnTypeFormattingParams(org.eclipse.lsp4j.DocumentOnTypeFormattingParams) CompletionItem(org.eclipse.lsp4j.CompletionItem) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) List(java.util.List) Command(org.eclipse.lsp4j.Command) DidOpenTextDocumentParams(org.eclipse.lsp4j.DidOpenTextDocumentParams) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) Optional(java.util.Optional) Debouncer(org.ballerinalang.langserver.util.Debouncer) WorkspaceDocumentManager(org.ballerinalang.langserver.workspace.WorkspaceDocumentManager) DocumentSymbolParams(org.eclipse.lsp4j.DocumentSymbolParams) WorkspaceDocumentManagerImpl(org.ballerinalang.langserver.workspace.WorkspaceDocumentManagerImpl) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) DocumentRangeFormattingParams(org.eclipse.lsp4j.DocumentRangeFormattingParams) Hover(org.eclipse.lsp4j.Hover) Compiler(org.wso2.ballerinalang.compiler.Compiler) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) ArrayList(java.util.ArrayList) CommandUtil(org.ballerinalang.langserver.command.CommandUtil) SignatureHelpUtil(org.ballerinalang.langserver.signature.SignatureHelpUtil) PositionTreeVisitor(org.ballerinalang.langserver.common.position.PositionTreeVisitor) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) TextEdit(org.eclipse.lsp4j.TextEdit) PackageRepository(org.ballerinalang.repository.PackageRepository) DocumentFormattingParams(org.eclipse.lsp4j.DocumentFormattingParams) Position(org.eclipse.lsp4j.Position) CompletionKeys(org.ballerinalang.langserver.completions.CompletionKeys) CodeLensParams(org.eclipse.lsp4j.CodeLensParams) CompletionList(org.eclipse.lsp4j.CompletionList) SymbolFindingVisitor(org.ballerinalang.langserver.symbols.SymbolFindingVisitor) CompletionItemResolver(org.ballerinalang.langserver.completions.util.CompletionItemResolver) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) DocumentHighlight(org.eclipse.lsp4j.DocumentHighlight) DefinitionUtil(org.ballerinalang.langserver.definition.util.DefinitionUtil) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Paths(java.nio.file.Paths) CompletionCustomErrorStrategy(org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy) Collections(java.util.Collections) TopLevelResolver(org.ballerinalang.langserver.completions.resolvers.TopLevelResolver) TextDocumentFormatUtil(org.ballerinalang.langserver.format.TextDocumentFormatUtil) ReferenceParams(org.eclipse.lsp4j.ReferenceParams) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) SymbolFindingVisitor(org.ballerinalang.langserver.symbols.SymbolFindingVisitor) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) ArrayList(java.util.ArrayList) MarkedString(org.eclipse.lsp4j.MarkedString) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)

Example 10 with BLangCompilationUnit

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;
}
Also used : Path(java.nio.file.Path) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) LSDocument(org.ballerinalang.langserver.common.LSDocument) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)

Aggregations

BLangCompilationUnit (org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)9 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)5 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)4 JsonObject (com.google.gson.JsonObject)3 Swagger (io.swagger.models.Swagger)3 Path (java.nio.file.Path)3 HashMap (java.util.HashMap)3 BFile (org.ballerinalang.composer.service.ballerina.parser.service.model.BFile)3 ServiceNode (org.ballerinalang.model.tree.ServiceNode)3 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)3 Gson (com.google.gson.Gson)2 JsonArray (com.google.gson.JsonArray)2 JsonElement (com.google.gson.JsonElement)2 SwaggerConverter (io.swagger.v3.parser.converter.SwaggerConverter)2 IOException (java.io.IOException)2 Paths (java.nio.file.Paths)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Optional (java.util.Optional)2 BallerinaFile (org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile)2