diff --git a/core/names.js b/core/names.js index 402d99056..674e8214c 100644 --- a/core/names.js +++ b/core/names.js @@ -100,6 +100,30 @@ Blockly.Names.prototype.getNameForUserVariable_ = function(id) { } }; +/** + * Generate names for user variables, but only ones that are being used. + * @param {!Blockly.Workspace} workspace Workspace to generate variables from. + */ +Blockly.Names.prototype.populateVariables = function(workspace) { + var variables = Blockly.Variables.allUsedVarModels(workspace); + for (var i = 0; i < variables.length; i++) { + this.getName(variables[i].getId(), Blockly.VARIABLE_CATEGORY_NAME); + } +}; + +/** + * Generate names for procedures. + * @param {!Blockly.Workspace} workspace Workspace to generate procedures from. + */ +Blockly.Names.prototype.populateProcedures = function(workspace) { + var procedures = Blockly.Procedures.allProcedures(workspace); + // Flatten the return vs no-return procedure lists. + procedures = procedures[0].concat(procedures[1]); + for (var i = 0; i < procedures.length; i++) { + this.getName(procedures[i][0], Blockly.PROCEDURE_CATEGORY_NAME); + } +}; + /** * Convert a Blockly entity name to a legal exportable entity name. * @param {string} name The Blockly entity name (no constraints). diff --git a/generators/dart.js b/generators/dart.js index cc9368a02..9ed2e9876 100644 --- a/generators/dart.js +++ b/generators/dart.js @@ -93,6 +93,8 @@ Blockly.Dart.init = function(workspace) { } this.nameDB_.setVariableMap(workspace.getVariableMap()); + this.nameDB_.populateVariables(workspace); + this.nameDB_.populateProcedures(workspace); var defvars = []; // Add developer variables (not created or named by the user). @@ -109,14 +111,6 @@ Blockly.Dart.init = function(workspace) { Blockly.VARIABLE_CATEGORY_NAME)); } - // Add user procedures. - var procedures = Blockly.Procedures.allProcedures(workspace); - // Flatten the return vs no-return procedure lists. - procedures = procedures[0].concat(procedures[1]); - for (var i = 0; i < procedures.length; i++) { - this.nameDB_.getName(procedures[i][0], Blockly.PROCEDURE_CATEGORY_NAME); - } - // Declare all of the variables. if (defvars.length) { this.definitions_['variables'] = diff --git a/generators/javascript.js b/generators/javascript.js index 122171cf5..0c1cbe7cb 100644 --- a/generators/javascript.js +++ b/generators/javascript.js @@ -136,6 +136,8 @@ Blockly.JavaScript.init = function(workspace) { } this.nameDB_.setVariableMap(workspace.getVariableMap()); + this.nameDB_.populateVariables(workspace); + this.nameDB_.populateProcedures(workspace); var defvars = []; // Add developer variables (not created or named by the user). @@ -152,14 +154,6 @@ Blockly.JavaScript.init = function(workspace) { Blockly.VARIABLE_CATEGORY_NAME)); } - // Add user procedures. - var procedures = Blockly.Procedures.allProcedures(workspace); - // Flatten the return vs no-return procedure lists. - procedures = procedures[0].concat(procedures[1]); - for (var i = 0; i < procedures.length; i++) { - this.nameDB_.getName(procedures[i][0], Blockly.PROCEDURE_CATEGORY_NAME); - } - // Declare all of the variables. if (defvars.length) { this.definitions_['variables'] = 'var ' + defvars.join(', ') + ';'; diff --git a/generators/lua.js b/generators/lua.js index 5033307b1..54877523b 100644 --- a/generators/lua.js +++ b/generators/lua.js @@ -101,14 +101,8 @@ Blockly.Lua.init = function(workspace) { this.nameDB_.reset(); } this.nameDB_.setVariableMap(workspace.getVariableMap()); - - // Add user procedures. - var procedures = Blockly.Procedures.allProcedures(workspace); - // Flatten the return vs no-return procedure lists. - procedures = procedures[0].concat(procedures[1]); - for (var i = 0; i < procedures.length; i++) { - this.nameDB_.getName(procedures[i][0], Blockly.PROCEDURE_CATEGORY_NAME); - } + this.nameDB_.populateVariables(workspace); + this.nameDB_.populateProcedures(workspace); this.isInitialized = true; }; diff --git a/generators/php.js b/generators/php.js index 8c8703f61..f6ff94edd 100644 --- a/generators/php.js +++ b/generators/php.js @@ -140,6 +140,8 @@ Blockly.PHP.init = function(workspace) { } this.nameDB_.setVariableMap(workspace.getVariableMap()); + this.nameDB_.populateVariables(workspace); + this.nameDB_.populateProcedures(workspace); var defvars = []; // Add developer variables (not created or named by the user). @@ -156,14 +158,6 @@ Blockly.PHP.init = function(workspace) { Blockly.VARIABLE_CATEGORY_NAME) + ';'); } - // Add user procedures. - var procedures = Blockly.Procedures.allProcedures(workspace); - // Flatten the return vs no-return procedure lists. - procedures = procedures[0].concat(procedures[1]); - for (var i = 0; i < procedures.length; i++) { - this.nameDB_.getName(procedures[i][0], Blockly.PROCEDURE_CATEGORY_NAME); - } - // Declare all of the variables. this.definitions_['variables'] = defvars.join('\n'); this.isInitialized = true; diff --git a/generators/python.js b/generators/python.js index b8125691a..2c317b75d 100644 --- a/generators/python.js +++ b/generators/python.js @@ -153,6 +153,8 @@ Blockly.Python.init = function(workspace) { } this.nameDB_.setVariableMap(workspace.getVariableMap()); + this.nameDB_.populateVariables(workspace); + this.nameDB_.populateProcedures(workspace); var defvars = []; // Add developer variables (not created or named by the user). @@ -169,14 +171,6 @@ Blockly.Python.init = function(workspace) { Blockly.VARIABLE_CATEGORY_NAME) + ' = None'); } - // Add user procedures. - var procedures = Blockly.Procedures.allProcedures(workspace); - // Flatten the return vs no-return procedure lists. - procedures = procedures[0].concat(procedures[1]); - for (var i = 0; i < procedures.length; i++) { - this.nameDB_.getName(procedures[i][0], Blockly.PROCEDURE_CATEGORY_NAME); - } - this.definitions_['variables'] = defvars.join('\n'); this.isInitialized = true; };