Factor out procedure population from generators

Also fix bug in Lua generator where variables were not populated in the nameDB (only language with no variable initialization).
This commit is contained in:
Neil Fraser
2021-05-24 11:12:40 -07:00
committed by Neil Fraser
parent 9869269ce2
commit 43b65d2aa1
6 changed files with 34 additions and 40 deletions

View File

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

View File

@@ -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'] =

View File

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

View File

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

View File

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

View File

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