From 9b091a8f9eac7906cd8b1b13909116754570dc8e Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Tue, 2 Jan 2018 16:27:17 -0800 Subject: [PATCH] Update procedure generators --- blocks/procedures.js | 60 ++++++++++++++++++++++++++++++++++++++++ core/workspace.js | 2 ++ core/xml.js | 4 +-- generators/dart.js | 2 +- generators/javascript.js | 2 +- generators/php.js | 4 +-- generators/python.js | 2 +- 7 files changed, 68 insertions(+), 8 deletions(-) diff --git a/blocks/procedures.js b/blocks/procedures.js index a6800f934..b53c62ae4 100644 --- a/blocks/procedures.js +++ b/blocks/procedures.js @@ -309,6 +309,49 @@ Blockly.Blocks['procedures_defnoreturn'] = { } } }, + /** + * Notification that a variable is renaming. + * If the name matches one of this block's variables, rename it. + * @param {string} oldId ID of variable to rename. + * @param {string} newId ID of new variable. May be the same as oldId, but + * with an updated name. Guaranteed to be the same type as the old + * variable. + * @this Blockly.Block + */ + renameVarById: function(oldId, newId) { + var oldVariable = this.workspace.getVariableById(oldId); + if (oldVariable.type != '') { + // Procedure arguments always have the empty type. + return; + } + var oldName = oldVariable.name; + var newVar = this.workspace.getVariableById(newId); + if (!newVar) { + return; + } + var newName = newVar.name; + + var change = false; + for (var i = 0; i < this.arguments_.length; i++) { + if (Blockly.Names.equals(oldName, this.arguments_[i])) { + this.arguments_[i] = newName; + change = true; + } + } + if (change) { + this.updateParams_(); + // Update the mutator's variables if the mutator is open. + if (this.mutator.isVisible()) { + var blocks = this.mutator.workspace_.getAllBlocks(); + for (var i = 0, block; block = blocks[i]; i++) { + if (block.type == 'procedures_mutatorarg' && + Blockly.Names.equals(oldName, block.getFieldValue('NAME'))) { + block.setFieldValue(newName, 'NAME'); + } + } + } + } + }, /** * Add custom menu options to this block's context menu. * @param {!Array} options List of menu options to add to. @@ -701,6 +744,23 @@ Blockly.Blocks['procedures_callnoreturn'] = { */ renameVar: function(oldName, newName) { for (var i = 0; i < this.arguments_.length; i++) { + // TODO: Fix. This will rename the wrong variables. + if (Blockly.Names.equals(oldName, this.arguments_[i])) { + this.arguments_[i] = newName; + this.getField('ARGNAME' + i).setValue(newName); + } + } + }, + /** + * Notification that a variable is renaming. + * If the name matches one of this block's variables, rename it. + * @param {string} oldName Previous name of variable. + * @param {string} newName Renamed variable. + * @this Blockly.Block + */ + renameVarById: function(oldId, newId) { + for (var i = 0; i < this.arguments_.length; i++) { + // TODO: Fix. This will rename the wrong variables. if (Blockly.Names.equals(oldName, this.arguments_[i])) { this.arguments_[i] = newName; this.getField('ARGNAME' + i).setValue(newName); diff --git a/core/workspace.js b/core/workspace.js index 29dfb860d..e0bf956b2 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -144,6 +144,8 @@ Blockly.Workspace.prototype.addTopBlock = function(block) { var variableNames = Blockly.Variables.allUsedVariables(block); for (var i = 0, name; name = variableNames[i]; i++) { if (!this.getVariable(name)) { + // TODO (fenichel): Is this still necessary? Is allUsedVariables still + // necessary? this.createVariable(name); } } diff --git a/core/xml.js b/core/xml.js index 347e0eb8a..1faaa7a3d 100644 --- a/core/xml.js +++ b/core/xml.js @@ -728,8 +728,8 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) { 'Shadow block not allowed non-shadow child.'); } // Ensure this block doesn't have any variable inputs. - goog.asserts.assert(block.getVars().length == 0, - 'Shadow blocks cannot have variable fields.'); + goog.asserts.assert(block.getVarModels().length == 0, + 'Shadow blocks cannot have variable references.'); block.setShadow(true); } return block; diff --git a/generators/dart.js b/generators/dart.js index 1d3c93b75..e89c160c8 100644 --- a/generators/dart.js +++ b/generators/dart.js @@ -108,7 +108,7 @@ Blockly.Dart.init = function(workspace) { // Add user variables. var variables = workspace.getAllVariables(); for (var i = 0; i < variables.length; i++) { - defvars[i] = Blockly.Dart.variableDB_.getName(variables[i].name, + defvars[i] = Blockly.Dart.variableDB_.getName(variables[i].getId(), Blockly.Variables.NAME_TYPE); } diff --git a/generators/javascript.js b/generators/javascript.js index 864ae50c3..c7396591b 100644 --- a/generators/javascript.js +++ b/generators/javascript.js @@ -158,7 +158,7 @@ Blockly.JavaScript.init = function(workspace) { // Add user variables. var variables = workspace.getAllVariables(); for (var i = 0; i < variables.length; i++) { - defvars[i] = Blockly.JavaScript.variableDB_.getName(variables[i].name, + defvars[i] = Blockly.JavaScript.variableDB_.getName(variables[i].getId(), Blockly.Variables.NAME_TYPE); } diff --git a/generators/php.js b/generators/php.js index 6dc81ba62..3f999528e 100644 --- a/generators/php.js +++ b/generators/php.js @@ -152,11 +152,9 @@ Blockly.PHP.init = function(workspace) { Blockly.PHP.variableDB_.setVariableMap(workspace.getVariableMap()); var defvars = []; - var varName; var variables = workspace.getAllVariables(); for (var i = 0, variable; variable = variables[i]; i++) { - varName = variable.name; - defvars[i] = Blockly.PHP.variableDB_.getName(varName, + defvars[i] = Blockly.PHP.variableDB_.getName(variable.getId(), Blockly.Variables.NAME_TYPE) + ';'; } diff --git a/generators/python.js b/generators/python.js index ab927d13b..4b88a6fad 100644 --- a/generators/python.js +++ b/generators/python.js @@ -165,7 +165,7 @@ Blockly.Python.init = function(workspace) { var defvars = []; var variables = workspace.getAllVariables(); for (var i = 0; i < variables.length; i++) { - defvars[i] = Blockly.Python.variableDB_.getName(variables[i].name, + defvars[i] = Blockly.Python.variableDB_.getName(variables[i].getId(), Blockly.Variables.NAME_TYPE) + ' = None'; }