diff --git a/src/blocks/mrc_class_method_def.ts b/src/blocks/mrc_class_method_def.ts index 5865442f..1bb367e6 100644 --- a/src/blocks/mrc_class_method_def.ts +++ b/src/blocks/mrc_class_method_def.ts @@ -337,12 +337,13 @@ export const setup = function() { Blockly.Blocks[PARAM_CONTAINER_BLOCK_NAME] = METHOD_PARAM_CONTAINER; }; -import { Order, PythonGenerator } from 'blockly/python'; +import { Order } from 'blockly/python'; +import { ExtendedPythonGenerator } from '../editor/extended_python_generator'; export const pythonFromBlock = function ( block: ClassMethodDefBlock, - generator: PythonGenerator, + generator: ExtendedPythonGenerator, ) { const blocklyName = block.mrcPythonMethodName ? block.mrcPythonMethodName : block.getFieldValue('NAME'); @@ -378,6 +379,9 @@ export const pythonFromBlock = function ( // After executing the function body, revisit this block for the return. xfix2 = xfix1; } + if(block.mrcPythonMethodName == '__init__'){ + branch = generator.INDENT + "self.mechanisms = []\n" + branch; + } if (returnValue) { returnValue = generator.INDENT + 'return ' + returnValue + '\n'; } else if (!branch) { @@ -397,11 +401,6 @@ export const pythonFromBlock = function ( '(' + paramString + '):\n'; - - if(block.mrcPythonMethodName == '__init__'){ - code += generator.INDENT + "self.mechanisms = []\n"; - - } code += xfix1 + @@ -410,6 +409,7 @@ export const pythonFromBlock = function ( xfix2 + returnValue; code = generator.scrub_(block, code); + generator.addMethod(funcName, code); return code; } \ No newline at end of file diff --git a/src/editor/extended_python_generator.ts b/src/editor/extended_python_generator.ts index 3dbadb30..4ece77f7 100644 --- a/src/editor/extended_python_generator.ts +++ b/src/editor/extended_python_generator.ts @@ -31,6 +31,8 @@ import * as commonStorage from '../storage/common_storage'; export class ExtendedPythonGenerator extends PythonGenerator { private currentModule: commonStorage.Module | null = null; private mapWorkspaceIdToExportedBlocks: { [key: string]: Block[] } = Object.create(null); + protected methods_: {[key: string]: string} = Object.create(null); + constructor() { super('Python'); @@ -158,6 +160,9 @@ export class ExtendedPythonGenerator extends PythonGenerator { addImport(importModule: string): void { this.definitions_['import_' + importModule] = 'import ' + importModule; } + addMethod(methodName: string, code : string): void { + this.methods_[methodName] = code; + } classParentFromModuleType(moduleType : string) : string{ if(moduleType == commonStorage.MODULE_TYPE_PROJECT){ @@ -186,31 +191,32 @@ export class ExtendedPythonGenerator extends PythonGenerator { // Convert the definitions dictionary into a list. const imports = []; const definitions = []; + for (let name in this.definitions_) { const def = this.definitions_[name]; if (def.match(/^(from\s+\S+\s+)?import\s+\S+/)) { imports.push(def); - } else { + } else{ definitions.push(def); } } - // Call Blockly.CodeGenerator's finish. This is required to be done this way - // because we derive from PythonGenerator which dervies from CodeGenerator - // This section except for the class_def part is all copied from Blockly's - // PythonGenerator. It can't be derived because it needs the class insertion - // in the middle. - code = Blockly.CodeGenerator.prototype.finish(code); + const methods = []; + for (let name in this.methods_){ + methods.push(this.methods_[name]) + } + + this.definitions_ = Object.create(null); + this.functionNames_ = Object.create(null); + this.methods_ = Object.create(null); + this.isInitialized = false; let class_def = "class " + className + "(" + classParent + "):\n"; - if (!code) { - code = "pass"; - } this.nameDB_!.reset(); const allDefs = imports.join('\n') + '\n\n' + definitions.join('\n\n'); return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + class_def + - this.prefixLines(code, this.INDENT); + this.prefixLines(methods.join('\n\n'), this.INDENT); } }