Ejemplo n.º 1
0
    def test_findsReferenceInModuleWhichImportsClassWithFrom(self):
        src = trimLines("""
        from b.bah import TheClass
        def foo():
            a = TheClass()
            a.theMethod()
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")

        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename, 1, 6)]

        self.assertEqual(refs[0].filename,
                         os.path.abspath(os.path.join("a", "foo.py")))
        self.assertEqual(refs[0].lineno, 1)
        self.assertEqual(refs[0].colno, 18)
        self.assertEqual(refs[0].confidence, 100)

        self.assertEqual(refs[1].filename,
                         os.path.abspath(os.path.join("a/foo.py")))
        self.assertEqual(refs[1].lineno, 3)
        self.assertEqual(refs[1].colno, 8)
        self.assertEqual(refs[1].confidence, 100)
Ejemplo n.º 2
0
 def test_renamesMethodReferenceOfInstanceCreatedInSubsequentFunction(self):
     src = trimLines("""
     class TheClass:
         def theMethod():
             pass
     class NotTheClass:
         def theMethod():
             pass
         
     def foo():
         a = bah()
         a.theMethod()
         
     def bah():
         return TheClass()
     """)
     root = createSourceNodeAt(src, "a.foo")
     filename = os.path.abspath("a/foo.py")
     refs = [
         x for x in findReferences(filename, 2, 8, [])
         if x.confidence == 100
     ]
     self.assertEqual(refs[0].filename,
                      os.path.abspath(os.path.join("a", "foo.py")))
     self.assertEqual(refs[0].lineno, 10)
     self.assertEqual(refs[0].colno, 6)
Ejemplo n.º 3
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())
Ejemplo n.º 4
0
 def helper4(self, src, importedsrc, line, col):
     try:
         createPackageStructure(src,importedsrc)
         filename = pkgstructureFile1
         refs =  [x for x in findReferences(filename,line,col)
                  if x.confidence == 100]
     finally:
         removePackageStructure()
     return refs
Ejemplo n.º 5
0
 def helper4(self, src, importedsrc, line, col):
     try:
         createPackageStructure(src,importedsrc)
         filename = pkgstructureFile1
         refs =  [x for x in findReferences(filename,line,col)
                  if x.confidence == 100]
     finally:
         removePackageStructure()
     return refs
 def test_returnsEmptyListIfNoReferences(self):
     src = trimLines("""
     class MyClass:
         pass
     a = TheClass()
     """)
     root = createSourceNodeAt(src,"mymodule")
     refs = [x for x in findReferences(os.path.abspath("mymodule.py"),1,6)]
     self.assertEqual(refs,[])
Ejemplo n.º 7
0
def inlineLocalVariable_old(sourcenode, lineno, col):
    definition, region, regionlinecount = getLocalVariableInfo(
        sourcenode, lineno, col)
    addUndo(sourcenode)
    replaceReferences(
        sourcenode,
        findReferences(sourcenode.filename, definition.lineno,
                       definition.colno), region)
    delLines(sourcenode, definition.lineno - 1, regionlinecount)
    updateSource(sourcenode)
Ejemplo n.º 8
0
    def test_doesntfindReferenceInModuleWhichDoesntImportClass(self):
        src = trimLines("""
        a = TheClass()
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename, 1, 6)]
        assert refs == []
    def test_doesntBarfOnFromImportStarWhenNameIsInFromClause(self):
        src = trimLines("""
        from a.b.bah import TheClass
        a = TheClass()
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename,1,6)]
    def test_doesntfindReferenceInModuleWhichDoesntImportClass(self):
        src = trimLines("""
        a = TheClass()
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename,1,6)]
        assert refs == []
Ejemplo n.º 11
0
    def test_doesntBarfOnFromImportStarWhenNameIsInFromClause(self):
        src = trimLines("""
        from a.b.bah import TheClass
        a = TheClass()
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename, 1, 6)]
Ejemplo n.º 12
0
 def test_returnsEmptyListIfNoReferences(self):
     src = trimLines("""
     class MyClass:
         pass
     a = TheClass()
     """)
     root = createSourceNodeAt(src, "mymodule")
     refs = [
         x for x in findReferences(os.path.abspath("mymodule.py"), 1, 6)
     ]
     self.assertEqual(refs, [])
 def test_findsSimpleReferenceInSameModule(self):
     src = trimLines("""
     class TheClass:
         pass
     a = TheClass()
     """)
     root = createSourceNodeAt(src,"mymodule")
     refs = [x for x in findReferences(os.path.abspath("mymodule.py"),1,6)]
     self.assertEqual(refs[0].filename,os.path.abspath("mymodule.py"))
     self.assertEqual(refs[0].lineno,3)
     self.assertEqual(refs[0].colno,4)
     self.assertEqual(refs[0].confidence,100)
    def test_findsReferenceToClassImportedInSameClassScope(self):
        src=trimLines("""
        class AnotherClass:
            from b.bah import TheClass
            TheClass.baz = 0
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")

        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename,1,6)]
        assert refs != []
Ejemplo n.º 15
0
    def test_findsReferenceToClassImportedInSameClassScope(self):
        src = trimLines("""
        class AnotherClass:
            from b.bah import TheClass
            TheClass.baz = 0
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")

        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename, 1, 6)]
        assert refs != []
 def test_findsClassReferenceInInstanceCreation(self):
     src = trimLines("""
     class TheClass:
         def theMethod(self): pass
     TheClass().theMethod()
     """)
     root = createSourceNodeAt(src, "a.foo")
     filename = os.path.abspath("a/foo.py")
     refs = [x for x in findReferences(filename,1,6)]
     self.assertEqual(refs[0].filename,os.path.abspath(os.path.join("a","foo.py")))
     self.assertEqual(refs[0].lineno,3)
     self.assertEqual(refs[0].colno,0)
     self.assertEqual(refs[0].confidence,100)
    def test_findsReferenceInMultiLineImportStatement(self):
        src =trimLines("""
        from b.bah import foo, \\
                  TheFooBah, TheClass, Foobah, SomethingElse
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename,1,6)]
        self.assertEqual(refs[0].filename,os.path.abspath(os.path.join("a","foo.py")))
        self.assertEqual(refs[0].lineno,2)
        self.assertEqual(refs[0].colno,21)
        self.assertEqual(refs[0].confidence,100)
Ejemplo n.º 18
0
 def test_doesntGetReferenceToMethodWhenObjectCreatedInChildScopeToMethodReference(self):
     src = trimLines("""
     from b.bah import TheClass
     a = AnotherClass()
     def foo():
         a = TheClass()
     a.theMethod()
     """)
     root = createSourceNodeAt(src,"a.foo")
     root = createSourceNodeAt(MethodTestdata, "a.b.bah")
     filename = os.path.abspath("a/b/bah.py")
     refs = [x for x in findReferences(filename,2,8)
             if x.confidence == 100]
     assert len(refs) == 0
Ejemplo n.º 19
0
 def test_findsSimpleReferenceInSameModule(self):
     src = trimLines("""
     class TheClass:
         pass
     a = TheClass()
     """)
     root = createSourceNodeAt(src, "mymodule")
     refs = [
         x for x in findReferences(os.path.abspath("mymodule.py"), 1, 6)
     ]
     self.assertEqual(refs[0].filename, os.path.abspath("mymodule.py"))
     self.assertEqual(refs[0].lineno, 3)
     self.assertEqual(refs[0].colno, 4)
     self.assertEqual(refs[0].confidence, 100)
    def test_findsReferenceInModuleWhichImportsClassWithFromImportStar2(self):
        src = trimLines("""
        from a.b.bah import *
        a = TheClass()
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename,1,6)]
        self.assertEqual(refs[0].filename,os.path.abspath(os.path.join("a","foo.py")))
        self.assertEqual(refs[0].lineno,2)
        self.assertEqual(refs[0].colno,4)
        self.assertEqual(refs[0].confidence,100)
Ejemplo n.º 21
0
 def test_findsClassReferenceInInstanceCreation(self):
     src = trimLines("""
     class TheClass:
         def theMethod(self): pass
     TheClass().theMethod()
     """)
     root = createSourceNodeAt(src, "a.foo")
     filename = os.path.abspath("a/foo.py")
     refs = [x for x in findReferences(filename, 1, 6)]
     self.assertEqual(refs[0].filename,
                      os.path.abspath(os.path.join("a", "foo.py")))
     self.assertEqual(refs[0].lineno, 3)
     self.assertEqual(refs[0].colno, 0)
     self.assertEqual(refs[0].confidence, 100)
Ejemplo n.º 22
0
 def test_doesntGetReferenceToMethodWhenObjectCreatedInChildScopeToMethodReference(self):
     src = trimLines("""
     from b.bah import TheClass
     a = AnotherClass()
     def foo():
         a = TheClass()
     a.theMethod()
     """)
     root = createSourceNodeAt(src,"a.foo")
     root = createSourceNodeAt(MethodTestdata, "a.b.bah")
     filename = os.path.abspath("a/b/bah.py")
     refs = [x for x in findReferences(filename,2,8)
             if x.confidence == 100]
     assert len(refs) == 0
Ejemplo n.º 23
0
    def test_findsReferenceInMultiLineImportStatement(self):
        src = trimLines("""
        from b.bah import foo, \\
                  TheFooBah, TheClass, Foobah, SomethingElse
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename, 1, 6)]
        self.assertEqual(refs[0].filename,
                         os.path.abspath(os.path.join("a", "foo.py")))
        self.assertEqual(refs[0].lineno, 2)
        self.assertEqual(refs[0].colno, 21)
        self.assertEqual(refs[0].confidence, 100)
    def test_findsReferenceInClassBases(self):
        src =trimLines("""
        from b.bah import TheClass
        class DerivedClass(TheClass):
            pass
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename,1,6)]
        self.assertEqual(refs[1].filename,os.path.abspath(os.path.join("a","foo.py")))
        self.assertEqual(refs[1].lineno,2)
        self.assertEqual(refs[1].colno,19)
        self.assertEqual(refs[1].confidence,100)
    def test_findsReferencesInModuleWhichImportsClass(self):
        src = trimLines("""
        import b.bah
        def foo():
            a = b.bah.TheClass()
            a.theMethod()
        """)
        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        refs = [x for x in findReferences(os.path.abspath("a/b/bah.py"),1,6)]

        self.assertEqual(refs[0].filename,os.path.abspath(os.path.join("a","foo.py")))
        self.assertEqual(refs[0].lineno,3)
        self.assertEqual(refs[0].colno,14)
        self.assertEqual(refs[0].confidence,100)
Ejemplo n.º 26
0
 def test_getsReferenceOfSimpleMethodCall(self):
     src = trimLines("""
     from b.bah import TheClass
     a = TheClass()
     a.theMethod()
     """)
     root = createSourceNodeAt(src,"a.foo")
     root = createSourceNodeAt(MethodTestdata, "a.b.bah")
     filename = os.path.abspath("a/b/bah.py")
     refs = [x for x in findReferences(filename,2,8)
             if x.confidence == 100]
     self.assertEqual(refs[0].filename,
                      os.path.abspath(os.path.join("a","foo.py")))
     self.assertEqual(refs[0].lineno,3)
     self.assertEqual(refs[0].colno,2)
     self.assertEqual(refs[0].colno,2)
Ejemplo n.º 27
0
    def test_findsReferenceInClassBases(self):
        src = trimLines("""
        from b.bah import TheClass
        class DerivedClass(TheClass):
            pass
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename, 1, 6)]
        self.assertEqual(refs[1].filename,
                         os.path.abspath(os.path.join("a", "foo.py")))
        self.assertEqual(refs[1].lineno, 2)
        self.assertEqual(refs[1].colno, 19)
        self.assertEqual(refs[1].confidence, 100)
Ejemplo n.º 28
0
    def test_getsReferenceInMiddleOfBiggerCompoundCall(self):
        src = trimLines("""
        class TheClass:
            def theMethod(self): return AnotherClass()
        TheClass().theMethod().anotherMethod()
        """)

        root = createSourceNodeAt(src,"a.foo")
        filename = os.path.abspath("a/foo.py")
        refs = [x for x in findReferences(filename,2,8)
                if x.confidence == 100]
        self.assertEqual(refs[0].filename,
                         os.path.abspath(os.path.join("a","foo.py")))
        self.assertEqual(refs[0].lineno,3)
        self.assertEqual(refs[0].colno,11)
        self.assertEqual(refs[0].colend,20)
Ejemplo n.º 29
0
    def test_getsReferenceInMiddleOfBiggerCompoundCall(self):
        src = trimLines("""
        class TheClass:
            def theMethod(self): return AnotherClass()
        TheClass().theMethod().anotherMethod()
        """)

        root = createSourceNodeAt(src,"a.foo")
        filename = os.path.abspath("a/foo.py")
        refs = [x for x in findReferences(filename,2,8)
                if x.confidence == 100]
        self.assertEqual(refs[0].filename,
                         os.path.abspath(os.path.join("a","foo.py")))
        self.assertEqual(refs[0].lineno,3)
        self.assertEqual(refs[0].colno,11)
        self.assertEqual(refs[0].colend,20)
Ejemplo n.º 30
0
 def test_getsReferenceOfSimpleMethodCall(self):
     src = trimLines("""
     from b.bah import TheClass
     a = TheClass()
     a.theMethod()
     """)
     root = createSourceNodeAt(src,"a.foo")
     root = createSourceNodeAt(MethodTestdata, "a.b.bah")
     filename = os.path.abspath("a/b/bah.py")
     refs = [x for x in findReferences(filename,2,8)
             if x.confidence == 100]
     self.assertEqual(refs[0].filename,
                      os.path.abspath(os.path.join("a","foo.py")))
     self.assertEqual(refs[0].lineno,3)
     self.assertEqual(refs[0].colno,2)
     self.assertEqual(refs[0].colno,2)
Ejemplo n.º 31
0
    def test_getsReferenceOfMethodCallInSameClass(self):
        src = trimLines("""
        class TheClass:
            def theMethod(self):
                pass
            def anotherMethod(self):
                self.theMethod()
        """)

        root = createSourceNodeAt(src,"a.foo")
        filename = os.path.abspath("a/foo.py")
        refs = [x for x in findReferences(filename,2,8)
                if x.confidence == 100]
        self.assertEqual(refs[0].filename,
                         os.path.abspath(os.path.join("a","foo.py")))
        self.assertEqual(refs[0].lineno,5)
        self.assertEqual(refs[0].colno,13)
    def test_findsReferenceInModuleWhichImportsClassWithFromAndAlias(self):
        src = trimLines("""
        from b.bah import TheClass as MyTheClass
        def foo():
            a = MyTheClass()
            a.theMethod()
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename,1,6)]

        self.assertEqual(refs[0].filename,os.path.abspath(os.path.join("a","foo.py")))
        self.assertEqual(refs[0].lineno,1)
        self.assertEqual(refs[0].colno,18)
        self.assertEqual(refs[0].confidence,100)
Ejemplo n.º 33
0
    def test_getsReferenceOfMethodCallInSameClass(self):
        src = trimLines("""
        class TheClass:
            def theMethod(self):
                pass
            def anotherMethod(self):
                self.theMethod()
        """)

        root = createSourceNodeAt(src,"a.foo")
        filename = os.path.abspath("a/foo.py")
        refs = [x for x in findReferences(filename,2,8)
                if x.confidence == 100]
        self.assertEqual(refs[0].filename,
                         os.path.abspath(os.path.join("a","foo.py")))
        self.assertEqual(refs[0].lineno,5)
        self.assertEqual(refs[0].colno,13)
    def test_findsReferenceWhenModulenameSameAsClassMethodName(self):
        # asserts that brm doesnt search class scope after not finding name
        # in method scope (since class scope is invisible unless called on 'self'
        src =trimLines("""
        from a.b import bah
        class baz:
            def bah(self):
                print bah.TheClass
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename,1,6)]
        self.assertEqual(refs[0].filename,os.path.abspath(os.path.join("a","foo.py")))
        self.assertEqual(refs[0].lineno,4)
        self.assertEqual(refs[0].colno,18)
        self.assertEqual(refs[0].confidence,100)
Ejemplo n.º 35
0
    def test_findsClassReferenceInInstanceCreationWithFQN(self):
        src = trimLines("""
        import b.bah
        def foo():
            a = b.bah.TheClass()
            a.theMethod()
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename, 1, 6)]

        self.assertEqual(refs[0].filename,
                         os.path.abspath(os.path.join("a", "foo.py")))
        self.assertEqual(refs[0].lineno, 3)
        self.assertEqual(refs[0].colno, 14)
        self.assertEqual(refs[0].confidence, 100)
Ejemplo n.º 36
0
    def test_findsReferenceWhenModulenameSameAsClassMethodName(self):
        # asserts that brm doesnt search class scope after not finding name
        # in method scope (since class scope is invisible unless called on 'self'
        src = trimLines("""
        from a.b import bah
        class baz:
            def bah(self):
                print bah.TheClass
        """)

        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(ClassTestdata, "a.b.bah")
        filename = os.path.abspath("a/b/bah.py")
        refs = [x for x in findReferences(filename, 1, 6)]
        self.assertEqual(refs[0].filename,
                         os.path.abspath(os.path.join("a", "foo.py")))
        self.assertEqual(refs[0].lineno, 4)
        self.assertEqual(refs[0].colno, 18)
        self.assertEqual(refs[0].confidence, 100)
Ejemplo n.º 37
0
 def test_renamesMethodReferenceOfInstanceCreatedInSubsequentFunction(self):
     src = trimLines("""
     class TheClass:
         def theMethod():
             pass
     class NotTheClass:
         def theMethod():
             pass
         
     def foo():
         a = bah()
         a.theMethod()
         
     def bah():
         return TheClass()
     """)
     root = createSourceNodeAt(src,"a.foo")
     filename = os.path.abspath("a/foo.py")
     refs = [x for x in findReferences(filename,2,8,[])
             if x.confidence == 100]
     self.assertEqual(refs[0].filename,
                      os.path.abspath(os.path.join("a","foo.py")))
     self.assertEqual(refs[0].lineno,10)
     self.assertEqual(refs[0].colno,6)
Ejemplo n.º 38
0
 def findReferencesByCoordinates(self, filename_path, line, column):
     filename_path = self.normalizeFilename(filename_path)
     self._setNonLibPythonPath(filename_path)
     return findReferences(filename_path,line,column)
Ejemplo n.º 39
0
def inlineLocalVariable_old(sourcenode,lineno,col):
    definition, region, regionlinecount = getLocalVariableInfo(sourcenode, lineno, col)
    addUndo(sourcenode)
    replaceReferences(sourcenode, findReferences(sourcenode.filename, definition.lineno, definition.colno), region)
    delLines(sourcenode, definition.lineno-1, regionlinecount)
    updateSource(sourcenode)
Ejemplo n.º 40
0
 def findReferencesByCoordinates(self, filename_path, line, column):
     filename_path = self.normalizeFilename(filename_path)
     path = self._removeLibdirsFromPath(sys.path)
     return findReferences(filename_path, line, column, 0, path)
Ejemplo n.º 41
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())
Ejemplo n.º 42
0
 def findReferencesByCoordinates(self, filename_path, line, column):
     filename_path = self.normalizeFilename(filename_path)
     self._setNonLibPythonPath(filename_path)
     return findReferences(filename_path, line, column)
Ejemplo n.º 43
0
 def findReferencesByCoordinates(self, filename_path, line, column):
     filename_path = self.normalizeFilename(filename_path) 
     path = self._removeLibdirsFromPath(sys.path)
     return findReferences(filename_path,line,column,0,path)