List of usage examples for com.google.gson JsonObject getAsJsonArray
public JsonArray getAsJsonArray(String memberName)
From source file:org.ballerinalang.langserver.formatting.FormattingNodeTree.java
License:Open Source License
/** * format Array Type node./*from w w w .j a v a 2 s. c om*/ * * @param node {JsonObject} node as json object */ public void formatArrayTypeNode(JsonObject node) { if (node.has(FormattingConstants.FORMATTING_CONFIG)) { JsonObject formatConfig = node.getAsJsonObject(FormattingConstants.FORMATTING_CONFIG); String indentWithParentIndentation = this.getParentIndentation(formatConfig); String indentation = this.getIndentation(formatConfig, false); // Update whitespace for element type. if (node.has("elementType")) { if (node.has(FormattingConstants.GROUPED) && node.get(FormattingConstants.GROUPED).getAsBoolean()) { JsonObject elementTypeFormatConfig = this.getFormattingConfig(0, 0, 0, false, this.getWhiteSpaceCount(indentWithParentIndentation), false); node.getAsJsonObject("elementType").add(FormattingConstants.FORMATTING_CONFIG, elementTypeFormatConfig); } else { node.getAsJsonObject("elementType").add(FormattingConstants.FORMATTING_CONFIG, formatConfig); } } if (node.has(FormattingConstants.WS)) { JsonArray ws = node.getAsJsonArray(FormattingConstants.WS); this.preserveHeight(ws, formatConfig.get(FormattingConstants.USE_PARENT_INDENTATION).getAsBoolean() ? indentWithParentIndentation : indentation); for (int i = 0; i < ws.size(); i++) { JsonObject wsItem = ws.get(i).getAsJsonObject(); if (this.noHeightAvailable(wsItem.get(FormattingConstants.WS).getAsString())) { String text = wsItem.get(FormattingConstants.TEXT).getAsString(); if (text.equals("(") && i == 0) { // Update grouped opening parentheses whitespace. if (node.has(FormattingConstants.GROUPED) && node.get(FormattingConstants.GROUPED).getAsBoolean()) { wsItem.addProperty(FormattingConstants.WS, this.getNewLines( formatConfig.get(FormattingConstants.NEW_LINE_COUNT).getAsInt()) + this.getWhiteSpaces( formatConfig.get(FormattingConstants.SPACE_COUNT).getAsInt()) + indentation); } } else { // Update rest of the token whitespaces. wsItem.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE); } } } } } }
From source file:org.ballerinalang.langserver.formatting.FormattingNodeTree.java
License:Open Source License
/** * format Assignment node.// www .j av a2s . c om * * @param node {JsonObject} node as json object */ public void formatAssignmentNode(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); // Get the indentation for the node. String indentation = this.getIndentation(formatConfig, false); String indentWithParentIndentation = this.getParentIndentation(formatConfig); // Preserve comments and new lines that already available. this.preserveHeight(ws, indentWithParentIndentation); if (node.has("declaredWithVar") && node.get("declaredWithVar").getAsBoolean()) { // Update whitespaces for var. JsonObject varWhitespace = ws.get(0).getAsJsonObject(); if (this.noHeightAvailable(varWhitespace.get(FormattingConstants.WS).getAsString())) { // If declared with var add new line varWhitespace.addProperty(FormattingConstants.WS, this.getNewLines(formatConfig.get(FormattingConstants.NEW_LINE_COUNT).getAsInt()) + indentation); } // Update whitespace for variable when declared with var. if (node.has("variable")) { JsonObject variable = node.getAsJsonObject("variable"); JsonObject variableFormatConfig = this.getFormattingConfig(0, 1, formatConfig.get(FormattingConstants.START_COLUMN).getAsInt(), false, this.getWhiteSpaceCount(indentation), false); variable.add(FormattingConstants.FORMATTING_CONFIG, variableFormatConfig); } // Update whitespace for = JsonObject equalWhitespace = ws.get(1).getAsJsonObject(); if (this.noHeightAvailable(equalWhitespace.get(FormattingConstants.WS).getAsString())) { equalWhitespace.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE); } // Update whitespace for ; JsonObject semicolonWhitespace = ws.get(2).getAsJsonObject(); if (this.noHeightAvailable(semicolonWhitespace.get(FormattingConstants.WS).getAsString())) { semicolonWhitespace.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE); } } else { // Update whitespace for variable when not declared with var. if (node.has("variable")) { JsonObject variable = node.getAsJsonObject("variable"); JsonObject variableFormatConfig = this.getFormattingConfig( formatConfig.get(FormattingConstants.NEW_LINE_COUNT).getAsInt(), 0, formatConfig.get(FormattingConstants.START_COLUMN).getAsInt(), formatConfig.get(FormattingConstants.DO_INDENT).getAsBoolean(), this.getWhiteSpaceCount(indentation), false); variable.add(FormattingConstants.FORMATTING_CONFIG, variableFormatConfig); } // Update whitespace for = JsonObject equalWhitespace = ws.get(0).getAsJsonObject(); if (this.noHeightAvailable(equalWhitespace.get(FormattingConstants.WS).getAsString())) { equalWhitespace.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE); } // Update whitespace for ; JsonObject semicolonWhitespace = ws.get(1).getAsJsonObject(); if (this.noHeightAvailable(semicolonWhitespace.get(FormattingConstants.WS).getAsString())) { semicolonWhitespace.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE); } } // Update whitespaces for the expression. if (node.has("expression") && node.getAsJsonObject("expression").has(FormattingConstants.WS)) { JsonObject expression = node.getAsJsonObject("expression"); JsonObject expressionFormatConfig = this.getFormattingConfig(0, 1, 0, false, this.getWhiteSpaceCount(indentation), true); expression.add(FormattingConstants.FORMATTING_CONFIG, expressionFormatConfig); } } }
From source file:org.ballerinalang.langserver.formatting.FormattingNodeTree.java
License:Open Source License
/** * format arrow expr node./* ww w . ja v a 2s . c om*/ * * @param node {JsonObject} node as json object */ public void formatArrowExprNode(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 indentWithParentIndentation = this.getParentIndentation(formatConfig); // Preserve line separations that already available. this.preserveHeight(ws, indentWithParentIndentation); for (JsonElement wsItem : ws) { JsonObject arrowExprWS = wsItem.getAsJsonObject(); if (this.noHeightAvailable(arrowExprWS.get(FormattingConstants.WS).getAsString())) { String text = arrowExprWS.get(FormattingConstants.TEXT).getAsString(); if (text.equals("(")) { arrowExprWS.addProperty(FormattingConstants.WS, this.getWhiteSpaces(formatConfig.get(FormattingConstants.SPACE_COUNT).getAsInt())); } if (text.equals(",")) { arrowExprWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE); } if (text.equals(")")) { arrowExprWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE); } if (text.equals("=>")) { arrowExprWS.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE); } } } // Update whitespaces of parameters. if (node.has("parameters")) { JsonArray parameters = node.getAsJsonArray("parameters"); boolean hasParentheses = node.has("hasParantheses") && node.get("hasParantheses").getAsBoolean(); for (int i = 0; i < parameters.size(); i++) { JsonObject param = parameters.get(i).getAsJsonObject(); JsonObject paramFormatConfig; // If parentheses available first param should fronted with empty space // Else first param should fronted with space count parent provided. if (i == 0) { if (hasParentheses) { paramFormatConfig = this.getFormattingConfig(0, 0, 0, false, this.getWhiteSpaceCount(indentWithParentIndentation), false); } else { paramFormatConfig = this.getFormattingConfig(0, formatConfig.get(FormattingConstants.SPACE_COUNT).getAsInt(), 0, false, this.getWhiteSpaceCount(indentWithParentIndentation), false); } } else { paramFormatConfig = this.getFormattingConfig(0, 1, 0, false, this.getWhiteSpaceCount(indentWithParentIndentation), false); } param.add(FormattingConstants.FORMATTING_CONFIG, paramFormatConfig); } } // Update whitespace of expression. if (node.has(FormattingConstants.EXPRESSION)) { JsonObject expression = node.getAsJsonObject(FormattingConstants.EXPRESSION); JsonObject expressionFormatConfig = this.getFormattingConfig(0, 1, 0, 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 Binary Expr node.// w ww .j ava2s.c o m * * @param node {JsonObject} node as json object */ public void formatBinaryExprNode(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 indentWithParentIndentation = this.getParentIndentation(formatConfig); // Preserve line separations that already available. this.preserveHeight(ws, indentWithParentIndentation); // Update the operator symbol whitespace. JsonObject operatorSymbolWS = ws.get(0).getAsJsonObject(); if (this.noHeightAvailable(operatorSymbolWS.get(FormattingConstants.WS).getAsString())) { operatorSymbolWS.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE); } // Handle left expression whitespaces. if (node.has("leftExpression")) { node.getAsJsonObject("leftExpression").add(FormattingConstants.FORMATTING_CONFIG, formatConfig); } // Handle right expression whitespaces. if (node.has("rightExpression")) { JsonObject rightExpression = node.getAsJsonObject("rightExpression"); JsonObject rightExprFormatConfig = this.getFormattingConfig(0, 1, 0, false, this.getWhiteSpaceCount(indentWithParentIndentation), true); rightExpression.add(FormattingConstants.FORMATTING_CONFIG, rightExprFormatConfig); } } }
From source file:org.ballerinalang.langserver.formatting.FormattingNodeTree.java
License:Open Source License
/** * format Block node.//w ww .j a va2s . c o m * * @param node {JsonObject} node as json object */ public void formatBlockNode(JsonObject node) { JsonObject position = new JsonObject(); // TODO: revisit code of how block node is handled. JsonObject formatConfig = node.has(FormattingConstants.FORMATTING_CONFIG) ? node.getAsJsonObject(FormattingConstants.FORMATTING_CONFIG) : null; // Get the start column of the parent. position.addProperty(FormattingConstants.START_COLUMN, node.get("parent").getAsJsonObject().get(FormattingConstants.POSITION).getAsJsonObject() .get(FormattingConstants.START_COLUMN).getAsInt()); // Add block position to be the parent's position. node.add(FormattingConstants.POSITION, position); // Update the statements whitespaces. for (int i = 0; i < node.getAsJsonArray(FormattingConstants.STATEMENTS).size(); i++) { JsonElement child = node.getAsJsonArray(FormattingConstants.STATEMENTS).get(i); JsonObject childFormatConfig = formatConfig; if (formatConfig == null) { childFormatConfig = this.getFormattingConfig(1, 0, node.get(FormattingConstants.POSITION).getAsJsonObject() .get(FormattingConstants.START_COLUMN).getAsInt(), true, node.get(FormattingConstants.POSITION).getAsJsonObject() .get(FormattingConstants.START_COLUMN).getAsInt(), false); } child.getAsJsonObject().add(FormattingConstants.FORMATTING_CONFIG, childFormatConfig); } // If this is a else block continue to following. if (node.has(FormattingConstants.WS) && node.getAsJsonArray(FormattingConstants.WS).get(0).getAsJsonObject() .get(FormattingConstants.TEXT).getAsString().equals("else")) { JsonArray ws = node.getAsJsonArray(FormattingConstants.WS); this.preserveHeight(ws, this.getWhiteSpaces(node.getAsJsonObject(FormattingConstants.POSITION) .get(FormattingConstants.START_COLUMN).getAsInt())); // Update the else keyword whitespace. JsonObject elseKeywordWS = ws.get(0).getAsJsonObject(); if (this.noHeightAvailable(elseKeywordWS.get(FormattingConstants.WS).getAsString())) { elseKeywordWS.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE); } // Update the opening brace whitespace. JsonObject openingBraceWS = ws.get(ws.size() - 2).getAsJsonObject(); if (this.noHeightAvailable(openingBraceWS.get(FormattingConstants.WS).getAsString())) { openingBraceWS.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE); } // Update the closing brace whitespace. JsonObject closingBraceWS = ws.get(ws.size() - 1).getAsJsonObject(); if (node.getAsJsonArray(FormattingConstants.STATEMENTS).size() <= 0) { if (this.noHeightAvailable(closingBraceWS.get(FormattingConstants.WS).getAsString())) { closingBraceWS.addProperty(FormattingConstants.WS, FormattingConstants.NEW_LINE + this.getWhiteSpaces(node.getAsJsonObject(FormattingConstants.POSITION) .get(FormattingConstants.START_COLUMN).getAsInt()) + FormattingConstants.NEW_LINE + this.getWhiteSpaces(node.getAsJsonObject(FormattingConstants.POSITION) .get(FormattingConstants.START_COLUMN).getAsInt())); } } else if (this.noHeightAvailable(closingBraceWS.get(FormattingConstants.WS).getAsString())) { closingBraceWS.addProperty(FormattingConstants.WS, FormattingConstants.NEW_LINE + this.getWhiteSpaces(node.getAsJsonObject(FormattingConstants.POSITION) .get(FormattingConstants.START_COLUMN).getAsInt())); } } }
From source file:org.ballerinalang.langserver.formatting.FormattingNodeTree.java
License:Open Source License
/** * format Braced Tuple Expr node.//ww w . j a v a2s . c o m * * @param node {JsonObject} node as json object */ public void formatBracedTupleExprNode(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); // Get the indentation for the node. String indentation = this.getIndentation(formatConfig, true); String indentationOfParent = this.getParentIndentation(formatConfig); this.preserveHeight(ws, formatConfig.get(FormattingConstants.USE_PARENT_INDENTATION).getAsBoolean() ? indentationOfParent : indentation); // Update opening parentheses whitespace. JsonObject openingParenthesesWS = ws.get(0).getAsJsonObject(); if (this.noHeightAvailable(openingParenthesesWS.get(FormattingConstants.WS).getAsString())) { openingParenthesesWS.addProperty(FormattingConstants.WS, this.getNewLines(formatConfig.get(FormattingConstants.NEW_LINE_COUNT).getAsInt()) + indentation); } // Update expressions' whitespaces. modifyExpressions(node, formatConfig.get(FormattingConstants.USE_PARENT_INDENTATION).getAsBoolean() ? indentationOfParent : indentation); // Update closing parentheses whitespace. JsonObject closingParenthesesWS = ws.get(ws.size() - 1).getAsJsonObject(); if (this.noHeightAvailable(closingParenthesesWS.get(FormattingConstants.WS).getAsString())) { closingParenthesesWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE); } } }
From source file:org.ballerinalang.langserver.formatting.FormattingNodeTree.java
License:Open Source License
/** * format built in ref type.//from ww w . j av a 2 s . c om * * @param node {JsonObject} node as json object */ public void formatBuiltInRefTypeNode(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); // Get the indentation for the node. String indentation = this.getIndentation(formatConfig, true); // Preserve the line separators that already available. this.preserveHeight(ws, indentation); // Update the ref type whitespace. JsonObject refTypeWhitespace = ws.get(0).getAsJsonObject(); if (this.noHeightAvailable(refTypeWhitespace.get(FormattingConstants.WS).getAsString())) { refTypeWhitespace.addProperty(FormattingConstants.WS, this.getNewLines(formatConfig.get(FormattingConstants.NEW_LINE_COUNT).getAsInt()) + indentation); } } }
From source file:org.ballerinalang.langserver.formatting.FormattingNodeTree.java
License:Open Source License
/** * format Check Expr node.// w ww . j ava 2 s .c o m * * @param node {JsonObject} node as json object */ public void formatCheckExprNode(JsonObject node) { if (node.has(FormattingConstants.FORMATTING_CONFIG) && node.has(FormattingConstants.WS)) { JsonArray ws = node.getAsJsonArray(FormattingConstants.WS); JsonObject formatConfig = node.getAsJsonObject(FormattingConstants.FORMATTING_CONFIG); boolean isExpression = node.has("isExpression") && node.get("isExpression").getAsBoolean(); // Get the indentation for the node. String indentation = this.getIndentation(formatConfig, false); // Get the indentation for the node. String indentationWithParent = this.getParentIndentation(formatConfig); if (isExpression) { this.preserveHeight(ws, indentationWithParent); } else { this.preserveHeight(ws, indentation); } // Update whitespaces for check. for (JsonElement wsItem : ws) { JsonObject currentWS = wsItem.getAsJsonObject(); if (this.noHeightAvailable(currentWS.get(FormattingConstants.WS).getAsString())) { String text = currentWS.get(FormattingConstants.TEXT).getAsString(); if (text.equals("check")) { if (isExpression) { currentWS.addProperty(FormattingConstants.WS, this .getWhiteSpaces(formatConfig.get(FormattingConstants.SPACE_COUNT).getAsInt())); } else { currentWS.addProperty(FormattingConstants.WS, this.getNewLines( formatConfig.get(FormattingConstants.NEW_LINE_COUNT).getAsInt()) + indentation); } } } } // Handle whitespace for expression. if (node.has(FormattingConstants.EXPRESSION)) { node.getAsJsonObject(FormattingConstants.EXPRESSION).add(FormattingConstants.FORMATTING_CONFIG, this.getFormattingConfig(0, 1, 0, false, this.getWhiteSpaceCount(isExpression ? indentationWithParent : indentation), false)); } } }
From source file:org.ballerinalang.langserver.formatting.FormattingNodeTree.java
License:Open Source License
/** * format Compound Assignment node.//from ww w .j a v a 2s .c o m * * @param node {JsonObject} node as json object */ public void formatCompoundAssignmentNode(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 compoundOperator = node.get("compoundOperator").getAsString(); String indentation = this.getIndentation(formatConfig, false); // Preserve line separations that already available. this.preserveHeight(ws, indentation); // Iterate and format compound assignment whitespaces. for (JsonElement wsItem : ws) { JsonObject currentWS = wsItem.getAsJsonObject(); if (this.noHeightAvailable(currentWS.get(FormattingConstants.WS).getAsString())) { String text = currentWS.get(FormattingConstants.TEXT).getAsString(); if (text.equals(compoundOperator)) { currentWS.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE); } else if (text.equals(";")) { currentWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE); } } } // Handle variable whitespaces. if (node.has("variable")) { node.getAsJsonObject("variable").add(FormattingConstants.FORMATTING_CONFIG, formatConfig); } // Handle expression whitespaces. if (node.has("expression")) { node.getAsJsonObject("expression").add(FormattingConstants.FORMATTING_CONFIG, this.getFormattingConfig(0, 1, 0, false, this.getWhiteSpaceCount(indentation), false)); } } }
From source file:org.ballerinalang.langserver.formatting.FormattingNodeTree.java
License:Open Source License
/** * format constant node.//from www . j av a 2 s . c o m * * @param node {JsonObject} node as json object */ public void formatConstantNode(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); boolean isPublic = node.has(FormattingConstants.PUBLIC) && node.get(FormattingConstants.PUBLIC).getAsBoolean(); // Preserve line separators that already available. this.preserveHeight(ws, indentation); // Iterate and update whitespaces for constant node. for (JsonElement wsItem : ws) { JsonObject currentWS = wsItem.getAsJsonObject(); if (this.noHeightAvailable(currentWS.get(FormattingConstants.WS).getAsString())) { String text = currentWS.get(FormattingConstants.TEXT).getAsString(); if (text.equals("public")) { currentWS.addProperty(FormattingConstants.WS, this.getNewLines(formatConfig.get(FormattingConstants.NEW_LINE_COUNT).getAsInt()) + indentation); } else if (text.equals("const")) { if (isPublic) { currentWS.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE); } else { currentWS.addProperty(FormattingConstants.WS, this.getNewLines( formatConfig.get(FormattingConstants.NEW_LINE_COUNT).getAsInt()) + indentation); } } else if (text.equals("=")) { currentWS.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE); } else if (text.equals(";")) { currentWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE); } else { currentWS.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE); } } } if (node.has("typeNode")) { node.getAsJsonObject("typeNode").add(FormattingConstants.FORMATTING_CONFIG, this.getFormattingConfig(0, 1, 0, false, this.getWhiteSpaceCount(indentation), false)); } if (node.has("value")) { node.getAsJsonObject("value").add(FormattingConstants.FORMATTING_CONFIG, this.getFormattingConfig(0, 1, 0, false, this.getWhiteSpaceCount(indentation), false)); } } }