From 315a5fc7623fd1dcdec44e7e6ff5abf52dad5649 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Mon, 12 Jan 2015 14:57:00 -0800 Subject: [PATCH] Remove global workspace references from variable and procedure utilities. --- blockly_compressed.js | 23 +++++++++--------- core/field_variable.js | 43 ++++++++++++++++++++++++++------- core/flyout.js | 1 + core/procedures.js | 9 +++---- core/variables.js | 51 ++++++++++++++++++---------------------- generators/dart.js | 7 +++--- generators/javascript.js | 7 +++--- generators/python.js | 7 +++--- 8 files changed, 85 insertions(+), 63 deletions(-) diff --git a/blockly_compressed.js b/blockly_compressed.js index ab5123a19..a937ac379 100644 --- a/blockly_compressed.js +++ b/blockly_compressed.js @@ -1133,15 +1133,16 @@ Blockly.FieldImage.prototype.init=function(a){if(!this.sourceBlock_){this.source a=this.rectElement_||this.imageElement_;a.tooltip=this.sourceBlock_;Blockly.Tooltip.bindMouseEvents(a)}};Blockly.FieldImage.prototype.dispose=function(){goog.dom.removeNode(this.fieldGroup_);this.rectElement_=this.imageElement_=this.fieldGroup_=null};Blockly.FieldImage.prototype.setTooltip=function(a){(this.rectElement_||this.imageElement_).tooltip=a};Blockly.FieldImage.prototype.getValue=function(){return this.src_}; Blockly.FieldImage.prototype.setValue=function(a){null!==a&&(this.src_=a,this.imageElement_&&this.imageElement_.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",goog.isString(a)?a:""))};Blockly.FieldImage.prototype.setText=function(a){null!==a&&(this.text_=a)};Blockly.FieldImage.prototype.render_=function(){}; // Copyright 2012 Google Inc. Apache License 2.0 -Blockly.Variables={};Blockly.Variables.NAME_TYPE="VARIABLE";Blockly.Variables.allVariables=function(a){a=a||Blockly.mainWorkspace;var b;if(a.getDescendants)b=a.getDescendants();else if(a.getAllBlocks)b=a.getAllBlocks();else throw"Not Block or Workspace: "+a;a=Object.create(null);for(var c=0;cd?1:cd?1:c>} Pair of arrays, the * first contains procedures without return variables, the second with. * Each procedure is defined by a three-element list of name, parameter * list, and return value boolean. */ -Blockly.Procedures.allProcedures = function() { - var blocks = Blockly.mainWorkspace.getAllBlocks(); +Blockly.Procedures.allProcedures = function(root) { + var blocks = root.getAllBlocks(); var proceduresReturn = []; var proceduresNoReturn = []; for (var x = 0; x < blocks.length; x++) { @@ -207,7 +208,7 @@ Blockly.Procedures.flyoutCategory = function(blocks, gaps, margin, workspace) { } } - var tuple = Blockly.Procedures.allProcedures(); + var tuple = Blockly.Procedures.allProcedures(workspace.targetWorkspace); populateProcedures(tuple[0], 'procedures_callnoreturn'); populateProcedures(tuple[1], 'procedures_callreturn'); }; diff --git a/core/variables.js b/core/variables.js index 4cd8e95e0..84922868d 100644 --- a/core/variables.js +++ b/core/variables.js @@ -38,12 +38,10 @@ Blockly.Variables.NAME_TYPE = 'VARIABLE'; /** * Find all user-created variables. - * @param {Blockly.Block|Blockly.Workspace|undefined} opt_root Optional root - * block or workspace. Defaults to main workspace. + * @param {!Blockly.Block|!Blockly.Workspace} root Root block or workspace. * @return {!Array.} Array of variable names. */ -Blockly.Variables.allVariables = function(opt_root) { - var root = opt_root || Blockly.mainWorkspace; +Blockly.Variables.allVariables = function(root) { var blocks; if (root.getDescendants) { // Root is Block. @@ -81,11 +79,9 @@ Blockly.Variables.allVariables = function(opt_root) { * Find all instances of the specified variable and rename them. * @param {string} oldName Variable to rename. * @param {string} newName New variable name. - * @param {Blockly.Workspace=} opt_workspace Workspace rename variables in. - * Defaults to main workspace. + * @param {!Blockly.Workspace} workspace Workspace rename variables in. */ -Blockly.Variables.renameVariable = function(oldName, newName, opt_workspace) { - var workspace = opt_workspace || Blockly.mainWorkspace; +Blockly.Variables.renameVariable = function(oldName, newName, workspace) { var blocks = workspace.getAllBlocks(); // Iterate through every block. for (var x = 0; x < blocks.length; x++) { @@ -104,7 +100,7 @@ Blockly.Variables.renameVariable = function(oldName, newName, opt_workspace) { * @param {!Blockly.Workspace} workspace The flyout's workspace. */ Blockly.Variables.flyoutCategory = function(blocks, gaps, margin, workspace) { - var variableList = Blockly.Variables.allVariables(); + var variableList = Blockly.Variables.allVariables(workspace.targetWorkspace); variableList.sort(goog.string.caseInsensitiveCompare); // In addition to the user's variables, we also want to display the default // variable name at the top. We also don't want this duplicated if the @@ -140,40 +136,39 @@ Blockly.Variables.flyoutCategory = function(blocks, gaps, margin, workspace) { /** * Return a new variable name that is not yet being used. This will try to * generate single letter variable names in the range 'i' to 'z' to start with. -* If no unique name is located it will try 'i1' to 'z1', then 'i2' to 'z2' etc. +* If no unique name is located it will try 'i' to 'z', 'a' to 'h', +* then 'i2' to 'z2' etc. Skip 'l'. + * @param {!Blockly.Workspace} workspace The workspace to be unique in. * @return {string} New variable name. */ -Blockly.Variables.generateUniqueName = function() { - var variableList = Blockly.Variables.allVariables(); +Blockly.Variables.generateUniqueName = function(workspace) { + var variableList = Blockly.Variables.allVariables(workspace); var newName = ''; if (variableList.length) { - variableList.sort(goog.string.caseInsensitiveCompare); - var nameSuffix = 0, potName = 'i', i = 0, inUse = false; + var nameSuffix = 1; + var letters = 'ijkmnopqrstuvwxyzabcdefgh'; // No 'l'. + var letterIndex = 0; + var potName = letters.charAt(letterIndex); while (!newName) { - i = 0; - inUse = false; - while (i < variableList.length && !inUse) { + var inUse = false; + for (var i = 0; i < variableList.length; i++) { if (variableList[i].toLowerCase() == potName) { // This potential name is already used. inUse = true; + break; } - i++; } if (inUse) { // Try the next potential name. - if (potName[0] === 'z') { - // Reached the end of the character sequence so back to 'a' but with + letterIndex++; + if (letterIndex == letters.length) { + // Reached the end of the character sequence so back to 'i'. // a new suffix. + letterIndex = 0; nameSuffix++; - potName = 'a'; - } else { - potName = String.fromCharCode(potName.charCodeAt(0) + 1); - if (potName[0] == 'l') { - // Avoid using variable 'l' because of ambiguity with '1'. - potName = String.fromCharCode(potName.charCodeAt(0) + 1); - } } - if (nameSuffix > 0) { + potName = letters.charAt(letterIndex); + if (nameSuffix > 1) { potName += nameSuffix; } } else { diff --git a/generators/dart.js b/generators/dart.js index 01311efc9..0bcc4a721 100644 --- a/generators/dart.js +++ b/generators/dart.js @@ -73,10 +73,9 @@ Blockly.Dart.ORDER_NONE = 99; // (...) /** * Initialise the database of variable names. - * @param {Blockly.Workspace=} opt_workspace Workspace to generate code from. - * Defaults to main workspace. + * @param {!Blockly.Workspace} workspace Workspace to generate code from. */ -Blockly.Dart.init = function(opt_workspace) { +Blockly.Dart.init = function(workspace) { // Create a dictionary of definitions to be printed before the code. Blockly.Dart.definitions_ = Object.create(null); // Create a dictionary mapping desired function names in definitions_ @@ -91,7 +90,7 @@ Blockly.Dart.init = function(opt_workspace) { } var defvars = []; - var variables = Blockly.Variables.allVariables(opt_workspace); + var variables = Blockly.Variables.allVariables(workspace); for (var x = 0; x < variables.length; x++) { defvars[x] = 'var ' + Blockly.Dart.variableDB_.getName(variables[x], diff --git a/generators/javascript.js b/generators/javascript.js index 12b0c5926..76f2d87ba 100644 --- a/generators/javascript.js +++ b/generators/javascript.js @@ -105,10 +105,9 @@ Blockly.JavaScript.ORDER_NONE = 99; // (...) /** * Initialise the database of variable names. - * @param {Blockly.Workspace=} opt_workspace Workspace to generate code from. - * Defaults to main workspace. + * @param {!Blockly.Workspace} workspace Workspace to generate code from. */ -Blockly.JavaScript.init = function(opt_workspace) { +Blockly.JavaScript.init = function(workspace) { // Create a dictionary of definitions to be printed before the code. Blockly.JavaScript.definitions_ = Object.create(null); // Create a dictionary mapping desired function names in definitions_ @@ -123,7 +122,7 @@ Blockly.JavaScript.init = function(opt_workspace) { } var defvars = []; - var variables = Blockly.Variables.allVariables(opt_workspace); + var variables = Blockly.Variables.allVariables(workspace); for (var x = 0; x < variables.length; x++) { defvars[x] = 'var ' + Blockly.JavaScript.variableDB_.getName(variables[x], diff --git a/generators/python.js b/generators/python.js index ba2169162..dbefed7ea 100644 --- a/generators/python.js +++ b/generators/python.js @@ -81,10 +81,9 @@ Blockly.Python.ORDER_NONE = 99; // (...) /** * Initialise the database of variable names. - * @param {Blockly.Workspace=} opt_workspace Workspace to generate code from. - * Defaults to main workspace. + * @param {!Blockly.Workspace} workspace Workspace to generate code from. */ -Blockly.Python.init = function(opt_workspace) { +Blockly.Python.init = function(workspace) { // Create a dictionary of definitions to be printed before the code. Blockly.Python.definitions_ = Object.create(null); // Create a dictionary mapping desired function names in definitions_ @@ -99,7 +98,7 @@ Blockly.Python.init = function(opt_workspace) { } var defvars = []; - var variables = Blockly.Variables.allVariables(opt_workspace); + var variables = Blockly.Variables.allVariables(workspace); for (var x = 0; x < variables.length; x++) { defvars[x] = Blockly.Python.variableDB_.getName(variables[x], Blockly.Variables.NAME_TYPE) + ' = None';