-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f4a50ea
chore: moves the public loop mixin into its own file
alschmiedt 5f824c8
chore: fix spacing
alschmiedt 81e8fd7
chore: fix types and format
alschmiedt 2359f32
chore: update renamings file
alschmiedt f36c3e8
chore: fix comment and lint
alschmiedt c1500cc
chore: fix lint
alschmiedt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
|
||
/** | ||
* 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 inblocks/
.Since modules in
generators/
require modules inblocks/
, and since it would be easy to enable external code toimport {CONTROL_FLOW_IN_LOOP_CHECK_MIXIN} from 'blockly/blocks';
I don't really know why it wouldn't be better to leave this code inblocks/loops.js
(but move it to theBlockly.blocks.loops
module, and reexport fromBlockly.loops.all
)There was a problem hiding this comment.
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.