Beispiel #1
0
 def __init__(self):
     # Used because some refactorings delegate back to the user.
     # this flag ensures that code isnt imported during those times
     self.readyToLoadNewCode = 1 
     self.paths = []
     getUndoStack(1)  # force new undo stack
     self.promptUserClientCallback = None
Beispiel #2
0
 def __init__(self):
     # Used because some refactorings delegate back to the user.
     # this flag ensures that code isnt imported during those times
     self.readyToLoadNewCode = 1
     self.paths = []
     getUndoStack(1)  # force new undo stack
     self.promptUserClientCallback = None
 def rewriteString(self, srcnode, lineno, colno, newname):
     filename = srcnode.filename
     if not self.modifiedsrc.has_key(filename):
         getUndoStack().addSource(filename,srcnode.getSource())
         self.modifiedsrc[filename] = {}
     if not self.modifiedsrc[filename].has_key(lineno):
         line = srcnode.getLines()[lineno-1]
         self.modifiedsrc[filename][lineno] = self._lineToDict(line)
     self.modifiedsrc[filename][lineno][colno] = newname
Beispiel #4
0
    def __init__(self):
        self.ast = getRoot()

        # Used because some refactorings delegate back to the user.
        # this flag ensures that code isnt imported during those times
        self.readyToLoadNewCode = 1 
        self.paths = []
        getUndoStack(1)  # force new undo stack
        if not getRoot().unittestmode:
            log.warning = sys.stderr            
        self.promptUserClientCallback = None
Beispiel #5
0
    def __init__(self):
        self.ast = getRoot()

        # Used because some refactorings delegate back to the user.
        # this flag ensures that code isnt imported during those times
        self.readyToLoadNewCode = 1
        self.paths = []
        getUndoStack(1)  # force new undo stack
        if not getRoot().unittestmode:
            log.warning = sys.stderr
        self.promptUserClientCallback = None
    def execute(self):
        self.deduceArguments()
        getUndoStack().addSource(self.sourcenode.filename,
                                 self.sourcenode.getSource())
        srclines = self.sourcenode.getLines()
        newFnInsertPosition = self.fn.getEndLine()-1
        self.insertNewFunctionIntoSrcLines(srclines, self.newfn,
                                           newFnInsertPosition)
        self.writeCallToNewFunction(srclines)

        src = "".join(srclines)
        queueFileToSave(self.sourcenode.filename,src)        
Beispiel #7
0
def save():
    from bike.transformer.undo import getUndoStack

    global outputqueue
    savedFiles = []
    for filename, src in outputqueue.iteritems():
        print >> log.progress, "Writing:", filename
        f = file(filename, "w+")
        f.write(outputqueue[filename])
        f.close()
        savedFiles.append(filename)
    outputqueue = {}
    #print "stack is "+ str(getUndoStack().stack)
    getUndoStack().commitUndoFrame()
    return savedFiles
Beispiel #8
0
def save():
    from bike.transformer.undo import getUndoStack

    global outputqueue
    savedFiles = []
    for filename,src in outputqueue.iteritems():
        print >> log.progress, "Writing:",filename
        f = file(filename, "w+")
        f.write(outputqueue[filename])
        f.close()
        savedFiles.append(filename)
    outputqueue = {}
    #print "stack is "+ str(getUndoStack().stack)
    getUndoStack().commitUndoFrame()    
    return savedFiles
def moveClassToNewModule(origfile, line, newfile):
    srcnode = getSourceNode(origfile)
    targetsrcnode = getSourceNode(newfile)
    classnode = getScopeForLine(srcnode, line)
    classlines = srcnode.getLines()[classnode.getStartLine() -
                                    1:classnode.getEndLine() - 1]
    getUndoStack().addSource(srcnode.filename, srcnode.getSource())
    getUndoStack().addSource(targetsrcnode.filename, targetsrcnode.getSource())

    srcnode.getLines()[classnode.getStartLine() - 1:classnode.getEndLine() -
                       1] = []

    targetsrcnode.getLines().extend(classlines)

    queueFileToSave(srcnode.filename, srcnode.getSource())
    queueFileToSave(targetsrcnode.filename, targetsrcnode.getSource())
Beispiel #10
0
def moveClassToNewModule(origfile,line,newfile):
    srcnode = getSourceNode(origfile)
    targetsrcnode = getSourceNode(newfile)
    classnode = getScopeForLine(srcnode,line)
    classlines = srcnode.getLines()[classnode.getStartLine()-1:
                                    classnode.getEndLine()-1]
    getUndoStack().addSource(srcnode.filename,
                             srcnode.getSource())
    getUndoStack().addSource(targetsrcnode.filename,
                             targetsrcnode.getSource())

    srcnode.getLines()[classnode.getStartLine()-1:
                       classnode.getEndLine()-1] = []
    
    targetsrcnode.getLines().extend(classlines)

    queueFileToSave(srcnode.filename,srcnode.getSource())
    queueFileToSave(targetsrcnode.filename,targetsrcnode.getSource())
Beispiel #11
0
def extractLocalVariable(filename, startcoords, endcoords, varname):
    sourceobj = getSourceNode(filename)
    if startcoords.line != endcoords.line:
        raise "Can't do multi-line extracts yet"
    startcoords, endcoords = \
                 reverseCoordsIfWrongWayRound(startcoords,endcoords)
    line = sourceobj.getLine(startcoords.line)
    tabwidth = getTabWidthOfLine(line)
    linesep = getLineSeperator(line)
    region = line[startcoords.column:endcoords.column]

    getUndoStack().addSource(sourceobj.filename, sourceobj.getSource())
    sourceobj.getLines()[startcoords.line-1] = \
          line[:startcoords.column] + varname + line[endcoords.column:]

    defnline = tabwidth * " " + varname + " = " + region + linesep

    sourceobj.getLines().insert(startcoords.line - 1, defnline)

    queueFileToSave(sourceobj.filename, "".join(sourceobj.getLines()))
Beispiel #12
0
def extractLocalVariable(filename, startcoords, endcoords, varname):
    sourceobj = getSourceNode(filename)
    if startcoords.line != endcoords.line:
        raise "Can't do multi-line extracts yet"
    startcoords, endcoords = \
                 reverseCoordsIfWrongWayRound(startcoords,endcoords)
    line = sourceobj.getLine(startcoords.line)
    tabwidth = getTabWidthOfLine(line)
    linesep = getLineSeperator(line)
    region = line[startcoords.column:endcoords.column]
    
    getUndoStack().addSource(sourceobj.filename,sourceobj.getSource())
    sourceobj.getLines()[startcoords.line-1] = \
          line[:startcoords.column] + varname + line[endcoords.column:]

    defnline = tabwidth*" " + varname + " = " + region + linesep
    
    sourceobj.getLines().insert(startcoords.line-1,defnline)

    queueFileToSave(sourceobj.filename,"".join(sourceobj.getLines()))
Beispiel #13
0
def moveFunctionToNewModule(origfile, line, newfile):

    srcnode = getSourceNode(origfile)
    targetsrcnode = getSourceNode(newfile)
    scope = getScopeForLine(srcnode, line)

    linesep = getLineSeperator(srcnode.getLines()[0])

    matches = findReferences(origfile, line, scope.getColumnOfName())

    origFileImport = []
    fromline = 'from %s import %s' % (filenameToModulePath(newfile),
                                      scope.name)

    for match in matches:
        if match.filename == origfile:
            origFileImport = fromline + linesep
        else:
            s = getSourceNode(match.filename)
            m = s.fastparseroot
            if match.lineno in m.getImportLineNumbers():
                getUndoStack().addSource(s.filename, s.getSource())

                maskedline = m.getLogicalLine(match.lineno)
                origline = s.getLines()[match.lineno - 1]
                reMatch = re.match(exactFromRE % (scope.name), maskedline)
                if reMatch and not (',' in reMatch.group(2) or \
                                    '\\' in reMatch.group(2)):
                    restOfOrigLine = origline[len(reMatch.group(1)):]
                    s.getLines()[match.lineno - 1] = fromline + restOfOrigLine

                elif re.match(fromRE, maskedline):
                    #remove the element from the import stmt
                    line = re.sub('%s\s*?,' % (scope.name), '', origline)
                    s.getLines()[match.lineno - 1] = line
                    #and add a new line
                    nextline = match.lineno + maskedline.count('\\') + 1
                    s.getLines()[nextline - 1:nextline -
                                 1] = [fromline + linesep]

                queueFileToSave(s.filename, s.getSource())

    refs = getVariableReferencesInLines(scope.getMaskedLines())

    scopeLines = srcnode.getLines()[scope.getStartLine() -
                                    1:scope.getEndLine() - 1]
    importModules = deduceImportsForNewFile(refs, scope)
    importlines = composeNewFileImportLines(importModules, linesep)

    getUndoStack().addSource(srcnode.filename, srcnode.getSource())
    getUndoStack().addSource(targetsrcnode.filename, targetsrcnode.getSource())

    srcnode.getLines()[scope.getStartLine() - 1:scope.getEndLine() -
                       1] = origFileImport

    targetsrcnode.getLines().extend(importlines + scopeLines)

    queueFileToSave(srcnode.filename, srcnode.getSource())
    queueFileToSave(targetsrcnode.filename, targetsrcnode.getSource())
Beispiel #14
0
 def undo(self):
     getUndoStack().undo()
Beispiel #15
0
def moveFunctionToNewModule(origfile,line,newfile):
    
    srcnode = getSourceNode(origfile)
    targetsrcnode = getSourceNode(newfile)
    scope = getScopeForLine(srcnode,line)

    linesep = getLineSeperator(srcnode.getLines()[0])

    matches =[m for m in findReferences(origfile, line, scope.getColumnOfName())]

    origFileImport = []
    fromline = 'from %s import %s'%(filenameToModulePath(newfile),scope.name)
    
    for match in matches:
        if match.filename == origfile:
            origFileImport = fromline + linesep
        else:
            s = getSourceNode(match.filename)
            m = s.fastparseroot
            if match.lineno in m.getImportLineNumbers():                
                getUndoStack().addSource(s.filename,
                                         s.getSource())

                maskedline = m.getLogicalLine(match.lineno)
                origline = s.getLines()[match.lineno-1]
                reMatch =  re.match(exactFromRE%(scope.name),maskedline)
                if reMatch and not (',' in reMatch.group(2) or \
                                    '\\' in reMatch.group(2)):
                    # i.e. line is 'from module import foo'

                    if match.filename == newfile:
                        #remove the import
                        s.getLines()[match.lineno-1:match.lineno] = []
                        pass
                    else:
                        restOfOrigLine = origline[len(reMatch.group(1)):]
                        s.getLines()[match.lineno-1] = fromline + restOfOrigLine

                elif re.match(fromRE,maskedline):
                    # i.e. line is 'from module import foo,bah,baz'
                    #remove the element from the import stmt
                    line = removeNameFromMultipleImportLine(scope.name, origline)
                    s.getLines()[match.lineno-1] = line
                    #and add a new line
                    nextline = match.lineno + maskedline.count('\\') + 1
                    s.getLines()[nextline-1:nextline-1] = [fromline+linesep]
                    
                queueFileToSave(s.filename,s.getSource())
                    
    
    refs = getVariableReferencesInLines(scope.getMaskedLines())

    scopeLines = srcnode.getLines()[scope.getStartLine()-1:
                                    scope.getEndLine()-1]
    importModules = deduceImportsForNewFile(refs, scope)
    importlines = composeNewFileImportLines(importModules, linesep)



    getUndoStack().addSource(srcnode.filename,
                             srcnode.getSource())
    getUndoStack().addSource(targetsrcnode.filename,
                             targetsrcnode.getSource())

    srcnode.getLines()[scope.getStartLine()-1:
                       scope.getEndLine()-1] = origFileImport

    targetsrcnode.getLines().extend(importlines+scopeLines)

    queueFileToSave(srcnode.filename,srcnode.getSource())
    queueFileToSave(targetsrcnode.filename,targetsrcnode.getSource())
def addUndo(sourcenode):
    getUndoStack().addSource(sourcenode.filename,sourcenode.getSource())
Beispiel #17
0
 def undo(self):
     getUndoStack().undo()