Skip to content

Commit 04ec144

Browse files
authored
Merge pull request #55 from alan412/remove_duplicate_comment
Remove duplicate comment from methods
2 parents 479f398 + 36ffe1a commit 04ec144

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/blocks/mrc_class_method_def.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,13 @@ export const setup = function() {
337337
Blockly.Blocks[PARAM_CONTAINER_BLOCK_NAME] = METHOD_PARAM_CONTAINER;
338338
};
339339

340-
import { Order, PythonGenerator } from 'blockly/python';
340+
import { Order } from 'blockly/python';
341+
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
341342

342343

343344
export const pythonFromBlock = function (
344345
block: ClassMethodDefBlock,
345-
generator: PythonGenerator,
346+
generator: ExtendedPythonGenerator,
346347
) {
347348
const blocklyName = block.mrcPythonMethodName ? block.mrcPythonMethodName : block.getFieldValue('NAME');
348349

@@ -378,6 +379,9 @@ export const pythonFromBlock = function (
378379
// After executing the function body, revisit this block for the return.
379380
xfix2 = xfix1;
380381
}
382+
if(block.mrcPythonMethodName == '__init__'){
383+
branch = generator.INDENT + "self.mechanisms = []\n" + branch;
384+
}
381385
if (returnValue) {
382386
returnValue = generator.INDENT + 'return ' + returnValue + '\n';
383387
} else if (!branch) {
@@ -397,11 +401,6 @@ export const pythonFromBlock = function (
397401
'(' +
398402
paramString +
399403
'):\n';
400-
401-
if(block.mrcPythonMethodName == '__init__'){
402-
code += generator.INDENT + "self.mechanisms = []\n";
403-
404-
}
405404

406405
code +=
407406
xfix1 +
@@ -410,6 +409,7 @@ export const pythonFromBlock = function (
410409
xfix2 +
411410
returnValue;
412411
code = generator.scrub_(block, code);
412+
generator.addMethod(funcName, code);
413413

414414
return code;
415415
}

src/editor/extended_python_generator.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import * as commonStorage from '../storage/common_storage';
3131
export class ExtendedPythonGenerator extends PythonGenerator {
3232
private currentModule: commonStorage.Module | null = null;
3333
private mapWorkspaceIdToExportedBlocks: { [key: string]: Block[] } = Object.create(null);
34+
protected methods_: {[key: string]: string} = Object.create(null);
35+
3436

3537
constructor() {
3638
super('Python');
@@ -158,6 +160,9 @@ export class ExtendedPythonGenerator extends PythonGenerator {
158160
addImport(importModule: string): void {
159161
this.definitions_['import_' + importModule] = 'import ' + importModule;
160162
}
163+
addMethod(methodName: string, code : string): void {
164+
this.methods_[methodName] = code;
165+
}
161166

162167
classParentFromModuleType(moduleType : string) : string{
163168
if(moduleType == commonStorage.MODULE_TYPE_PROJECT){
@@ -186,31 +191,32 @@ export class ExtendedPythonGenerator extends PythonGenerator {
186191
// Convert the definitions dictionary into a list.
187192
const imports = [];
188193
const definitions = [];
194+
189195
for (let name in this.definitions_) {
190196
const def = this.definitions_[name];
191197
if (def.match(/^(from\s+\S+\s+)?import\s+\S+/)) {
192198
imports.push(def);
193-
} else {
199+
} else{
194200
definitions.push(def);
195201
}
196202
}
197-
// Call Blockly.CodeGenerator's finish. This is required to be done this way
198-
// because we derive from PythonGenerator which dervies from CodeGenerator
199-
// This section except for the class_def part is all copied from Blockly's
200-
// PythonGenerator. It can't be derived because it needs the class insertion
201-
// in the middle.
202-
code = Blockly.CodeGenerator.prototype.finish(code);
203+
const methods = [];
204+
for (let name in this.methods_){
205+
methods.push(this.methods_[name])
206+
}
207+
208+
this.definitions_ = Object.create(null);
209+
this.functionNames_ = Object.create(null);
210+
this.methods_ = Object.create(null);
211+
203212
this.isInitialized = false;
204213

205214
let class_def = "class " + className + "(" + classParent + "):\n";
206-
if (!code) {
207-
code = "pass";
208-
}
209215

210216
this.nameDB_!.reset();
211217
const allDefs = imports.join('\n') + '\n\n' + definitions.join('\n\n');
212218
return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + class_def +
213-
this.prefixLines(code, this.INDENT);
219+
this.prefixLines(methods.join('\n\n'), this.INDENT);
214220
}
215221
}
216222

0 commit comments

Comments
 (0)