@@ -25,6 +25,7 @@ import { extendedPythonGenerator } from './extended_python_generator';
2525import { GeneratorContext } from './generator_context' ;
2626import * as commonStorage from '../storage/common_storage' ;
2727import * as mechanismComponentHolder from '../blocks/mrc_mechanism_component_holder' ;
28+ import * as classMethodDef from '../blocks/mrc_class_method_def' ;
2829//import { testAllBlocksInToolbox } from '../toolbox/toolbox_tests';
2930import { MethodsCategory } from '../toolbox/methods_category' ;
3031import { EventsCategory } from '../toolbox/event_category' ;
@@ -71,38 +72,15 @@ export class Editor {
7172 return ;
7273 }
7374
74- // TODO(lizlooney): As blocks are loaded, determine whether any blocks
75- // are accessing variable or calling functions thar are defined in another
76- // blocks file (like the Robot) and check whether the variable or function
77- // definition has changed. This might happen if the user defines a variable
78- // or function in the Robot, uses the variable or function in the
79- // OpMode, and then removes or changes the variable or function in the
80- // Robot.
75+ // TODO(lizlooney): Look at blocks with type 'mrc_call_python_function' that
76+ // are calling methods defined in the Robot and check that the Robot method
77+ // still exists and hasn't been changed. If it has changed, update the block
78+ // if possible or put a visible warning on it.
8179
82- // TODO(lizlooney): We will need a way to identify which variable or
83- // function, other than by the variable name or function name, because the
84- // user might change the name. This will take some thought and I should
85- // write up a design doc and discuss it with others to make sure we have a
86- // good solution.
87-
88- // TODO(lizlooney): Look at blocks with type 'mrc_get_python_variable' or
89- // 'mrc_set_python_variable', and where block.mrcExportedVariable === true.
90- // Look at block.mrcImportModule and get the exported blocks for that module.
91- // It could be from the Robot (or a Mechanism?) and we already have the Robot content.
92- // Check whether block.mrcActualVariableName matches any exportedBlock's
93- // extraState.actualVariableName. If there is no match, put a warning on the
94- // block.
95-
96- // TODO(lizlooney): Look at blocks with type 'mrc_call_python_function' and
97- // where block.mrcExportedFunction === true.
98- // Look at block.mrcImportModule and get the exported blocks for that module.
99- // It could be from the Robot (or a Mechanism?) and we already have the Robot content.
100- // Check whether block.mrcActualFunctionName matches any exportedBlock's
101- // extraState.actualFunctionName. If there is no match, put a warning on the block.
102- // If there is a match, check whether
103- // block.mrcArgs.length === exportedBlock.extraState.args.length and
104- // block.mrcArgs[i].name === exportedBlock.extraState.args[i].name for all args.
105- // If there is any differences, put a warning on the block.
80+ // TODO(lizlooney): Look at blocks with type 'mrc_call_python_function' that
81+ // are calling methods on components defined in the Robot and check that the
82+ // component and the method still exists and hasn't been changed. If it has
83+ // changed, update the block if possible or put a visible warning on it.
10684 }
10785
10886 private onChangeAfterLoading ( event : Blockly . Events . Abstract ) {
@@ -221,12 +199,12 @@ export class Editor {
221199 }
222200 const pythonCode = extendedPythonGenerator . mrcWorkspaceToCode (
223201 this . blocklyWorkspace , this . generatorContext ) ;
224- const exportedBlocks = JSON . stringify ( this . generatorContext . getExportedBlocks ( ) ) ;
225202 const blocksContent = JSON . stringify (
226203 Blockly . serialization . workspaces . save ( this . blocklyWorkspace ) ) ;
204+ const methodsContent = JSON . stringify ( this . getMethodsFromWorkspace ( ) ) ;
227205 const componentsContent = JSON . stringify ( this . getComponentsFromWorkspace ( ) ) ;
228206 return commonStorage . makeModuleContent (
229- this . currentModule , pythonCode , blocksContent , exportedBlocks , componentsContent ) ;
207+ this . currentModule , pythonCode , blocksContent , methodsContent , componentsContent ) ;
230208 }
231209
232210 public getComponentsFromWorkspace ( ) : commonStorage . Component [ ] {
@@ -246,6 +224,22 @@ export class Editor {
246224 return components ;
247225 }
248226
227+ public getMethodsFromWorkspace ( ) : commonStorage . Method [ ] {
228+ const methods : commonStorage . Method [ ] = [ ] ;
229+ if ( this . currentModule ?. moduleType === commonStorage . MODULE_TYPE_ROBOT ||
230+ this . currentModule ?. moduleType === commonStorage . MODULE_TYPE_MECHANISM ) {
231+ // Get the class method definition blocks.
232+ const methodDefBlocks = this . blocklyWorkspace . getBlocksByType ( classMethodDef . BLOCK_NAME ) ;
233+ methodDefBlocks . forEach ( methodDefBlock => {
234+ const method = ( methodDefBlock as classMethodDef . ClassMethodDefBlock ) . getMethod ( ) ;
235+ if ( method ) {
236+ methods . push ( method ) ;
237+ }
238+ } ) ;
239+ }
240+ return methods ;
241+ }
242+
249243 public async saveBlocks ( ) {
250244 const moduleContent = this . getModuleContent ( ) ;
251245 try {
@@ -260,8 +254,6 @@ export class Editor {
260254 * Returns the components defined in the robot.
261255 */
262256 public getComponentsFromRobot ( ) : commonStorage . Component [ ] {
263- let components : commonStorage . Component [ ] ;
264-
265257 if ( this . currentModule ?. moduleType === commonStorage . MODULE_TYPE_ROBOT ) {
266258 return this . getComponentsFromWorkspace ( ) ;
267259 }
@@ -271,6 +263,19 @@ export class Editor {
271263 return commonStorage . extractComponents ( this . robotContent ) ;
272264 }
273265
266+ /**
267+ * Returns the methods defined in the robot.
268+ */
269+ public getMethodsFromRobot ( ) : commonStorage . Method [ ] {
270+ if ( this . currentModule ?. moduleType === commonStorage . MODULE_TYPE_ROBOT ) {
271+ return this . getMethodsFromWorkspace ( ) ;
272+ }
273+ if ( ! this . robotContent ) {
274+ throw new Error ( 'getMethodsFromRobot: this.robotContent is null.' ) ;
275+ }
276+ return commonStorage . extractMethods ( this . robotContent ) ;
277+ }
278+
274279 public static getEditorForBlocklyWorkspace ( workspace : Blockly . Workspace ) : Editor | null {
275280 if ( workspace . id in Editor . workspaceIdToEditor ) {
276281 return Editor . workspaceIdToEditor [ workspace . id ] ;
0 commit comments