File tree Expand file tree Collapse file tree 3 files changed +35
-6
lines changed
Expand file tree Collapse file tree 3 files changed +35
-6
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,8 @@ import { MRC_STYLE_CLASS_BLOCKS } from '../themes/styles';
2424import { createFieldNonEditableText } from '../fields/FieldNonEditableText'
2525import * as ChangeFramework from './utils/change_framework'
2626import { getLegalName } from './utils/python' ;
27+ import { Order } from 'blockly/python' ;
28+ import { ExtendedPythonGenerator } from '../editor/extended_python_generator' ;
2729
2830export const BLOCK_NAME = 'mrc_class_method_def' ;
2931
@@ -351,10 +353,6 @@ export const setup = function() {
351353 Blockly . Blocks [ PARAM_CONTAINER_BLOCK_NAME ] = METHOD_PARAM_CONTAINER ;
352354} ;
353355
354- import { Order } from 'blockly/python' ;
355- import { ExtendedPythonGenerator } from '../editor/extended_python_generator' ;
356-
357-
358356export const pythonFromBlock = function (
359357 block : ClassMethodDefBlock ,
360358 generator : ExtendedPythonGenerator ,
@@ -394,7 +392,7 @@ export const pythonFromBlock = function (
394392 xfix2 = xfix1 ;
395393 }
396394 if ( block . mrcPythonMethodName == '__init__' ) {
397- branch = generator . INDENT + "self.mechanisms = []\n" + branch ;
395+ branch = generator . defineClassVariables ( block . workspace ) + branch ;
398396 }
399397 if ( returnValue ) {
400398 returnValue = generator . INDENT + 'return ' + returnValue + '\n' ;
Original file line number Diff line number Diff line change 1- import * as Blockly from 'blockly' ;
21
32import * as CallPythonFunction from './mrc_call_python_function' ;
43import * as GetPythonEnumValue from './mrc_get_python_enum_value' ;
Original file line number Diff line number Diff line change @@ -39,6 +39,38 @@ export class ExtendedPythonGenerator extends PythonGenerator {
3939 super ( 'Python' ) ;
4040 }
4141
42+ init ( workspace : Blockly . Workspace ) {
43+ super . init ( workspace ) ;
44+ // This will have all variables in the defintion 'variables' so we will need to destroy it and make our own
45+ delete this . definitions_ [ 'variables' ] ;
46+
47+ const defvars = [ ] ;
48+ // Add developer variables (not created or named by the user).
49+ const devVarList = Blockly . Variables . allDeveloperVariables ( workspace ) ;
50+ for ( let i = 0 ; i < devVarList . length ; i ++ ) {
51+ defvars . push (
52+ this . nameDB_ ! . getName ( devVarList [ i ] , Blockly . Names . DEVELOPER_VARIABLE_TYPE ) + ' = None' ,
53+ ) ;
54+ }
55+ this . definitions_ [ 'variables' ] = defvars . join ( '\n' ) ;
56+ // user variables are dealt with in init code generation
57+ }
58+
59+ defineClassVariables ( workspace : Blockly . Workspace ) : string {
60+ const classVars = Blockly . Variables . allUsedVarModels ( workspace ) ;
61+
62+ let variableDefinitions = '' ;
63+
64+ for ( let i = 0 ; i < classVars . length ; i ++ ) {
65+ variableDefinitions += this . INDENT + this . getVariableName ( classVars [ i ] . getId ( ) ) + ' = None\n' ;
66+ }
67+ return variableDefinitions ;
68+ }
69+ getVariableName ( name : string ) : string {
70+ const varName = super . getVariableName ( name ) ;
71+ return "self." + varName ;
72+ }
73+
4274 workspaceToCode ( workspace : Blockly . Workspace , context : GeneratorContext ) : string {
4375 this . workspace = workspace ;
4476 this . context = context ;
You can’t perform that action at this time.
0 commit comments