diff --git a/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangExpressionModelBuilder.java b/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangExpressionModelBuilder.java index 0ba077247ca6..c6fb6c9af0a7 100644 --- a/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangExpressionModelBuilder.java +++ b/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangExpressionModelBuilder.java @@ -255,11 +255,9 @@ public void visit(ActionInvocationStmt actionInvocationStmt) { public void visit(ReplyStmt replyStmt) { StringBuffer buffer = new StringBuffer(); bufferStack.push(buffer); - buffer.append("reply "); replyStmt.getReplyExpr().accept(this); buffer.append(bufferStack.peek()); bufferStack.pop(); - buffer.append(";"); } @Override diff --git a/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangFileRestService.java b/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangFileRestService.java index 00b1d57eaf2c..c4a21b857449 100644 --- a/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangFileRestService.java +++ b/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangFileRestService.java @@ -33,6 +33,7 @@ import org.wso2.ballerina.core.semantics.SemanticAnalyzer; import javax.ws.rs.Consumes; +import javax.ws.rs.OPTIONS; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.GET; @@ -81,7 +82,7 @@ public Response getBallerinaJsonDataModelGivenContent(BFileContent content) { try { InputStream stream = new ByteArrayInputStream(content.getContent().getBytes(StandardCharsets.UTF_8)); String response = parseJsonDataModel(stream); - return Response.ok(response, MediaType.APPLICATION_JSON).build(); + return Response.ok(response, MediaType.APPLICATION_JSON).header("Access-Control-Allow-Origin", '*').build(); } catch (IOException ex) { logger.error("IOException occured while generating JSON data model for ballerina file", ex); JsonObject entity = new JsonObject(); @@ -92,6 +93,19 @@ public Response getBallerinaJsonDataModelGivenContent(BFileContent content) { } } + @OPTIONS + @Path("/model/content") + @Consumes("application/json") + @Produces("application/json") + public Response options() { + return Response.ok() + .header("Access-Control-Allow-Origin", "*") + .header("Access-Control-Allow-Credentials", "true") + .header("Access-Control-Allow-Methods", "POST, GET, PUT, UPDATE, DELETE, OPTIONS, HEAD") + .header("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With") + .build(); + } + private String parseJsonDataModel(InputStream stream) throws IOException { ANTLRInputStream antlrInputStream = new ANTLRInputStream(stream); diff --git a/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangJSONModelBuilder.java b/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangJSONModelBuilder.java index 66dd7ccb75b7..e221650b78aa 100644 --- a/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangJSONModelBuilder.java +++ b/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangJSONModelBuilder.java @@ -474,15 +474,7 @@ public void visit(VariableDcl variableDcl) { public void visit(BlockStmt blockStmt) { if (blockStmt.getStatements() != null) { for (Statement statement : blockStmt.getStatements()) { - if (isExprAsString) { - JsonObject jsonObject = new JsonObject(); - statement.accept(exprVisitor); - jsonObject.addProperty(BLangJSONModelConstants.STATEMENT, - exprVisitor.getBuffer().toString()); - tempJsonArrayRef.peek().add(jsonObject); - } else { - statement.accept(this); - } + statement.accept(this); } } } @@ -564,6 +556,11 @@ public void visit(ReplyStmt replyStmt) { JsonObject replyStmtObj = new JsonObject(); replyStmtObj.addProperty(BLangJSONModelConstants.STATEMENT_TYPE, BLangJSONModelConstants.REPLY_STATEMENT); + if (isExprAsString) { + replyStmt.accept(exprVisitor); + String stmtExpression = exprVisitor.getBuffer().toString(); + replyStmtObj.addProperty(BLangJSONModelConstants.EXPRESSION, stmtExpression); + } tempJsonArrayRef.push(new JsonArray()); replyStmt.getReplyExpr().accept(this); replyStmtObj.add(BLangJSONModelConstants.CHILDREN, tempJsonArrayRef.peek()); diff --git a/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangJSONModelConstants.java b/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangJSONModelConstants.java index 6e2c84a8ee72..5dc0ee9ec75d 100644 --- a/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangJSONModelConstants.java +++ b/modules/editor/services/workspace-service/src/main/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangJSONModelConstants.java @@ -160,4 +160,6 @@ public class BLangJSONModelConstants { public static final String STATEMENT = "statement"; + public static final String EXPRESSION = "expression"; + } diff --git a/modules/editor/services/workspace-service/src/test/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangJSONModelTest.java b/modules/editor/services/workspace-service/src/test/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangJSONModelTest.java index c7126b58d978..05b5692b59b1 100644 --- a/modules/editor/services/workspace-service/src/test/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangJSONModelTest.java +++ b/modules/editor/services/workspace-service/src/test/java/org/wso2/ballerina/tooling/service/workspace/rest/datamodel/BLangJSONModelTest.java @@ -44,18 +44,29 @@ public class BLangJSONModelTest { private MicroservicesRunner microservicesRunner; //private HashMap packages = new HashMap(); - private String exptdStrFunc = "{\"root\":[{\"type\":\"package\",\"package_name\":\"test.samples\"},{\"type\":\"import\"," + - "\"import_package_name\":\"twitter\",\"import_package_path\":\"ballerina.connectors.twitter\"},{\"type\":" + - "\"import\",\"import_package_name\":\"sf\",\"import_package_path\":\"ballerina.connectors.salesforce\"}," + - "{\"type\":\"import\",\"import_package_name\":\"samples\",\"import_package_path\":\"test.samples\"},{\"type\":" + - "\"service_definition\",\"service_name\":\"HelloService\",\"annotations\":[],\"children\":[{\"type\":" + - "\"resource_definition\",\"resource_name\":\"tweet\",\"annotations\":[{\"type\":\"annotation\",\"annotation_name\":" + - "\"GET\",\"annotation_value\":null,\"children\":[]},{\"type\":\"annotation\",\"annotation_name\":\"Path\"," + - "\"annotation_value\":\"/tweet\",\"children\":[]}],\"children\":[{\"type\":\"argument_declaration\",\"parameter_name\":\"" + - "m\",\"parameter_type\":\"message\",\"children\":[]},{\"statement\":\"reply m;\"}]}]},{\"type\":\"function_definition\"," + - "\"function_name\":\"test_int\",\"is_public_function\":false,\"annotations\":[],\"children\":[{\"type\":\"argument_declaration\"," + - "\"parameter_name\":\"a\",\"parameter_type\":\"int\",\"children\":[]},{\"type\":\"return_type\",\"children\":" + - "[{\"type\":\"type_name\",\"type_name\":\"int\"}]},{\"statement\":\"reply a + 2 ;\"}]}]}"; + private String exptdStrFunc = "{\"root\":[{\"type\":\"package\",\"package_name\":\"test.samples\"}," + + "{\"type\":\"import\",\"import_package_name\":\"twitter\"," + + "\"import_package_path\":\"ballerina.connectors.twitter\"},{\"type\":\"import\"," + + "\"import_package_name\":\"sf\",\"import_package_path\":\"ballerina.connectors" + + ".salesforce\"},{\"type\":\"import\",\"import_package_name\":\"samples\"," + + "\"import_package_path\":\"test.samples\"},{\"type\":\"service_definition\"," + + "\"service_name\":\"HelloService\",\"annotations\":[]," + + "\"children\":[{\"type\":\"resource_definition\",\"resource_name\":\"tweet\"," + + "\"annotations\":[{\"type\":\"annotation\",\"annotation_name\":\"GET\"," + + "\"annotation_value\":null,\"children\":[]},{\"type\":\"annotation\"," + + "\"annotation_name\":\"Path\",\"annotation_value\":\"/tweet\",\"children\":[]}]," + + "\"children\":[{\"type\":\"argument_declaration\",\"parameter_name\":\"m\"," + + "\"parameter_type\":\"message\",\"children\":[]},{\"type\":\"reply_statement\"," + + "\"expression\":\"m\",\"children\":[{\"type\":\"variable_reference_expression\"," + + "\"variable_reference_name\":\"m\"}]}]}]},{\"type\":\"function_definition\"," + + "\"function_name\":\"test_int\",\"is_public_function\":false,\"annotations\":[]," + + "\"children\":[{\"type\":\"argument_declaration\",\"parameter_name\":\"a\"," + + "\"parameter_type\":\"int\",\"children\":[]},{\"type\":\"return_type\"," + + "\"children\":[{\"type\":\"type_name\",\"type_name\":\"int\"}]}," + + "{\"type\":\"return_statement\",\"children\":[{\"type\":\"add_expression\"," + + "\"children\":[{\"type\":\"variable_reference_expression\"," + + "\"variable_reference_name\":\"a\"},{\"type\":\"basic_literal_expression\"," + + "\"basic_literal_type\":\"int\",\"basic_literal_value\":\"2\"}]}]}]}]}"; public static void main(String[] args) { try { diff --git a/modules/editor/web/js/ballerina/ast/assignment.js b/modules/editor/web/js/ballerina/ast/assignment.js index b5f08ab9fe85..1813e8b14c09 100644 --- a/modules/editor/web/js/ballerina/ast/assignment.js +++ b/modules/editor/web/js/ballerina/ast/assignment.js @@ -46,5 +46,13 @@ define(['lodash', './statement'], function (_, Statement) { return this._expression; }; + /** + * initialize from json + * @param jsonNode + */ + Assignment.prototype.initFromJson = function (jsonNode) { + this._expression = jsonNode.statement; + }; + return Assignment; }); diff --git a/modules/editor/web/js/ballerina/ast/ballerina-ast-factory.js b/modules/editor/web/js/ballerina/ast/ballerina-ast-factory.js index ffe4c5b81cc9..040537fe2b0b 100644 --- a/modules/editor/web/js/ballerina/ast/ballerina-ast-factory.js +++ b/modules/editor/web/js/ballerina/ast/ballerina-ast-factory.js @@ -589,56 +589,62 @@ define(['./ballerina-ast-root', './service-definition', './function-definition', BallerinaASTFactory.createFromJson = function (jsonNode) { var node; - switch (jsonNode.type) { - case 'package': - node = BallerinaASTFactory.createPackageDefinition(); - break; - case 'import': - node = BallerinaASTFactory.createImportDeclaration(); - break; - case 'service_definition': - node = BallerinaASTFactory.createServiceDefinition(); - break; - case 'function_definition': - node = BallerinaASTFactory.createFunctionDefinition(); - break; - case 'connector_definition': - node = BallerinaASTFactory.createConnectorDefinition(); - break; - case 'type_definition': - node = BallerinaASTFactory.createTypeDefinition(); - break; - case 'resource_definition': - node = BallerinaASTFactory.createResourceDefinition(); - break; - case 'connector_declaration': - node = BallerinaASTFactory.createConnectorDeclaration(); - break; - case 'variable_declaration': - node = BallerinaASTFactory.createVariableDeclaration(); - break; - case 'argument_declaration': - node = BallerinaASTFactory.createResourceArgument(); - break; - case 'reply_statement': - node = BallerinaASTFactory.createReplyStatement(); - break; - case 'return_statement': - node = BallerinaASTFactory.createReturnStatement(); - break; - case 'return_type': - node = BallerinaASTFactory.createReturnType(); - break; - case 'type_name': - node = BallerinaASTFactory.createTypeName(); - break; - case 'function_invocation': - node = BallerinaASTFactory.createFunctionInvocationStatement(); - break; - default: - throw "Unknown node definition for " + jsonNode.type; + var nodeType = jsonNode.type; + + if (_.isUndefined(jsonNode.type)) { + var statement = jsonNode.statement; + node = BallerinaASTFactory.createAssignmentStatement(); + } else { + switch (nodeType) { + case 'package': + node = BallerinaASTFactory.createPackageDefinition(); + break; + case 'import': + node = BallerinaASTFactory.createImportDeclaration(); + break; + case 'service_definition': + node = BallerinaASTFactory.createServiceDefinition(); + break; + case 'function_definition': + node = BallerinaASTFactory.createFunctionDefinition(); + break; + case 'connector_definition': + node = BallerinaASTFactory.createConnectorDefinition(); + break; + case 'type_definition': + node = BallerinaASTFactory.createTypeDefinition(); + break; + case 'resource_definition': + node = BallerinaASTFactory.createResourceDefinition(); + break; + case 'connector_declaration': + node = BallerinaASTFactory.createConnectorDeclaration(); + break; + case 'variable_declaration': + node = BallerinaASTFactory.createVariableDeclaration(); + break; + case 'argument_declaration': + node = BallerinaASTFactory.createResourceArgument(); + break; + case 'reply_statement': + node = BallerinaASTFactory.createReplyStatement(); + break; + case 'return_statement': + node = BallerinaASTFactory.createReturnStatement(); + break; + case 'return_type': + node = BallerinaASTFactory.createReturnType(); + break; + case 'type_name': + node = BallerinaASTFactory.createTypeName(); + break; + case 'function_invocation': + node = BallerinaASTFactory.createFunctionInvocationStatement(); + break; + default: + throw "Unknown node definition for " + jsonNode.type; + } } - node.initFromJson(jsonNode); return node; }; diff --git a/modules/editor/web/js/ballerina/ast/function-definition.js b/modules/editor/web/js/ballerina/ast/function-definition.js index e0b25e1b5a70..a1e40d064714 100644 --- a/modules/editor/web/js/ballerina/ast/function-definition.js +++ b/modules/editor/web/js/ballerina/ast/function-definition.js @@ -21,9 +21,9 @@ define(['lodash', './callable-definition'], /** * Constructor for FunctionDefinition * @param {Object} args - The arguments to create the FunctionDefinition - * @param {string} args.functionName [args.functionName=newFunction] - Function name - * @param {string} args.isPublic [args.isPublic=false] - Public or not of the function - * @param {string[]} args.annotations - Function annotations + * @param {string} [args.functionName=newFunction] - Function name + * @param {boolean} [args.isPublic=false] - Public or not of the function + * @param {string[]} [args.annotations] - Function annotations * @constructor */ var FunctionDefinition = function (args) { @@ -283,9 +283,9 @@ define(['lodash', './callable-definition'], /** * initialize FunctionDefinition from json object * @param {Object} jsonNode to initialize from - * @param {string} jsonNode.function_name - Name of the function definition - * @param {string} jsonNode.annotations - Annotations of the function definition - * @param {string} jsonNode.is_public_function - Public or not of the function + * @param {string} [jsonNode.function_name] - Name of the function definition + * @param {string} [jsonNode.annotations] - Annotations of the function definition + * @param {boolean} [jsonNode.is_public_function] - Public or not of the function */ FunctionDefinition.prototype.initFromJson = function (jsonNode) { this._functionName = jsonNode.function_name; diff --git a/modules/editor/web/js/ballerina/ast/reply-statement.js b/modules/editor/web/js/ballerina/ast/reply-statement.js index 9e496d0355fb..9d84dcfa8392 100644 --- a/modules/editor/web/js/ballerina/ast/reply-statement.js +++ b/modules/editor/web/js/ballerina/ast/reply-statement.js @@ -53,7 +53,7 @@ define(['lodash', 'log', './statement'], function (_, log, Statement) { * @param jsonNode */ ReplyStatement.prototype.initFromJson = function (jsonNode) { - //TODO : load properties from expression + this._message = jsonNode.expression; }; return ReplyStatement; diff --git a/modules/editor/web/js/ballerina/ast/resource-definition.js b/modules/editor/web/js/ballerina/ast/resource-definition.js index 09b069ac00ea..4a16598983d5 100644 --- a/modules/editor/web/js/ballerina/ast/resource-definition.js +++ b/modules/editor/web/js/ballerina/ast/resource-definition.js @@ -21,10 +21,10 @@ define(['lodash', 'log', './node'], /** * Constructor for ResourceDefinition * @param {Object} args - The arguments to create the ServiceDefinition - * @param {string} args.resourceName [args.resourceName=newResource] - Service name - * @param {string[]} args.annotations - Resource annotations - * @param {string} args.annotations.Method - Resource annotation for Method - * @param {string} args.annotations.Path - Resource annotation for Path + * @param {string} [args.resourceName=newResource] - Service name + * @param {string[]} [args.annotations] - Resource annotations + * @param {string} [args.annotations.Method] - Resource annotation for Method + * @param {string} [args.annotations.Path] - Resource annotation for Path * @constructor */ var ResourceDefinition = function (args) { @@ -242,8 +242,8 @@ define(['lodash', 'log', './node'], /** * initialize ResourceDefinition from json object * @param {Object} jsonNode to initialize from - * @param {string} jsonNode.resource_name - Name of the resource definition - * @param {string} jsonNode.annotations - Annotations of the resource definition + * @param {string} [jsonNode.resource_name] - Name of the resource definition + * @param {string} [jsonNode.annotations] - Annotations of the resource definition */ ResourceDefinition.prototype.initFromJson = function (jsonNode) { this._resourceName = jsonNode.resource_name; diff --git a/modules/editor/web/js/ballerina/ast/service-definition.js b/modules/editor/web/js/ballerina/ast/service-definition.js index 2b172affd79a..5946c819dc07 100644 --- a/modules/editor/web/js/ballerina/ast/service-definition.js +++ b/modules/editor/web/js/ballerina/ast/service-definition.js @@ -21,9 +21,9 @@ define(['lodash', './node'], /** * Constructor for ServiceDefinition * @param {Object} args - The arguments to create the ServiceDefinition - * @param {string} args.serviceName [args.serviceName=newService] - Service name - * @param {string[]} args.annotations - Service annotations - * @param {string} args.annotations.BasePath - Service annotation for BasePath + * @param {string} [args.serviceName=newService] - Service name + * @param {string[]} [args.annotations] - Service annotations + * @param {string} [args.annotations.BasePath] - Service annotation for BasePath * @constructor */ var ServiceDefinition = function (args) { @@ -160,8 +160,8 @@ define(['lodash', './node'], /** * initialize ServiceDefinition from json object * @param {Object} jsonNode to initialize from - * @param {string} jsonNode.service_name - Name of the service definition - * @param {string} jsonNode.annotations - Annotations of the function definition + * @param {string} [jsonNode.service_name] - Name of the service definition + * @param {string} [jsonNode.annotations] - Annotations of the function definition */ ServiceDefinition.prototype.initFromJson = function (jsonNode) { this._serviceName = jsonNode.service_name; diff --git a/modules/editor/web/js/dialog/open-file-dialog.js b/modules/editor/web/js/dialog/open-file-dialog.js index 693843984a59..e9f7a4743689 100644 --- a/modules/editor/web/js/dialog/open-file-dialog.js +++ b/modules/editor/web/js/dialog/open-file-dialog.js @@ -162,12 +162,31 @@ define(['require', 'jquery', 'log', 'backbone', 'file_browser', 'ballerina', 'ba }); }; - function openModel(data){ - var BallerinaASTDeserializer = Ballerina.ast.BallerinaASTDeserializer; - var root = BallerinaASTDeserializer.getASTModel(data); + function openModel(source){ + $.ajax({ + url: "http://localhost:8289/ballerina/model/content", + type: "POST", + data: JSON.stringify(source), + contentType: "application/json; charset=utf-8", + async: false, + dataType:"json", + success: function (data, textStatus, xhr) { + if (xhr.status == 200) { + var BallerinaASTDeserializer = Ballerina.ast.BallerinaASTDeserializer; + var root = BallerinaASTDeserializer.getASTModel(data); + + var command = app.commandManager; + command.dispatch("create-new-tab", root); - var command = app.commandManager; - command.dispatch("create-new-tab", root); + alertSuccess(); + } else { + alertError(); + } + }, + error: function (res, errorCode, error) { + alertError(); + } + }); } function openConfiguration() { @@ -185,10 +204,8 @@ define(['require', 'jquery', 'log', 'backbone', 'file_browser', 'ballerina', 'ba contentType: "text/plain; charset=utf-8", async: false, success: function (data, textStatus, xhr) { - fileContent = $.parseJSON(data.content); if (xhr.status == 200) { - openModel(fileContent); - alertSuccess(); + openModel(data); } else { alertError(); } diff --git a/modules/editor/web/js/menu-bar/file-menu.js b/modules/editor/web/js/menu-bar/file-menu.js index 317783f2d838..d6ee76fec2bb 100644 --- a/modules/editor/web/js/menu-bar/file-menu.js +++ b/modules/editor/web/js/menu-bar/file-menu.js @@ -31,7 +31,7 @@ define(([],function (){ id: "open", label: "Open", action: "open-file-open-dialog", - disabled: true + disabled: false }, { id: "save",