Skip to content

Commit dc7a6fb

Browse files
committed
Make all variables class instance variables
Fixes #29
1 parent 612a7e3 commit dc7a6fb

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/blocks/mrc_class_method_def.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { MRC_STYLE_CLASS_BLOCKS } from '../themes/styles';
2424
import { createFieldNonEditableText } from '../fields/FieldNonEditableText'
2525
import * as ChangeFramework from './utils/change_framework'
2626
import { getLegalName } from './utils/python';
27+
import { Order } from 'blockly/python';
28+
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2729

2830
export 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-
358356
export 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';

src/blocks/setup_custom_blocks.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as Blockly from 'blockly';
21

32
import * as CallPythonFunction from './mrc_call_python_function';
43
import * as GetPythonEnumValue from './mrc_get_python_enum_value';

src/editor/extended_python_generator.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff 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;

0 commit comments

Comments
 (0)