Custom block templates also visited, both options available from context menu

pull/29/head
bromagosa 2017-01-03 11:35:36 +01:00
rodzic c462786a9a
commit 7623cc9f4a
2 zmienionych plików z 71 dodań i 50 usunięć

Wyświetl plik

@ -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,6 +2404,9 @@ BlockMorph.prototype.userMenu = function () {
menu.addItem( menu.addItem(
'rename just here...', 'rename just here...',
function () { function () {
if (this.isTemplate) {
myself.refactorThisVar(true); // just the template
} else {
new DialogBoxMorph( new DialogBoxMorph(
myself, myself,
myself.userSetSpec, myself.userSetSpec,
@ -2417,7 +2419,11 @@ BlockMorph.prototype.userMenu = function () {
InputSlotMorph.prototype.getVarNamesDict.call(myself) 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,11 +3109,23 @@ BlockMorph.prototype.refactorThisVar = function () {
varExistsError('as a global variable'); varExistsError('as a global variable');
return; return;
} else { } else {
oldValue = receiver.variables.getVar(oldName);
receiver.deleteVariable(oldName);
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.refactorVariableInstances(oldName, newName, false);
receiver.customBlocks.forEach(function (eachBlock) { receiver.customBlocks.forEach(function (eachBlock) {
eachBlock.definition.body.expression.refactorVarInStack(oldName, newName); eachBlock.body.expression.refactorVarInStack(oldName, newName);
}); });
} }
}
} else { } else {
// global var // global var
if (!isNil(ide.globalVariables.vars[newName])) { if (!isNil(ide.globalVariables.vars[newName])) {
@ -3112,29 +3135,39 @@ 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);
if (oldWatcher && oldWatcher.isVisible) {
newWatcher = receiver.toggleVariableWatcher(newName, true);
newWatcher.setPosition(oldWatcher.position());
}
if (!justTheTemplate) {
stage.refactorVariableInstances(oldName, newName, true); stage.refactorVariableInstances(oldName, newName, true);
stage.globalBlocks.forEach(function (eachBlock) {
eachBlock.body.expression.refactorVarInStack(oldName, newName);
});
stage.forAllChildren(function (child) { stage.forAllChildren(function (child) {
if (child instanceof SpriteMorph) { if (child instanceof SpriteMorph) {
child.refactorVariableInstances(oldName, newName, true); 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');

Wyświetl plik

@ -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);