Commits

Ali Gholami Rudi  committed 04968ee

fixsyntax: moved fixing syntax errors to fixsyntax module

  • Participants
  • Parent commits 98b0038

Comments (0)

Files changed (3)

File rope/contrib/codeassist.py

 import rope.base.codeanalyze
 import rope.base.evaluate
 from rope.base import pyobjects, pynames, builtins, exceptions, worder
-from rope.base.codeanalyze import ArrayLinesAdapter, LogicalLineFinder, SourceLinesAdapter
-from rope.refactor import functionutils, importutils
+from rope.base.codeanalyze import SourceLinesAdapter
+from rope.contrib import fixsyntax
+from rope.refactor import functionutils
 
 
 def code_assist(project, source_code, offset, resource=None,
 
 def get_doc(project, source_code, offset, resource=None, maxfixes=1):
     """Get the pydoc"""
-    pymodule = _get_pymodule(project.pycore, source_code, resource, maxfixes)
-    pyname = _find_pyname_at(project, source_code, offset, pymodule, maxfixes)
+    pymodule = fixsyntax.get_pymodule(project.pycore,
+                                       source_code, resource, maxfixes)
+    pyname = fixsyntax.find_pyname_at(project, source_code,
+                                       offset, pymodule, maxfixes)
     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 = _get_pymodule(project.pycore, source_code, resource, maxfixes)
-    pyname = _find_pyname_at(project, source_code, offset, pymodule, maxfixes)
+    pymodule = fixsyntax.get_pymodule(project.pycore, source_code,
+                                       resource, maxfixes)
+    pyname = fixsyntax.find_pyname_at(project, source_code,
+                                       offset, pymodule, maxfixes)
     if pyname is None:
         return None
     pyobject = pyname.get_object()
     location cannot be determined ``(None, None)`` is returned.
 
     """
-    pymodule = _get_pymodule(project.pycore, source_code, resource, maxfixes)
-    pyname = _find_pyname_at(project, source_code, offset, pymodule, maxfixes)
+    pymodule = fixsyntax.get_pymodule(project.pycore, source_code,
+                                       resource, maxfixes)
+    pyname = fixsyntax.find_pyname_at(project, source_code, offset,
+                                       pymodule, maxfixes)
     if pyname is not None:
         module, lineno = pyname.get_definition_location()
         if module is not None:
 
     def _code_completions(self):
         lineno = self.code.count('\n', 0, self.offset) + 1
-        pymodule = _get_pymodule(self.pycore, self.code,
-                                 self.resource, self.maxfixes)
+        pymodule = fixsyntax.get_pymodule(self.pycore, self.code,
+                                           self.resource, self.maxfixes)
         module_scope = pymodule.get_scope()
         code = pymodule.source_code
         lines = code.split('\n')
         result = {}
-        start = _logical_start(lines, lineno)
-        indents = _get_line_indents(lines[start - 1])
+        start = fixsyntax._logical_start(lines, lineno)
+        indents = fixsyntax._get_line_indents(lines[start - 1])
         inner_scope = module_scope.get_inner_scope_for_line(start, indents)
         if self.word_finder.is_a_name_after_from_import(self.offset):
             return self._from_import_completions(pymodule)
         return cmp(name1, name2)
 
 
-class _Commenter(object):
-
-    def __init__(self, lines):
-        self.lines = lines
-
-    def comment(self, lineno):
-        start = _logical_start(self.lines, lineno, check_prev=True) - 1
-        end = self._get_block_end(start)
-        indents = _get_line_indents(self.lines[start])
-        if 0 < start:
-            last_lineno = self._last_non_blank(start - 1)
-            last_line = self.lines[last_lineno]
-            if last_line.rstrip().endswith(':'):
-                indents = _get_line_indents(last_line) + 4
-        self.lines[start] = ' ' * indents + 'pass'
-        for line in range(start + 1, end + 1):
-            self.lines[line] = self.lines[start]
-        self._fix_incomplete_try_blocks(lineno, indents)
-
-    def _last_non_blank(self, start):
-        while start > 0 and self.lines[start].strip() == '':
-            start -= 1
-        return start
-
-    def _get_block_end(self, lineno):
-        end_line = lineno
-        base_indents = _get_line_indents(self.lines[lineno])
-        for i in range(lineno + 1, len(self.lines)):
-            if _get_line_indents(self.lines[i]) >= base_indents:
-                end_line = i
-            else:
-                break
-        return end_line
-
-    def _fix_incomplete_try_blocks(self, lineno, indents):
-        block_start = lineno
-        last_indents = current_indents = indents
-        while block_start > 0:
-            block_start = rope.base.codeanalyze.get_block_start(
-                ArrayLinesAdapter(self.lines), block_start) - 1
-            if self.lines[block_start].strip().startswith('try:'):
-                indents = _get_line_indents(self.lines[block_start])
-                if indents > last_indents:
-                    continue
-                last_indents = indents
-                block_end = self._find_matching_deindent(block_start)
-                line = self.lines[block_end].strip()
-                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')
-
-    def _find_matching_deindent(self, line_number):
-        indents = _get_line_indents(self.lines[line_number])
-        current_line = line_number + 1
-        while current_line < len(self.lines):
-            line = self.lines[current_line]
-            if not line.strip().startswith('#') and not line.strip() == '':
-                # HACK: We should have used logical lines here
-                if _get_line_indents(self.lines[current_line]) <= indents:
-                    return current_line
-            current_line += 1
-        return len(self.lines) - 1
-
-
-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
-
-
-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)
-
-
-def _get_line_indents(line):
-    return rope.base.codeanalyze.count_line_indents(line)
-
-
-def _logical_start(lines, lineno, check_prev=False):
-    logical_finder = LogicalLineFinder(ArrayLinesAdapter(lines))
-    if check_prev:
-        prev = lineno - 1
-        while prev > 0:
-            start, end = logical_finder.logical_line_in(prev)
-            if end is None or start <= lineno < end:
-                return start
-            if start <= prev:
-                break
-            prev -= 1
-    return logical_finder.logical_line_in(lineno)[0]
-
-
 class PyDocExtractor(object):
 
     def get_doc(self, pyobject):

File rope/contrib/findit.py

 import rope.base.evaluate
 import rope.base.pyobjects
 from rope.base import taskhandle, exceptions, worder
+from rope.contrib import fixsyntax
 from rope.refactor import occurrences
 
 
     A `Location` object is returned if the definition location can be
     determined, otherwise ``None`` is returned.
     """
-    import rope.contrib.codeassist
-    main_module = rope.contrib.codeassist._get_pymodule(
-        project.pycore, code, resource, maxfixes)
-    pyname = rope.contrib.codeassist._find_pyname_at(
-        project, code, offset, main_module, maxfixes)
+    main_module = fixsyntax.get_pymodule(project.pycore, code,
+                                          resource, maxfixes)
+    pyname = fixsyntax.find_pyname_at(project, code, offset,
+                                       main_module, maxfixes)
     if pyname is not None:
         module, lineno = pyname.get_definition_location()
         name = rope.base.worder.Worder(code).get_word_at(offset)

File rope/contrib/fixsyntax.py

+import rope.base.codeanalyze
+import rope.base.evaluate
+from rope.base import worder, exceptions
+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)
+
+
+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
+
+
+class _Commenter(object):
+
+    def __init__(self, lines):
+        self.lines = lines
+
+    def comment(self, lineno):
+        start = _logical_start(self.lines, lineno, check_prev=True) - 1
+        end = self._get_block_end(start)
+        indents = _get_line_indents(self.lines[start])
+        if 0 < start:
+            last_lineno = self._last_non_blank(start - 1)
+            last_line = self.lines[last_lineno]
+            if last_line.rstrip().endswith(':'):
+                indents = _get_line_indents(last_line) + 4
+        self.lines[start] = ' ' * indents + 'pass'
+        for line in range(start + 1, end + 1):
+            self.lines[line] = self.lines[start]
+        self._fix_incomplete_try_blocks(lineno, indents)
+
+    def _last_non_blank(self, start):
+        while start > 0 and self.lines[start].strip() == '':
+            start -= 1
+        return start
+
+    def _get_block_end(self, lineno):
+        end_line = lineno
+        base_indents = _get_line_indents(self.lines[lineno])
+        for i in range(lineno + 1, len(self.lines)):
+            if _get_line_indents(self.lines[i]) >= base_indents:
+                end_line = i
+            else:
+                break
+        return end_line
+
+    def _fix_incomplete_try_blocks(self, lineno, indents):
+        block_start = lineno
+        last_indents = current_indents = indents
+        while block_start > 0:
+            block_start = rope.base.codeanalyze.get_block_start(
+                ArrayLinesAdapter(self.lines), block_start) - 1
+            if self.lines[block_start].strip().startswith('try:'):
+                indents = _get_line_indents(self.lines[block_start])
+                if indents > last_indents:
+                    continue
+                last_indents = indents
+                block_end = self._find_matching_deindent(block_start)
+                line = self.lines[block_end].strip()
+                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')
+
+    def _find_matching_deindent(self, line_number):
+        indents = _get_line_indents(self.lines[line_number])
+        current_line = line_number + 1
+        while current_line < len(self.lines):
+            line = self.lines[current_line]
+            if not line.strip().startswith('#') and not line.strip() == '':
+                # HACK: We should have used logical lines here
+                if _get_line_indents(self.lines[current_line]) <= indents:
+                    return current_line
+            current_line += 1
+        return len(self.lines) - 1
+
+def _logical_start(lines, lineno, check_prev=False):
+    logical_finder = LogicalLineFinder(ArrayLinesAdapter(lines))
+    if check_prev:
+        prev = lineno - 1
+        while prev > 0:
+            start, end = logical_finder.logical_line_in(prev)
+            if end is None or start <= lineno < end:
+                return start
+            if start <= prev:
+                break
+            prev -= 1
+    return logical_finder.logical_line_in(lineno)[0]
+
+
+def _get_line_indents(line):
+    return rope.base.codeanalyze.count_line_indents(line)