Skip to content

Commit

Permalink
Feat: type inference + text operators (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverben authored Jan 30, 2024
1 parent 3ea3d21 commit 21032f7
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 289 deletions.
69 changes: 0 additions & 69 deletions blocks/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,74 +367,6 @@ const WHILE_UNTIL_TOOLTIPS = {
UNTIL: "%{BKY_CONTROLS_WHILEUNTIL_TOOLTIP_UNTIL}",
};

/**
* Adds dynamic type validation for the left and right sides of a logic_compare
* block.
* @mixin
* @augments Block
* @readonly
*/
const LOGIC_COMPARE_ONCHANGE_MIXIN = {
/**
* Called whenever anything on the workspace changes.
* Prevent mismatched types from being compared.
* @param {!AbstractEvent} e Change event.
* @this {Block}
*/
onchange: function (e) {
if (!this.prevBlocks_) {
this.prevBlocks_ = [null, null];
}

const blockA = this.getInputTargetBlock("A");
const blockB = this.getInputTargetBlock("B");
// Disconnect blocks that existed prior to this change if they don't match.
if (
blockA &&
blockB &&
!this.workspace.connectionChecker.doTypeChecks(
blockA.outputConnection,
blockB.outputConnection,
)
) {
// Mismatch between two inputs. Revert the block connections,
// bumping away the newly connected block(s).
Blockly.Events.setGroup(e.group);
const prevA = this.prevBlocks_[0];
if (prevA !== blockA) {
blockA.unplug();
if (prevA && !prevA.isDisposed() && !prevA.isShadow()) {
// The shadow block is automatically replaced during unplug().
this.getInput("A").connection.connect(prevA.outputConnection);
}
}
const prevB = this.prevBlocks_[1];
if (prevB !== blockB) {
blockB.unplug();
if (prevB && !prevB.isDisposed() && !prevB.isShadow()) {
// The shadow block is automatically replaced during unplug().
this.getInput("B").connection.connect(prevB.outputConnection);
}
}
this.bumpNeighbours();
Blockly.Events.setGroup(false);
}
this.prevBlocks_[0] = this.getInputTargetBlock("A");
this.prevBlocks_[1] = this.getInputTargetBlock("B");
},
};

/**
* "logic_compare" extension function. Adds type left and right side type
* checking to "logic_compare" blocks.
* @this {Block}
* @readonly
*/
const LOGIC_COMPARE_EXTENSION = function () {
// Add onchange handler to ensure types are compatible.
this.mixin(LOGIC_COMPARE_ONCHANGE_MIXIN);
};

/**
* Tooltip text, keyed by block OP value. Used by logic_compare and
* logic_operation blocks.
Expand Down Expand Up @@ -741,5 +673,4 @@ export {
CONTROLS_IF_MUTATOR_MIXIN,
CONTROLS_IF_TOOLTIP_EXTENSION,
WHILE_UNTIL_TOOLTIPS,
LOGIC_COMPARE_EXTENSION,
};
2 changes: 1 addition & 1 deletion blocks/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const blocks = [
output: "Boolean",
style: "numbers_blocks",
helpUrl: "%{BKY_LOGIC_COMPARE_HELPURL}",
extensions: ["logic_compare", "logic_op_tooltip"],
extensions: ["logic_op_tooltip"],
},
// Block for logical operations: 'and', 'or'.
{
Expand Down
Loading

0 comments on commit 21032f7

Please sign in to comment.