Skip to content

Commit

Permalink
Merge pull request #2 from Wowfunhappy/return
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin authored Jul 13, 2023
2 parents 9e6acb5 + 3e38f3e commit 8fa219d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
3 changes: 2 additions & 1 deletion blocks_vertical/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ Blockly.ScratchBlocks.ProcedureUtils.updateDisplay_ = function() {
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
} else {
this.setOutput(true, null);
if (this.getReturn() === Blockly.PROCEDURES_CALL_TYPE_BOOLEAN) {
this.setOutput(true, null);
this.setOutputShape(Blockly.OUTPUT_SHAPE_HEXAGONAL);
} else {
this.setOutput(true, 'Number');
this.setOutputShape(Blockly.OUTPUT_SHAPE_ROUND);
}
}
Expand Down
6 changes: 2 additions & 4 deletions core/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ Blockly.Connection.prototype.connect_ = function(childConnection) {

if (
Blockly.Events.isEnabled() &&
!childBlock.isInsertionMarker() &&
Blockly.Procedures.blockContainsReturn(childBlock)
!childBlock.isInsertionMarker()
) {
childBlock.workspace.procedureReturnsChanged();
}
Expand Down Expand Up @@ -599,8 +598,7 @@ Blockly.Connection.prototype.disconnectInternal_ = function(parentBlock,

if (
Blockly.Events.isEnabled() &&
!childBlock.isInsertionMarker() &&
Blockly.Procedures.blockContainsReturn(childBlock)
!childBlock.isInsertionMarker()
) {
childBlock.workspace.procedureReturnsChanged();
}
Expand Down
28 changes: 20 additions & 8 deletions core/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,10 @@ Blockly.Procedures.flyoutCategory = function(workspace) {
for (var i = 0; i < mutations.length; i++) {
var mutation = mutations[i].cloneNode(false);
var procCode = mutation.getAttribute('proccode');
if (Blockly.Procedures.procedureContainsReturn(procCode, workspace)) {
if (Blockly.Procedures.procedureContainsReturnType(procCode, workspace) === Blockly.PROCEDURES_CALL_TYPE_REPORTER) {
mutation.setAttribute('return', Blockly.PROCEDURES_CALL_TYPE_REPORTER);
} else if (Blockly.Procedures.procedureContainsReturnType(procCode, workspace) === Blockly.PROCEDURES_CALL_TYPE_BOOLEAN) {
mutation.setAttribute('return', Blockly.PROCEDURES_CALL_TYPE_BOOLEAN);
}
// <block type="procedures_call">
// <mutation ...></mutation>
Expand Down Expand Up @@ -637,27 +639,37 @@ Blockly.Procedures.deleteProcedureDefCallback = function(procCode,
/**
* @param {string} procCode The procedure code
* @param {Blockly.Workspace} workspace The workspace
* @returns {boolean} True if the procedure contains a return block.
* @returns The type of the return block, or false if no define block
*/
Blockly.Procedures.procedureContainsReturn = function(procCode, workspace) {
Blockly.Procedures.procedureContainsReturnType = function(procCode, workspace) {
var defineBlock = Blockly.Procedures.getDefineBlock(procCode, workspace);
if (!defineBlock) {
return false;
}
return Blockly.Procedures.blockContainsReturn(defineBlock);
return Blockly.Procedures.blockContainsReturnType(defineBlock);
};

/**
* @param {Blockly.Block} block The block
* @returns {boolean} True if the block contains a return block.
* @returns The type of the return block
*/
Blockly.Procedures.blockContainsReturn = function(block) {
Blockly.Procedures.blockContainsReturnType = function(block) {
/** @type {Blockly.Block[]} */
var hasSeenBooleanReturn = false;
var descendants = block.getDescendants();
for (var i = 0; i < descendants.length; i++) {
if (descendants[i].type === Blockly.PROCEDURES_RETURN_BLOCK_TYPE) {
return true;
if (i+1 < descendants.length && descendants[i+1].outputShape_ === Blockly.OUTPUT_SHAPE_HEXAGONAL) {
//keep searching, because there may be other, non-boolean returns in this function definition.
hasSeenBooleanReturn = true;
} else {
return Blockly.PROCEDURES_CALL_TYPE_REPORTER;
}
}
}
return false;
if (hasSeenBooleanReturn) {
return Blockly.PROCEDURES_CALL_TYPE_BOOLEAN;
} else {
return Blockly.PROCEDURES_CALL_TYPE_STATEMENT;
}
};
10 changes: 4 additions & 6 deletions core/workspace_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,12 +714,10 @@ Blockly.WorkspaceSvg.prototype.processProcedureReturnsChanged = function() {
var block = topBlocks[i];
if (!block.getNextBlock() && block.type === Blockly.PROCEDURES_CALL_BLOCK_TYPE) {
var procCode = block.getProcCode();
var actuallyReturns = Blockly.Procedures.procedureContainsReturn(procCode, this);

if (actuallyReturns && block.getReturn() === Blockly.PROCEDURES_CALL_TYPE_STATEMENT) {
Blockly.Procedures.changeReturnType(block, Blockly.PROCEDURES_CALL_TYPE_REPORTER);
} else if (!actuallyReturns && block.getReturn() !== Blockly.PROCEDURES_CALL_TYPE_STATEMENT) {
Blockly.Procedures.changeReturnType(block, Blockly.PROCEDURES_CALL_TYPE_STATEMENT);
var actuallyReturns = Blockly.Procedures.procedureContainsReturnType(procCode, this);

if (actuallyReturns !== block.getReturn()) {
Blockly.Procedures.changeReturnType(block, actuallyReturns);
}
}
}
Expand Down

0 comments on commit 8fa219d

Please sign in to comment.