Skip to content

Commit 4b9ea34

Browse files
committed
Use makeLegalName to make names unique when mechanisms, components, and events are dragged into the holder.
Use makeLegalName to validate parameter names.
1 parent 2cbf2da commit 4b9ea34

File tree

3 files changed

+16
-35
lines changed

3 files changed

+16
-35
lines changed

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import * as Blockly from 'blockly';
2323

2424
import { MRC_STYLE_MECHANISMS } from '../themes/styles';
25-
import { getLegalName } from './utils/python';
25+
import { makeLegalName } from './utils/validator';
2626
import { Editor } from '../editor/editor';
2727
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2828
import * as storageModule from '../storage/module';
@@ -220,14 +220,16 @@ const MECHANISM_COMPONENT_HOLDER = {
220220
*/
221221
setNameOfChildBlock(this: MechanismComponentHolderBlock, child: Blockly.Block): void {
222222
const otherNames: string[] = []
223-
const descendants = this.getDescendants(true);
224-
descendants
223+
this.getDescendants(true)
225224
.filter(descendant => descendant.id !== child.id)
226225
.forEach(descendant => {
227226
otherNames.push(descendant.getFieldValue('NAME'));
228227
});
229228
const currentName = child.getFieldValue('NAME');
230-
const legalName = getLegalName(currentName, otherNames);
229+
// mechanism and component names must be valid python identifiers.
230+
// event names do not need to be valid python identifiers.
231+
const mustBeValidPythonIdentifier = (child.type === MRC_MECHANISM_NAME || child.type === MRC_COMPONENT_NAME);
232+
const legalName = makeLegalName(currentName, otherNames, mustBeValidPythonIdentifier);
231233
if (legalName !== currentName) {
232234
child.setFieldValue(legalName, 'NAME');
233235
}

src/blocks/mrc_param_container.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
import * as Blockly from 'blockly';
2323
import { MRC_STYLE_CLASS_BLOCKS } from '../themes/styles';
24-
import { getLegalName } from './utils/python';
24+
import { makeLegalName } from './utils/validator';
2525

2626
export const PARAM_CONTAINER_BLOCK_NAME = 'mrc_param_container';
2727
const PARAM_ITEM_BLOCK_NAME = 'mrc_param_item';
@@ -85,13 +85,16 @@ const PARAM_ITEM = {
8585
const rootBlock: Blockly.Block | null = this.getRootBlock();
8686
if (rootBlock) {
8787
const otherNames: string[] = []
88-
rootBlock!.getDescendants(true)?.forEach(itemBlock => {
89-
if (itemBlock != this) {
90-
otherNames.push(itemBlock.getFieldValue(FIELD_NAME));
91-
}
92-
});
88+
rootBlock.getDescendants(true)
89+
.filter(descendant => descendant.type === PARAM_ITEM_BLOCK_NAME && descendant !== this)
90+
.forEach(itemBlock => {
91+
if (itemBlock != this) {
92+
otherNames.push(itemBlock.getFieldValue(FIELD_NAME));
93+
}
94+
});
9395
const currentName = this.getFieldValue(FIELD_NAME);
94-
this.setFieldValue(getLegalName(currentName, otherNames), FIELD_NAME);
96+
const legalName = makeLegalName(currentName, otherNames, /* mustBeValidPythonIdentifier */ true);
97+
this.setFieldValue(legalName, FIELD_NAME);
9598
updateMutatorFlyout(this.workspace);
9699
}
97100
},

src/blocks/utils/python.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -281,30 +281,6 @@ export function getOutputCheck(type: string): string {
281281
return type;
282282
}
283283

284-
// Function to return a legal name based off of proposed names and making sure it doesn't conflict
285-
// This is a legal name for python methods and variables.
286-
export function getLegalName(proposedName: string, existingNames: string[]){
287-
let newName = proposedName.trim().replace(' ', '_');
288-
289-
// TODO: Allow the user to put numbers in the name.
290-
291-
if (!/^[A-Za-z_]/.test(newName)){
292-
newName = "_" + newName;
293-
}
294-
295-
while (existingNames.includes(newName)){
296-
const match = /(.*?)(\d+)$/.exec(newName)
297-
298-
if (match) {
299-
let lastNumber = +match[2]
300-
newName = match[1] + (lastNumber + 1)
301-
} else {
302-
newName += "2"
303-
}
304-
}
305-
return newName;
306-
}
307-
308284
export function isExistingPythonModule(moduleName: string): boolean {
309285
for (const pythonData of allPythonData) {
310286
// Process modules.

0 commit comments

Comments
 (0)