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

chore: moves the public loop mixin into its own file #5730

Merged
merged 6 commits into from
Nov 24, 2021
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
71 changes: 1 addition & 70 deletions blocks/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ goog.require('Blockly.FieldLabel');
goog.require('Blockly.FieldNumber');
goog.require('Blockly.FieldVariable');
goog.require('Blockly.Warning');
goog.require('Blockly.loopMixin');


/**
Expand Down Expand Up @@ -272,73 +273,3 @@ Blockly.Extensions.register('controls_for_tooltip',
Blockly.Extensions.register('controls_forEach_tooltip',
Blockly.Extensions.buildTooltipWithFieldText(
'%{BKY_CONTROLS_FOREACH_TOOLTIP}', 'VAR'));

/**
* This mixin adds a check to make sure the 'controls_flow_statements' block
* is contained in a loop. Otherwise a warning is added to the block.
* @mixin
* @augments Blockly.Block
* @package
* @readonly
*/
Blockly.Constants.Loops.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN = {
/**
* List of block types that are loops and thus do not need warnings.
* To add a new loop type add this to your code:
* Blockly.Constants.Loops.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.LOOP_TYPES.push('custom_loop');
*/
LOOP_TYPES: [
'controls_repeat',
'controls_repeat_ext',
'controls_forEach',
'controls_for',
'controls_whileUntil',
],

/**
* Is the given block enclosed (at any level) by a loop?
* @param {!Blockly.Block} block Current block.
* @return {Blockly.Block} The nearest surrounding loop, or null if none.
*/
getSurroundLoop: function(block) {
// Is the block nested in a loop?
do {
if (Blockly.Constants.Loops.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.LOOP_TYPES
.indexOf(block.type) !== -1) {
return block;
}
block = block.getSurroundParent();
} while (block);
return null;
},

/**
* Called whenever anything on the workspace changes.
* Add warning if this flow block is not nested inside a loop.
* @param {!Blockly.Events.Abstract} e Change event.
* @this {Blockly.Block}
*/
onchange: function(e) {
// Don't change state if:
// * It's at the start of a drag.
// * It's not a move event.
if (!this.workspace.isDragging || this.workspace.isDragging() ||
e.type !== Blockly.Events.BLOCK_MOVE) {
return;
}
const enabled = Blockly.Constants.Loops.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN
.getSurroundLoop(this);
this.setWarningText(enabled ? null :
Blockly.Msg['CONTROLS_FLOW_STATEMENTS_WARNING']);
if (!this.isInFlyout) {
const group = Blockly.Events.getGroup();
// Makes it so the move and the disable event get undone together.
Blockly.Events.setGroup(e.group);
this.setEnabled(enabled);
Blockly.Events.setGroup(group);
}
},
};

Blockly.Extensions.registerMixin('controls_flow_in_loop_check',
Blockly.Constants.Loops.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN);
2 changes: 2 additions & 0 deletions core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const dialog = goog.require('Blockly.dialog');
const fieldRegistry = goog.require('Blockly.fieldRegistry');
const geras = goog.require('Blockly.geras');
const internalConstants = goog.require('Blockly.internalConstants');
const loopMixin = goog.require('Blockly.loopMixin');
const minimalist = goog.require('Blockly.minimalist');
const registry = goog.require('Blockly.registry');
const svgMath = goog.require('Blockly.utils.svgMath');
Expand Down Expand Up @@ -682,6 +683,7 @@ exports.fieldRegistry = fieldRegistry;
exports.geras = geras;
exports.inject = inject;
exports.inputTypes = inputTypes;
exports.loopMixin = loopMixin;
exports.minimalist = minimalist;
exports.registry = registry;
exports.thrasos = thrasos;
Expand Down
96 changes: 96 additions & 0 deletions core/loop_mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview The mixin that holds logic for setting a warning on a block if
* it is not inside a loop.
*/
'use strict';

goog.module('Blockly.loopMixin');

/* eslint-disable-next-line no-unused-vars */
const Abstract = goog.requireType('Blockly.Events.Abstract');
const Events = goog.require('Blockly.Events');
const Extensions = goog.require('Blockly.Extensions');
const Msg = goog.require('Blockly.Msg');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');


/**
* This mixin adds a check to make sure the 'controls_flow_statements' block
* is contained in a loop. Otherwise a warning is added to the block.
* @mixin
* @augments Block
* @readonly
*/
const CONTROL_FLOW_IN_LOOP_CHECK_MIXIN = {
/**
* List of block types that are loops and thus do not need warnings.
* To add a new loop type add this to your code:
* Blockly.loopMixin.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.LOOP_TYPES.push('custom_loop');
*
* Please leave a comment on this github issue
* https://github.com/mit-cml/appinventor-sources/issues/2032 if you end up
* needing to use this.
*/
LOOP_TYPES: [
'controls_repeat',
'controls_repeat_ext',
'controls_forEach',
'controls_for',
'controls_whileUntil',
Comment on lines +42 to +46
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit that I probably did not fully grok our recent discussion about this change—it was getting rather late here—but looking at this resulting PR I am completely perplexed about why this code has been moved to core/, since, for example, these block types are totally meaningless without the definitions given in blocks/.

Since modules in generators/ require modules in blocks/, and since it would be easy to enable external code to import {CONTROL_FLOW_IN_LOOP_CHECK_MIXIN} from 'blockly/blocks'; I don't really know why it wouldn't be better to leave this code in blocks/loops.js (but move it to the Blockly.blocks.loops module, and reexport from Blockly.loops.all)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea that some blocks can contain break or continue, and others can't, seemed like a core idea that had gotten pushed out into the blocks. Although perhaps the controls block definition file should push each of these names onto the list as it defines the block, rather than having it hardcoded from the start.

],

/**
* Is the given block enclosed (at any level) by a loop?
* @param {!Block} block Current block.
* @return {?Block} The nearest surrounding loop, or null if none.
*/
getSurroundLoop: function(block) {
let searchBlock = block;
// Is the block nested in a loop?
do {
if (CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.LOOP_TYPES.indexOf(
searchBlock.type) !== -1) {
return searchBlock;
}
searchBlock = searchBlock.getSurroundParent();
} while (searchBlock);
return null;
},

/**
* Called whenever anything on the workspace changes.
* Add warning if this flow block is not nested inside a loop.
* @param {!Abstract} e Change event.
* @this {Block}
*/
onchange: function(e) {
// Don't change state if:
// * It's at the start of a drag.
// * It's not a move event.
if (!this.workspace.isDragging || this.workspace.isDragging() ||
e.type !== Events.BLOCK_MOVE) {
return;
}
const enabled = !!CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(this);
this.setWarningText(
enabled ? null : Msg['CONTROLS_FLOW_STATEMENTS_WARNING']);
if (!this.isInFlyout) {
const group = Events.getGroup();
// Makes it so the move and the disable event get undone together.
Events.setGroup(e.group);
this.setEnabled(enabled);
Events.setGroup(group);
}
},
};
exports.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN = CONTROL_FLOW_IN_LOOP_CHECK_MIXIN;

Extensions.registerMixin(
'controls_flow_in_loop_check', CONTROL_FLOW_IN_LOOP_CHECK_MIXIN);
1 change: 1 addition & 0 deletions externs/block-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ goog.provide('Blockly.FieldTextInput');
goog.provide('Blockly.FieldVariable');
goog.provide('Blockly.Mutator');
goog.provide('Blockly.Warning');
goog.provide('Blockly.loopMixin');

var Blockly;
1 change: 1 addition & 0 deletions externs/generator-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
goog.provide('Blockly');
goog.provide('Blockly.Generator');
goog.provide('Blockly.inputTypes');
goog.provide('Blockly.loopMixin');
goog.provide('Blockly.utils.global');
goog.provide('Blockly.utils.object');
goog.provide('Blockly.utils.string');
Expand Down
2 changes: 1 addition & 1 deletion generators/dart/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Blockly.Dart['controls_flow_statements'] = function(block) {
xfix += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_SUFFIX, block);
}
if (Blockly.Dart.STATEMENT_PREFIX) {
const loop = Blockly.Constants.Loops
const loop = Blockly.loopMixin
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
Expand Down
3 changes: 2 additions & 1 deletion generators/javascript/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
goog.provide('Blockly.JavaScript.loops');

goog.require('Blockly.JavaScript');
goog.require('Blockly.loopMixin');


Blockly.JavaScript['controls_repeat_ext'] = function(block) {
Expand Down Expand Up @@ -164,7 +165,7 @@ Blockly.JavaScript['controls_flow_statements'] = function(block) {
block);
}
if (Blockly.JavaScript.STATEMENT_PREFIX) {
const loop = Blockly.Constants.Loops
const loop = Blockly.loopMixin
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
Expand Down
2 changes: 1 addition & 1 deletion generators/lua/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Blockly.Lua['controls_flow_statements'] = function(block) {
xfix += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block);
}
if (Blockly.Lua.STATEMENT_PREFIX) {
const loop = Blockly.Constants.Loops
const loop = Blockly.loopMixin
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
Expand Down
2 changes: 1 addition & 1 deletion generators/php/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Blockly.PHP['controls_flow_statements'] = function(block) {
xfix += Blockly.PHP.injectId(Blockly.PHP.STATEMENT_SUFFIX, block);
}
if (Blockly.PHP.STATEMENT_PREFIX) {
const loop = Blockly.Constants.Loops
const loop = Blockly.loopMixin
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
Expand Down
2 changes: 1 addition & 1 deletion generators/python/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Blockly.Python['controls_flow_statements'] = function(block) {
xfix += Blockly.Python.injectId(Blockly.Python.STATEMENT_SUFFIX, block);
}
if (Blockly.Python.STATEMENT_PREFIX) {
const loop = Blockly.Constants.Loops
const loop = Blockly.loopMixin
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
Expand Down
5 changes: 5 additions & 0 deletions scripts/migration/renamings.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ const renamings = {
{module: 'Blockly.Extensions', export: 'runAfterPageLoad'},
},
},
'Blockly.Constants.Loops': {
exports: {
CONTROL_FLOW_IN_LOOP_CHECK_MIXIN: {module: 'Blockly.loopMixin'},
},
},
'Blockly.Events.Abstract': {
export: 'Abstract',
path: 'Blockly.Events.Abstract',
Expand Down
Loading