Commits

Takafumi Arakaki committed 9f593ae

Added noerror option to code_assist

  • Participants
  • Parent commits bd22278

Comments (0)

Files changed (2)

File rope/contrib/codeassist.py

 
 
 def code_assist(project, source_code, offset, resource=None,
-                templates=None, maxfixes=1, later_locals=True):
+                templates=None, maxfixes=1, later_locals=True, noerror=False):
     """Return python code completions as a list of `CodeAssistProposal`\s
 
     `resource` is a `rope.base.resources.Resource` object.  If
     `maxfixes` is the maximum number of errors to fix if the code has
     errors in it.
 
+    If `noerror` is True empty result is returned when the errors
+    in the code cannot be fixed with `maxfixes` tries.
+
     If `later_locals` is `False` names defined in this scope and after
     this line is ignored.
 
                       DeprecationWarning, stacklevel=2)
     assist = _PythonCodeAssist(
         project, source_code, offset, resource=resource,
-        maxfixes=maxfixes, later_locals=later_locals)
+        maxfixes=maxfixes, later_locals=later_locals, noerror=noerror)
     return assist()
 
 
 class _PythonCodeAssist(object):
 
     def __init__(self, project, source_code, offset, resource=None,
-                 maxfixes=1, later_locals=True):
+                 maxfixes=1, later_locals=True, noerror=False):
         self.project = project
         self.pycore = self.project.pycore
         self.code = source_code
         self.resource = resource
         self.maxfixes = maxfixes
         self.later_locals = later_locals
+        self.noerror = noerror
         self.word_finder = worder.Worder(source_code, True)
         self.expression, self.starting, self.offset = \
             self.word_finder.get_splitted_primary_before(offset)
     def __call__(self):
         if self.offset > len(self.code):
             return []
-        completions = list(self._code_completions().values())
+        try:
+            completions = list(self._code_completions().values())
+        except exceptions.ModuleSyntaxError as err:
+            if self.noerror:
+                return []
+            else:
+                raise err
         if self.expression.strip() == '' and self.starting.strip() != '':
             completions.extend(self._matching_keywords(self.starting))
         return completions

File ropetest/contrib/codeassisttest.py

         code = 'sample (sdf+)\n'
         self._assist(code, maxfixes=0)
 
+    def test_noerror(self):
+        code = 'sample (sdf+)\n'
+        self.assertEquals(self._assist(code, maxfixes=0, noerror=True), [])
+
     def test_fixing_errors_with_maxfixes(self):
         code = 'def f():\n    sldj sldj\ndef g():\n    ran'
         result = self._assist(code, maxfixes=2)