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ęć

109
blocks.js
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,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();
}, },

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