Skip to content

Commit 41b6b30

Browse files
committed
Renaming a parameter now updates it in method
1 parent 62d00c4 commit 41b6b30

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

src/blocks/mrc_class_method_def.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import { getLegalName } from './utils/python';
2828
import { Order } from 'blockly/python';
2929
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
3030
import { renameMethodCallers, mutateMethodCallers } from './mrc_call_python_function'
31+
import { findConnectedBlocksOfType } from './utils/find_connected_blocks';
32+
import { BLOCK_NAME as MRC_GET_PARAMETER_BLOCK_NAME } from './mrc_get_parameter';
33+
3134

3235
export const BLOCK_NAME = 'mrc_class_method_def';
3336

@@ -170,11 +173,16 @@ const CLASS_METHOD_DEF = {
170173
this.mrcParameters = [];
171174

172175
let paramBlock = containerBlock.getInputTargetBlock('STACK');
173-
while (paramBlock && !paramBlock.isInsertionMarker()) {
176+
while (paramBlock) {
174177
const param: Parameter = {
175178
name: paramBlock.getFieldValue('NAME'),
176179
type: ''
177180
}
181+
if (paramBlock.originalName) {
182+
// This is a mutator arg block, so we can get the original name.
183+
this.mrcRenameParameter(paramBlock.originalName, param.name);
184+
paramBlock.originalName = param.name;
185+
}
178186
this.mrcParameters.push(param);
179187
paramBlock =
180188
paramBlock.nextConnection && paramBlock.nextConnection.targetBlock();
@@ -194,13 +202,25 @@ const CLASS_METHOD_DEF = {
194202
for (let i = 0; i < this.mrcParameters.length; i++) {
195203
let itemBlock = workspace.newBlock(MUTATOR_BLOCK_NAME);
196204
(itemBlock as Blockly.BlockSvg).initSvg();
197-
itemBlock.setFieldValue(this.mrcParameters[i].name, 'NAME')
205+
itemBlock.setFieldValue(this.mrcParameters[i].name, 'NAME');
206+
(itemBlock as MethodMutatorArgBlock).originalName = this.mrcParameters[i].name;
198207

199208
connection!.connect(itemBlock.previousConnection!);
200209
connection = itemBlock.nextConnection;
201210
}
202211
return topBlock;
203212
},
213+
mrcRenameParameter: function (this: ClassMethodDefBlock, oldName: string, newName: string) {
214+
let nextBlock = this.getInputTargetBlock('STACK');
215+
216+
if(nextBlock){
217+
findConnectedBlocksOfType(nextBlock, MRC_GET_PARAMETER_BLOCK_NAME).forEach((block) => {
218+
if (block.getFieldValue('PARAMETER_NAME') === oldName) {
219+
block.setFieldValue(newName, 'PARAMETER_NAME');
220+
}
221+
});
222+
}
223+
},
204224
mrcUpdateParams: function (this: ClassMethodDefBlock) {
205225
if (this.mrcParameters.length > 0) {
206226
let input = this.getInput('TITLE');
@@ -305,8 +325,9 @@ const METHOD_PARAM_CONTAINER = {
305325

306326
type MethodMutatorArgBlock = Blockly.Block & MethodMutatorArgMixin & Blockly.BlockSvg;
307327
interface MethodMutatorArgMixin extends MethodMutatorArgMixinType {
308-
328+
originalName: string,
309329
}
330+
310331
type MethodMutatorArgMixinType = typeof METHODS_MUTATORARG;
311332

312333
function setName(block: Blockly.BlockSvg) {
@@ -332,6 +353,7 @@ const METHODS_MUTATORARG = {
332353
this.setPreviousStatement(true);
333354
this.setNextStatement(true);
334355
this.setStyle(MRC_STYLE_CLASS_BLOCKS);
356+
this.originalName = '';
335357
this.contextMenu = false;
336358
ChangeFramework.registerCallback(MUTATOR_BLOCK_NAME, [Blockly.Events.BLOCK_MOVE, Blockly.Events.BLOCK_CHANGE], this.onBlockChanged);
337359
},
@@ -350,6 +372,7 @@ const METHODS_MUTATORARG = {
350372
},
351373
}
352374

375+
353376
/**
354377
* Updates the procedure mutator's flyout so that the arg block is not a
355378
* duplicate of another arg.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Porpoiseful LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @fileoverview Allow registering for specific callbacks
20+
* @author [email protected] (Alan Smith)
21+
*/
22+
import * as Blockly from 'blockly';
23+
24+
export function findConnectedBlocksOfType(block: Blockly.Block, targetType: string): Blockly.Block[] {
25+
const foundBlocks: Blockly.Block[] = [];
26+
const visited = new Set<string>(); // Prevent infinite loops
27+
28+
function searchRecursive(currentBlock: Blockly.Block): void {
29+
if (visited.has(currentBlock.id)) return;
30+
visited.add(currentBlock.id);
31+
32+
// Check if current block matches target type
33+
if (currentBlock.type === targetType) {
34+
foundBlocks.push(currentBlock);
35+
}
36+
37+
// Search through all inputs
38+
currentBlock.inputList.forEach(input => {
39+
if (input.connection && input.connection.isConnected()) {
40+
const connectedBlock = input.connection.targetBlock();
41+
if (connectedBlock) {
42+
searchRecursive(connectedBlock);
43+
}
44+
}
45+
});
46+
}
47+
48+
searchRecursive(block);
49+
return foundBlocks;
50+
}

0 commit comments

Comments
 (0)