Skip to content

Commit 8094b7b

Browse files
committed
Make an enum for ModuleType.
1 parent 73acc9e commit 8094b7b

18 files changed

+192
-182
lines changed

src/blocks/mrc_call_python_function.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ const CALL_PYTHON_FUNCTION = {
553553
let foundComponent = false;
554554
const componentsInScope: storageModuleContent.Component[] = [];
555555
componentsInScope.push(...this.getComponentsFromRobot());
556-
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
556+
if (editor.getCurrentModuleType() === storageModule.ModuleType.MECHANISM) {
557557
componentsInScope.push(...editor.getComponentsFromWorkspace());
558558
}
559559
for (const component of componentsInScope) {
@@ -600,7 +600,7 @@ const CALL_PYTHON_FUNCTION = {
600600
// If the robot method has changed, update the block if possible or put a
601601
// visible warning on it.
602602
if (this.mrcFunctionKind === FunctionKind.INSTANCE_ROBOT) {
603-
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
603+
if (editor.getCurrentModuleType() === storageModule.ModuleType.MECHANISM) {
604604
warnings.push('This block is not allowed to be used inside a mechanism.');
605605
} else {
606606
let foundRobotMethod = false;
@@ -646,7 +646,7 @@ const CALL_PYTHON_FUNCTION = {
646646
// If the method has changed, update the block if possible or put a
647647
// visible warning on it.
648648
if (this.mrcFunctionKind === FunctionKind.INSTANCE_MECHANISM) {
649-
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
649+
if (editor.getCurrentModuleType() === storageModule.ModuleType.MECHANISM) {
650650
warnings.push('This block is not allowed to be used inside a mechanism.');
651651
} else {
652652
let foundMechanism = false;
@@ -805,11 +805,11 @@ export function pythonFromBlock(
805805
: block.getFieldValue(FIELD_FUNCTION_NAME);
806806
// Generate the correct code depending on the module type.
807807
switch (generator.getModuleType()) {
808-
case storageModule.MODULE_TYPE_ROBOT:
809-
case storageModule.MODULE_TYPE_MECHANISM:
808+
case storageModule.ModuleType.ROBOT:
809+
case storageModule.ModuleType.MECHANISM:
810810
code = 'self.';
811811
break;
812-
case storageModule.MODULE_TYPE_OPMODE:
812+
case storageModule.ModuleType.OPMODE:
813813
code = 'self.robot.';
814814
break;
815815
}
@@ -830,13 +830,13 @@ export function pythonFromBlock(
830830
: block.getFieldValue(FIELD_FUNCTION_NAME);
831831
// Generate the correct code depending on the module type.
832832
switch (generator.getModuleType()) {
833-
case storageModule.MODULE_TYPE_ROBOT:
833+
case storageModule.ModuleType.ROBOT:
834834
code = 'self.' + mechanismName;
835835
break;
836-
case storageModule.MODULE_TYPE_OPMODE:
836+
case storageModule.ModuleType.OPMODE:
837837
code = 'self.robot.' + mechanismName;
838838
break;
839-
case storageModule.MODULE_TYPE_MECHANISM:
839+
case storageModule.ModuleType.MECHANISM:
840840
// The INSTANCE_MECHANISM version should not be used in a mechanism.
841841
// TODO(lizlooney): What if the user copies a block from an robot or opmode and pastes
842842
// it into a mechanism?

src/blocks/mrc_class_method_def.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ export const pythonFromBlock = function (
422422
// After executing the function body, revisit this block for the return.
423423
xfix2 = xfix1;
424424
}
425-
if (block.mrcPythonMethodName == '__init__') {
425+
if (block.mrcPythonMethodName === '__init__') {
426426
let class_specific = generator.getClassSpecificForInit();
427427
branch = generator.INDENT + 'super().__init__(' + class_specific + ')\n' +
428428
generator.generateInitStatements() + branch;
@@ -440,7 +440,7 @@ export const pythonFromBlock = function (
440440

441441
let params = block.mrcParameters;
442442
let paramString = "self";
443-
if (generator.getModuleType() == storageModule.MODULE_TYPE_MECHANISM && block.mrcPythonMethodName == '__init__') {
443+
if (generator.getModuleType() === storageModule.ModuleType.MECHANISM && block.mrcPythonMethodName === '__init__') {
444444
const ports: string[] = generator.getComponentPortParameters();
445445
if (ports.length) {
446446
paramString += ', ' + ports.join(', ');

src/blocks/mrc_component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const COMPONENT = {
130130
*/
131131
updateBlock_: function (this: ComponentBlock): void {
132132
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace);
133-
if (editor && editor.getCurrentModuleType() === storageModule.MODULE_TYPE_ROBOT) {
133+
if (editor && editor.getCurrentModuleType() === storageModule.ModuleType.ROBOT) {
134134
// Add input sockets for the arguments.
135135
for (let i = 0; i < this.mrcArgs.length; i++) {
136136
const input = this.appendValueInput('ARG' + i)
@@ -207,7 +207,7 @@ export const pythonFromBlock = function (
207207
if (i != 0) {
208208
code += ', ';
209209
}
210-
if (generator.getModuleType() === storageModule.MODULE_TYPE_ROBOT) {
210+
if (generator.getModuleType() === storageModule.ModuleType.ROBOT) {
211211
code += block.mrcArgs[i].name + ' = ' + generator.valueToCode(block, 'ARG' + i, Order.NONE);
212212
} else {
213213
code += block.mrcArgs[i].name + ' = ' + block.getArgName(i);
@@ -260,7 +260,7 @@ function createComponentBlock(
260260
'name': argData.name,
261261
'type': argData.type,
262262
});
263-
if (moduleType == storageModule.MODULE_TYPE_ROBOT) {
263+
if (moduleType == storageModule.ModuleType.ROBOT) {
264264
if (argData.type === 'int') {
265265
const portType = getPortTypeForArgument(argData.name);
266266
if (portType) {

src/blocks/mrc_event_handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,10 @@ function generateRegisterEventHandler(
393393
fullSender = 'self.' + sender;
394394
} else if (block.mrcSenderType === SenderType.MECHANISM) {
395395
switch (generator.getModuleType()) {
396-
case storageModule.MODULE_TYPE_ROBOT:
396+
case storageModule.ModuleType.ROBOT:
397397
fullSender = 'self.' + sender;
398398
break;
399-
case storageModule.MODULE_TYPE_OPMODE:
399+
case storageModule.ModuleType.OPMODE:
400400
fullSender = 'self.robot.' + sender;
401401
break;
402402
}

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ export const pythonFromBlock = function (
254254
block: MechanismComponentHolderBlock,
255255
generator: ExtendedPythonGenerator) {
256256
switch (generator.getModuleType()) {
257-
case storageModule.MODULE_TYPE_ROBOT:
257+
case storageModule.ModuleType.ROBOT:
258258
pythonFromBlockInRobot(block, generator);
259259
break;
260-
case storageModule.MODULE_TYPE_MECHANISM:
260+
case storageModule.ModuleType.MECHANISM:
261261
pythonFromBlockInMechanism(block, generator);
262262
break;
263263
}

src/editor/editor.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export class Editor {
231231
if (this.currentModule) {
232232
return this.currentModule.moduleType;
233233
}
234-
return storageModule.MODULE_TYPE_UNKNOWN;
234+
return '';
235235
}
236236

237237
private getModuleContentText(): string {
@@ -248,8 +248,8 @@ export class Editor {
248248
const components: storageModuleContent.Component[] = this.getComponentsFromWorkspace();
249249
const events: storageModuleContent.Event[] = this.getEventsFromWorkspace();
250250
const methods: storageModuleContent.Method[] = (
251-
this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT ||
252-
this.currentModule?.moduleType === storageModule.MODULE_TYPE_MECHANISM)
251+
this.currentModule?.moduleType === storageModule.ModuleType.ROBOT ||
252+
this.currentModule?.moduleType === storageModule.ModuleType.MECHANISM)
253253
? this.getMethodsForOutsideFromWorkspace()
254254
: [];
255255
return storageModuleContent.makeModuleContentText(
@@ -258,16 +258,16 @@ export class Editor {
258258

259259
public getMechanismsFromWorkspace(): storageModuleContent.MechanismInRobot[] {
260260
const mechanisms: storageModuleContent.MechanismInRobot[] = [];
261-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
261+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT) {
262262
mechanismComponentHolder.getMechanisms(this.blocklyWorkspace, mechanisms);
263263
}
264264
return mechanisms;
265265
}
266266

267267
public getComponentsFromWorkspace(): storageModuleContent.Component[] {
268268
const components: storageModuleContent.Component[] = [];
269-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT ||
270-
this.currentModule?.moduleType === storageModule.MODULE_TYPE_MECHANISM) {
269+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT ||
270+
this.currentModule?.moduleType === storageModule.ModuleType.MECHANISM) {
271271
mechanismComponentHolder.getComponents(this.blocklyWorkspace, components);
272272
}
273273
return components;
@@ -294,8 +294,8 @@ export class Editor {
294294

295295
public getEventsFromWorkspace(): storageModuleContent.Event[] {
296296
const events: storageModuleContent.Event[] = [];
297-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT ||
298-
this.currentModule?.moduleType === storageModule.MODULE_TYPE_MECHANISM) {
297+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT ||
298+
this.currentModule?.moduleType === storageModule.ModuleType.MECHANISM) {
299299
mechanismComponentHolder.getEvents(this.blocklyWorkspace, events);
300300
}
301301
return events;
@@ -329,7 +329,7 @@ export class Editor {
329329
* Returns the mechanisms defined in the robot.
330330
*/
331331
public getMechanismsFromRobot(): storageModuleContent.MechanismInRobot[] {
332-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
332+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT) {
333333
return this.getMechanismsFromWorkspace();
334334
}
335335
if (this.robotContent) {
@@ -342,7 +342,7 @@ export class Editor {
342342
* Returns the components defined in the robot.
343343
*/
344344
public getComponentsFromRobot(): storageModuleContent.Component[] {
345-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
345+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT) {
346346
return this.getComponentsFromWorkspace();
347347
}
348348
if (this.robotContent) {
@@ -355,7 +355,7 @@ export class Editor {
355355
* Returns the events defined in the robot.
356356
*/
357357
public getEventsFromRobot(): storageModuleContent.Event[] {
358-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
358+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT) {
359359
return this.getEventsFromWorkspace();
360360
}
361361
if (this.robotContent) {
@@ -368,7 +368,7 @@ export class Editor {
368368
* Returns the methods defined in the robot.
369369
*/
370370
public getMethodsFromRobot(): storageModuleContent.Method[] {
371-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
371+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT) {
372372
return this.getMethodsForWithinFromWorkspace();
373373
}
374374
if (this.robotContent) {

src/editor/extended_python_generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
111111
generateInitStatements() : string {
112112
let initStatements = '';
113113

114-
if (this.getModuleType() === storageModule.MODULE_TYPE_MECHANISM && this.hasAnyComponents) {
114+
if (this.getModuleType() === storageModule.ModuleType.MECHANISM && this.hasAnyComponents) {
115115
initStatements += this.INDENT + 'self.define_hardware(' +
116116
this.getComponentPortParameters().join(', ') + ')\n';
117117
}
@@ -134,7 +134,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
134134

135135
this.hasAnyComponents = false;
136136
this.componentPorts = Object.create(null);
137-
if (this.getModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
137+
if (this.getModuleType() === storageModule.ModuleType.MECHANISM) {
138138
this.hasAnyComponents = mechanismContainerHolder.hasAnyComponents(this.workspace);
139139
mechanismContainerHolder.getComponentPorts(this.workspace, this.componentPorts);
140140
}

src/editor/generator_context.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,13 @@ export class GeneratorContext {
5252
if (!this.module) {
5353
throw new Error('getParentClassName: this.module is null.');
5454
}
55-
if (this.module.moduleType === storageModule.MODULE_TYPE_ROBOT) {
56-
return CLASS_NAME_ROBOT_BASE;
57-
}
58-
if (this.module.moduleType === storageModule.MODULE_TYPE_OPMODE) {
59-
return CLASS_NAME_OPMODE;
60-
}
61-
if (this.module.moduleType === storageModule.MODULE_TYPE_MECHANISM) {
62-
return CLASS_NAME_MECHANISM;
55+
switch (this.module.moduleType) {
56+
case storageModule.ModuleType.ROBOT:
57+
return CLASS_NAME_ROBOT_BASE;
58+
case storageModule.ModuleType.OPMODE:
59+
return CLASS_NAME_OPMODE;
60+
case storageModule.ModuleType.MECHANISM:
61+
return CLASS_NAME_MECHANISM;
6362
}
6463
return '';
6564
}

src/reactComponents/AddTabDialog.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ export default function AddTabDialog(props: AddTabDialogProps) {
129129
return;
130130
}
131131

132-
const storageType = tabType === TabType.MECHANISM ?
133-
storageModule.MODULE_TYPE_MECHANISM :
134-
storageModule.MODULE_TYPE_OPMODE;
132+
const moduleType = (tabType === TabType.MECHANISM)
133+
? storageModule.ModuleType.MECHANISM
134+
: storageModule.ModuleType.OPMODE;
135135

136136
await storageProject.addModuleToProject(
137-
props.storage, props.project, storageType, newClassName);
137+
props.storage, props.project, moduleType, newClassName);
138138

139139
const newModule = storageProject.findModuleByClassName(props.project, newClassName);
140140
if (newModule) {

src/reactComponents/FileManageModal.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ interface FileManageModalProps {
4444
gotoTab: (path: string) => void;
4545
setAlertErrorMessage: (message: string) => void;
4646
storage: commonStorage.Storage | null;
47-
moduleType: TabType;
47+
tabType: TabType;
4848
}
4949

5050
/** Default page size for table pagination. */
@@ -75,20 +75,20 @@ export default function FileManageModal(props: FileManageModalProps) {
7575
}
7676
}
7777
React.useEffect(() => {
78-
if (!props.project || props.moduleType === null) {
78+
if (!props.project || props.tabType === null) {
7979
setModules([]);
8080
return;
8181
}
8282

8383
let moduleList: Module[] = [];
8484

85-
if (props.moduleType === TabType.MECHANISM) {
85+
if (props.tabType === TabType.MECHANISM) {
8686
moduleList = props.project.mechanisms.map((m) => ({
8787
path: m.modulePath,
8888
title: m.className,
8989
type: TabType.MECHANISM,
9090
}));
91-
} else if (props.moduleType === TabType.OPMODE) {
91+
} else if (props.tabType === TabType.OPMODE) {
9292
moduleList = props.project.opModes.map((o) => ({
9393
path: o.modulePath,
9494
title: o.className,
@@ -99,7 +99,7 @@ export default function FileManageModal(props: FileManageModalProps) {
9999
// Sort modules alphabetically by title
100100
moduleList.sort((a, b) => a.title.localeCompare(b.title));
101101
setModules(moduleList);
102-
}, [props.project, props.moduleType]);
102+
}, [props.project, props.tabType]);
103103

104104
/** Handles renaming a module. */
105105
const handleRename = async (origModule: Module, newClassName: string): Promise<void> => {
@@ -177,14 +177,14 @@ export default function FileManageModal(props: FileManageModalProps) {
177177
return;
178178
}
179179

180-
const storageType = props.moduleType === TabType.MECHANISM ?
181-
storageModule.MODULE_TYPE_MECHANISM :
182-
storageModule.MODULE_TYPE_OPMODE;
180+
const moduleType = (props.tabType === TabType.MECHANISM)
181+
? storageModule.ModuleType.MECHANISM
182+
: storageModule.ModuleType.OPMODE;
183183

184184
await storageProject.addModuleToProject(
185185
props.storage,
186186
props.project,
187-
storageType,
187+
moduleType,
188188
newClassName
189189
);
190190

@@ -193,7 +193,7 @@ export default function FileManageModal(props: FileManageModalProps) {
193193
const module: Module = {
194194
path: newModule.modulePath,
195195
title: newModule.className,
196-
type: props.moduleType,
196+
type: props.tabType,
197197
};
198198
setModules([...modules, module]);
199199
}
@@ -309,7 +309,7 @@ export default function FileManageModal(props: FileManageModalProps) {
309309

310310
/** Gets the modal title based on module type. */
311311
const getModalTitle = (): string => {
312-
return `${TabTypeUtils.toString(props.moduleType)} Management`;
312+
return `${TabTypeUtils.toString(props.tabType)} Management`;
313313
};
314314

315315
/** Gets the rename modal title. */
@@ -328,10 +328,10 @@ export default function FileManageModal(props: FileManageModalProps) {
328328
return `Copy ${TabTypeUtils.toString(currentRecord.type)}: ${currentRecord.title}`;
329329
};
330330

331-
/** Gets the empty table text based on module type. */
331+
/** Gets the empty table text based on tab type. */
332332
const getEmptyText = (): string => {
333-
const moduleTypeString = TabTypeUtils.toString(props.moduleType || TabType.OPMODE);
334-
return `No ${moduleTypeString.toLowerCase()} files found`;
333+
const tabTypeString = TabTypeUtils.toString(props.tabType || TabType.OPMODE);
334+
return `No ${tabTypeString.toLowerCase()} files found`;
335335
};
336336

337337
return (
@@ -412,7 +412,7 @@ export default function FileManageModal(props: FileManageModalProps) {
412412
padding: '12px',
413413
}}>
414414
<ClassNameComponent
415-
tabType={props.moduleType}
415+
tabType={props.tabType}
416416
newItemName={newItemName}
417417
setNewItemName={setNewItemName}
418418
onAddNewItem={handleAddNewItem}

0 commit comments

Comments
 (0)