Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: type inference + text operators #43

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading