{"action":"created","issue":{"url":"https://api.github.com/repos/google/blockly/issues/57","labels_url":"https://api.github.com/repos/google/blockly/issues/57/labels{/name}","comments_url":"https://api.github.com/repos/google/blockly/issues/57/comments","events_url":"https://api.github.com/repos/google/blockly/issues/57/events","html_url":"https://github.com/google/blockly/issues/57","id":56171154,"number":57,"title":"Keep the operator precedence when collapse a block","user":{"login":"carloslfu","id":5993168,"avatar_url":"https://avatars.githubusercontent.com/u/5993168?v=3","gravatar_id":"","url":"https://api.github.com/users/carloslfu","html_url":"https://github.com/carloslfu","followers_url":"https://api.github.com/users/carloslfu/followers","following_url":"https://api.github.com/users/carloslfu/following{/other_user}","gists_url":"https://api.github.com/users/carloslfu/gists{/gist_id}","starred_url":"https://api.github.com/users/carloslfu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carloslfu/subscriptions","organizations_url":"https://api.github.com/users/carloslfu/orgs","repos_url":"https://api.github.com/users/carloslfu/repos","events_url":"https://api.github.com/users/carloslfu/events{/privacy}","received_events_url":"https://api.github.com/users/carloslfu/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-02-01T17:50:11Z","updated_at":"2015-03-02T18:19:05Z","closed_at":null,"body":"![operator-precedence](https://cloud.githubusercontent.com/assets/5993168/5993040/4f6fde08-aa10-11e4-9e50-83d09041706d.jpg)\r\n"},"comment":{"url":"https://api.github.com/repos/google/blockly/issues/comments/76768256","html_url":"https://github.com/google/blockly/issues/57#issuecomment-76768256","issue_url":"https://api.github.com/repos/google/blockly/issues/57","id":76768256,"user":{"login":"carloslfu","id":5993168,"avatar_url":"https://avatars.githubusercontent.com/u/5993168?v=3","gravatar_id":"","url":"https://api.github.com/users/carloslfu","html_url":"https://github.com/carloslfu","followers_url":"https://api.github.com/users/carloslfu/followers","following_url":"https://api.github.com/users/carloslfu/following{/other_user}","gists_url":"https://api.github.com/users/carloslfu/gists{/gist_id}","starred_url":"https://api.github.com/users/carloslfu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carloslfu/subscriptions","organizations_url":"https://api.github.com/users/carloslfu/orgs","repos_url":"https://api.github.com/users/carloslfu/repos","events_url":"https://api.github.com/users/carloslfu/events{/privacy}","received_events_url":"https://api.github.com/users/carloslfu/received_events","type":"User","site_admin":false},"created_at":"2015-03-02T18:19:05Z","updated_at":"2015-03-02T18:19:05Z","body":"I have tested this solution and should be good:\r\n```\r\nBlockly.Block.prototype.toString = function(opt_maxLength) {\r\n  var text = [];\r\n  for (var i = 0, input; input = this.inputList[i]; i++) {\r\n    for (var j = 0, field; field = input.fieldRow[j]; j++) {\r\n      text.push(field.getText());\r\n    }\r\n    if (input.connection) {\r\n      var child = input.connection.targetBlock();\r\n      if (child) {\r\n        /////------------------------------\r\n        // could be declared in block definition like (in 'math_arithmetic' case):\r\n        /*\r\n          this.setPrecedence(true); //default false\r\n          this.setPrecedenceOrder({'ADD' : 0,\r\n                             'MINUS': 0,\r\n                             'MULTIPLY': 1,\r\n                             'DIVIDE': 1,\r\n                             'POWER': 2\r\n                             });\r\n          this.getPrecedence = function(){\r\n            return this.precedenceOrder[this.getFieldValue('OP')];\r\n          };\r\n        */\r\n        //simulate blocks using precedence (for testing)\r\n        this.precedence = false; //default value\r\n        if (this.type == \"math_arithmetic\") {\r\n          this.precedence = true;\r\n          this.precedenceOrder = {'ADD' : 0,\r\n                             'MINUS': 0,\r\n                             'MULTIPLY': 1,\r\n                             'DIVIDE': 1,\r\n                             'POWER': 2\r\n                             };\r\n          this.getPrecedence = function(){\r\n            return this.precedenceOrder[this.getFieldValue('OP')];\r\n          };\r\n        }\r\n        if(child.type == \"math_number\") {\r\n          child.precedence = false; //if not precedence , it have 99 order\r\n        } else if (child.type == \"math_arithmetic\") {\r\n          child.precedence = true;\r\n          child.precedenceOrder = {'ADD' : 0,\r\n                             'MINUS': 0,\r\n                             'MULTIPLY': 1,\r\n                             'DIVIDE': 1,\r\n                             'POWER': 2\r\n                             };\r\n          child.getPrecedence = function(){\r\n            return this.precedenceOrder[this.getFieldValue('OP')];\r\n          };\r\n        } else {\r\n          //others like 'remainder of' block, this may be declared in each math block\r\n          child.precedence = true;\r\n          child.getPrecedence = function() {\r\n            return -1;\r\n          } \r\n        } \r\n        ///////////------------------------------------\r\n\r\n        //compare precedence\r\n        if (this.precedence) {\r\n          var childPrecedence = child.precedence?child.getPrecedence():99;\r\n          var precedenceCondition = childPrecedence < this.getPrecedence();\r\n        }\r\n        if (this.precedence && precedenceCondition) {\r\n          text.push('(' + child.toString() + ')');\r\n        } else {\r\n          text.push(child.toString());\r\n        }\r\n      } else {\r\n        text.push('?');\r\n      }\r\n    }\r\n  }\r\n\r\n  text = goog.string.trim(text.join(' ')) || '???';\r\n  if (opt_maxLength) {\r\n    // TODO: Improve truncation so that text from this block is given priority.\r\n    // TODO: Handle FieldImage better.\r\n    text = goog.string.truncate(text, opt_maxLength);\r\n  }\r\n  return text;\r\n};\r\n```"}}