kopia lustrzana https://github.com/backface/turtlestitch
Custom block templates also visited, both options available from context menu
rodzic
c462786a9a
commit
7623cc9f4a
109
blocks.js
109
blocks.js
|
@ -2394,10 +2394,9 @@ BlockMorph.prototype.userMenu = function () {
|
||||||
'refactorThisVar'
|
'refactorThisVar'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return menu;
|
} else {
|
||||||
}
|
menu.addLine();
|
||||||
|
}
|
||||||
menu.addLine();
|
|
||||||
|
|
||||||
if (this.selector === 'reportGetVar') {
|
if (this.selector === 'reportGetVar') {
|
||||||
blck = this.fullCopy();
|
blck = this.fullCopy();
|
||||||
|
@ -2405,19 +2404,26 @@ BlockMorph.prototype.userMenu = function () {
|
||||||
menu.addItem(
|
menu.addItem(
|
||||||
'rename just here...',
|
'rename just here...',
|
||||||
function () {
|
function () {
|
||||||
new DialogBoxMorph(
|
if (this.isTemplate) {
|
||||||
myself,
|
myself.refactorThisVar(true); // just the template
|
||||||
myself.userSetSpec,
|
} else {
|
||||||
myself
|
new DialogBoxMorph(
|
||||||
).prompt(
|
myself,
|
||||||
"Variable name",
|
myself.userSetSpec,
|
||||||
myself.blockSpec,
|
myself
|
||||||
world,
|
).prompt(
|
||||||
blck.fullImage(), // pic
|
"Variable name",
|
||||||
InputSlotMorph.prototype.getVarNamesDict.call(myself)
|
myself.blockSpec,
|
||||||
);
|
world,
|
||||||
|
blck.fullImage(), // pic
|
||||||
|
InputSlotMorph.prototype.getVarNamesDict.call(myself)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
if (this.isTemplate) {
|
||||||
|
return menu;
|
||||||
|
}
|
||||||
} else if (SpriteMorph.prototype.blockAlternatives[this.selector]) {
|
} else if (SpriteMorph.prototype.blockAlternatives[this.selector]) {
|
||||||
menu.addItem(
|
menu.addItem(
|
||||||
'relabel...',
|
'relabel...',
|
||||||
|
@ -3064,14 +3070,15 @@ BlockMorph.prototype.codeMappingHeader = function () {
|
||||||
|
|
||||||
// Variable refactoring
|
// Variable refactoring
|
||||||
|
|
||||||
BlockMorph.prototype.refactorThisVar = function () {
|
BlockMorph.prototype.refactorThisVar = function (justTheTemplate) {
|
||||||
// Rename all occurrences of the variable this block is holding, taking care of its
|
// Rename all occurrences of the variable this block is holding,
|
||||||
// lexical scope
|
// taking care of its lexical scope
|
||||||
|
|
||||||
var oldName = this.blockSpec,
|
var myself = this,
|
||||||
|
oldName = this.blockSpec,
|
||||||
receiver = this.receiver(),
|
receiver = this.receiver(),
|
||||||
ide = this.parentThatIsA(IDE_Morph),
|
ide = this.parentThatIsA(IDE_Morph),
|
||||||
stage = receiver.parentThatIsA(StageMorph),
|
stage = ide.stage,
|
||||||
oldWatcher = receiver.findVariableWatcher(oldName),
|
oldWatcher = receiver.findVariableWatcher(oldName),
|
||||||
oldValue, newWatcher;
|
oldValue, newWatcher;
|
||||||
|
|
||||||
|
@ -3082,6 +3089,10 @@ BlockMorph.prototype.refactorThisVar = function () {
|
||||||
|
|
||||||
if (this.parent instanceof SyntaxElementMorph) {
|
if (this.parent instanceof SyntaxElementMorph) {
|
||||||
// script var
|
// script var
|
||||||
|
if (justTheTemplate) {
|
||||||
|
myself.userSetSpec(newName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
definer = this.parentThatIsA(CommandBlockMorph);
|
definer = this.parentThatIsA(CommandBlockMorph);
|
||||||
if (definer.definesScriptVariable(newName)) {
|
if (definer.definesScriptVariable(newName)) {
|
||||||
varExistsError();
|
varExistsError();
|
||||||
|
@ -3098,10 +3109,22 @@ BlockMorph.prototype.refactorThisVar = function () {
|
||||||
varExistsError('as a global variable');
|
varExistsError('as a global variable');
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
receiver.refactorVariableInstances(oldName, newName, false);
|
oldValue = receiver.variables.getVar(oldName);
|
||||||
receiver.customBlocks.forEach(function (eachBlock) {
|
receiver.deleteVariable(oldName);
|
||||||
eachBlock.definition.body.expression.refactorVarInStack(oldName, newName);
|
receiver.addVariable(newName, false);
|
||||||
});
|
receiver.variables.setVar(newName, oldValue);
|
||||||
|
|
||||||
|
if (oldWatcher && oldWatcher.isVisible) {
|
||||||
|
newWatcher = receiver.toggleVariableWatcher(newName, false);
|
||||||
|
newWatcher.setPosition(oldWatcher.position());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!justTheTemplate) {
|
||||||
|
receiver.refactorVariableInstances(oldName, newName, false);
|
||||||
|
receiver.customBlocks.forEach(function (eachBlock) {
|
||||||
|
eachBlock.body.expression.refactorVarInStack(oldName, newName);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// global var
|
// global var
|
||||||
|
@ -3112,31 +3135,41 @@ BlockMorph.prototype.refactorThisVar = function () {
|
||||||
detect(
|
detect(
|
||||||
stage.children,
|
stage.children,
|
||||||
function (any) {
|
function (any) {
|
||||||
return any.hasSpriteVariable(newName);
|
return any instanceof SpriteMorph &&
|
||||||
|
any.hasSpriteVariable(newName);
|
||||||
})
|
})
|
||||||
) {
|
) {
|
||||||
varExistsError('as a sprite local variable');
|
varExistsError('as a sprite local variable');
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
oldValue = ide.globalVariables.vars[oldName];
|
oldValue = ide.globalVariables.getVar(oldName);
|
||||||
stage.deleteVariable(oldName);
|
stage.deleteVariable(oldName);
|
||||||
stage.addVariable(newName, true);
|
stage.addVariable(newName, true);
|
||||||
ide.globalVariables.vars[newName] = oldValue;
|
ide.globalVariables.setVar(newName, oldValue);
|
||||||
|
|
||||||
stage.refactorVariableInstances(oldName, newName, true);
|
if (oldWatcher && oldWatcher.isVisible) {
|
||||||
stage.forAllChildren(function (child) {
|
newWatcher = receiver.toggleVariableWatcher(newName, true);
|
||||||
if (child instanceof SpriteMorph) {
|
newWatcher.setPosition(oldWatcher.position());
|
||||||
child.refactorVariableInstances(oldName, newName, true);
|
}
|
||||||
}
|
|
||||||
});
|
if (!justTheTemplate) {
|
||||||
|
stage.refactorVariableInstances(oldName, newName, true);
|
||||||
|
stage.globalBlocks.forEach(function (eachBlock) {
|
||||||
|
eachBlock.body.expression.refactorVarInStack(oldName, newName);
|
||||||
|
});
|
||||||
|
|
||||||
|
stage.forAllChildren(function (child) {
|
||||||
|
if (child instanceof SpriteMorph) {
|
||||||
|
child.refactorVariableInstances(oldName, newName, true);
|
||||||
|
child.customBlocks.forEach(function (eachBlock) {
|
||||||
|
eachBlock.body.expression.refactorVarInStack(oldName, newName);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oldWatcher && oldWatcher.isVisible) {
|
|
||||||
newWatcher = stage.toggleVariableWatcher(newName, true);
|
|
||||||
newWatcher.setPosition(oldWatcher.position());
|
|
||||||
}
|
|
||||||
|
|
||||||
ide.flushBlocksCache('variables');
|
ide.flushBlocksCache('variables');
|
||||||
ide.refreshPalette();
|
ide.refreshPalette();
|
||||||
},
|
},
|
||||||
|
|
12
objects.js
12
objects.js
|
@ -4821,22 +4821,10 @@ SpriteMorph.prototype.hasSpriteVariable = function (varName) {
|
||||||
// Variable refactoring
|
// Variable refactoring
|
||||||
|
|
||||||
SpriteMorph.prototype.refactorVariableInstances = function (oldName, newName, isGlobal) {
|
SpriteMorph.prototype.refactorVariableInstances = function (oldName, newName, isGlobal) {
|
||||||
var oldValue;
|
|
||||||
|
|
||||||
if (isGlobal && this.hasSpriteVariable(oldName)) {
|
if (isGlobal && this.hasSpriteVariable(oldName)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isGlobal) {
|
|
||||||
oldValue = this.variables.vars[oldName];
|
|
||||||
this.deleteVariable(oldName);
|
|
||||||
this.addVariable(newName, false);
|
|
||||||
this.variables.vars[newName] = oldValue;
|
|
||||||
this.blocksCache['variables'] = null;
|
|
||||||
this.paletteCache['variables'] = null;
|
|
||||||
this.parentThatIsA(IDE_Morph).refreshPalette();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.scripts.children.forEach(function (child) {
|
this.scripts.children.forEach(function (child) {
|
||||||
if (child instanceof BlockMorph) {
|
if (child instanceof BlockMorph) {
|
||||||
child.refactorVarInStack(oldName, newName);
|
child.refactorVarInStack(oldName, newName);
|
||||||
|
|
Ładowanie…
Reference in New Issue