Anonymous avatar Anonymous committed 3cd0c6f

moved ScopeNameFinder to evaluate

Comments (0)

Files changed (16)

docs/dev/issues.txt

 decisions.
 
 
-Custom Restructoring Wildcards
+Custom Restructuring Wildcards
 ==============================
 
 There is a need to add more custom wildcards in restructuring

rope/base/codeanalyze.py

 import token
 import tokenize
 
-import rope.base.exceptions
-import rope.base.pynames
-from rope.base import pyobjects, evaluate
-
 
 class WordRangeFinder(object):
     # XXX: many of these methods fail on comments
         return (None, None)
 
 
-class ScopeNameFinder(object):
-
-    def __init__(self, pymodule):
-        self.source_code = pymodule.source_code
-        self.module_scope = pymodule.get_scope()
-        self.lines = pymodule.lines
-        self.word_finder = WordRangeFinder(self.source_code)
-
-    def _is_defined_in_class_body(self, holding_scope, offset, lineno):
-        if lineno == holding_scope.get_start() and \
-           holding_scope.parent is not None and \
-           holding_scope.parent.get_kind() == 'Class' and \
-           self.word_finder.is_a_class_or_function_name_in_header(offset):
-            return True
-        if lineno != holding_scope.get_start() and \
-           holding_scope.get_kind() == 'Class' and \
-           self.word_finder._is_name_assigned_in_class_body(offset):
-            return True
-        return False
-
-    def _is_function_name_in_function_header(self, scope, offset, lineno):
-        if scope.get_start() <= lineno <= scope.get_body_start() and \
-           scope.get_kind() == 'Function' and \
-           self.word_finder.is_a_class_or_function_name_in_header(offset):
-            return True
-        return False
-
-    def get_pyname_at(self, offset):
-        return self.get_primary_and_pyname_at(offset)[1]
-
-    def get_primary_and_pyname_at(self, offset):
-        lineno = self.lines.get_line_number(offset)
-        holding_scope = self.module_scope.get_inner_scope_for_line(lineno)
-        # function keyword parameter
-        if self.word_finder.is_function_keyword_parameter(offset):
-            keyword_name = self.word_finder.get_word_at(offset)
-            pyobject = self.get_enclosing_function(offset)
-            if isinstance(pyobject, pyobjects.PyFunction):
-                return (None, pyobject.get_parameters().get(keyword_name, None))
-
-        # class body
-        if self._is_defined_in_class_body(holding_scope, offset, lineno):
-            class_scope = holding_scope
-            if lineno == holding_scope.get_start():
-                class_scope = holding_scope.parent
-            name = self.word_finder.get_primary_at(offset).strip()
-            try:
-                return (None, class_scope.pyobject.get_attribute(name))
-            except rope.base.exceptions.AttributeNotFoundError:
-                return (None, None)
-        # function header
-        if self._is_function_name_in_function_header(holding_scope, offset, lineno):
-            name = self.word_finder.get_primary_at(offset).strip()
-            return (None, holding_scope.parent.get_name(name))
-        # from statement module
-        if self.word_finder.is_from_statement_module(offset):
-            module = self.word_finder.get_primary_at(offset)
-            module_pyname = self._find_module(module)
-            return (None, module_pyname)
-        if self.word_finder.is_from_aliased(offset):
-            name = self.word_finder.get_from_aliased(offset)
-        else:
-            name = self.word_finder.get_primary_at(offset)
-        return evaluate.get_primary_and_pyname_in_scope(holding_scope, name)
-
-    def get_enclosing_function(self, offset):
-        function_parens = self.word_finder.find_parens_start_from_inside(offset)
-        try:
-            function_pyname = self.get_pyname_at(function_parens - 1)
-        except evaluate.BadIdentifierError:
-            function_pyname = None
-        if function_pyname is not None:
-            pyobject = function_pyname.get_object()
-            if isinstance(pyobject, pyobjects.AbstractFunction):
-                return pyobject
-            elif isinstance(pyobject, pyobjects.AbstractClass) and \
-                 '__init__' in pyobject.get_attributes():
-                return pyobject.get_attribute('__init__').get_object()
-            elif '__call__' in pyobject.get_attributes():
-                return pyobject.get_attribute('__call__').get_object()
-        return None
-
-    def _find_module(self, module_name):
-        dot_count = 0
-        if module_name.startswith('.'):
-            for c in module_name:
-                if c == '.':
-                    dot_count += 1
-                else:
-                    break
-        return rope.base.pynames.ImportedModule(
-            self.module_scope.pyobject, module_name[dot_count:], dot_count)
-
-
-def get_pyname_at(pycore, resource, offset):
-    """Finds the pyname at the offset
-
-    This function is inefficient for multiple calls because of the
-    recalculation of initialization data.
-    """
-    return get_primary_and_pyname_at(pycore, resource, offset)[1]
-
-def get_primary_and_pyname_at(pycore, resource, offset):
-    """Finds the primary and pyname at offset
-
-    See notes about `get_pyname_at`.
-    """
-    pymodule = pycore.resource_to_pyobject(resource)
-    pyname_finder = ScopeNameFinder(pymodule)
-    return pyname_finder.get_primary_and_pyname_at(offset)
-
 def get_name_at(resource, offset):
     source_code = resource.read()
     word_finder = WordRangeFinder(source_code)

rope/base/evaluate.py

 import rope.base.exceptions
 import rope.base.pynames
 import rope.base.pyobjects
-from rope.base import ast, astutils, exceptions
+from rope.base import ast, astutils, exceptions, pyobjects
+from rope.base.codeanalyze import WordRangeFinder
+
+
+def get_primary_and_pyname_at(pycore, resource, offset):
+    """Finds the primary and pyname at offset
+
+    See notes about `get_pyname_at`.
+    """
+    pymodule = pycore.resource_to_pyobject(resource)
+    pyname_finder = ScopeNameFinder(pymodule)
+    return pyname_finder.get_primary_and_pyname_at(offset)
+
+
+def get_pyname_at(pycore, resource, offset):
+    """Finds the pyname at the offset
+
+    This function is inefficient for multiple calls because of the
+    recalculation of initialization data.
+    """
+    return get_primary_and_pyname_at(pycore, resource, offset)[1]
+
+
+def get_statement_result(scope, node):
+    """Evaluate a `ast.AST` node and return a PyName
+
+    Returns `None` if the expression cannot be evaluated.
+
+    """
+    return get_primary_and_result(scope, node)[1]
+
+
+def get_primary_and_result(scope, node):
+    evaluator = StatementEvaluator(scope)
+    ast.walk(node, evaluator)
+    return evaluator.old_result, evaluator.result
+
+
+def get_string_result(scope, string):
+    evaluator = StatementEvaluator(scope)
+    node = ast.parse(string)
+    ast.walk(node, evaluator)
+    return evaluator.result
+
+
+def get_pyname_in_scope(holding_scope, name):
+    return get_primary_and_pyname_in_scope(holding_scope, name)[1]
+
+
+def get_primary_and_pyname_in_scope(holding_scope, name):
+    try:
+        # parenthesizing for handling cases like 'a_var.\nattr'
+        node = ast.parse('(%s)' % name)
+    except SyntaxError:
+        raise BadIdentifierError('Not a resolvable python identifier selected.')
+    return get_primary_and_result(holding_scope, node)
 
 
 class BadIdentifierError(rope.base.exceptions.RopeError):
     pass
 
 
+class ScopeNameFinder(object):
+
+    def __init__(self, pymodule):
+        self.source_code = pymodule.source_code
+        self.module_scope = pymodule.get_scope()
+        self.lines = pymodule.lines
+        self.word_finder = WordRangeFinder(self.source_code)
+
+    def _is_defined_in_class_body(self, holding_scope, offset, lineno):
+        if lineno == holding_scope.get_start() and \
+           holding_scope.parent is not None and \
+           holding_scope.parent.get_kind() == 'Class' and \
+           self.word_finder.is_a_class_or_function_name_in_header(offset):
+            return True
+        if lineno != holding_scope.get_start() and \
+           holding_scope.get_kind() == 'Class' and \
+           self.word_finder._is_name_assigned_in_class_body(offset):
+            return True
+        return False
+
+    def _is_function_name_in_function_header(self, scope, offset, lineno):
+        if scope.get_start() <= lineno <= scope.get_body_start() and \
+           scope.get_kind() == 'Function' and \
+           self.word_finder.is_a_class_or_function_name_in_header(offset):
+            return True
+        return False
+
+    def get_pyname_at(self, offset):
+        return self.get_primary_and_pyname_at(offset)[1]
+
+    def get_primary_and_pyname_at(self, offset):
+        lineno = self.lines.get_line_number(offset)
+        holding_scope = self.module_scope.get_inner_scope_for_line(lineno)
+        # function keyword parameter
+        if self.word_finder.is_function_keyword_parameter(offset):
+            keyword_name = self.word_finder.get_word_at(offset)
+            pyobject = self.get_enclosing_function(offset)
+            if isinstance(pyobject, pyobjects.PyFunction):
+                return (None, pyobject.get_parameters().get(keyword_name, None))
+
+        # class body
+        if self._is_defined_in_class_body(holding_scope, offset, lineno):
+            class_scope = holding_scope
+            if lineno == holding_scope.get_start():
+                class_scope = holding_scope.parent
+            name = self.word_finder.get_primary_at(offset).strip()
+            try:
+                return (None, class_scope.pyobject.get_attribute(name))
+            except rope.base.exceptions.AttributeNotFoundError:
+                return (None, None)
+        # function header
+        if self._is_function_name_in_function_header(holding_scope, offset, lineno):
+            name = self.word_finder.get_primary_at(offset).strip()
+            return (None, holding_scope.parent.get_name(name))
+        # from statement module
+        if self.word_finder.is_from_statement_module(offset):
+            module = self.word_finder.get_primary_at(offset)
+            module_pyname = self._find_module(module)
+            return (None, module_pyname)
+        if self.word_finder.is_from_aliased(offset):
+            name = self.word_finder.get_from_aliased(offset)
+        else:
+            name = self.word_finder.get_primary_at(offset)
+        return get_primary_and_pyname_in_scope(holding_scope, name)
+
+    def get_enclosing_function(self, offset):
+        function_parens = self.word_finder.find_parens_start_from_inside(offset)
+        try:
+            function_pyname = self.get_pyname_at(function_parens - 1)
+        except BadIdentifierError:
+            function_pyname = None
+        if function_pyname is not None:
+            pyobject = function_pyname.get_object()
+            if isinstance(pyobject, pyobjects.AbstractFunction):
+                return pyobject
+            elif isinstance(pyobject, pyobjects.AbstractClass) and \
+                 '__init__' in pyobject.get_attributes():
+                return pyobject.get_attribute('__init__').get_object()
+            elif '__call__' in pyobject.get_attributes():
+                return pyobject.get_attribute('__call__').get_object()
+        return None
+
+    def _find_module(self, module_name):
+        dot_count = 0
+        if module_name.startswith('.'):
+            for c in module_name:
+                if c == '.':
+                    dot_count += 1
+                else:
+                    break
+        return rope.base.pynames.ImportedModule(
+            self.module_scope.pyobject, module_name[dot_count:], dot_count)
+
+
 class StatementEvaluator(object):
 
     def __init__(self, scope):
             pyobject=rope.base.builtins.Lambda(node, self.scope))
 
 
-def get_statement_result(scope, node):
-    """Evaluate a `ast.AST` node and return a PyName
-
-    Returns `None` if the expression cannot be evaluated.
-
-    """
-    return get_primary_and_result(scope, node)[1]
-
-
-def get_primary_and_result(scope, node):
-    evaluator = StatementEvaluator(scope)
-    ast.walk(node, evaluator)
-    return evaluator.old_result, evaluator.result
-
-
-def get_string_result(scope, string):
-    evaluator = StatementEvaluator(scope)
-    node = ast.parse(string)
-    ast.walk(node, evaluator)
-    return evaluator.result
-
-
-def get_pyname_in_scope(holding_scope, name):
-    return get_primary_and_pyname_in_scope(holding_scope, name)[1]
-
-
-def get_primary_and_pyname_in_scope(holding_scope, name):
-    try:
-        # parenthesizing for handling cases like 'a_var.\nattr'
-        node = ast.parse('(%s)' % name)
-    except SyntaxError:
-        raise BadIdentifierError('Not a resolvable python identifier selected.')
-    return get_primary_and_result(holding_scope, node)
-
-
 class Arguments(object):
     """A class for evaluating parameters passed to a function
 

rope/contrib/codeassist.py

 import sys
 
 import rope.base.codeanalyze
+import rope.base.evaluate
 from rope.base import pyobjects, pynames, taskhandle, builtins, exceptions
 from rope.base.codeanalyze import (ArrayLinesAdapter, LogicalLineFinder,
-                                   ScopeNameFinder,
                                    SourceLinesAdapter, WordRangeFinder)
 from rope.refactor import occurrences, functionutils
 
     """
     pymodule = _get_pymodule(project.pycore, source_code, resource,
                              error_limit=offset)
-    scope_finder = ScopeNameFinder(pymodule)
+    scope_finder = rope.base.evaluate.ScopeNameFinder(pymodule)
     element = scope_finder.get_pyname_at(offset)
     if element is None:
         return None
 def get_definition_location(project, source_code, offset, resource=None):
     """Return a (`rope.base.resources.Resource`, lineno) tuple"""
     pymodule = project.pycore.get_string_module(source_code, resource)
-    scope_finder = ScopeNameFinder(pymodule)
+    scope_finder = rope.base.evaluate.ScopeNameFinder(pymodule)
     element = scope_finder.get_pyname_at(offset)
     if element is not None:
         module, lineno = element.get_definition_location()
 
     """
     name = rope.base.codeanalyze.get_name_at(resource, offset)
-    pyname = rope.base.codeanalyze.get_pyname_at(project.get_pycore(),
-                                                 resource, offset)
+    pyname = rope.base.evaluate.get_pyname_at(project.get_pycore(),
+                                              resource, offset)
     def is_match(occurrence):
         return unsure
     finder = occurrences.FilteredFinder(
 
     def _dotted_completions(self, module_scope, holding_scope):
         result = {}
-        pyname_finder = ScopeNameFinder(module_scope.pyobject)
+        pyname_finder = rope.base.evaluate.ScopeNameFinder(module_scope.pyobject)
         found_pyname = rope.base.evaluate.get_pyname_in_scope(
             holding_scope, self.expression)
         if found_pyname is not None:
         stop_line = LogicalLineFinder(lines).logical_line_in(lineno)[0]
         stop = lines.get_line_start(stop_line)
         if word_finder.is_on_function_call_keyword(offset - 1, stop):
-            name_finder = ScopeNameFinder(pymodule)
+            name_finder = rope.base.evaluate.ScopeNameFinder(pymodule)
             function_parens = word_finder.\
                 find_parens_start_from_inside(offset - 1, stop)
             primary = word_finder.get_primary_at(function_parens - 1)

rope/contrib/generate.py

+import rope.base.evaluate
 from rope.base import change, codeanalyze, pyobjects, exceptions, pynames
 from rope.refactor import sourceutils, importutils, functionutils, suites
 
         self.resource = resource
         self.offset = offset
         self.source_pymodule = self.pycore.resource_to_pyobject(resource)
-        finder = codeanalyze.ScopeNameFinder(self.source_pymodule)
+        finder = rope.base.evaluate.ScopeNameFinder(self.source_pymodule)
         self.primary, self.pyname = finder.get_primary_and_pyname_at(offset)
         self._init_fields()
 

rope/refactor/change_signature.py

 import copy
 
 import rope.base.exceptions
-from rope.base import pyobjects, codeanalyze, taskhandle
+from rope.base import pyobjects, codeanalyze, taskhandle, evaluate
 from rope.base.change import ChangeContents, ChangeSet
 from rope.refactor import occurrences, sourceutils, functionutils, rename
 
 
     def _set_name_and_pyname(self):
         self.name = codeanalyze.get_name_at(self.resource, self.offset)
-        self.primary, self.pyname = codeanalyze.get_primary_and_pyname_at(
+        self.primary, self.pyname = evaluate.get_primary_and_pyname_at(
             self.pycore, self.resource, self.offset)
         if self.pyname is None:
             return

rope/refactor/encapsulate_field.py

-import rope.base.codeanalyze
-from rope.base import pynames, taskhandle
+from rope.base import pynames, taskhandle, codeanalyze, evaluate, exceptions
 from rope.base.change import ChangeSet, ChangeContents
-from rope.refactor import sourceutils
+from rope.refactor import sourceutils, occurrences
 
 
 class EncapsulateField(object):
 
     def __init__(self, project, resource, offset):
         self.pycore = project.pycore
-        self.name = rope.base.codeanalyze.get_name_at(resource, offset)
-        self.pyname = rope.base.codeanalyze.get_pyname_at(self.pycore,
-                                                          resource, offset)
+        self.name = codeanalyze.get_name_at(resource, offset)
+        self.pyname = evaluate.get_pyname_at(self.pycore, resource, offset)
         if not self._is_an_attribute(self.pyname):
-            raise rope.base.exceptions.RefactoringError(
+            raise exceptions.RefactoringError(
                 'Encapsulate field should be performed on class attributes.')
         self.resource = self.pyname.get_definition_location()[0].get_resource()
 
     def __init__(self, pycore, name, pynames):
         self.pycore = pycore
         self.name = name
-        self.occurrences_finder = rope.refactor.occurrences.\
-                                  FilteredFinder(pycore, name, pynames)
+        self.occurrences_finder = occurrences.FilteredFinder(pycore, name,
+                                                             pynames)
         self.getter = 'get_' + name
         self.setter = 'set_' + name
 
 
     def get_changed_module(self):
         result = []
-        word_finder = rope.base.codeanalyze.WordRangeFinder(self.source)
+        word_finder = codeanalyze.WordRangeFinder(self.source)
         for occurrence in self.occurrences_finder.find_occurrences(self.resource,
                                                                    self.pymodule):
             start, end = occurrence.get_word_range()
             self._manage_writes(start, result)
             result.append(self.source[self.last_modified:start])
             if self._is_assigned_in_a_tuple_assignment(occurrence):
-                raise rope.base.exceptions.RefactoringError(
+                raise exceptions.RefactoringError(
                     'Cannot handle tuple assignments in encapsulate field.')
             if occurrence.is_written():
                 assignment_type = word_finder.get_assignment_type(start)
         start_offset = self.lines.get_line_start(start_line)
 
         line = self.source[start_offset:self.lines.get_line_end(end_line)]
-        word_finder = rope.base.codeanalyze.WordRangeFinder(line)
+        word_finder = codeanalyze.WordRangeFinder(line)
 
         relative_offset = offset - start_offset
         relative_primary_start = occurance.get_primary_range()[0] - start_offset

rope/refactor/inline.py

 
 import rope.base.exceptions
 import rope.refactor.functionutils
-from rope.base import pynames, pyobjects, codeanalyze, taskhandle
+from rope.base import pynames, pyobjects, codeanalyze, taskhandle, evaluate
 from rope.base.change import ChangeSet, ChangeContents
 from rope.refactor import occurrences, rename, sourceutils, importutils, move
 
 
     """
     pycore = project.pycore
-    pyname = codeanalyze.get_pyname_at(pycore, resource, offset)
+    pyname = evaluate.get_pyname_at(pycore, resource, offset)
     if pyname is None:
         raise rope.base.exceptions.RefactoringError(
             'Inline refactoring should be performed on a method/local variable.')
     def __init__(self, project, resource, offset):
         self.project = project
         self.pycore = project.pycore
-        self.pyname = codeanalyze.get_pyname_at(self.pycore, resource, offset)
+        self.pyname = evaluate.get_pyname_at(self.pycore, resource, offset)
         range_finder = codeanalyze.WordRangeFinder(resource.read())
         self.region = range_finder.get_primary_range(offset)
         self.name = range_finder.get_word_at(offset)

rope/refactor/introduce_factory.py

-import rope.base.codeanalyze
 import rope.base.exceptions
 import rope.base.pyobjects
 import rope.refactor.importutils
-from rope.base import taskhandle
+from rope.base import taskhandle, evaluate
 from rope.base.change import (ChangeSet, ChangeContents)
 from rope.refactor import rename, occurrences, sourceutils
 
         self.pycore = project.pycore
         self.offset = offset
 
-        self.old_pyname = \
-            rope.base.codeanalyze.get_pyname_at(self.pycore, resource, offset)
-        if self.old_pyname is None or \
-           not isinstance(self.old_pyname.get_object(), rope.base.pyobjects.PyClass):
+        self.old_pyname = evaluate.get_pyname_at(self.pycore, resource, offset)
+        if self.old_pyname is None or not isinstance(self.old_pyname.get_object(),
+                                                     rope.base.pyobjects.PyClass):
             raise rope.base.exceptions.RefactoringError(
                 'Introduce factory should be performed on a class.')
         self.old_name = self.old_pyname.get_object().get_name()

rope/refactor/introduce_parameter.py

 import rope.base.change
-from rope.base import codeanalyze, exceptions
+from rope.base import codeanalyze, exceptions, evaluate
 from rope.refactor import functionutils, sourceutils, occurrences
 
 
 
     def _get_name_and_pyname(self):
         return (codeanalyze.get_name_at(self.resource, self.offset),
-                codeanalyze.get_pyname_at(self.pycore, self.resource, self.offset))
+                evaluate.get_pyname_at(self.pycore, self.resource, self.offset))
 
     def get_changes(self, new_parameter):
         definition_info = functionutils.DefinitionInfo.read(self.pyfunction)

rope/refactor/localtofield.py

-import rope.base.codeanalyze
-from rope.base import pynames
+from rope.base import pynames, codeanalyze, evaluate, exceptions
 from rope.refactor.rename import Rename
 
 
         self.offset = offset
 
     def get_changes(self):
-        name = rope.base.codeanalyze.get_name_at(self.resource, self.offset)
-        pyname = rope.base.codeanalyze.get_pyname_at(self.pycore, self.resource, self.offset)
+        name = codeanalyze.get_name_at(self.resource, self.offset)
+        pyname = evaluate.get_pyname_at(self.pycore, self.resource, self.offset)
         if not self._is_a_method_local(pyname):
-            raise rope.base.exceptions.RefactoringError(
+            raise exceptions.RefactoringError(
                 'Convert local variable to field should be performed on \n'
                 'a local variable of a method.')
 
     def _check_redefinition(self, name, function_scope):
         class_scope = function_scope.parent
         if name in class_scope.pyobject.get_attributes():
-            raise rope.base.exceptions.RefactoringError(
+            raise exceptions.RefactoringError(
                 'The field %s already exists' % name)
 
     def _get_field_name(self, pyfunction, name):

rope/refactor/method_object.py

-from rope.base import codeanalyze, pyobjects, exceptions, change
+from rope.base import pyobjects, exceptions, change, evaluate
 from rope.refactor import sourceutils, occurrences, rename
 
 
 
     def __init__(self, project, resource, offset):
         self.pycore = project.pycore
-        pyname = codeanalyze.get_pyname_at(self.pycore, resource, offset)
+        pyname = evaluate.get_pyname_at(self.pycore, resource, offset)
         if pyname is None or not isinstance(pyname.get_object(),
                                             pyobjects.PyFunction):
             raise exceptions.RefactoringError(

rope/refactor/move.py

 based on inputs.
 
 """
-from rope.base import pyobjects, codeanalyze, exceptions, pynames, taskhandle
+from rope.base import pyobjects, codeanalyze, exceptions, pynames, taskhandle, evaluate
 from rope.base.change import ChangeSet, ChangeContents, MoveResource
-from rope.refactor import (importutils, rename, occurrences, sourceutils,
-                           functionutils, extract)
+from rope.refactor import importutils, rename, occurrences, sourceutils, functionutils
 
 
 def create_move(project, resource, offset=None):
     """
     if offset is None:
         return MoveModule(project, resource)
-    pyname = codeanalyze.get_pyname_at(project.pycore, resource, offset)
+    pyname = evaluate.get_pyname_at(project.pycore, resource, offset)
     if pyname is None:
         raise exceptions.RefactoringError(
             'Move only works on classes, functions, modules and methods.')
     def __init__(self, project, resource, offset):
         self.project = project
         self.pycore = project.pycore
-        pyname = codeanalyze.get_pyname_at(project.pycore, resource, offset)
+        pyname = evaluate.get_pyname_at(project.pycore, resource, offset)
         self.method_name = codeanalyze.get_name_at(resource, offset)
         self.pyfunction = pyname.get_object()
         if self.pyfunction.get_kind() != 'method':
 
     def __init__(self, project, resource, offset):
         self.pycore = project.pycore
-        self.old_pyname = codeanalyze.get_pyname_at(self.pycore, resource, offset)
+        self.old_pyname = evaluate.get_pyname_at(self.pycore, resource, offset)
         self.old_name = self.old_pyname.get_object().get_name()
         pymodule = self.old_pyname.get_object().get_module()
         self.source = pymodule.get_resource()

rope/refactor/occurrences.py

 import re
 
-from rope.base import pynames, pyobjects, codeanalyze
+from rope.base import pynames, pyobjects, codeanalyze, evaluate
 
 
 class FilteredFinder(object):
         if self._name_finder is None:
             if self.pymodule is None:
                 self.pymodule = self.pycore.resource_to_pyobject(self.resource)
-            self._name_finder = codeanalyze.ScopeNameFinder(self.pymodule)
+            self._name_finder = evaluate.ScopeNameFinder(self.pymodule)
         return self._name_finder
 
     def get_source_code(self):

rope/refactor/rename.py

-from rope.base import exceptions, codeanalyze, pyobjects, pynames, taskhandle
+import warnings
+
+from rope.base import exceptions, codeanalyze, pyobjects, pynames, taskhandle, evaluate
 from rope.base.change import ChangeSet, ChangeContents, MoveResource
 from rope.refactor import occurrences, sourceutils
-import warnings
 
 
 class Rename(object):
         if offset is not None:
             self.old_name = codeanalyze.get_name_at(self.resource, offset)
             self.old_instance, self.old_pyname = \
-                codeanalyze.get_primary_and_pyname_at(self.pycore,
-                                                      resource, offset)
+                evaluate.get_primary_and_pyname_at(self.pycore,
+                                                   resource, offset)
             if self.old_pyname is None:
                 raise exceptions.RefactoringError(
                     'Rename refactoring should be performed'
         self.resource = resource
         self.offset = offset
         self.old_name = codeanalyze.get_name_at(resource, offset)
-        self.old_pyname = codeanalyze.get_pyname_at(self.pycore,
-                                                    resource, offset)
+        self.old_pyname = evaluate.get_pyname_at(self.pycore, resource, offset)
         self.pymodule = self.pycore.resource_to_pyobject(self.resource)
 
     def get_old_name(self):

ropetest/codeanalyzetest.py

 from rope.base import exceptions
 from rope.base.codeanalyze import \
     (CachingLogicalLineFinder, SourceLinesAdapter,
-     WordRangeFinder, ScopeNameFinder, LogicalLineFinder, get_block_start)
+     WordRangeFinder, LogicalLineFinder, get_block_start)
 from ropetest import testutils
+import rope.base.evaluate
 
 
 LogicalLineFinder = CachingLogicalLineFinder
         result = word_finder.get_word_parens_range(0)
         self.assertEquals((1, len(code) - 1), result)
 
+    def test_is_assigned_here_for_equality_test(self):
+        code = 'a == 1\n'
+        word_finder = WordRangeFinder(code)
+        self.assertFalse(word_finder.is_assigned_here(0))
+
+    # XXX: is assigned here should work for tuple assignments
+    def xxx_test_is_assigned_here_for_tuple_assignment(self):
+        code = 'a, b == (1, 2)\n'
+        word_finder = WordRangeFinder(code)
+        self.assertTrue(word_finder.is_assigned_here(0))
+
 
 class ScopeNameFinderTest(unittest.TestCase):
 
     # FIXME: in normal scopes the interpreter raises `UnboundLocalName`
     # exception, but not in class bodies
     def xxx_test_global_name_in_class_body(self):
-        code = 'a_var = 10\nclass Sample(object):\n    a_var = a_var\n'
+        code = 'a_var = 10\nclass C(object):\n    a_var = a_var\n'
         scope = self.pycore.get_string_scope(code)
-        name_finder = ScopeNameFinder(scope.pyobject)
-        self.assertEquals(scope.get_name('a_var'), name_finder.get_pyname_at(len(code) - 3))
+        name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject)
+        result = name_finder.get_pyname_at(len(code) - 3)
+        self.assertEquals(scope.get_name('a_var'), result)
 
     def test_class_variable_attribute_in_class_body(self):
-        code = 'a_var = 10\nclass Sample(object):\n    a_var = a_var\n'
+        code = 'a_var = 10\nclass C(object):\n    a_var = a_var\n'
         scope = self.pycore.get_string_scope(code)
-        name_finder = ScopeNameFinder(scope.pyobject)
-        a_var_pyname = scope.get_name('Sample').get_object().get_attribute('a_var')
-        self.assertEquals(a_var_pyname, name_finder.get_pyname_at(len(code) - 12))
+        name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject)
+        a_var_pyname = scope.get_name('C').\
+                       get_object().get_attribute('a_var')
+        result = name_finder.get_pyname_at(len(code) - 12)
+        self.assertEquals(a_var_pyname, result)
 
     def test_class_variable_attribute_in_class_body2(self):
-        code = 'a_var = 10\nclass Sample(object):\n    a_var \\\n= a_var\n'
+        code = 'a_var = 10\nclass C(object):\n    a_var \\\n= a_var\n'
         scope = self.pycore.get_string_scope(code)
-        name_finder = ScopeNameFinder(scope.pyobject)
-        a_var_pyname = scope.get_name('Sample').get_object().get_attribute('a_var')
-        self.assertEquals(a_var_pyname, name_finder.get_pyname_at(len(code) - 12))
+        name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject)
+        a_var_pyname = scope.get_name('C').\
+                       get_object().get_attribute('a_var')
+        result = name_finder.get_pyname_at(len(code) - 12)
+        self.assertEquals(a_var_pyname, result)
 
     def test_class_method_attribute_in_class_body(self):
-        code = 'class Sample(object):\n    def a_method(self):\n        pass\n'
+        code = 'class C(object):\n    def a_method(self):\n        pass\n'
         scope = self.pycore.get_string_scope(code)
-        name_finder = ScopeNameFinder(scope.pyobject)
-        a_method_pyname = scope.get_name('Sample').get_object().get_attribute('a_method')
-        self.assertEquals(a_method_pyname,
-                          name_finder.get_pyname_at(code.index('a_method') + 2))
+        name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject)
+        a_method_pyname = scope.get_name('C').\
+                          get_object().get_attribute('a_method')
+        result = name_finder.get_pyname_at(code.index('a_method') + 2)
+        self.assertEquals(a_method_pyname, result)
 
     def test_inner_class_attribute_in_class_body(self):
-        code = 'class Sample(object):\n    class AClass(object):\n        pass\n'
+        code = 'class C(object):\n    class CC(object):\n        pass\n'
         scope = self.pycore.get_string_scope(code)
-        name_finder = ScopeNameFinder(scope.pyobject)
-        a_class_pyname = scope.get_name('Sample').get_object().get_attribute('AClass')
-        self.assertEquals(a_class_pyname,
-                          name_finder.get_pyname_at(code.index('AClass') + 2))
+        name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject)
+        a_class_pyname = scope.get_name('C').get_object().get_attribute('CC')
+        result = name_finder.get_pyname_at(code.index('CC') + 2)
+        self.assertEquals(a_class_pyname, result)
 
     def test_class_method_in_class_body_but_not_indexed(self):
-        code = 'class Sample(object):\n    def a_func(self, a_func):\n        pass\n'
+        code = 'class C(object):\n    def func(self, func):\n        pass\n'
         scope = self.pycore.get_string_scope(code)
-        a_func_pyname = scope.get_scopes()[0].get_scopes()[0].get_name('a_func')
-        name_finder = ScopeNameFinder(scope.pyobject)
-        self.assertEquals(a_func_pyname, name_finder.get_pyname_at(code.index(', a_func') + 3))
+        a_func_pyname = scope.get_scopes()[0].get_scopes()[0].get_name('func')
+        name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject)
+        result = name_finder.get_pyname_at(code.index(', func') + 3)
+        self.assertEquals(a_func_pyname, result)
 
     def test_function_but_not_indexed(self):
         code = 'def a_func(a_func):\n    pass\n'
         scope = self.pycore.get_string_scope(code)
         a_func_pyname = scope.get_name('a_func')
-        name_finder = ScopeNameFinder(scope.pyobject)
-        self.assertEquals(a_func_pyname, name_finder.get_pyname_at(code.index('a_func') + 3))
+        name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject)
+        result = name_finder.get_pyname_at(code.index('a_func') + 3)
+        self.assertEquals(a_func_pyname, result)
 
     def test_modules_after_from_statements(self):
         root_folder = self.project.root
         mod.write('def a_func():\n    pass\n')
         code = 'from mod import a_func\n'
         scope = self.pycore.get_string_scope(code)
-        name_finder = ScopeNameFinder(scope.pyobject)
+        name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject)
         mod_pyobject = self.pycore.resource_to_pyobject(mod)
         found_pyname = name_finder.get_pyname_at(code.index('mod') + 1)
         self.assertEquals(mod_pyobject, found_pyname.get_object())
         mod1.write('def afunc():\n    pass\n')
         code = 'from mod1 import (\n    afunc as func)\n'
         scope = self.pycore.get_string_scope(code)
-        name_finder = ScopeNameFinder(scope.pyobject)
+        name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject)
         mod_pyobject = self.pycore.resource_to_pyobject(mod1)
         afunc = mod_pyobject.get_attribute('afunc')
         found_pyname = name_finder.get_pyname_at(code.index('afunc') + 1)
         code = 'from ..mod1 import a_func\n'
         mod2.write(code)
         mod2_scope = self.pycore.resource_to_pyobject(mod2).get_scope()
-        name_finder = ScopeNameFinder(mod2_scope.pyobject)
+        name_finder = rope.base.evaluate.ScopeNameFinder(mod2_scope.pyobject)
         mod1_pyobject = self.pycore.resource_to_pyobject(mod1)
         found_pyname = name_finder.get_pyname_at(code.index('mod1') + 1)
         self.assertEquals(mod1_pyobject, found_pyname.get_object())
         mod1.write('import pkg1.pkg2.mod2')
 
         mod1_scope = self.pycore.resource_to_pyobject(mod1).get_scope()
-        name_finder = ScopeNameFinder(mod1_scope.pyobject)
+        name_finder = rope.base.evaluate.ScopeNameFinder(mod1_scope.pyobject)
         pkg2_pyobject = self.pycore.resource_to_pyobject(pkg2)
         found_pyname = name_finder.get_pyname_at(mod1.read().index('pkg2') + 1)
         self.assertEquals(pkg2_pyobject, found_pyname.get_object())
     def test_get_pyname_at_on_language_keywords(self):
         code = 'def a_func(a_func):\n    pass\n'
         pymod = self.pycore.get_string_module(code)
-        name_finder = ScopeNameFinder(pymod)
+        name_finder = rope.base.evaluate.ScopeNameFinder(pymod)
         name_finder.get_pyname_at(code.index('pass'))
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.