Skip to content

Commit

Permalink
updated core to 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kevthunder committed May 26, 2015
1 parent 2c0dbd0 commit 3eb02ba
Show file tree
Hide file tree
Showing 16 changed files with 609 additions and 292 deletions.
67 changes: 56 additions & 11 deletions codewave_core/box_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@
class BoxHelper():
def __init__(self, context, options = {}):
self.context = context
defaults = {
self.defaults = {
'deco': self.context.codewave.deco,
'pad': 2,
'width': 50,
'height': 3,
'openText': '',
'closeText': '',
'prefix': '',
'suffix': '',
'indent': 0
}
for key, val in defaults.items():
for key, val in self.defaults.items():
if key in options:
setattr(self,key,options[key])
else:
setattr(self,key,val)
def clone(self):
opt = {}
for key, val in self.defaults.items():
opt[key] = getattr(self,key)
return BoxHelper(self.context,opt)
def draw(self,text):
return self.startSep() + "\n" + self.lines(text) + "\n"+ self.endSep()
def wrapComment(self,str):
Expand All @@ -28,10 +35,10 @@ def separator(self):
return self.wrapComment(self.decoLine(len))
def startSep(self):
ln = self.width + 2 * self.pad + 2 * len(self.deco) - len(self.openText)
return self.wrapComment(self.openText+self.decoLine(ln))
return self.prefix + self.wrapComment(self.openText+self.decoLine(ln))
def endSep(self):
ln = self.width + 2 * self.pad + 2 * len(self.deco) - len(self.closeText)
return self.wrapComment(self.closeText+self.decoLine(ln))
return self.wrapComment(self.closeText+self.decoLine(ln)) + self.suffix
def decoLine(self,len):
return util.repeatToLength(self.deco, len)
def padding(self):
Expand All @@ -53,20 +60,58 @@ def line(self,text = ''):
self.padding() +
self.deco
))
def left(self):
return self.context.wrapCommentLeft(self.deco + self.padding())
def right(self):
return self.context.wrapCommentRight(self.padding() + self.deco)
def removeIgnoredContent(self,text):
return self.context.codewave.removeMarkers(self.context.codewave.removeCarret(text))
def textBounds(self,text):
return util.getTxtSize(self.removeIgnoredContent(text))
def getBoxForPos(self,pos):
startFind = self.context.wrapCommentLeft(self.deco + self.deco)
endFind = self.context.wrapCommentRight(self.deco + self.deco)
start = self.context.codewave.findPrev(pos.start, startFind)
end = self.context.codewave.findNext(pos.end, endFind)
if start is not None and end is not None:
return util.Pos(start,end + len(endFind))
depth = self.getNestedLvl(pos.start)
if depth > 0:
left = self.left()
curLeft = util.repeat(left,depth-1)

clone = self.clone()
placeholder = "###PlaceHolder###"
clone.width = len(placeholder)
clone.openText = clone.closeText = self.deco + self.deco + placeholder + self.deco + self.deco
escPlaceholder = util.escapeRegExp("###PlaceHolder###")


startFind = re.compile(util.escapeRegExp(curLeft + clone.startSep()).replace(escPlaceholder,'.*'))
endFind = re.compile(util.escapeRegExp(curLeft + clone.endSep()).replace(escPlaceholder,'.*'))


pair = util.Pair(startFind,endFind,{
'validMatch': self.validPairMatch
})
res = pair.wrapperPos(pos,self.context.codewave.editor.text)

if res is not None:
res.start += len(curLeft)
return res

def validPairMatch(self,match):
left = self.left()
f = self.context.codewave.findAnyNext(match.start() ,[left,"\n","\r"],-1)
return not f is not None or f.str != left

def getNestedLvl(self,index):
depth = 0
left = self.left()
while True:
f = self.context.codewave.findAnyNext(index ,[left,"\n","\r"],-1)
if f is None or f.str != left:
break
index = f.pos
depth+=1
return depth
def getOptFromLine(self,line,getPad=True):
rStart = re.compile("(\\s*)("+util.escapeRegExp(self.context.wrapCommentLeft(self.deco))+")(\\s*)")
rEnd = re.compile("(\\s*)("+util.escapeRegExp(self.context.wrapCommentRight(self.deco))+")")
rEnd = re.compile("(\\s*)("+util.escapeRegExp(self.context.wrapCommentRight(self.deco))+")(\n|$)")
resStart = rStart.search(line)
resEnd = rEnd.search(line)
if resStart is not None and resEnd is not None:
Expand Down
18 changes: 11 additions & 7 deletions codewave_core/closing_promp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ClosingPromp():
def __init__(self, codewave,selections):
self.codewave = codewave
self._typed = None
self.nbChanges = 0
self.selections = util.PosCollection(selections)
def begin(self):
self.started = True
Expand All @@ -24,6 +25,7 @@ def onChange(self,ch = None):
self.invalidTyped()
if self.skipEvent(ch):
return
self.nbChanges+=1
if self.shouldStop():
self.stop()
self.cleanClose()
Expand All @@ -44,13 +46,14 @@ def cleanClose(self):
selections = self.getSelections()
start = None
for sel in selections:
sel.withEditor(self.codewave.editor)
pos = self.whithinOpenBounds(sel)
if pos is not None:
start = sel
elif start is not None:
end = self.whithinCloseBounds(sel)
if end is not None:
res = end.innerTextFromEditor(self.codewave.editor).split(' ')[0]
res = end.innerText().split(' ')[0]
repl = util.Replacement(end.innerStart,end.innerEnd,res)
repl.selections = [start]
replacements.append(repl)
Expand All @@ -71,15 +74,16 @@ def cancelSelections(self,selections):
replacements = []
start = None
for sel in selections:
sel.withEditor(self.codewave.editor)
pos = self.whithinOpenBounds(sel)
if pos is not None:
start = pos
else:
end = self.whithinCloseBounds(sel)
if end is not None and start is not None:
start = None
replacements.append(util.Replacement(start.start,end.end,self.codewave.editor.textSubstr(start.end+1, end.start-1)).selectContent())
self.codewave.editor.applyReplacements(self.replacements)
start = None
self.codewave.editor.applyReplacements(replacements)
def typed(self):
if self._typed is None:
cpos = self.codewave.editor.getCursorPos()
Expand All @@ -97,26 +101,26 @@ def whithinOpenBounds(self,pos):
for i, repl in enumerate(self.replacements):
targetPos = self.startPosAt(i)
targetText = self.codewave.brakets + (self.typed() or '') + self.codewave.brakets
if targetPos.innerContainsPos(pos) and targetPos.textFromEditor(self.codewave.editor) == targetText:
if targetPos.innerContainsPos(pos) and targetPos.text() == targetText:
return targetPos
return None
def whithinCloseBounds(self,pos):
for i, repl in enumerate(self.replacements):
targetPos = self.endPosAt(i)
targetText = self.codewave.brakets + self.codewave.closeChar + self.typed() + self.codewave.brakets
if targetPos.innerContainsPos(pos) and targetPos.textFromEditor(self.codewave.editor) == targetText:
if targetPos.innerContainsPos(pos) and targetPos.text() == targetText:
return targetPos
return None
def startPosAt(self,index):
return util.Pos(
self.replacements[index].selections[0].start + len(self.typed() or '') * (index*2),
self.replacements[index].selections[0].end + len(self.typed() or '') * (index*2 +1)
).wrappedBy(self.codewave.brakets, self.codewave.brakets)
).wrappedBy(self.codewave.brakets, self.codewave.brakets).withEditor(self.codewave.editor)
def endPosAt(self,index):
return util.Pos(
self.replacements[index].selections[1].start + len(self.typed()) * (index*2 +1),
self.replacements[index].selections[1].end + len(self.typed()) * (index*2 +2)
).wrappedBy(self.codewave.brakets + self.codewave.closeChar, self.codewave.brakets)
).wrappedBy(self.codewave.brakets + self.codewave.closeChar, self.codewave.brakets).withEditor(self.codewave.editor)

class SimulatedClosingPromp(ClosingPromp):
def resume(self):
Expand Down
36 changes: 24 additions & 12 deletions codewave_core/cmd_finder.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import codewave_core.command as command
import codewave_core.core_cmds as core_cmds
import codewave_core.logger as logger
import codewave_core.util as util
import codewave_core.context as context

import codewave_core.cmds.core
import codewave_core.cmds.html
import codewave_core.cmds.js
import codewave_core.cmds.php

class CmdFinder():
def __init__(self,names, **options):


self.posibilities = None
if not util.isArray(names):
names = [names]
defaults = {
Expand Down Expand Up @@ -40,10 +45,10 @@ def find(self):
self.triggerDetectors()
self.cmd = self.findIn(self.root)
return self.cmd
def getPosibilities(self):
self.triggerDetectors()
path = list(self.path)
return self.findPosibilitiesIn(self.root,path)
# def getPosibilities(self):
# self.triggerDetectors()
# path = list(self.path)
# return self.findPosibilitiesIn(self.root,path)
def getNamesWithPaths(self):
paths = {}
for name in self.names :
Expand Down Expand Up @@ -90,13 +95,13 @@ def findPosibilities(self):
self.root.init()
posibilities = []
for space, names in self.getNamesWithPaths().items():
next = self.getCmdFollowAlias(space)
if next is not None :
nexts = self.getCmdFollowAlias(space)
for next in nexts:
posibilities += CmdFinder(names, parent=self, root=next).findPosibilities()
for nspc in self.context.getNameSpaces():
nspcName,rest = util.splitFirstNamespace(nspc,True)
next = self.getCmdFollowAlias(nspcName)
if next is not None :
nexts = self.getCmdFollowAlias(nspcName)
for next in nexts:
posibilities += CmdFinder(self.applySpaceOnNames(nspc), parent=self, root=next).findPosibilities()
for name in self.getDirectNames():
direct = self.root.getCmd(name)
Expand All @@ -106,18 +111,25 @@ def findPosibilities(self):
fallback = self.root.getCmd('fallback')
if self.cmdIsValid(fallback):
posibilities.append(fallback)
self.posibilities = posibilities
return posibilities
def getCmdFollowAlias(self,name):
cmd = self.root.getCmd(name)
if cmd is not None :
cmd.init()
if cmd.aliasOf is not None:
return cmd.getAliased()
return cmd
return [cmd,cmd.getAliased()]
return [cmd]
def cmdIsValid(self,cmd):
if cmd is None:
return False
if cmd.name != 'fallback' and cmd in self.ancestors():
return False
return not self.mustExecute or self.cmdIsExecutable(cmd)
def ancestors(self):
if self.codewave is not None and self.codewave.inInstance is not None:
return self.codewave.inInstance.ancestorCmdsAndSelf()
return []
def cmdIsExecutable(self,cmd):
names = self.getDirectNames()
if len(names) == 1:
Expand Down
29 changes: 16 additions & 13 deletions codewave_core/cmd_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ class CmdInstance(object):
def __init__(self, cmd = None, context = None):
self.cmd,self.context = cmd,context
self.content = self.cmdObj = None
self.indentLen = self.cmd = self.aliasedCmd = self.aliasedFinalCmd = self.cmdOptions = None
self.inited = False
self.indentLen = self.aliasedCmd = self.aliasedFinalCmd = self.cmdOptions = None

def init(self):
if not self.isEmpty() or self.inited:
if not self.isEmpty() and not self.inited:
self.inited = True
self._getCmdObj()
self._initParams()
Expand Down Expand Up @@ -43,7 +44,7 @@ def _getCmdObj(self):
def _initParams(self):
self.named = self.getDefaults()
def _getParentNamespaces(self):
return array()
return []
def isEmpty(self):
return self.cmd is not None
def resultIsAvailable(self):
Expand All @@ -61,7 +62,7 @@ def getDefaults(self):
aliased = self.getAliased()
if aliased is not None:
res = util.merge(res,aliased.getDefaults())
res = util.merge(res,cmd.defaults)
res = util.merge(res,self.cmd.defaults)
if self.cmdObj is not None:
res = util.merge(res,self.cmdObj.getDefaults())
return res
Expand All @@ -77,13 +78,13 @@ def getAliasedFinal(self):
if self.cmd.aliasOf is not None:
aliased = self.cmd
while aliased is not None and aliased.aliasOf is not None:
nspc, cmdName = util.splitNamespace(self.cmdName)
aliasOf = aliased.aliasOf.replace('%name%',cmdName)
aliased = aliased._aliasedFromFinder(self.getFinder(aliasOf))
aliased = aliased._aliasedFromFinder(self.getFinder(self.alterAliasOf(aliased.aliasOf)))
if self.aliasedCmd is None:
self.aliasedCmd = aliased or False
self.aliasedFinalCmd = aliased or False
return aliased
def alterAliasOf(self,aliasOf):
return aliasOf
def getOptions(self):
if self.cmd is not None:
if self.cmdOptions is not None:
Expand All @@ -98,14 +99,20 @@ def getOption(self,key):
if options is not None and key in options:
return options[key]
def getParam(self,names, defVal = None):
if type(names) is not list :
if not util.isArray(names) :
names = [names]
for n in names:
if isinstance( n, int ) and n < len(self.params) :
return self.params[n]
if n in self.named :
return self.named[n]
return defVal
def ancestorCmds(self):
if self.context.codewave is not None and self.context.codewave.inInstance is not None:
return self.context.codewave.inInstance.ancestorCmdsAndSelf()
return []
def ancestorCmdsAndSelf(self):
return self.ancestorCmds() + [self.cmd]
def runExecuteFunct(self):
if self.cmd is not None:
if self.cmdObj is not None:
Expand Down Expand Up @@ -149,8 +156,4 @@ def formatIndent(self,text):
else:
return text
def applyIndent(self,text):
if text is not None:
reg = re.compile(r'\n',re.M)
return re.sub(reg, "\n" + util.repeatToLength(" ",self.getIndent()),text)
else:
return text
return util.indentNotFirst(text,self.getIndent()," ")
Empty file added codewave_core/cmds/__init__.py
Empty file.
Loading

0 comments on commit 3eb02ba

Please sign in to comment.