Search in sources :

Example 11 with BLangImportPackage

use of org.wso2.ballerinalang.compiler.tree.BLangImportPackage in project ballerina by ballerina-lang.

the class PackageLoader method addImportPkg.

// Private methods
private void addImportPkg(BLangPackage bLangPackage, String orgName, String sourcePkgName, String version) {
    List<Name> nameComps = getPackageNameComps(sourcePkgName);
    List<BLangIdentifier> pkgNameComps = new ArrayList<>();
    nameComps.forEach(comp -> {
        IdentifierNode node = TreeBuilder.createIdentifierNode();
        node.setValue(comp.value);
        pkgNameComps.add((BLangIdentifier) node);
    });
    BLangIdentifier orgNameNode = (BLangIdentifier) TreeBuilder.createIdentifierNode();
    orgNameNode.setValue(orgName);
    BLangIdentifier versionNode = (BLangIdentifier) TreeBuilder.createIdentifierNode();
    versionNode.setValue(version);
    BLangImportPackage importDcl = (BLangImportPackage) TreeBuilder.createImportPackageNode();
    importDcl.pos = bLangPackage.pos;
    importDcl.pkgNameComps = pkgNameComps;
    importDcl.orgName = orgNameNode;
    importDcl.version = versionNode;
    BLangIdentifier alias = (BLangIdentifier) TreeBuilder.createIdentifierNode();
    alias.setValue(names.merge(Names.DOT, nameComps.get(nameComps.size() - 1)).value);
    importDcl.alias = alias;
    bLangPackage.imports.add(importDcl);
}
Also used : IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) ArrayList(java.util.ArrayList) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 12 with BLangImportPackage

use of org.wso2.ballerinalang.compiler.tree.BLangImportPackage in project ballerina by ballerina-lang.

the class TreeVisitor method visit.

public void visit(BLangImportPackage importPkgNode) {
    BPackageSymbol pkgSymbol = importPkgNode.symbol;
    SymbolEnv pkgEnv = symTable.pkgEnvMap.get(pkgSymbol);
    acceptNode(pkgEnv.node, pkgEnv);
}
Also used : BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 13 with BLangImportPackage

use of org.wso2.ballerinalang.compiler.tree.BLangImportPackage 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 14 with BLangImportPackage

use of org.wso2.ballerinalang.compiler.tree.BLangImportPackage in project ballerina by ballerina-lang.

the class CommandExecutor method executeImportPackage.

/**
 * Execute the command, import package.
 * @param context   Workspace service context
 */
private static void executeImportPackage(WorkspaceServiceContext context) {
    String documentUri = null;
    VersionedTextDocumentIdentifier textDocumentIdentifier = new VersionedTextDocumentIdentifier();
    for (Object arg : context.get(ExecuteCommandKeys.COMMAND_ARGUMENTS_KEY)) {
        if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_DOC_URI)) {
            documentUri = (String) ((LinkedTreeMap) arg).get(ARG_VALUE);
            textDocumentIdentifier.setUri(documentUri);
            context.put(DocumentServiceKeys.FILE_URI_KEY, documentUri);
        } else if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_PKG_NAME)) {
            context.put(ExecuteCommandKeys.PKG_NAME_KEY, (String) ((LinkedTreeMap) arg).get(ARG_VALUE));
        }
    }
    if (documentUri != null && context.get(ExecuteCommandKeys.PKG_NAME_KEY) != null) {
        String fileContent = context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY).getFileContent(Paths.get(URI.create(documentUri)));
        String[] contentComponents = fileContent.split("\\n|\\r\\n|\\r");
        int totalLines = contentComponents.length;
        int lastNewLineCharIndex = Math.max(fileContent.lastIndexOf("\n"), fileContent.lastIndexOf("\r"));
        int lastCharCol = fileContent.substring(lastNewLineCharIndex + 1).length();
        BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(context, context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY), false, LSCustomErrorStrategy.class, false).get(0);
        context.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
        String pkgName = context.get(ExecuteCommandKeys.PKG_NAME_KEY);
        DiagnosticPos pos;
        // Filter the imports except the runtime import
        List<BLangImportPackage> imports = bLangPackage.getImports().stream().filter(bLangImportPackage -> !bLangImportPackage.getAlias().toString().equals(RUNTIME_PKG_ALIAS)).collect(Collectors.toList());
        if (!imports.isEmpty()) {
            BLangImportPackage lastImport = bLangPackage.getImports().get(bLangPackage.getImports().size() - 1);
            pos = lastImport.getPosition();
        } else if (imports.isEmpty() && bLangPackage.getPackageDeclaration() != null) {
            pos = (DiagnosticPos) bLangPackage.getPackageDeclaration().getPosition();
        } else {
            pos = null;
        }
        int endCol = pos == null ? -1 : pos.getEndColumn() - 1;
        int endLine = pos == null ? 0 : pos.getEndLine() - 1;
        String remainingTextToReplace;
        if (endCol != -1) {
            int contentLengthToReplaceStart = fileContent.substring(0, fileContent.indexOf(contentComponents[endLine])).length() + endCol + 1;
            remainingTextToReplace = fileContent.substring(contentLengthToReplaceStart);
        } else {
            remainingTextToReplace = fileContent;
        }
        String editText = (pos != null ? "\r\n" : "") + "import " + pkgName + ";" + (remainingTextToReplace.startsWith("\n") || remainingTextToReplace.startsWith("\r") ? "" : "\r\n") + remainingTextToReplace;
        Range range = new Range(new Position(endLine, endCol + 1), new Position(totalLines + 1, lastCharCol));
        applySingleTextEdit(editText, range, textDocumentIdentifier, context.get(ExecuteCommandKeys.LANGUAGE_SERVER_KEY).getClient());
    }
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) Arrays(java.util.Arrays) ApplyWorkspaceEditParams(org.eclipse.lsp4j.ApplyWorkspaceEditParams) WorkspaceServiceContext(org.ballerinalang.langserver.WorkspaceServiceContext) CommandConstants(org.ballerinalang.langserver.common.constants.CommandConstants) Range(org.eclipse.lsp4j.Range) ArrayList(java.util.ArrayList) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) TextEdit(org.eclipse.lsp4j.TextEdit) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) Position(org.eclipse.lsp4j.Position) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) URI(java.net.URI) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Collectors(java.util.stream.Collectors) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) TextDocumentServiceUtil(org.ballerinalang.langserver.TextDocumentServiceUtil) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy) List(java.util.List) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Paths(java.nio.file.Paths) Node(org.ballerinalang.model.tree.Node) UtilSymbolKeys(org.ballerinalang.langserver.common.UtilSymbolKeys) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) Collections(java.util.Collections) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) Position(org.eclipse.lsp4j.Position) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) Range(org.eclipse.lsp4j.Range) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy)

Aggregations

BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)9 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)8 BLangImportPackage (org.wso2.ballerinalang.compiler.tree.BLangImportPackage)5 BLangIdentifier (org.wso2.ballerinalang.compiler.tree.BLangIdentifier)4 ArrayList (java.util.ArrayList)3 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 IdentifierNode (org.ballerinalang.model.tree.IdentifierNode)2 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)2 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)2 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)2 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)2 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)2 BLangResource (org.wso2.ballerinalang.compiler.tree.BLangResource)2 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)2 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)2 BLangTransformer (org.wso2.ballerinalang.compiler.tree.BLangTransformer)2 Name (org.wso2.ballerinalang.compiler.util.Name)2 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)2 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)1