Example usage for com.google.gson JsonObject getAsJsonArray

List of usage examples for com.google.gson JsonObject getAsJsonArray

Introduction

In this page you can find the example usage for com.google.gson JsonObject getAsJsonArray.

Prototype

public JsonArray getAsJsonArray(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonArray.

Usage

From source file:org.ballerinalang.langserver.extensions.ballerina.document.BallerinaDocumentServiceImpl.java

License:Open Source License

/**
 * Util method to merge updated compilation unit to the current compilation unit.
 *
 * @param targetCompUnit    target compilation unit
 * @param generatedCompUnit generated compilation unit which needs to be merged
 *//*from  w ww. j a  va 2  s .c  om*/
private void mergeAst(JsonObject targetCompUnit, JsonObject generatedCompUnit) {
    generatedCompUnit.getAsJsonArray("topLevelNodes").forEach(item -> {
        JsonObject topLevelNode = item.getAsJsonObject();
        if (topLevelNode.get("kind").getAsString().equals("Import")) {
            if (!hasImport(targetCompUnit, topLevelNode)) {
                int startPosition = FormattingSourceGen.getStartPosition(targetCompUnit, "imports", -1);
                FormattingSourceGen.reconcileWS(topLevelNode, targetCompUnit.getAsJsonArray("topLevelNodes"),
                        targetCompUnit, startPosition);
                targetCompUnit.getAsJsonArray("topLevelNodes").add(topLevelNode);
            }
        }

        if (topLevelNode.get("kind").getAsString().equals("Service")) {
            for (JsonElement astNode : targetCompUnit.getAsJsonArray("topLevelNodes")) {
                JsonObject targetNode = astNode.getAsJsonObject();
                if (targetNode.get("kind").getAsString().equals("Service")) {
                    if (targetNode.get("name").getAsJsonObject().get("value")
                            .equals(topLevelNode.get("name").getAsJsonObject().get("value"))) {
                        mergeServices(targetNode, topLevelNode, targetCompUnit);
                    }
                }
            }
        }
    });
}

From source file:org.ballerinalang.langserver.extensions.ballerina.document.BallerinaDocumentServiceImpl.java

License:Open Source License

/**
 * Util method to merge given two service nodes.
 *
 * @param originService Origin service// ww  w. j a v a  2s  .  c  o  m
 * @param targetService Target service which will get merged to origin service
 */
private void mergeServices(JsonObject originService, JsonObject targetService, JsonObject tree) {
    mergeAnnotations(originService, targetService, tree);
    List<JsonObject> targetServices = new ArrayList<>();

    for (JsonElement targetItem : targetService.getAsJsonArray("resources")) {
        JsonObject targetResource = targetItem.getAsJsonObject();
        boolean matched = false;
        for (JsonElement originItem : originService.getAsJsonArray("resources")) {
            JsonObject originResource = originItem.getAsJsonObject();
            if (matchResource(originResource, targetResource)) {
                matched = true;
                mergeAnnotations(originResource, targetResource, tree);
            }
        }

        if (!matched) {
            targetResource.getAsJsonObject("body").add("statements", new JsonArray());
            targetServices.add(targetResource);
        }
    }

    targetServices.forEach(resource -> {
        int startIndex = FormattingSourceGen.getStartPosition(originService, "resources", -1);
        FormattingSourceGen.reconcileWS(resource, originService.getAsJsonArray("resources"), tree, startIndex);
        originService.getAsJsonArray("resources").add(resource);
    });
}

From source file:org.ballerinalang.langserver.extensions.ballerina.document.BallerinaDocumentServiceImpl.java

License:Open Source License

/**
 * Util method to merge annotation attachments.
 *
 * @param targetNode target node//from www .j a v a 2 s . c om
 * @param sourceNode source node which will get merged to target node
 */
private void mergeAnnotations(JsonObject targetNode, JsonObject sourceNode, JsonObject tree) {
    JsonArray annotationAttachments = sourceNode.has("annotationAttachments")
            ? sourceNode.getAsJsonArray("annotationAttachments")
            : sourceNode.getAsJsonArray("annAttachments");
    for (JsonElement item : annotationAttachments) {
        JsonObject sourceNodeAttachment = item.getAsJsonObject();

        JsonObject matchedTargetNode = findAttachmentNode(targetNode, sourceNodeAttachment);

        if (matchedTargetNode != null) {
            if (sourceNodeAttachment.getAsJsonObject("expression").get("kind").getAsString()
                    .equals("RecordLiteralExpr")
                    && matchedTargetNode.getAsJsonObject("expression").get("kind").getAsString()
                            .equals("RecordLiteralExpr")) {

                JsonObject sourceRecord = sourceNodeAttachment.getAsJsonObject("expression");
                JsonObject matchedTargetRecord = matchedTargetNode.getAsJsonObject("expression");

                if (sourceNodeAttachment.getAsJsonObject("annotationName").get("value").getAsString()
                        .equals("MultiResourceInfo")) {
                    JsonArray sourceResourceInformations = sourceRecord.getAsJsonArray("keyValuePairs").get(0)
                            .getAsJsonObject().getAsJsonObject("value").getAsJsonArray("keyValuePairs");
                    JsonArray targetResourceInformations = matchedTargetRecord.getAsJsonArray("keyValuePairs")
                            .get(0).getAsJsonObject().getAsJsonObject("value").getAsJsonArray("keyValuePairs");

                    // Get map values of the resourceInformation map in MultiResourceInfo annotation.
                    for (JsonElement sourceResourceInfoItem : sourceResourceInformations) {
                        JsonObject sourceResourceInfo = sourceResourceInfoItem.getAsJsonObject();
                        JsonObject matchedTargetResourceInfo = null;
                        for (JsonElement targetResourceInfoItem : targetResourceInformations) {
                            JsonObject targetResourceInfo = targetResourceInfoItem.getAsJsonObject();
                            if (targetResourceInfo.has("key") && targetResourceInfo.getAsJsonObject("key")
                                    .get("kind").getAsString().equals("Literal")) {
                                JsonObject targetResourceInfoKey = targetResourceInfo.getAsJsonObject("key");
                                JsonObject sourceResourceInfoKey = sourceResourceInfo.getAsJsonObject("key");

                                if (sourceResourceInfoKey.get("value").getAsString()
                                        .equals(targetResourceInfoKey.get("value").getAsString())) {
                                    matchedTargetResourceInfo = targetResourceInfo;
                                }
                            }
                        }

                        if (matchedTargetResourceInfo != null) {
                            JsonArray sourceResourceInfoOperation = sourceResourceInfo.getAsJsonObject("value")
                                    .getAsJsonArray("keyValuePairs");
                            JsonArray targetResourceInfoOperation = matchedTargetResourceInfo
                                    .getAsJsonObject("value").getAsJsonArray("keyValuePairs");

                            for (JsonElement keyValueItem : sourceResourceInfoOperation) {
                                JsonObject sourceKeyValue = keyValueItem.getAsJsonObject();
                                int matchedKeyValuePairIndex = 0;
                                JsonObject matchedObj = null;
                                for (JsonElement matchedKeyValueItem : targetResourceInfoOperation) {
                                    JsonObject matchedKeyValue = matchedKeyValueItem.getAsJsonObject();
                                    if ((matchedKeyValue.has("key") && matchedKeyValue.getAsJsonObject("key")
                                            .get("kind").getAsString().equals("SimpleVariableRef"))) {
                                        JsonObject matchedKey = matchedKeyValue.getAsJsonObject("key");
                                        JsonObject sourceKey = sourceKeyValue.getAsJsonObject("key");
                                        if (matchedKey.getAsJsonObject("variableName").get("value")
                                                .getAsString().equals(sourceKey.getAsJsonObject("variableName")
                                                        .get("value").getAsString())) {
                                            matchedObj = matchedKeyValue;
                                            break;
                                        }
                                    }
                                    matchedKeyValuePairIndex++;
                                }

                                if (matchedObj != null) {
                                    List<JsonObject> matchedObjWS = FormattingSourceGen.extractWS(matchedObj);
                                    int firstTokenIndex = matchedObjWS.get(0).get("i").getAsInt();
                                    targetResourceInfoOperation.remove(matchedKeyValuePairIndex);
                                    FormattingSourceGen.reconcileWS(sourceKeyValue, targetResourceInfoOperation,
                                            tree, firstTokenIndex);
                                    targetResourceInfoOperation.add(sourceKeyValue);
                                } else {
                                    // Add new key value pair to the annotation record.
                                    FormattingSourceGen.reconcileWS(sourceKeyValue, targetResourceInfoOperation,
                                            tree, -1);
                                    targetResourceInfoOperation.add(sourceKeyValue);

                                    if (targetResourceInfoOperation.size() > 1) {
                                        // Add a new comma to separate the new key value pair.
                                        int startIndex = FormattingSourceGen.extractWS(sourceKeyValue).get(0)
                                                .getAsJsonObject().get("i").getAsInt();
                                        FormattingSourceGen.addNewWS(
                                                matchedTargetResourceInfo.getAsJsonObject("value"), tree, "",
                                                ",", true, startIndex);
                                    }
                                }
                            }

                        } else {
                            FormattingSourceGen.reconcileWS(sourceResourceInfo, targetResourceInformations,
                                    tree, -1);
                            targetResourceInformations.add(sourceResourceInfo);
                        }
                    }

                } else {
                    for (JsonElement keyValueItem : sourceRecord.getAsJsonArray("keyValuePairs")) {
                        JsonObject sourceKeyValue = keyValueItem.getAsJsonObject();
                        int matchedKeyValuePairIndex = 0;
                        JsonObject matchedObj = null;

                        for (JsonElement matchedKeyValueItem : matchedTargetRecord
                                .getAsJsonArray("keyValuePairs")) {
                            JsonObject matchedKeyValue = matchedKeyValueItem.getAsJsonObject();
                            if ((matchedKeyValue.has("key") && matchedKeyValue.getAsJsonObject("key")
                                    .get("kind").getAsString().equals("SimpleVariableRef"))) {
                                JsonObject matchedKey = matchedKeyValue.getAsJsonObject("key");
                                JsonObject sourceKey = sourceKeyValue.getAsJsonObject("key");
                                if (matchedKey.getAsJsonObject("variableName").get("value").getAsString()
                                        .equals(sourceKey.getAsJsonObject("variableName").get("value")
                                                .getAsString())) {
                                    matchedObj = matchedKeyValue;
                                    break;
                                }
                            }
                            matchedKeyValuePairIndex++;
                        }

                        if (matchedObj != null) {
                            List<JsonObject> matchedObjWS = FormattingSourceGen.extractWS(matchedObj);
                            int firstTokenIndex = matchedObjWS.get(0).get("i").getAsInt();
                            matchedTargetRecord.getAsJsonArray("keyValuePairs")
                                    .remove(matchedKeyValuePairIndex);
                            FormattingSourceGen.reconcileWS(sourceKeyValue,
                                    matchedTargetRecord.getAsJsonArray("keyValuePairs"), tree, firstTokenIndex);
                            matchedTargetRecord.getAsJsonArray("keyValuePairs").add(sourceKeyValue);
                        } else {
                            // Add the new record key value pair.
                            FormattingSourceGen.reconcileWS(sourceKeyValue,
                                    matchedTargetRecord.getAsJsonArray("keyValuePairs"), tree, -1);
                            matchedTargetRecord.getAsJsonArray("keyValuePairs").add(sourceKeyValue);

                            if (matchedTargetRecord.getAsJsonArray("keyValuePairs").size() > 1) {
                                // Add a new comma to separate the new key value pair.
                                int startIndex = FormattingSourceGen.extractWS(sourceKeyValue).get(0)
                                        .getAsJsonObject().get("i").getAsInt();
                                FormattingSourceGen.addNewWS(matchedTargetRecord, tree, "", ",", true,
                                        startIndex);
                            }
                        }
                    }
                }
            }
        } else {
            int startIndex = FormattingSourceGen.getStartPosition(targetNode, "annAttachments", -1);
            JsonArray targetAnnAttachments = targetNode.has("annotationAttachments")
                    ? targetNode.getAsJsonArray("annotationAttachments")
                    : targetNode.getAsJsonArray("annAttachments");
            FormattingSourceGen.reconcileWS(sourceNodeAttachment, targetAnnAttachments, tree, startIndex);
            targetAnnAttachments.add(sourceNodeAttachment);
        }

    }
}

From source file:org.ballerinalang.langserver.extensions.ballerina.document.BallerinaDocumentServiceImpl.java

License:Open Source License

private JsonObject findAttachmentNode(JsonObject targetNode, JsonObject sourceNodeAttachment) {
    JsonObject matchedNode = null;/*  w  w w  . ja v a  2 s .  c  om*/
    JsonArray annotationAttachments = targetNode.has("annotationAttachments")
            ? targetNode.getAsJsonArray("annotationAttachments")
            : targetNode.getAsJsonArray("annAttachments");
    for (JsonElement item : annotationAttachments) {
        JsonObject attachmentNode = item.getAsJsonObject();
        if (sourceNodeAttachment.getAsJsonObject("annotationName").get("value").getAsString()
                .equals(attachmentNode.getAsJsonObject("annotationName").get("value").getAsString())
                && sourceNodeAttachment.getAsJsonObject("packageAlias").get("value").getAsString()
                        .equals(attachmentNode.getAsJsonObject("packageAlias").get("value").getAsString())) {
            matchedNode = attachmentNode;
            break;
        }
    }
    return matchedNode;
}

From source file:org.ballerinalang.langserver.extensions.ballerina.document.BallerinaDocumentServiceImpl.java

License:Open Source License

/**
 * Util method to check if given node is an existing import in current AST model.
 *
 * @param originAst    - current AST model
 * @param mergePackage - Import Node/*  w ww.  j av  a  2  s .  com*/
 * @return - boolean status
 */
private boolean hasImport(JsonObject originAst, JsonObject mergePackage) {
    boolean importFound = false;

    for (JsonElement node : originAst.getAsJsonArray("topLevelNodes")) {
        JsonObject originNode = node.getAsJsonObject();
        if (importFound) {
            break;
        } else if (originNode.get("kind").getAsString().equals("Import")
                && originNode.get("orgName").getAsJsonObject().get("value").getAsString()
                        .equals(mergePackage.get("orgName").getAsJsonObject().get("value").getAsString())
                && originNode.getAsJsonArray("packageName").size() == mergePackage.getAsJsonArray("packageName")
                        .size()) {
            JsonArray packageName = originNode.getAsJsonArray("packageName");
            for (int i = 0; i < packageName.size(); i++) {
                JsonArray mergePackageName = mergePackage.getAsJsonArray("packageName");
                if (mergePackageName.get(i).getAsJsonObject().get("value").getAsString()
                        .equals(packageName.get(i).getAsJsonObject().get("value").getAsString())) {
                    importFound = true;
                } else {
                    importFound = false;
                    break;
                }
            }
        }
    }

    return importFound;
}

From source file:org.ballerinalang.langserver.extensions.ballerina.fragment.BallerinaFragmentServiceImpl.java

License:Open Source License

private static JsonObject parseFragment(BallerinaFragmentASTRequest sourceFragment) {
    try {//from ww w .  ja va2 s .  c o  m
        String parsableString = getParsableString(sourceFragment);
        JsonElement jsonElement = getJsonModel(parsableString);
        if (jsonElement instanceof JsonObject) {
            JsonObject jsonModel = (JsonObject) jsonElement;
            if (jsonModel.getAsJsonArray(SYNTAX_ERRORS) != null) {
                return jsonModel;
            }
            JsonObject jsonASTFragment = getJsonNodeForFragment(jsonModel, sourceFragment);
            return FormattingSourceGen.build(jsonASTFragment, null);
        }
    } catch (JSONGenerationException | LSCompilerException e) {
        logger.error("Error while generating AST for fragment", e);
    }
    return null;
}

From source file:org.ballerinalang.langserver.extensions.ballerina.fragment.BallerinaFragmentServiceImpl.java

License:Open Source License

private static JsonObject getJsonNodeForFragment(JsonObject jsonModel, BallerinaFragmentASTRequest fragment) {
    JsonObject fragmentNode;//from   w w w  .ja v  a 2s. co  m
    JsonArray jsonArray = jsonModel.getAsJsonArray(JSONModelConstants.TOP_LEVEL_NODES);
    JsonObject rootConstruct = jsonArray.get(0).getAsJsonObject(); // 0 is package def
    switch (fragment.getExpectedNodeType()) {
    case BLangFragmentParserConstants.TOP_LEVEL_NODE:
        fragmentNode = rootConstruct;
        break;
    case BLangFragmentParserConstants.SERVICE_RESOURCE:
        fragmentNode = rootConstruct.getAsJsonArray(JSONModelConstants.RESOURCES).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.CONNECTOR_ACTION:
        fragmentNode = rootConstruct.getAsJsonArray(JSONModelConstants.ACTIONS).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.WORKER:
        fragmentNode = rootConstruct.getAsJsonArray(JSONModelConstants.WORKERS).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.VARIABLE_REFERENCE_LIST:
        // 0 & 1 are function args and return types, 2 is the assignment statement
        JsonObject assignmentStmt = rootConstruct.getAsJsonArray(JSONModelConstants.CHILDREN).get(2)
                .getAsJsonObject();
        // 0th child is the var ref list expression of assignment stmt
        fragmentNode = assignmentStmt.getAsJsonArray(JSONModelConstants.CHILDREN).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.FIELD_DEFINITION_LIST:
        // 0th element in the fields property of the record is fieldVariable.
        fragmentNode = rootConstruct.getAsJsonObject(JSONModelConstants.TYPE_NODE)
                .getAsJsonArray(JSONModelConstants.FIELDS).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.ANON_RECORD:
        fragmentNode = jsonModel;
        break;
    case BLangFragmentParserConstants.TRANSACTION_FAILED:
    case BLangFragmentParserConstants.EXPRESSION:
    case BLangFragmentParserConstants.STATEMENT:
        // For Expression - 0th child is the var ref expression of var def stmt
        // For Statement - 0 & 1 are function args and return types, 2 is the statement came from source
        // fragment
        fragmentNode = rootConstruct.getAsJsonObject(JSONModelConstants.BODY)
                .getAsJsonArray(JSONModelConstants.STATEMENTS).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.ENDPOINT_VAR_DEF:
        fragmentNode = rootConstruct.getAsJsonArray(JSONModelConstants.ENDPOINT_NODES).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.JOIN_CONDITION:
        JsonObject bodyJsonObj = rootConstruct.getAsJsonObject(JSONModelConstants.BODY);
        fragmentNode = bodyJsonObj.getAsJsonArray(JSONModelConstants.STATEMENTS).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.ARGUMENT_PARAMETER:
        fragmentNode = rootConstruct.getAsJsonArray(JSONModelConstants.PARAMETERS).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.RETURN_PARAMETER:
        fragmentNode = rootConstruct.getAsJsonArray(JSONModelConstants.RETURN_PARAMETERS).get(0)
                .getAsJsonObject();
        break;
    default:
        fragmentNode = new JsonObject();
        fragmentNode.addProperty(ERROR, "cannot find node for given fragment");
    }
    return fragmentNode;
}

From source file:org.ballerinalang.langserver.formatting.FormattingNodeTree.java

License:Open Source License

/**
 * format annotation node./*from ww w.j a v a 2  s. c  o m*/
 *
 * @param node {JsonObject} node as json object
 */
public void formatAnnotationNode(JsonObject node) {
    if (node.has(FormattingConstants.WS) && node.has(FormattingConstants.FORMATTING_CONFIG)) {
        JsonArray ws = node.getAsJsonArray(FormattingConstants.WS);
        JsonObject formatConfig = node.getAsJsonObject(FormattingConstants.FORMATTING_CONFIG);
        boolean isPublic = node.has(FormattingConstants.PUBLIC)
                && node.get(FormattingConstants.PUBLIC).getAsBoolean();
        String indentation = this.getIndentation(formatConfig, false);

        this.preserveHeight(ws, indentation);

        int attachPointCounter = 0;
        for (int i = 0; i < ws.size(); i++) {
            JsonObject annotationWS = ws.get(i).getAsJsonObject();
            String text = annotationWS.get(FormattingConstants.TEXT).getAsString();
            if (this.noHeightAvailable(annotationWS.get(FormattingConstants.WS).getAsString())) {
                if (i == 0) {
                    // If annotation or documentation attachments exists add only one new line.
                    // Else add given number of new lines.
                    String whiteSpace = ((node.has("annotationAttachments")
                            && node.getAsJsonArray("annotationAttachments").size() > 0)
                            || node.has("markdownDocumentationAttachment")
                            || (node.has("deprecatedAttachments")
                                    && node.getAsJsonArray("deprecatedAttachments").size() > 0))
                                            ? (FormattingConstants.NEW_LINE + indentation)
                                            : (this.getNewLines(formatConfig
                                                    .get(FormattingConstants.NEW_LINE_COUNT).getAsInt())
                                                    + indentation);
                    annotationWS.addProperty(FormattingConstants.WS, whiteSpace);
                } else if (isPublic && text.equals("annotation")) {
                    annotationWS.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE);
                } else if (text.equals("<")) {
                    annotationWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE);
                    ++attachPointCounter;
                } else if (text.equals(",")) {
                    annotationWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE);
                } else if (text.equals(">")) {
                    annotationWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE);
                    attachPointCounter = 0;
                } else {
                    if (attachPointCounter == 1) {
                        annotationWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE);
                        ++attachPointCounter;
                    } else if (attachPointCounter > 1) {
                        annotationWS.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE);
                        ++attachPointCounter;
                    } else {
                        if (text.equals(";")) {
                            annotationWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE);
                        } else {
                            annotationWS.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE);
                        }
                    }
                }
            }
        }

        // Update whitespaces for type node.
        if (node.has("typeNode")) {
            JsonObject typeNode = node.getAsJsonObject("typeNode");
            JsonObject typeNodeFormattingConfig = this.getFormattingConfig(0, 1, 0, false,
                    this.getWhiteSpaceCount(indentation), false);
            typeNode.add(FormattingConstants.FORMATTING_CONFIG, typeNodeFormattingConfig);
        }
    }
}

From source file:org.ballerinalang.langserver.formatting.FormattingNodeTree.java

License:Open Source License

/**
 * format annotation attachment node.//from  w  w  w.java  2 s  . c  o  m
 *
 * @param node {JsonObject} node as json object
 */
public void formatAnnotationAttachmentNode(JsonObject node) {
    if (node.has(FormattingConstants.WS) && node.has(FormattingConstants.FORMATTING_CONFIG)) {
        JsonArray ws = node.getAsJsonArray(FormattingConstants.WS);
        JsonObject formatConfig = node.getAsJsonObject(FormattingConstants.FORMATTING_CONFIG);
        String indentation = this.getIndentation(formatConfig, false);
        String indentWithParentIndentation = this.getParentIndentation(formatConfig);

        // Preserve any comments or new lines that already available.
        this.preserveHeight(node.getAsJsonArray(FormattingConstants.WS), indentWithParentIndentation);

        // Update whitespace for annotation symbol, @.
        JsonObject annotationSymbolWhitespace = ws.get(0).getAsJsonObject();
        if (this.noHeightAvailable(annotationSymbolWhitespace.get(FormattingConstants.WS).getAsString())) {
            annotationSymbolWhitespace.addProperty(FormattingConstants.WS,
                    this.getNewLines(formatConfig.get(FormattingConstants.NEW_LINE_COUNT).getAsInt())
                            + indentWithParentIndentation);
        } else if (this.noNewLine(
                annotationSymbolWhitespace.get(FormattingConstants.WS).getAsString().charAt(0) + "")) {
            annotationSymbolWhitespace.addProperty(FormattingConstants.WS,
                    FormattingConstants.NEW_LINE + indentWithParentIndentation);
        }

        // Update whitespace for annotation identifier.
        JsonObject identifierWhitespace = ws.get(ws.size() - 1).getAsJsonObject();
        if (this.noHeightAvailable(identifierWhitespace.get(FormattingConstants.WS).getAsString())) {
            identifierWhitespace.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE);
        }

        // Update whitespace for expression.
        if (node.has("expression") && node.getAsJsonObject("expression").has(FormattingConstants.WS)) {
            JsonObject expression = node.getAsJsonObject("expression");
            JsonObject expressionFormatConfig = this.getFormattingConfig(0, 1,
                    this.getWhiteSpaceCount(indentation), false,
                    this.getWhiteSpaceCount(indentWithParentIndentation), false);
            expression.add(FormattingConstants.FORMATTING_CONFIG, expressionFormatConfig);
        }
    }
}

From source file:org.ballerinalang.langserver.formatting.FormattingNodeTree.java

License:Open Source License

/**
 * format Array Literal Expr node.//from   w w w .  j  a  v  a 2s  .  c o m
 *
 * @param node {JsonObject} node as json object
 */
public void formatArrayLiteralExprNode(JsonObject node) {
    if (node.has(FormattingConstants.WS) && node.has(FormattingConstants.FORMATTING_CONFIG)) {
        JsonArray ws = node.getAsJsonArray(FormattingConstants.WS);
        JsonObject formatConfig = node.getAsJsonObject(FormattingConstants.FORMATTING_CONFIG);
        String indentation = this.getIndentation(formatConfig, false);
        String indentWithParentIndentation = this.getParentIndentation(formatConfig);

        this.preserveHeight(ws, indentWithParentIndentation);

        // Update opening bracket whitespace.
        JsonObject openingBracketWS = ws.get(0).getAsJsonObject();
        if (this.noHeightAvailable(openingBracketWS.get(FormattingConstants.WS).getAsString())) {
            openingBracketWS.addProperty(FormattingConstants.WS,
                    this.getNewLines(formatConfig.get(FormattingConstants.NEW_LINE_COUNT).getAsInt())
                            + this.getWhiteSpaces(formatConfig.get(FormattingConstants.SPACE_COUNT).getAsInt())
                            + indentation);
        }

        // Update whitespace for the separator.
        for (JsonElement item : ws) {
            JsonObject wsItem = item.getAsJsonObject();
            String text = wsItem.get(FormattingConstants.TEXT).getAsString();
            if (text.equals(",") && this.noHeightAvailable(wsItem.get(FormattingConstants.WS).getAsString())) {
                wsItem.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE);
            }
        }

        // Update closing bracket whitespace.
        JsonObject closingBracketWS = ws.get(ws.size() - 1).getAsJsonObject();
        if (this.noHeightAvailable(closingBracketWS.get(FormattingConstants.WS).getAsString())) {
            closingBracketWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE);
        }

        // Update expressions whitespaces.
        modifyExpressions(node, indentWithParentIndentation);
    }
}