diff --git a/jedi/__init__.py b/jedi/__init__.py index d698bbaa..b69130db 100644 --- a/jedi/__init__.py +++ b/jedi/__init__.py @@ -29,7 +29,7 @@ import sys # imports and circular imports... Just avoid it: sys.path.insert(0, __path__[0]) -from .api import Script, NotFoundError, set_debug_function +from .api import Script, NotFoundError, quick_complete, set_debug_function from . import settings from . import api diff --git a/jedi/api.py b/jedi/api.py index ab0c8af2..9b770f91 100644 --- a/jedi/api.py +++ b/jedi/api.py @@ -7,7 +7,7 @@ catch :exc:`NotFoundError` which is being raised if your completion is not possible. """ from __future__ import with_statement -__all__ = ['Script', 'NotFoundError', 'set_debug_function'] +__all__ = ['Script', 'NotFoundError', 'quick_complete', 'set_debug_function'] import re @@ -454,6 +454,26 @@ class Script(object): api_classes._clear_caches() +def quick_complete(source): + """ + Convenience function to complete a source string at the end. + + Example:: + + >>> quick_complete('import json\\njson.l') + [, ] + + :param source: The source code to be completed. + :type source: string + :return: Completion objects as returned by :meth:`complete`. + :rtype: list of :class:`api_classes.Completion` + """ + lines = re.sub(r'[\n\r\s]*$', '', source).splitlines() + pos = len(lines), len(lines[-1]) + script = Script(source, pos[0], pos[1], '') + return script.complete() + + def set_debug_function(func_cb=debug.print_to_stdout, warnings=True, notices=True, speed=True): """ diff --git a/test/regression.py b/test/regression.py index 428e0f3f..dcbc4b1e 100755 --- a/test/regression.py +++ b/test/regression.py @@ -336,6 +336,27 @@ class TestFeature(Base): any_re""" self.assertEqual(self.get_def(s)[0].full_name, 're.RegexObject') + def test_quick_completion(self): + sources = [ + ('import json; json.l', (1, 19)), + ('import json; json.l ', (1, 19)), + ('import json\njson.l', (2, 6)), + ('import json\njson.l ', (2, 6)), + ('import json\njson.l\n\n', (2, 6)), + ('import json\njson.l \n\n', (2, 6)), + ('import json\njson.l \n \n\n', (2, 6)), + ] + for source, pos in sources: + # Run quick_complete + quick_completions = api.quick_complete(source) + # Run real completion + script = api.Script(source, pos[0], pos[1], '') + real_completions = script.complete() + # Compare results + quick_values = [(c.full_name, c.line, c.column) for c in quick_completions] + real_values = [(c.full_name, c.line, c.column) for c in real_completions] + self.assertEqual(quick_values, real_values) + class TestSpeed(Base): def _check_speed(time_per_run, number=4, run_warm=True):