Anonymous avatar Anonymous committed b0b2a6c

fixsyntax: removed old functions

Comments (0)

Files changed (3)

rope/contrib/codeassist.py

 
 def get_doc(project, source_code, offset, resource=None, maxfixes=1):
     """Get the pydoc"""
-    pymodule = fixsyntax.get_pymodule(project.pycore,
-                                       source_code, resource, maxfixes)
-    pyname = fixsyntax.find_pyname_at(project, source_code,
-                                       offset, pymodule, maxfixes)
+    fixer = fixsyntax.FixSyntax(project.pycore, source_code,
+                                resource, maxfixes)
+    pymodule = fixer.get_pymodule()
+    pyname = fixer.pyname_at(offset)
     if pyname is None:
         return None
     pyobject = pyname.get_object()
     If `remove_self` is `True`, the first parameter whose name is self
     will be removed for methods.
     """
-    pymodule = fixsyntax.get_pymodule(project.pycore, source_code,
-                                       resource, maxfixes)
-    pyname = fixsyntax.find_pyname_at(project, source_code,
-                                       offset, pymodule, maxfixes)
+    fixer = fixsyntax.FixSyntax(project.pycore, source_code,
+                                resource, maxfixes)
+    pymodule = fixer.get_pymodule()
+    pyname = fixer.pyname_at(offset)
     if pyname is None:
         return None
     pyobject = pyname.get_object()
     location cannot be determined ``(None, None)`` is returned.
 
     """
-    pymodule = fixsyntax.get_pymodule(project.pycore, source_code,
-                                       resource, maxfixes)
-    pyname = fixsyntax.find_pyname_at(project, source_code, offset,
-                                       pymodule, maxfixes)
+    fixer = fixsyntax.FixSyntax(project.pycore, source_code,
+                                resource, maxfixes)
+    pymodule = fixer.get_pymodule()
+    pyname = fixer.pyname_at(offset)
     if pyname is not None:
         module, lineno = pyname.get_definition_location()
         if module is not None:
         self.maxfixes = maxfixes
         self.later_locals = later_locals
         self.word_finder = worder.Worder(source_code, True)
+        offset = min(offset, len(source_code))
         self.expression, self.starting, self.offset = \
             self.word_finder.get_splitted_primary_before(offset)
 
 
     def _code_completions(self):
         lineno = self.code.count('\n', 0, self.offset) + 1
-        pymodule = fixsyntax.get_pymodule(self.pycore, self.code,
-                                           self.resource, self.maxfixes)
+        fixer = fixsyntax.FixSyntax(self.pycore, self.code,
+                                    self.resource, self.maxfixes)
+        pymodule = fixer.get_pymodule()
         module_scope = pymodule.get_scope()
         code = pymodule.source_code
         lines = code.split('\n')

rope/contrib/findit.py

     A `Location` object is returned if the definition location can be
     determined, otherwise ``None`` is returned.
     """
-    main_module = fixsyntax.get_pymodule(project.pycore, code,
-                                          resource, maxfixes)
-    pyname = fixsyntax.find_pyname_at(project, code, offset,
-                                       main_module, maxfixes)
+    fixer = fixsyntax.FixSyntax(project.pycore, code, resource, maxfixes)
+    main_module = fixer.get_pymodule()
+    pyname = fixer.find_pyname_at(offset)
     if pyname is not None:
         module, lineno = pyname.get_definition_location()
         name = rope.base.worder.Worder(code).get_word_at(offset)

rope/contrib/fixsyntax.py

 import rope.base.codeanalyze
 import rope.base.evaluate
-from rope.base import worder, exceptions
+from rope.base import worder, exceptions, utils
 from rope.base.codeanalyze import ArrayLinesAdapter, LogicalLineFinder
 
 
-def get_pymodule(pycore, code, resource, maxfixes=1):
-    """Get a `PyModule`"""
-    commenter = None
-    errors = []
-    tries = 0
-    while True:
-        try:
-            if tries == 0 and resource is not None and resource.read() == code:
-                return pycore.resource_to_pyobject(resource, force_errors=True)
-            return pycore.get_string_module(code, resource=resource,
-                                            force_errors=True)
-        except exceptions.ModuleSyntaxError, e:
-            if tries < maxfixes:
-                tries += 1
-                if commenter is None:
-                    lines = code.split('\n')
-                    lines.append('\n')
-                    commenter = _Commenter(lines)
-                commenter.comment(e.lineno)
-                code = '\n'.join(commenter.lines)
-                errors.append('  * line %s: %s ... fixed' % (e.lineno,
-                                                             e.message_))
-            else:
-                errors.append('  * line %s: %s ... raised!' % (e.lineno,
-                                                               e.message_))
-                new_message = ('\nSyntax errors in file %s:\n' % e.filename) \
-                               + '\n'.join(errors)
-                raise exceptions.ModuleSyntaxError(e.filename, e.lineno,
-                                                   new_message)
+class FixSyntax(object):
 
+    def __init__(self, pycore, code, resource, maxfixes=1):
+        self.pycore = pycore
+        self.code = code
+        self.resource = resource
+        self.maxfixes = maxfixes
 
-def find_pyname_at(project, code, offset, pymodule, maxfixes):
-    def old_pyname():
-        word_finder = worder.Worder(code, True)
-        expression = word_finder.get_primary_at(offset)
-        expression = expression.replace('\\\n', ' ').replace('\n', ' ')
-        lineno = code.count('\n', 0, offset)
-        scope = pymodule.get_scope().get_inner_scope_for_line(lineno)
-        return rope.base.evaluate.eval_str(scope, expression)
-    def new_pyname():
-        return rope.base.evaluate.eval_location(pymodule, offset)
-    new_code = pymodule.source_code
-    if new_code.startswith(code[:offset + 1]):
-        return new_pyname()
-    result = old_pyname()
-    if result is None and offset < len(new_code):
-        return new_pyname()
-    return result
+    @utils.saveit
+    def get_pymodule(self):
+        """Get a `PyModule`"""
+        errors = []
+        code = self.code
+        tries = 0
+        while True:
+            try:
+                if tries == 0 and self.resource is not None and \
+                   self.resource.read() == code:
+                    return self.pycore.resource_to_pyobject(self.resource,
+                                                            force_errors=True)
+                return self.pycore.get_string_module(
+                    code, resource=self.resource, force_errors=True)
+            except exceptions.ModuleSyntaxError, e:
+                if tries < self.maxfixes:
+                    tries += 1
+                    self.commenter.comment(e.lineno)
+                    code = '\n'.join(self.commenter.lines)
+                    errors.append('  * line %s: %s ... fixed' % (e.lineno,
+                                                                 e.message_))
+                else:
+                    errors.append('  * line %s: %s ... raised!' % (e.lineno,
+                                                                   e.message_))
+                    new_message = ('\nSyntax errors in file %s:\n' % e.filename) \
+                                   + '\n'.join(errors)
+                    raise exceptions.ModuleSyntaxError(e.filename, e.lineno,
+                                                       new_message)
+
+    @property
+    @utils.saveit
+    def commenter(self):
+        lines = self.code.split('\n')
+        lines.append('\n')
+        return _Commenter(lines)
+
+    def pyname_at(self, offset):
+        pymodule = self.get_pymodule()
+        def old_pyname():
+            word_finder = worder.Worder(self.code, True)
+            expression = word_finder.get_primary_at(offset)
+            expression = expression.replace('\\\n', ' ').replace('\n', ' ')
+            lineno = self.code.count('\n', 0, offset)
+            scope = pymodule.get_scope().get_inner_scope_for_line(lineno)
+            return rope.base.evaluate.eval_str(scope, expression)
+        def new_pyname():
+            return rope.base.evaluate.eval_location(pymodule, offset)
+        new_code = pymodule.source_code
+        if new_code.startswith(self.code[:offset + 1]):
+            return new_pyname()
+        result = old_pyname()
+        if result is None and offset < len(new_code):
+            return new_pyname()
+        return result
 
 
 class _Commenter(object):
 
     def __init__(self, lines):
         self.lines = lines
+        self.diffs = [0] * len(lines)
 
     def comment(self, lineno):
         start = _logical_start(self.lines, lineno, check_prev=True) - 1
             last_line = self.lines[last_lineno]
             if last_line.rstrip().endswith(':'):
                 indents = _get_line_indents(last_line) + 4
-        self.lines[start] = ' ' * indents + 'pass'
+        self._set(start, ' ' * indents + 'pass')
         for line in range(start + 1, end + 1):
-            self.lines[line] = self.lines[start]
+            self._set(line, self.lines[start])
         self._fix_incomplete_try_blocks(lineno, indents)
 
     def _last_non_blank(self, start):
                 if not (line.startswith('finally:') or
                         line.startswith('except ') or
                         line.startswith('except:')):
-                    self.lines.insert(block_end, ' ' * indents + 'finally:')
-                    self.lines.insert(block_end + 1, ' ' * indents + '    pass')
+                    self._insert(block_end, ' ' * indents + 'finally:')
+                    self._insert(block_end + 1, ' ' * indents + '    pass')
 
     def _find_matching_deindent(self, line_number):
         indents = _get_line_indents(self.lines[line_number])
             current_line += 1
         return len(self.lines) - 1
 
+    def _set(self, lineno, line):
+        if lineno < len(self.diffs):
+            self.diffs[lineno] += len(self.lines[lineno]) - len(line)
+        self.lines[lineno] = line
+
+    def _insert(self, lineno, line):
+        if lineno < len(self.diffs):
+            self.diffs[lineno] += len(line) + 1
+        self.lines.insert(lineno, line)
+
 def _logical_start(lines, lineno, check_prev=False):
     logical_finder = LogicalLineFinder(ArrayLinesAdapter(lines))
     if check_prev:
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.