Update procedure generators

This commit is contained in:
Rachel Fenichel
2018-01-02 16:27:17 -08:00
parent 24cf1cf7dd
commit 9b091a8f9e
7 changed files with 68 additions and 8 deletions

View File

@@ -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);

View File

@@ -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);
}
}

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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) + ';';
}

View File

@@ -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';
}