mirror of
https://github.com/google/blockly.git
synced 2026-01-07 17:10:11 +01:00
Use Object.values rather than loop
Only for JS, Lua, and PHP. Dart and Python need extra logic to split out import statements. Also use ‘this’ instead of fully qualified names in generators.
This commit is contained in:
@@ -144,37 +144,37 @@ Blockly.Python.init = function(workspace) {
|
||||
/**
|
||||
* Empty loops or conditionals are not allowed in Python.
|
||||
*/
|
||||
Blockly.Python.PASS = this.INDENT + 'pass\n';
|
||||
this.PASS = this.INDENT + 'pass\n';
|
||||
|
||||
if (!Blockly.Python.nameDB_) {
|
||||
Blockly.Python.nameDB_ = new Blockly.Names(Blockly.Python.RESERVED_WORDS_);
|
||||
if (!this.nameDB_) {
|
||||
this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_);
|
||||
} else {
|
||||
Blockly.Python.nameDB_.reset();
|
||||
this.nameDB_.reset();
|
||||
}
|
||||
|
||||
Blockly.Python.nameDB_.setVariableMap(workspace.getVariableMap());
|
||||
this.nameDB_.setVariableMap(workspace.getVariableMap());
|
||||
|
||||
var defvars = [];
|
||||
// Add developer variables (not created or named by the user).
|
||||
var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
|
||||
for (var i = 0; i < devVarList.length; i++) {
|
||||
defvars.push(Blockly.Python.nameDB_.getName(devVarList[i],
|
||||
defvars.push(this.nameDB_.getName(devVarList[i],
|
||||
Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ' = None');
|
||||
}
|
||||
|
||||
// Add user variables, but only ones that are being used.
|
||||
var variables = Blockly.Variables.allUsedVarModels(workspace);
|
||||
for (var i = 0; i < variables.length; i++) {
|
||||
defvars.push(Blockly.Python.nameDB_.getName(variables[i].getId(),
|
||||
defvars.push(this.nameDB_.getName(variables[i].getId(),
|
||||
Blockly.VARIABLE_CATEGORY_NAME) + ' = None');
|
||||
}
|
||||
|
||||
Blockly.Python.definitions_['variables'] = defvars.join('\n');
|
||||
this.definitions_['variables'] = defvars.join('\n');
|
||||
this.isInitialized = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Prepend the generated code with the variable definitions.
|
||||
* Prepend the generated code with import statements and variable definitions.
|
||||
* @param {string} code Generated code.
|
||||
* @return {string} Completed code.
|
||||
*/
|
||||
@@ -182,8 +182,8 @@ Blockly.Python.finish = function(code) {
|
||||
// Convert the definitions dictionary into a list.
|
||||
var imports = [];
|
||||
var definitions = [];
|
||||
for (var name in Blockly.Python.definitions_) {
|
||||
var def = Blockly.Python.definitions_[name];
|
||||
for (var name in this.definitions_) {
|
||||
var def = this.definitions_[name];
|
||||
if (def.match(/^(from\s+\S+\s+)?import\s+\S+/)) {
|
||||
imports.push(def);
|
||||
} else {
|
||||
@@ -194,7 +194,7 @@ Blockly.Python.finish = function(code) {
|
||||
code = Object.getPrototypeOf(this).finish.call(this, code);
|
||||
this.isInitialized = false;
|
||||
|
||||
Blockly.Python.nameDB_.reset();
|
||||
this.nameDB_.reset();
|
||||
var allDefs = imports.join('\n') + '\n\n' + definitions.join('\n\n');
|
||||
return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code;
|
||||
};
|
||||
@@ -240,7 +240,7 @@ Blockly.Python.quote_ = function(string) {
|
||||
* @protected
|
||||
*/
|
||||
Blockly.Python.multiline_quote_ = function(string) {
|
||||
var lines = string.split(/\n/g).map(Blockly.Python.quote_);
|
||||
var lines = string.split(/\n/g).map(this.quote_);
|
||||
// Join with the following, plus a newline:
|
||||
// + '\n' +
|
||||
return lines.join(' + \'\\n\' + \n');
|
||||
@@ -263,9 +263,8 @@ Blockly.Python.scrub_ = function(block, code, opt_thisOnly) {
|
||||
// Collect comment for this block.
|
||||
var comment = block.getCommentText();
|
||||
if (comment) {
|
||||
comment = Blockly.utils.string.wrap(comment,
|
||||
Blockly.Python.COMMENT_WRAP - 3);
|
||||
commentCode += Blockly.Python.prefixLines(comment + '\n', '# ');
|
||||
comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3);
|
||||
commentCode += this.prefixLines(comment + '\n', '# ');
|
||||
}
|
||||
// Collect comments for all value arguments.
|
||||
// Don't collect comments for nested statements.
|
||||
@@ -273,16 +272,16 @@ Blockly.Python.scrub_ = function(block, code, opt_thisOnly) {
|
||||
if (block.inputList[i].type == Blockly.inputTypes.VALUE) {
|
||||
var childBlock = block.inputList[i].connection.targetBlock();
|
||||
if (childBlock) {
|
||||
comment = Blockly.Python.allNestedComments(childBlock);
|
||||
comment = this.allNestedComments(childBlock);
|
||||
if (comment) {
|
||||
commentCode += Blockly.Python.prefixLines(comment, '# ');
|
||||
commentCode += this.prefixLines(comment, '# ');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
|
||||
var nextCode = opt_thisOnly ? '' : Blockly.Python.blockToCode(nextBlock);
|
||||
var nextCode = opt_thisOnly ? '' : this.blockToCode(nextBlock);
|
||||
return commentCode + code + nextCode;
|
||||
};
|
||||
|
||||
@@ -301,9 +300,8 @@ Blockly.Python.getAdjustedInt = function(block, atId, opt_delta, opt_negate) {
|
||||
delta--;
|
||||
}
|
||||
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
|
||||
var atOrder = delta ? Blockly.Python.ORDER_ADDITIVE :
|
||||
Blockly.Python.ORDER_NONE;
|
||||
var at = Blockly.Python.valueToCode(block, atId, atOrder) || defaultAtIndex;
|
||||
var atOrder = delta ? this.ORDER_ADDITIVE : this.ORDER_NONE;
|
||||
var at = this.valueToCode(block, atId, atOrder) || defaultAtIndex;
|
||||
|
||||
if (Blockly.isNumber(at)) {
|
||||
// If the index is a naked number, adjust it right now.
|
||||
|
||||
Reference in New Issue
Block a user