From 1ca029e2b6d531196a96911213dbfe8392d5cf3a Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Thu, 8 Sep 2011 22:39:44 -0400 Subject: [PATCH] refactor syntline to represent the empty line, and replace itself fix up matching for variable references --- scripting.py | 87 ++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/scripting.py b/scripting.py index 810be86..ced63da 100644 --- a/scripting.py +++ b/scripting.py @@ -29,19 +29,23 @@ class Rtype(TPrs): for lit in self.RglitFromSt(st): yield SyntLit(None, lit, self) + for synt in self.RgsyntVarRefFromSt(syntBlock, st): + yield synt + + def RgsyntVarRefFromSt(self, syntBlock, st): while syntBlock != None: if isinstance(syntBlock, SyntBlock): for synt in self.RgsyntFromSyntBlock(syntBlock, st): yield synt syntBlock = syntBlock.syntParent - - # helper for RgsyntFromSt + + # helper for RgsyntVarRefFromSt def RgsyntFromSyntBlock(self, syntBlock, st): # yield all vars in scope - for syntLine in syntBlock.rgsynt: - if SyntVar.FVarOfType(syntLine.rgsynt[0], self): - yield SyntVarRef(None, syntLine.rgsynt[0]) - + for syntVar in syntBlock.rgsynt: + if SyntVar.FVarOfType(syntVar, self) and syntVar.name.St().lower().startswith(st.lower()): + yield SyntVarRef(None, syntVar) + def StForSobj(self, sobj): return str(sobj) def FMember(self, sobj): @@ -111,7 +115,7 @@ class Synt(Typeable): @classmethod def RgsyntReplace(cls, syntChild, st, rtype): return [] - + def ProjectTypeinForChild(self, pwParent, syntChild, st): def OnSelect(synt, psel): self.Replace(syntChild, synt) @@ -243,28 +247,14 @@ class SyntExpr(Synt): def Project(self, pcur): self.syntParent.ProjectTypeinForChild(pcur.PwHoriz(self), self, " ") -class SyntLine(Synt): +class SyntLine(SyntDesc): rgclsStmt = [] - def Populate(self): - self.rgsynt.append(SyntBlank(self)) - def SetStmt(self, syntStmt): - self.Replace(self.rgsynt[0], syntStmt) - def Project(self, pcur): - pwKey = PwKeyHandler(pcur.pwVert, self.HandleKey) - with pcur.ProjectHoriz(pwKey, self): - self.rgsynt[0].Project(pcur) - - def RgsyntForChild(self, syntChild, st): - for clsStmt in self.rgclsStmt: + desc = [[" "]] + @classmethod + def RgsyntReplace(cls, syntChild, st, rtype): + for clsStmt in cls.rgclsStmt: if clsStmt.StForTypein().lower().startswith(st.lower()): yield clsStmt(None) - - def HandleKey(self, pwKey, pov, psel, key): - if ansi.FEnter(key): - self.syntParent.InsertLineAfter(self, None) - psel.Inc(pwKey.PwParent()) - return True - return False class SyntBlock(Synt): def Project(self, pcur): @@ -277,18 +267,29 @@ class SyntBlock(Synt): psel.Inc(pcur.pwVert) PwButtonHidden(pcur.pwVert, "[insert new line]", None, OnInsertNewLine, pcur.dxindent) for syntLine in self.rgsynt: - syntLine.Project(pcur) + pwKey = PwKeyHandler(pcur.pwVert, self.HandleKey) + with pcur.ProjectHoriz(pwKey, syntLine): + syntLine.Project(pcur) + + def RgsyntForChild(self, syntChild, st): + return SyntLine.RgsyntReplace(syntChild, st, None) def InsertLineAfter(self, syntAfter, syntStmt): - syntLine = SyntLine(self) - if syntStmt != None: - syntLine.SetStmt(syntStmt) + if syntStmt == None: + syntStmt = SyntLine(self) if syntAfter == None: - self.rgsynt.insert(0, syntLine) + self.rgsynt.insert(0, syntStmt) else: - self.rgsynt.insert(self.rgsynt.index(syntAfter) + 1, syntLine) - return syntLine - + self.rgsynt.insert(self.rgsynt.index(syntAfter) + 1, syntStmt) + syntStmt.syntParent = self + return syntStmt + + def HandleKey(self, pwKey, pov, psel, key): + if ansi.FEnter(key): + self.InsertLineAfter(pwKey.pwChild.Value(), None) + psel.Inc(pwKey.PwParent()) + return True + return False class SyntLit(Synt): def InitPersistent(self, syntParent, value, rtype): Synt.InitPersistent(self, syntParent) @@ -300,11 +301,7 @@ class SyntLit(Synt): return self.rtype def Project(self, pcur): self.syntParent.ProjectTypeinForChild(pcur.PwHoriz(self), self, self.StForTypein()) - -@RegStmt -class SyntBlank(SyntDesc): - desc = [[" "]] - + @RegStmt class SyntVar(SyntDesc): desc = [["Define"], " ", (SyntName, None), " to be ", (SyntExpr, 1)] @@ -318,6 +315,10 @@ class SyntVarRef(Synt): def __init__(self, syntParent, syntVar = None): Synt.__init__(self, syntParent) self.syntVar = syntVar + @classmethod + def RgsyntReplace(cls, syntChild, st, rtype): + rtype = rtype and RtypeAny() + return rtype.RgsyntVarRefForChild(syntChild, st) def Rtype(self): return self.syntVar.Rtype() def StForTypein(self): @@ -566,14 +567,14 @@ class PwButtonHidden(PwButton): if mpksel.Get(self) == Ksel.NAV: PwButton.Draw(self, ascr, x + self.dxIndent, y, w - self.dxIndent, dxStart, mpksel, fOverlay) -class PwKeyHandler(PwContainer): +class PwKeyHandler(PwContainOne): def __init__(self, pwParent, dgHandleKey): - PwContainer.__init__(self, pwParent) + PwContainOne.__init__(self, pwParent) self.dgHandleKey = dgHandleKey def DxDyNew(self, w, dxStart, mpksel): - return self.RgpwChild()[0].DxDyNew(w, dxStart, mpksel) + return self.pwChild.DxDyNew(w, dxStart, mpksel) def Draw(self, ascr, x, y, w, dxStart, mpksel, fOverlay): - self.RgpwChild()[0].Draw(ascr, x, y, w, dxStart, mpksel, fOverlay) + self.pwChild.Draw(ascr, x, y, w, dxStart, mpksel, fOverlay) def HandleKey(self, pov, psel, key): self.dgHandleKey(self, pov, psel, key)