Skip to content

Commit a8ebf33

Browse files
committed
Handle method names that are python reserved words.
1 parent 612a7e3 commit a8ebf33

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/editor/editor.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class Editor {
5050
this.blocklyWorkspace = blocklyWorkspace;
5151
this.generatorContext = generatorContext;
5252
this.storage = storage;
53-
this.methodsCategory = new MethodsCategory(blocklyWorkspace);
53+
this.methodsCategory = new MethodsCategory(blocklyWorkspace, generatorContext);
5454
}
5555

5656
private onChangeWhileLoading(event: Blockly.Events.Abstract) {
@@ -112,7 +112,6 @@ export class Editor {
112112
public async loadModuleBlocks(currentModule: commonStorage.Module | null) {
113113
this.generatorContext.setModule(currentModule);
114114
this.currentModule = currentModule;
115-
this.methodsCategory.setCurrentModule(currentModule);
116115
if (currentModule) {
117116
this.modulePath = currentModule.modulePath;
118117
this.projectPath = commonStorage.makeProjectPath(currentModule.projectName);
@@ -226,5 +225,4 @@ export class Editor {
226225
throw e;
227226
}
228227
}
229-
230228
}

src/toolbox/methods_category.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import * as Blockly from 'blockly/core';
2323

24+
import { GeneratorContext } from './generator_context';
2425
import * as commonStorage from '../storage/common_storage';
2526
import { MRC_CATEGORY_STYLE_METHODS } from '../themes/styles'
2627
import { mechanism_class_blocks } from './mechanism_class_methods';
@@ -38,16 +39,13 @@ export const category = {
3839
};
3940

4041
export class MethodsCategory {
41-
private currentModule: commonStorage.Module | null = null;
42+
private generatorContext: GeneratorContext;
4243

43-
constructor(blocklyWorkspace: Blockly.WorkspaceSvg) {
44+
constructor(blocklyWorkspace: Blockly.WorkspaceSvg, generatorContext: GeneratorContext) {
45+
this.generatorContext = generatorContext;
4446
blocklyWorkspace.registerToolboxCategoryCallback(CUSTOM_CATEGORY_METHODS, this.methodsFlyout.bind(this));
4547
}
4648

47-
public setCurrentModule(currentModule: commonStorage.Module | null) {
48-
this.currentModule = currentModule;
49-
}
50-
5149
public methodsFlyout(workspace: Blockly.WorkspaceSvg): ToolboxInfo {
5250
const toolboxInfo = {
5351
contents: [
@@ -110,6 +108,8 @@ export class MethodsCategory {
110108
// mrc_call_python_function block.
111109
workspace.getBlocksByType('mrc_class_method_def', false).forEach((classMethodDefBlock) => {
112110
if (classMethodDefBlock.mrcCanBeCalledWithinClass) {
111+
const nameFieldValue = classMethodDefBlock.getFieldValue('NAME');
112+
const classMethodName = this.generatorContext.getClassMethodName(nameFieldValue);
113113
const callPythonFunctionBlock = {
114114
kind: 'block',
115115
type: 'mrc_call_python_function',
@@ -118,10 +118,10 @@ export class MethodsCategory {
118118
functionKind: 'instance_within',
119119
returnType: classMethodDefBlock.mrcReturnType,
120120
args: [],
121-
actualFunctionName: classMethodDefBlock.mrcPythonMethodName,
121+
actualFunctionName: classMethodName,
122122
},
123123
fields: {
124-
FUNC: classMethodDefBlock.getFieldValue('NAME'),
124+
FUNC: nameFieldValue,
125125
},
126126
};
127127
classMethodDefBlock.mrcParameters.forEach((param) => {

0 commit comments

Comments
 (0)