Skip to content

Commit 75778e9

Browse files
authored
Merge pull request #34 from wpilibsuite/methods
Adding methods
2 parents 5299c9d + dd1cd0b commit 75778e9

12 files changed

+222
-80
lines changed

src/App.tsx

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ const App: React.FC = () => {
237237
const [treeExpandedKeys, setTreeExpandedKeys] = useState<React.Key[]>([]);
238238
const [treeSelectedKey, setTreeSelectedKey] = useState<React.Key>('');
239239
const [currentModulePath, setCurrentModulePath] = useState('');
240+
const [currentModule, setCurrentModule] = useState<commonStorage.Module | null>(null);
240241
const [renameTooltip, setRenameTooltip] = useState('Rename');
241242
const [copyTooltip, setCopyTooltip] = useState('Copy');
242243
const [deleteTooltip, setDeleteTooltip] = useState('Delete');
@@ -404,38 +405,47 @@ const App: React.FC = () => {
404405

405406
// When a module path has become the current module path (either by fetching
406407
// the most recent module path (soon after the app is loaded) or by the user
407-
// selecting it in the tree, set the current workspace, expend the
408-
// workspace node in the tree.
408+
// selecting it in the tree, set the current module, update some tooltips, and
409+
// load the module into the blockly editor.
409410
useEffect(() => {
410411
if (ignoreEffect()) {
411412
return;
412413
}
413-
if (currentModulePath && modules.length > 0) {
414-
const workspaceName = commonStorage.getWorkspaceName(currentModulePath);
415-
const workspacePath = commonStorage.makeModulePath(workspaceName, workspaceName);
416414

417-
if (currentModulePath === workspacePath) {
415+
const module = (modules.length > 0 && currentModulePath)
416+
? commonStorage.findModule(modules, currentModulePath)
417+
: null;
418+
setCurrentModule(module);
419+
420+
if (module != null) {
421+
if (module.moduleType == commonStorage.MODULE_TYPE_WORKSPACE) {
418422
setRenameTooltip('Rename Workspace');
419423
setCopyTooltip('Copy Workspace');
420424
setDeleteTooltip('Delete Workspace');
421-
} else {
425+
} else if (module.moduleType == commonStorage.MODULE_TYPE_OPMODE) {
422426
setRenameTooltip('Rename OpMode');
423427
setCopyTooltip('Copy OpMode');
424428
setDeleteTooltip('Delete OpMode');
429+
} else if (module.moduleType == commonStorage.MODULE_TYPE_MECHANISM) {
430+
setRenameTooltip('Rename Mechanism');
431+
setCopyTooltip('Copy Mechanism');
432+
setDeleteTooltip('Delete Mechanism');
425433
}
426434

427435
storage.saveEntry('mostRecentModulePath', currentModulePath);
428436
if (blocksEditor.current) {
429-
blocksEditor.current.loadModuleBlocks(currentModulePath, workspacePath);
437+
blocksEditor.current.loadModuleBlocks(module);
430438
}
431439
} else {
440+
setCurrentModule(null);
441+
432442
setRenameTooltip('Rename');
433443
setCopyTooltip('Copy');
434444
setDeleteTooltip('Delete');
435445

436446
storage.saveEntry('mostRecentModulePath', '');
437447
if (blocksEditor.current) {
438-
blocksEditor.current.loadModuleBlocks('', '');
448+
blocksEditor.current.loadModuleBlocks(null);
439449
}
440450
}
441451
}, [currentModulePath]);
@@ -540,9 +550,9 @@ const App: React.FC = () => {
540550
};
541551

542552
const handleNewWorkspaceNameOk = (newWorkspaceName: string) => {
543-
const newWorkspacePath = commonStorage.makeModulePath(newWorkspaceName, newWorkspaceName);
553+
const newWorkspacePath = commonStorage.makeWorkspacePath(newWorkspaceName);
544554
if (newWorkspaceNameModalPurpose === 'NewWorkspace') {
545-
const workspaceContent = commonStorage.newModuleContent();
555+
const workspaceContent = commonStorage.newWorkspaceContent(newWorkspaceName);
546556
storage.createModule(
547557
commonStorage.MODULE_TYPE_WORKSPACE, newWorkspacePath, workspaceContent,
548558
(success: boolean, errorMessage: string) => {
@@ -595,10 +605,14 @@ const App: React.FC = () => {
595605
});
596606
};
597607

608+
// Provide a callback so the NewOpModeNameModal will know what the current
609+
// workspace name is.
598610
const getCurrentWorkspaceName = (): string => {
599-
return commonStorage.getWorkspaceName(currentModulePath);
611+
return currentModule ? currentModule.workspaceName : '';
600612
};
601613

614+
// Provide a callback so the NewOpModeNameModal will know what the existing
615+
// OpMode names are in the current workspace.
602616
const getOpModeNames = (workspaceName: string): string[] => {
603617
const opModeNames: string[] = [];
604618
for (const workspace of modules) {
@@ -615,7 +629,7 @@ const App: React.FC = () => {
615629
const handleNewOpModeNameOk = (workspaceName: string, newOpModeName: string) => {
616630
const newOpModePath = commonStorage.makeModulePath(workspaceName, newOpModeName);
617631
if (newOpModeNameModalPurpose === 'NewOpMode') {
618-
const opModeContent = commonStorage.newModuleContent();
632+
const opModeContent = commonStorage.newOpModeContent(workspaceName, newOpModeName);
619633
storage.createModule(
620634
commonStorage.MODULE_TYPE_OPMODE, newOpModePath, opModeContent,
621635
(success: boolean, errorMessage: string) => {
@@ -685,61 +699,70 @@ const App: React.FC = () => {
685699

686700
const handleRenameClicked = () => {
687701
checkIfBlocksWereModified(() => {
688-
const workspaceName = commonStorage.getWorkspaceName(currentModulePath);
689-
const moduleName = commonStorage.getModuleName(currentModulePath);
690-
if (workspaceName === moduleName) {
702+
if (!currentModule) {
703+
return;
704+
}
705+
if (currentModule.moduleType == commonStorage.MODULE_TYPE_WORKSPACE) {
691706
// This is a workspace.
692707
setNewWorkspaceNameModalPurpose('RenameWorkspace');
693-
setNewWorkspaceNameModalInitialValue(workspaceName);
708+
setNewWorkspaceNameModalInitialValue(currentModule.workspaceName);
694709
setNewWorkspaceNameModalIsOpen(true);
695-
} else {
710+
} else if (currentModule.moduleType == commonStorage.MODULE_TYPE_OPMODE) {
696711
// This is an OpMode.
697712
setNewOpModeNameModalPurpose('RenameOpMode');
698-
setNewOpModeNameModalInitialValue(moduleName);
713+
setNewOpModeNameModalInitialValue(currentModule.moduleName);
699714
setNewOpModeNameModalIsOpen(true);
700715
}
701716
});
702717
};
703718

704719
const handleCopyClicked = () => {
705720
checkIfBlocksWereModified(() => {
706-
const workspaceName = commonStorage.getWorkspaceName(currentModulePath);
707-
const moduleName = commonStorage.getModuleName(currentModulePath);
708-
if (workspaceName === moduleName) {
721+
if (!currentModule) {
722+
return;
723+
}
724+
if (currentModule.moduleType == commonStorage.MODULE_TYPE_WORKSPACE) {
709725
// This is a workspace.
710726
setNewWorkspaceNameModalPurpose('CopyWorkspace');
711-
setNewWorkspaceNameModalInitialValue(workspaceName + '_copy');
727+
setNewWorkspaceNameModalInitialValue(currentModule.workspaceName + '_copy');
712728
setNewWorkspaceNameModalIsOpen(true);
713-
} else {
729+
} else if (currentModule.moduleType == commonStorage.MODULE_TYPE_OPMODE) {
714730
// This is an OpMode.
715731
setNewOpModeNameModalPurpose('CopyOpMode');
716-
setNewOpModeNameModalInitialValue(moduleName + '_copy');
732+
setNewOpModeNameModalInitialValue(currentModule.moduleName + '_copy');
717733
setNewOpModeNameModalIsOpen(true);
718734
}
719735
});
720736
};
721737

722738
const handleDeleteClicked = () => {
723-
const workspaceName = commonStorage.getWorkspaceName(currentModulePath);
724-
const moduleName = commonStorage.getModuleName(currentModulePath);
725-
739+
if (!currentModule) {
740+
return;
741+
}
726742
// Show a bubble confirmation box to ask the user if they are sure.
727743
setPopconfirmTitle('Are you sure?');
728-
if (workspaceName === moduleName) {
744+
if (currentModule.moduleType == commonStorage.MODULE_TYPE_WORKSPACE) {
729745
setPopconfirmDescription('Press ok to delete this Workspace');
730-
} else {
746+
} else if (currentModule.moduleType == commonStorage.MODULE_TYPE_OPMODE) {
731747
setPopconfirmDescription('Press ok to delete this OpMode');
748+
} else if (currentModule.moduleType == commonStorage.MODULE_TYPE_MECHANISM) {
749+
// TODO: delete the mechanism.
750+
return;
732751
}
733752
// Set the function to be executed if the user clicks 'ok'.
734753
afterPopconfirmOk.current = () => {
735754
setOpenPopconfirm(false);
736755
checkIfBlocksWereModified(() => {
737-
if (workspaceName === moduleName) {
756+
if (!currentModule) {
757+
return;
758+
}
759+
if (currentModule.moduleType == commonStorage.MODULE_TYPE_WORKSPACE) {
738760
// This is a workspace.
739761
// Before deleting it, select another workspace, if there is one.
762+
const workspaceNameToDelete = currentModule.workspaceName;
740763
let foundAnotherWorkspace = false;
741764
for (const workspace of modules) {
742-
if (workspace.workspaceName !== workspaceName) {
765+
if (workspace.workspaceName !== workspaceNameToDelete) {
743766
setCurrentModulePath(workspace.modulePath);
744767
foundAnotherWorkspace = true;
745768
break;
@@ -748,7 +771,7 @@ const App: React.FC = () => {
748771
if (!foundAnotherWorkspace) {
749772
setCurrentModulePath('');
750773
}
751-
storage.deleteWorkspace(workspaceName,
774+
storage.deleteWorkspace(workspaceNameToDelete,
752775
(success: boolean, errorMessage: string) => {
753776
if (success) {
754777
setTriggerListModules(!triggerListModules);
@@ -757,11 +780,12 @@ const App: React.FC = () => {
757780
setAlertErrorVisible(true);
758781
}
759782
});
760-
} else {
783+
} else if (currentModule.moduleType == commonStorage.MODULE_TYPE_OPMODE) {
761784
// This is an OpMode.
762-
const workspacePath = commonStorage.makeModulePath(workspaceName, workspaceName);
785+
const modulePathToDelete = currentModulePath;
786+
const workspacePath = commonStorage.makeWorkspacePath(currentModule.workspaceName);
763787
setCurrentModulePath(workspacePath);
764-
storage.deleteOpMode(currentModulePath,
788+
storage.deleteOpMode(modulePathToDelete,
765789
(success: boolean, errorMessage: string) => {
766790
if (success) {
767791
setTriggerListModules(!triggerListModules);

src/blocks/mrc_call_python_function.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import { Order, PythonGenerator } from 'blockly/python';
2525

2626
import * as pythonUtils from './utils/generated/python';
2727
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
28-
import { getAllowedTypesForSetCheck, getOutputCheck, addImport } from './utils/python';
28+
import { getAllowedTypesForSetCheck, getOutputCheck } from './utils/python';
29+
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2930
import { MRC_STYLE_FUNCTIONS } from '../themes/styles'
3031

3132
// A block to call a python function.
@@ -258,11 +259,11 @@ export const setup = function() {
258259

259260
export const pythonFromBlock = function(
260261
block: Blockly.Block,
261-
generator: PythonGenerator,
262+
generator: ExtendedPythonGenerator,
262263
) {
263264
const callPythonFunctionBlock = block as CallPythonFunctionBlock;
264265
if (callPythonFunctionBlock.mrcImportModule) {
265-
addImport(generator, callPythonFunctionBlock.mrcImportModule);
266+
generator.addImport(callPythonFunctionBlock.mrcImportModule);
266267
}
267268
let code;
268269
let argStartIndex = 0;
@@ -311,7 +312,7 @@ export const pythonFromBlock = function(
311312

312313
function generateCodeForArguments(
313314
block: CallPythonFunctionBlock,
314-
generator: PythonGenerator,
315+
generator: ExtendedPythonGenerator,
315316
startIndex: number) {
316317
let code = '';
317318
if (block.mrcArgs.length - startIndex === 1) {

src/blocks/mrc_class_method_def.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const CLASS_METHOD_DEF = {
9191
this.setOutput(false);
9292
this.setStyle(MRC_STYLE_CLASS_BLOCKS);
9393
this.appendStatementInput('STACK').appendField('');
94+
this.mrcParameters = [];
9495
},
9596
/**
9697
* Returns the state of this block as a JSON serializable object.
@@ -153,8 +154,8 @@ const CLASS_METHOD_DEF = {
153154
}
154155
else {
155156
input.insertFieldAt(0, createFieldNonEditableText(name), 'NAME');
156-
//TODO: This should turn it off, but I have an outstanding question on blockly mailing list on how to do that in typescript
157-
// this.setMutator( null );
157+
//Case because a current bug in blockly where it won't allow passing null to Blockly.Block.setMutator makes it necessary.
158+
(this as Blockly.BlockSvg).setMutator( null );
158159
}
159160
this.setDeletable(this.mrcCanDelete);
160161
this.mrcUpdateParams();
@@ -345,7 +346,6 @@ export const setup = function() {
345346
};
346347

347348
import { Order, PythonGenerator } from 'blockly/python';
348-
import { Block } from 'src/Blockly';
349349

350350
export const pythonFromBlock = function (
351351
block: ClassMethodDefBlock,

src/blocks/mrc_get_python_enum_value.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import { Order, PythonGenerator } from 'blockly/python';
2626
import * as pythonUtils from './utils/generated/python';
2727
import { createFieldDropdown } from '../fields/FieldDropdown';
2828
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
29-
import { getOutputCheck, addImport } from './utils/python';
29+
import { getOutputCheck } from './utils/python';
30+
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
3031
import { MRC_STYLE_ENUM } from '../themes/styles'
3132

3233

@@ -137,13 +138,13 @@ export const setup = function() {
137138

138139
export const pythonFromBlock = function(
139140
block: Blockly.Block,
140-
generator: PythonGenerator,
141+
generator: ExtendedPythonGenerator,
141142
) {
142143
const getPythonEnumValueBlock = block as GetPythonEnumValueBlock;
143144
const enumClassName = block.getFieldValue(pythonUtils.FIELD_ENUM_CLASS_NAME);
144145
const enumValue = block.getFieldValue(pythonUtils.FIELD_ENUM_VALUE);
145146
if (getPythonEnumValueBlock.mrcImportModule) {
146-
addImport(generator, getPythonEnumValueBlock.mrcImportModule);
147+
generator.addImport(getPythonEnumValueBlock.mrcImportModule);
147148
}
148149
const code = enumClassName + '.' + enumValue;
149150
return [code, Order.MEMBER];

src/blocks/mrc_get_python_variable.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import { Order, PythonGenerator } from 'blockly/python';
2626
import * as pythonUtils from './utils/generated/python';
2727
import { createFieldDropdown } from '../fields/FieldDropdown';
2828
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
29-
import { getAllowedTypesForSetCheck, getOutputCheck, addImport } from './utils/python';
29+
import { getAllowedTypesForSetCheck, getOutputCheck } from './utils/python';
30+
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
31+
3032
import { MRC_STYLE_VARIABLES } from '../themes/styles';
3133
// A block to get a python variable.
3234

@@ -255,7 +257,7 @@ export const setup = function() {
255257

256258
export const pythonFromBlock = function(
257259
block: Blockly.Block,
258-
generator: PythonGenerator,
260+
generator: ExtendedPythonGenerator,
259261
) {
260262
const getPythonVariableBlock = block as GetPythonVariableBlock;
261263
const varName = getPythonVariableBlock.mrcActualVariableName
@@ -265,15 +267,15 @@ export const pythonFromBlock = function(
265267
case VAR_KIND_MODULE: {
266268
const moduleName = block.getFieldValue(pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
267269
if (getPythonVariableBlock.mrcImportModule) {
268-
addImport(generator, getPythonVariableBlock.mrcImportModule);
270+
generator.addImport(getPythonVariableBlock.mrcImportModule);
269271
}
270272
const code = moduleName + '.' + varName;
271273
return [code, Order.MEMBER];
272274
}
273275
case VAR_KIND_CLASS: {
274276
const className = block.getFieldValue(pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
275277
if (getPythonVariableBlock.mrcImportModule) {
276-
addImport(generator, getPythonVariableBlock.mrcImportModule);
278+
generator.addImport(getPythonVariableBlock.mrcImportModule);
277279
}
278280
const code = className + '.' + varName;
279281
return [code, Order.MEMBER];

src/blocks/mrc_set_python_variable.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import { Order, PythonGenerator } from 'blockly/python';
2626
import * as pythonUtils from './utils/generated/python';
2727
import { createFieldDropdown } from '../fields/FieldDropdown';
2828
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
29-
import { getAllowedTypesForSetCheck, getOutputCheck, addImport } from './utils/python';
29+
import { getAllowedTypesForSetCheck, getOutputCheck } from './utils/python';
30+
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
3031
import { MRC_STYLE_VARIABLES } from '../themes/styles';
3132

3233
// A block to set a python variable.
@@ -256,7 +257,7 @@ export const setup = function() {
256257

257258
export const pythonFromBlock = function(
258259
block: Blockly.Block,
259-
generator: PythonGenerator,
260+
generator: ExtendedPythonGenerator,
260261
) {
261262
const setPythonVariableBlock = block as SetPythonVariableBlock;
262263
const varName = setPythonVariableBlock.mrcActualVariableName
@@ -267,7 +268,7 @@ export const pythonFromBlock = function(
267268
const moduleName = block.getFieldValue(pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
268269
const value = generator.valueToCode(block, 'VALUE', Order.NONE);
269270
if (setPythonVariableBlock.mrcImportModule) {
270-
addImport(generator, setPythonVariableBlock.mrcImportModule);
271+
generator.addImport(setPythonVariableBlock.mrcImportModule);
271272
}
272273
const code = moduleName + '.' + varName + ' = ' + value + ';\n';
273274
return code;
@@ -276,7 +277,7 @@ export const pythonFromBlock = function(
276277
const className = block.getFieldValue(pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
277278
const value = generator.valueToCode(block, 'VALUE', Order.NONE);
278279
if (setPythonVariableBlock.mrcImportModule) {
279-
addImport(generator, setPythonVariableBlock.mrcImportModule);
280+
generator.addImport(setPythonVariableBlock.mrcImportModule);
280281
}
281282
const code = className + '.' + varName + ' = ' + value + ';\n';
282283
return code;

src/blocks/utils/python.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* @author [email protected] (Liz Looney)
2020
*/
2121

22-
import { PythonGenerator } from 'blockly/python';
2322
import * as pythonUtils from './generated/python';
2423

2524
export const RETURN_TYPE_NONE = 'None';
@@ -94,13 +93,6 @@ export function getOutputCheck(type: string): string {
9493
return type;
9594
}
9695

97-
// Functions used in python code generation for multiple python blocks.
98-
99-
export function addImport(generator: PythonGenerator, importModule: string): void {
100-
(generator as any).definitions_['import_' + importModule] =
101-
'import ' + importModule;
102-
}
103-
10496
// Function to return a legal name based off of proposed names and making sure it doesn't conflict
10597
// This is a legal name for python methods and variables.
10698
export function getLegalName(proposedName: string, existingNames: string[]){

0 commit comments

Comments
 (0)