@@ -28,6 +28,8 @@ import { mechanism_class_blocks } from './mechanism_class_methods';
28
28
import { opmode_class_blocks } from './opmode_class_methods' ;
29
29
import { robot_class_blocks } from './robot_class_methods' ;
30
30
import { ClassMethodDefBlock } from '../blocks/mrc_class_method_def'
31
+ import { addInstanceWithinBlocks } from '../blocks/mrc_call_python_function'
32
+ import { Editor } from '../editor/editor' ;
31
33
32
34
33
35
const CUSTOM_CATEGORY_METHODS = 'METHODS' ;
@@ -56,32 +58,29 @@ export class MethodsCategory {
56
58
// Add blocks for defining any methods that can be defined in the current
57
59
// module. For example, if the current module is an OpMode, add blocks to
58
60
// define the methods declared in the OpMode class.
59
- if ( this . currentModule ) {
60
- // Collect the method names for mrc_class_method_def blocks that are
61
- // already in the blockly workspace.
62
- const methodNamesAlreadyUsed : string [ ] = [ ] ;
63
- workspace . getBlocksByType ( 'mrc_class_method_def' , false ) . forEach ( ( block ) => {
64
- const classMethodDefBlock = block as ClassMethodDefBlock ;
65
- if ( ! classMethodDefBlock . mrcCanChangeSignature ) {
66
- methodNamesAlreadyUsed . push ( classMethodDefBlock . getFieldValue ( 'NAME' ) ) ;
61
+
62
+ const editor = Editor . getEditorForBlocklyWorkspace ( workspace ) ;
63
+ if ( editor ) {
64
+ // Collect the method names that are already overridden in the blockly workspace.
65
+ const methodNamesAlreadyOverridden = editor . getMethodNamesAlreadyOverriddenInWorkspace ( ) ;
66
+
67
+ if ( this . currentModule ) {
68
+ if ( this . currentModule . moduleType == commonStorage . MODULE_TYPE_ROBOT ) {
69
+ // Add the methods for a Robot.
70
+ this . addClassBlocksForCurrentModule (
71
+ 'More Robot Methods' , robot_class_blocks ,
72
+ methodNamesAlreadyOverridden , contents ) ;
73
+ } else if ( this . currentModule . moduleType == commonStorage . MODULE_TYPE_MECHANISM ) {
74
+ // Add the methods for a Mechanism.
75
+ this . addClassBlocksForCurrentModule (
76
+ 'More Mechanism Methods' , mechanism_class_blocks ,
77
+ methodNamesAlreadyOverridden , contents ) ;
78
+ } else if ( this . currentModule . moduleType == commonStorage . MODULE_TYPE_OPMODE ) {
79
+ // Add the methods for an OpMode.
80
+ this . addClassBlocksForCurrentModule (
81
+ 'More OpMode Methods' , opmode_class_blocks ,
82
+ methodNamesAlreadyOverridden , contents ) ;
67
83
}
68
- } ) ;
69
-
70
- if ( this . currentModule . moduleType == commonStorage . MODULE_TYPE_ROBOT ) {
71
- // Add the methods for a Robot.
72
- this . addClassBlocksForCurrentModule (
73
- 'More Robot Methods' , robot_class_blocks ,
74
- methodNamesAlreadyUsed , contents ) ;
75
- } else if ( this . currentModule . moduleType == commonStorage . MODULE_TYPE_MECHANISM ) {
76
- // Add the methods for a Mechanism.
77
- this . addClassBlocksForCurrentModule (
78
- 'More Mechanism Methods' , mechanism_class_blocks ,
79
- methodNamesAlreadyUsed , contents ) ;
80
- } else if ( this . currentModule . moduleType == commonStorage . MODULE_TYPE_OPMODE ) {
81
- // Add the methods for an OpMode.
82
- this . addClassBlocksForCurrentModule (
83
- 'More OpMode Methods' , opmode_class_blocks ,
84
- methodNamesAlreadyUsed , contents ) ;
85
84
}
86
85
}
87
86
@@ -103,39 +102,14 @@ export class MethodsCategory {
103
102
returnType : 'None' ,
104
103
params : [ ] ,
105
104
} ,
106
- } ) ;
107
-
108
- // For each mrc_class_method_def block in the blockly workspace, check if it
109
- // can be called from within the class, and if so, add a
110
- // mrc_call_python_function block.
111
- workspace . getBlocksByType ( 'mrc_class_method_def' , false ) . forEach ( ( block ) => {
112
- const classMethodDefBlock = block as ClassMethodDefBlock ;
113
- if ( classMethodDefBlock . mrcCanBeCalledWithinClass ) {
114
- const callPythonFunctionBlock : toolboxItems . Block = {
115
- kind : 'block' ,
116
- type : 'mrc_call_python_function' ,
117
- extraState : {
118
- functionKind : 'instance_within' ,
119
- returnType : classMethodDefBlock . mrcReturnType ,
120
- args : [ ] ,
121
- importModule : '' ,
122
- } ,
123
- fields : {
124
- FUNC : classMethodDefBlock . getFieldValue ( 'NAME' ) ,
125
- } ,
126
- } ;
127
- classMethodDefBlock . mrcParameters . forEach ( ( param ) => {
128
- if ( callPythonFunctionBlock . extraState ) {
129
- callPythonFunctionBlock . extraState . args . push (
130
- {
131
- name : param . name ,
132
- type : param . type ?? '' ,
133
- } ) ;
134
- }
135
- } ) ;
136
- contents . push ( callPythonFunctionBlock ) ;
137
105
}
138
- } ) ;
106
+ ) ;
107
+
108
+ // Get blocks for calling methods defined in the current workspace.
109
+ if ( editor ) {
110
+ const methodsFromWorkspace = editor . getMethodsForWithinFromWorkspace ( ) ;
111
+ addInstanceWithinBlocks ( methodsFromWorkspace , contents ) ;
112
+ }
139
113
140
114
const toolboxInfo = {
141
115
contents : contents ,
@@ -146,12 +120,12 @@ export class MethodsCategory {
146
120
147
121
private addClassBlocksForCurrentModule (
148
122
label : string , class_blocks : toolboxItems . Block [ ] ,
149
- methodNamesAlreadyUsed : string [ ] , contents : toolboxItems . ContentsType [ ] ) {
123
+ methodNamesAlreadyOverridden : string [ ] , contents : toolboxItems . ContentsType [ ] ) {
150
124
let labelAdded = false ;
151
125
class_blocks . forEach ( ( blockInfo ) => {
152
126
if ( blockInfo . fields ) {
153
127
const methodName = blockInfo . fields [ 'NAME' ] ;
154
- if ( ! methodNamesAlreadyUsed . includes ( methodName ) ) {
128
+ if ( ! methodNamesAlreadyOverridden . includes ( methodName ) ) {
155
129
if ( ! labelAdded ) {
156
130
contents . push (
157
131
{
0 commit comments