Skip to content

Commit

Permalink
Get procedure renaming to work correctly dynamically
Browse files Browse the repository at this point in the history
  i.e., while typing in the name field.
  • Loading branch information
mark-friedman committed Sep 10, 2021
1 parent ee06205 commit 12602ca
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion block-lexical-variables/src/field_procedure.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ Blockly.AIProcedure.removeProcedureValues = function(name, workspace) {
*/
Blockly.AIProcedure.renameProcedure = function (newName) {
// this is bound to field_textinput object
var oldName = this.oldName_ || this.text_;
var oldName = this.oldName_ || this.getValue();
const originalNewName = newName;

// [lyn, 10/27/13] now check legality of identifiers
newName = Blockly.LexicalVariable.makeLegalIdentifier(newName);
Expand All @@ -151,6 +152,9 @@ Blockly.AIProcedure.renameProcedure = function (newName) {
var procBlocks = Blockly.AIProcedure.getAllProcedureDeclarationBlocksExcept(this.sourceBlock_);
var procNames = procBlocks.map(function (decl) { return decl.getFieldValue('NAME'); });
newName = Blockly.FieldLexicalVariable.nameNotIn(newName, procNames);
if (newName !== originalNewName) {
this.doValueUpdate_(newName);
}
// Rename any callers.
var blocks = this.sourceBlock_.workspace.getAllBlocks();
for (var x = 0; x < blocks.length; x++) {
Expand Down
2 changes: 2 additions & 0 deletions block-lexical-variables/src/field_procedurename.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ goog.inherits(AI.Blockly.FieldProcedureName, Blockly.FieldTextInput);
*/
AI.Blockly.FieldProcedureName.prototype.setValue = function(newValue) {
var oldValue = this.getValue();
this.oldName_ = oldValue;
this.doValueUpdate_(newValue);
AI.Blockly.FieldProcedureName.superClass_.setValue.call(this, newValue);
newValue = this.getValue();
if (typeof newValue === 'string' && this.sourceBlock_) {
Expand Down
5 changes: 5 additions & 0 deletions block-lexical-variables/src/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,11 @@ Blockly.Blocks['procedures_callnoreturn'] = {
renameProcedure: function(oldName, newName) {
if (!oldName ||
Blockly.Names.equals(oldName, this.getFieldValue('PROCNAME'))) {
const nameField = this.getField('PROCNAME');
// Force the options menu to get regenerated since we might be getting
// called because our defining procedure got renamed and
// this.setFieldValue() will fail if it's value isn't in the options set
nameField.getOptions();
this.setFieldValue(newName, 'PROCNAME');
}
},
Expand Down

0 comments on commit 12602ca

Please sign in to comment.