fix cut/paste accidentally cloning stuff we don't actually want to clone

This commit is contained in:
Jeremy Penner 2020-08-03 10:35:29 -04:00
parent d8baf72cf6
commit d8c8d6b825
2 changed files with 20 additions and 18 deletions

View file

@ -20,6 +20,7 @@ from util import *
from contextlib import contextmanager
import itertools
import random
import inspect
# sobj -- generic hungarian for any object that is usable in scripts
# stmt -- statement; a synt that can be used as a line in a SyntBlock
@ -66,7 +67,7 @@ class Defs(TPrs):
class Vm(TPrs):
def InitPersistent(self, defs):
self.defs = defs
self.mpflagdef = { flagdef: flagdef.value for flagdef in defs.rgflagdef }
self.mpflagdef = {}
self.mpbotdef = { botdef: Rbot(botdef) for botdef in defs.rgbotdef if not botdef.fPlayer }
self.rgrbotPlayer = []
@ -208,6 +209,20 @@ class Synt(Typeable):
"Return the stype that should be applied to the given child."
return None
def Clone(self):
synt = self.ShallowClone()
synt.rgsynt = [child.Clone().WithParent(synt) for child in self.rgsynt]
return synt
def ShallowClone(self):
# assumes constructor takes named arguments and assigns them to attributes with the same name
# if a more complex case comes up the subclass should override ShallowClone to allow cut/paste
# to work
rgarg = []
for arg in inspect.getargspec(self.InitPersistent).args[1:]:
rgarg.append(getattr(self, arg))
return self.__class__(*rgarg)
def ProjectTypein(self, pcur):
return self.syntParent.ProjectTypeinForChild(pcur, self, self.StForTypein())
@ -317,9 +332,9 @@ class SyntDesc(Synt):
isynt += 1
class SyntText(Synt):
def InitPersistent(self):
def InitPersistent(self, st=""):
Synt.InitPersistent(self)
self.st = ""
self.st = st
def InitTransient(self):
Synt.InitTransient(self)
self.SetStTypein(self.st)
@ -377,10 +392,10 @@ class SyntBlock(Synt):
elif (key == ansi.K_DEL or key == ansi.K_CTRL('x')) and pwKey.pwChild.RgpwChild()[0] == psel.PwSelected(pwKey):
self.RemoveLine(synt, psel)
elif key == ansi.K_CTRL('c') and pwKey.pwChild.RgpwChild()[0] == psel.PwSelected(pwKey):
psel.syntClipboard = synt.DeepClone().WithParent(None)
psel.syntClipboard = synt.Clone().WithParent(None)
elif key == ansi.K_CTRL('v') and psel.syntClipboard:
isyntInsert = self.rgsynt.index(synt)
self.InsertLineAt(isyntInsert, psel.syntClipboard.DeepClone())
self.InsertLineAt(isyntInsert, psel.syntClipboard.Clone())
else:
return False
return True

View file

@ -84,9 +84,6 @@ class TPrs(object):
def UpgradeFrom(self, versionOld):
pass
def DeepClone(self):
return Odb.DeepClone(self)
def __getattr__(self, key):
stPersistent = "no persistent yet"
if key != "_persistent":
@ -165,16 +162,6 @@ class Odb(object):
cls.rgtprsToUpgrade = None
cls.rgtprsToInit = None
return tprs
@classmethod
def DeepClone(cls, tprs):
cls.rgtprsToInit = []
clone = cPickle.loads(cPickle.dumps(tprs))
for tprsToInit in cls.rgtprsToInit:
with tprsToInit.SetTransiently():
tprsToInit.InitTransient()
cls.rgtprsToInit = None
return clone
clsList = list
clsDict = dict