Merge pull request #171 from nathan/rename-fix

Fixed renaming procedures and variables
This commit is contained in:
Shane M. Clements 2014-05-14 13:25:59 -06:00
commit 3a70350043
2 changed files with 17 additions and 14 deletions

View file

@ -563,7 +563,7 @@ public class BlockMenus implements DragClient {
if (block.nextBlock) block.nextBlock.allBlocksDo(function(b:Block):void {
if (b.op == Specs.GET_PARAM) b.parameterIndex = -1; // parameters may have changed; clear cached indices
});
for each (var caller:Block in app.runtime.allCallsOf(oldSpec)) {
for each (var caller:Block in app.runtime.allCallsOf(oldSpec, app.viewedObj())) {
var oldArgs:Array = caller.args;
caller.setSpec(newSpec, block.defaultArgValues);
for (var i:int = 0; i < oldArgs.length; i++) {

View file

@ -591,15 +591,17 @@ public class ScratchRuntime {
}
public function renameVariable(oldName:String, newName:String, block:Block):void {
var v:Variable = app.viewedObj().lookupVar(oldName);
var owner:ScratchObj = app.viewedObj();
var v:Variable = owner.lookupVar(oldName);
if (v != null) {
if (!owner.ownsVar(v.name)) owner = app.stagePane;
v.name = newName;
if (v.watcher) v.watcher.changeVarName(newName);
} else {
app.viewedObj().lookupOrCreateVar(newName);
owner.lookupOrCreateVar(newName);
}
updateVarRefs(oldName, newName);
updateVarRefs(oldName, newName, owner);
clearAllCaches();
}
@ -607,9 +609,9 @@ public class ScratchRuntime {
public function makeVariable(varObj:Object):Variable { return new Variable(varObj.name, varObj.value); }
public function makeListWatcher():ListWatcher { return new ListWatcher(); }
private function updateVarRefs(oldName:String, newName:String):void {
private function updateVarRefs(oldName:String, newName:String, owner:ScratchObj):void {
// Change the variable name in all blocks that use it.
for each (var b:Block in allUsesOfVariable(oldName)) {
for each (var b:Block in allUsesOfVariable(oldName, owner)) {
if (b.op == Specs.GET_VAR) b.setSpec(newName);
else b.args[0].setArgValue(newName);
}
@ -715,25 +717,26 @@ public class ScratchRuntime {
return false;
}
public function allUsesOfVariable(varName:String):Array {
public function allUsesOfVariable(varName:String, owner:ScratchObj):Array {
var variableBlocks:Array = [Specs.SET_VAR, Specs.CHANGE_VAR, "showVariable:", "hideVariable:"];
var result:Array = [];
for each (var stack:Block in allStacks()) {
var stacks:Array = owner.isStage ? allStacks() : owner.scripts;
for each (var stack:Block in stacks) {
// for each block in stack
stack.allBlocksDo(function (b:Block):void {
if ((b.op == Specs.GET_VAR) && (b.spec == varName)) result.push(b);
if ((b.op == Specs.SET_VAR) && (b.args[0].argValue == varName)) result.push(b);
if ((b.op == Specs.CHANGE_VAR) && (b.args[0].argValue == varName)) result.push(b);
if (b.op == Specs.GET_VAR && b.spec == varName) result.push(b);
if (variableBlocks.indexOf(b.op) != -1 && b.args[0].argValue == varName) result.push(b);
});
}
return result;
}
public function allCallsOf(callee:String):Array {
public function allCallsOf(callee:String, owner:ScratchObj):Array {
var result:Array = [];
for each (var stack:Block in allStacks()) {
for each (var stack:Block in owner.scripts) {
// for each block in stack
stack.allBlocksDo(function (b:Block):void {
if ((b.op == Specs.CALL) && (b.spec == callee)) result.push(b);
if (b.op == Specs.CALL && b.spec == callee) result.push(b);
});
}
return result;