Adds option to generate code for a single block

This commit is contained in:
Abby Schmiedt
2018-11-21 09:00:29 -08:00
parent 9423970755
commit 26a4774b35
6 changed files with 19 additions and 13 deletions

View File

@@ -158,11 +158,12 @@ Blockly.Generator.prototype.allNestedComments = function(block) {
/** /**
* Generate code for the specified block (and attached blocks). * Generate code for the specified block (and attached blocks).
* @param {Blockly.Block} block The block to generate code for. * @param {Blockly.Block} block The block to generate code for.
* @param {boolean} opt_thisOnly True to generate code for only this block.
* @return {string|!Array} For statement blocks, the generated code. * @return {string|!Array} For statement blocks, the generated code.
* For value blocks, an array containing the generated code and an * For value blocks, an array containing the generated code and an
* operator order value. Returns '' if block is null. * operator order value. Returns '' if block is null.
*/ */
Blockly.Generator.prototype.blockToCode = function(block) { Blockly.Generator.prototype.blockToCode = function(block, opt_thisOnly) {
if (!block) { if (!block) {
return ''; return '';
} }
@@ -186,13 +187,13 @@ Blockly.Generator.prototype.blockToCode = function(block) {
if (!block.outputConnection) { if (!block.outputConnection) {
throw TypeError('Expecting string from statement block: ' + block.type); throw TypeError('Expecting string from statement block: ' + block.type);
} }
return [this.scrub_(block, code[0]), code[1]]; return [this.scrub_(block, code[0], opt_thisOnly), code[1]];
} else if (typeof code == 'string') { } else if (typeof code == 'string') {
var id = block.id.replace(/\$/g, '$$$$'); // Issue 251. var id = block.id.replace(/\$/g, '$$$$'); // Issue 251.
if (this.STATEMENT_PREFIX) { if (this.STATEMENT_PREFIX) {
code = this.STATEMENT_PREFIX.replace(/%1/g, '\'' + id + '\'') + code; code = this.STATEMENT_PREFIX.replace(/%1/g, '\'' + id + '\'') + code;
} }
return this.scrub_(block, code); return this.scrub_(block, code, opt_thisOnly);
} else if (code === null) { } else if (code === null) {
// Block has handled code generation itself. // Block has handled code generation itself.
return ''; return '';

View File

@@ -188,10 +188,11 @@ Blockly.Dart.quote_ = function(string) {
* Calls any statements following this block. * Calls any statements following this block.
* @param {!Blockly.Block} block The current block. * @param {!Blockly.Block} block The current block.
* @param {string} code The Dart code created for this block. * @param {string} code The Dart code created for this block.
* @param {boolean} opt_thisOnly True to generate code for only this block.
* @return {string} Dart code with comments and subsequent blocks added. * @return {string} Dart code with comments and subsequent blocks added.
* @private * @private
*/ */
Blockly.Dart.scrub_ = function(block, code) { Blockly.Dart.scrub_ = function(block, code, opt_thisOnly) {
var commentCode = ''; var commentCode = '';
// Only collect comments for blocks that aren't inline. // Only collect comments for blocks that aren't inline.
if (!block.outputConnection || !block.outputConnection.targetConnection) { if (!block.outputConnection || !block.outputConnection.targetConnection) {
@@ -221,7 +222,7 @@ Blockly.Dart.scrub_ = function(block, code) {
} }
} }
var nextBlock = block.nextConnection && block.nextConnection.targetBlock(); var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
var nextCode = Blockly.Dart.blockToCode(nextBlock); var nextCode = opt_thisOnly ? "" : Blockly.Dart.blockToCode(nextBlock);
return commentCode + code + nextCode; return commentCode + code + nextCode;
}; };

View File

@@ -229,10 +229,11 @@ Blockly.JavaScript.quote_ = function(string) {
* Calls any statements following this block. * Calls any statements following this block.
* @param {!Blockly.Block} block The current block. * @param {!Blockly.Block} block The current block.
* @param {string} code The JavaScript code created for this block. * @param {string} code The JavaScript code created for this block.
* @param {boolean} opt_thisOnly True to generate code for only this block.
* @return {string} JavaScript code with comments and subsequent blocks added. * @return {string} JavaScript code with comments and subsequent blocks added.
* @private * @private
*/ */
Blockly.JavaScript.scrub_ = function(block, code) { Blockly.JavaScript.scrub_ = function(block, code, opt_thisOnly) {
var commentCode = ''; var commentCode = '';
// Only collect comments for blocks that aren't inline. // Only collect comments for blocks that aren't inline.
if (!block.outputConnection || !block.outputConnection.targetConnection) { if (!block.outputConnection || !block.outputConnection.targetConnection) {
@@ -264,7 +265,7 @@ Blockly.JavaScript.scrub_ = function(block, code) {
} }
} }
var nextBlock = block.nextConnection && block.nextConnection.targetBlock(); var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
var nextCode = Blockly.JavaScript.blockToCode(nextBlock); var nextCode = opt_thisOnly ? "" : Blockly.JavaScript.blockToCode(nextBlock);
return commentCode + code + nextCode; return commentCode + code + nextCode;
}; };

View File

@@ -162,10 +162,11 @@ Blockly.Lua.quote_ = function(string) {
* Calls any statements following this block. * Calls any statements following this block.
* @param {!Blockly.Block} block The current block. * @param {!Blockly.Block} block The current block.
* @param {string} code The Lua code created for this block. * @param {string} code The Lua code created for this block.
* @param {boolean} opt_thisOnly True to generate code for only this block.
* @return {string} Lua code with comments and subsequent blocks added. * @return {string} Lua code with comments and subsequent blocks added.
* @private * @private
*/ */
Blockly.Lua.scrub_ = function(block, code) { Blockly.Lua.scrub_ = function(block, code, opt_thisOnly) {
var commentCode = ''; var commentCode = '';
// Only collect comments for blocks that aren't inline. // Only collect comments for blocks that aren't inline.
if (!block.outputConnection || !block.outputConnection.targetConnection) { if (!block.outputConnection || !block.outputConnection.targetConnection) {
@@ -190,6 +191,6 @@ Blockly.Lua.scrub_ = function(block, code) {
} }
} }
var nextBlock = block.nextConnection && block.nextConnection.targetBlock(); var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
var nextCode = Blockly.Lua.blockToCode(nextBlock); var nextCode = opt_thisOnly ? "" : Blockly.Lua.blockToCode(nextBlock);
return commentCode + code + nextCode; return commentCode + code + nextCode;
}; };

View File

@@ -218,10 +218,11 @@ Blockly.PHP.quote_ = function(string) {
* Calls any statements following this block. * Calls any statements following this block.
* @param {!Blockly.Block} block The current block. * @param {!Blockly.Block} block The current block.
* @param {string} code The PHP code created for this block. * @param {string} code The PHP code created for this block.
* @param {boolean} opt_thisOnly True to generate code for only this block.
* @return {string} PHP code with comments and subsequent blocks added. * @return {string} PHP code with comments and subsequent blocks added.
* @private * @private
*/ */
Blockly.PHP.scrub_ = function(block, code) { Blockly.PHP.scrub_ = function(block, code, opt_thisOnly) {
var commentCode = ''; var commentCode = '';
// Only collect comments for blocks that aren't inline. // Only collect comments for blocks that aren't inline.
if (!block.outputConnection || !block.outputConnection.targetConnection) { if (!block.outputConnection || !block.outputConnection.targetConnection) {
@@ -246,7 +247,7 @@ Blockly.PHP.scrub_ = function(block, code) {
} }
} }
var nextBlock = block.nextConnection && block.nextConnection.targetBlock(); var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
var nextCode = Blockly.PHP.blockToCode(nextBlock); var nextCode = opt_thisOnly ? "" : Blockly.PHP.blockToCode(nextBlock);
return commentCode + code + nextCode; return commentCode + code + nextCode;
}; };

View File

@@ -244,10 +244,11 @@ Blockly.Python.quote_ = function(string) {
* Calls any statements following this block. * Calls any statements following this block.
* @param {!Blockly.Block} block The current block. * @param {!Blockly.Block} block The current block.
* @param {string} code The Python code created for this block. * @param {string} code The Python code created for this block.
* @param {boolean} opt_thisOnly True to generate code for only this block.
* @return {string} Python code with comments and subsequent blocks added. * @return {string} Python code with comments and subsequent blocks added.
* @private * @private
*/ */
Blockly.Python.scrub_ = function(block, code) { Blockly.Python.scrub_ = function(block, code, opt_thisOnly) {
var commentCode = ''; var commentCode = '';
// Only collect comments for blocks that aren't inline. // Only collect comments for blocks that aren't inline.
if (!block.outputConnection || !block.outputConnection.targetConnection) { if (!block.outputConnection || !block.outputConnection.targetConnection) {
@@ -277,7 +278,7 @@ Blockly.Python.scrub_ = function(block, code) {
} }
} }
var nextBlock = block.nextConnection && block.nextConnection.targetBlock(); var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
var nextCode = Blockly.Python.blockToCode(nextBlock); var nextCode = opt_thisOnly ? "" : Blockly.Python.blockToCode(nextBlock);
return commentCode + code + nextCode; return commentCode + code + nextCode;
}; };