From 20061fcf2e03ce3f5278914c85bee160c39ebf55 Mon Sep 17 00:00:00 2001 From: Sid Shanker Date: Mon, 4 May 2015 00:19:14 -0700 Subject: [PATCH 01/15] Added in support for autocompleting `enumerate`. Resolves #554 --- jedi/evaluate/compiled/fake/builtins.pym | 15 +++++++++++++++ test/completion/stdlib.py | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/jedi/evaluate/compiled/fake/builtins.pym b/jedi/evaluate/compiled/fake/builtins.pym index 1283de00..1ed9b0b2 100644 --- a/jedi/evaluate/compiled/fake/builtins.pym +++ b/jedi/evaluate/compiled/fake/builtins.pym @@ -207,6 +207,21 @@ class dict(): return d +class enumerate(): + def __init__(self, sequence, start=0): + self.__sequence = sequence + + def __iter__(self): + for i in self.__sequence: + yield 1, i + + def __next__(self): + return next(self.__iter__()) + + def next(self): + return next(self.__iter__()) + + class reversed(): def __init__(self, sequence): self.__sequence = sequence diff --git a/test/completion/stdlib.py b/test/completion/stdlib.py index 6c5a330c..c645ebb9 100644 --- a/test/completion/stdlib.py +++ b/test/completion/stdlib.py @@ -35,6 +35,15 @@ next(open('')) #? ['__itemsize__'] tuple.__itemsize__ +# ----------------- +# enumerate +# ----------------- +for i, j in enumerate(["as", "ad"]): + #? int() + i + #? str() + j + # ----------------- # re # ----------------- From 6237214bffaa48e7096c3db737c7150022d275a4 Mon Sep 17 00:00:00 2001 From: Sid Shanker Date: Mon, 4 May 2015 09:06:14 -0700 Subject: [PATCH 02/15] Added @squidarth to AUTHORS.txt. --- AUTHORS.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.txt b/AUTHORS.txt index 64aae316..b0f1fe65 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -33,5 +33,6 @@ Savor d'Isavano (@KenetJervet) Phillip Berndt (@phillipberndt) Ian Lee (@IanLee1521) Farkhad Khatamov (@hatamov) +Sid Shanker (@squidarth) Note: (@user) means a github user name. From 2d75efff2a9d34881eb5b9be203f43b029e28bed Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Tue, 5 May 2015 19:00:25 -0500 Subject: [PATCH 03/15] Use repr() for the Whitespace repr This makes whitespace appear as instead of . --- jedi/parser/tree.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/jedi/parser/tree.py b/jedi/parser/tree.py index 619067e1..079c2d70 100644 --- a/jedi/parser/tree.py +++ b/jedi/parser/tree.py @@ -248,6 +248,10 @@ class LeafWithNewLines(Leaf): return end_pos_line, end_pos_col + @utf8_repr + def __repr__(self): + return "<%s: %r>" % (type(self).__name__, self.value) + class Whitespace(LeafWithNewLines): """Contains NEWLINE and ENDMARKER tokens.""" __slots__ = () From 258d5aee4a8733b8aedcc12e6acafa1104b2796e Mon Sep 17 00:00:00 2001 From: Sid Shanker Date: Sun, 10 May 2015 18:59:55 -0700 Subject: [PATCH 04/15] Added support for correct continue & break autocompletion. --- jedi/api/__init__.py | 8 ++++-- jedi/api/keywords.py | 51 +++++++++++++++++++++++++++++-------- test/completion/keywords.py | 21 +++++++++++++++ 3 files changed, 68 insertions(+), 12 deletions(-) diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index a5b0b109..31300974 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -157,7 +157,7 @@ class Script(object): if unfinished_dotted: return completion_names else: - return keywords.keyword_names('import') + return set([keywords.keyword('import').name]) if isinstance(user_stmt, tree.Import): module = self._parser.module() @@ -168,7 +168,11 @@ class Script(object): if names is None and not isinstance(user_stmt, tree.Import): if not path and not dot: # add keywords - completion_names += keywords.keyword_names(all=True) + completion_names += keywords.completion_names( + self._evaluator, + user_stmt, + self._pos, + module) # TODO delete? We should search for valid parser # transformations. completion_names += self._simple_complete(path, dot, like) diff --git a/jedi/api/keywords.py b/jedi/api/keywords.py index 2a54ba2d..4feb39fd 100644 --- a/jedi/api/keywords.py +++ b/jedi/api/keywords.py @@ -5,7 +5,7 @@ from jedi._compatibility import is_py3 from jedi import common from jedi.evaluate import compiled from jedi.evaluate.helpers import FakeName - +from jedi.parser.tree import Leaf try: from pydoc_data import topics as pydoc_topics except ImportError: @@ -18,22 +18,49 @@ else: keys = keyword.kwlist + ['None', 'False', 'True'] -def keywords(string='', pos=(0, 0), all=False): - if all: - return set([Keyword(k, pos) for k in keys]) +def has_inappropriate_leaf_keyword(pos, module): + relevant_errors = filter( + lambda error: error.first_pos[0] == pos[0], + module.error_statement_stacks) + + for error in relevant_errors: + if error.next_token in keys: + return True + + return False + +def completion_names(evaluator, stmt, pos, module): + keyword_list = all_keywords() + + if not isinstance(stmt, Leaf) or has_inappropriate_leaf_keyword(pos, module): + keyword_list = filter( + lambda keyword: not keyword.only_valid_as_leaf, + keyword_list + ) + return [keyword.name for keyword in keyword_list] + + +def all_keywords(pos=(0,0)): + return set([Keyword(k, pos) for k in keys]) + + +def keyword(string, pos=(0,0)): if string in keys: - return set([Keyword(string, pos)]) - return set() - - -def keyword_names(*args, **kwargs): - return [k.name for k in keywords(*args, **kwargs)] + return Keyword(string, pos) + else: + return None def get_operator(string, pos): return Keyword(string, pos) +keywords_only_valid_as_leaf = ( + 'continue', + 'break', +) + + class Keyword(object): def __init__(self, name, pos): self.name = FakeName(name, self, pos) @@ -43,6 +70,10 @@ class Keyword(object): def get_parent_until(self): return self.parent + @property + def only_valid_as_leaf(self): + return self.name.value in keywords_only_valid_as_leaf + @property def names(self): """ For a `parsing.Name` like comparision """ diff --git a/test/completion/keywords.py b/test/completion/keywords.py index 851140b1..ba56c210 100644 --- a/test/completion/keywords.py +++ b/test/completion/keywords.py @@ -4,3 +4,24 @@ raise #? ['except', 'Exception'] except + +#? [] +b + continu + +#? [] +b + continue + +#? ['continue'] +b; continue + +#? ['continue'] +b; continu + +#? [] +c + brea + +#? [] +a + break + +#? ['break'] +b; break From de7273e04b3c8e4ca3f8ba32298a5d3d763b3ab3 Mon Sep 17 00:00:00 2001 From: Sid Shanker Date: Sun, 17 May 2015 23:11:02 -0700 Subject: [PATCH 05/15] Fixed utf-8 decoding error in build. --- test/run.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/run.py b/test/run.py index a48e1fb2..d7309143 100755 --- a/test/run.py +++ b/test/run.py @@ -290,9 +290,12 @@ def collect_dir_tests(base_dir, test_files, check_thirdparty=False): skip = 'Thirdparty-Library %s not found.' % lib path = os.path.join(base_dir, f_name) - source = open(path).read() - if not is_py3: - source = unicode(source, 'UTF-8') + + if is_py3: + source = open(path, encoding='utf-8').read() + else: + source = unicode(open(path).read(), 'UTF-8') + for case in collect_file_tests(StringIO(source), lines_to_execute): case.path = path From a5fd0b6c4fd3bc73677a02a57cb6ac49478a91a2 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 14 Jun 2015 19:46:53 +0900 Subject: [PATCH 06/15] Update README - add emacs company-mode --- README.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 17f1aba8..0fbb1088 100644 --- a/README.rst +++ b/README.rst @@ -33,7 +33,7 @@ It's really easy. Jedi can currently be used with the following editors: - Vim (jedi-vim_, YouCompleteMe_) -- Emacs (Jedi.el_, elpy_, anaconda-mode_, ycmd_) +- Emacs (Jedi.el_, company-mode_, elpy_, anaconda-mode_, ycmd_) - Sublime Text (SublimeJEDI_ [ST2 + ST3], anaconda_ [only ST3]) - SynWrite_ - TextMate_ (Not sure if it's actually working) @@ -176,6 +176,7 @@ For more detailed information visit the `testing documentation .. _jedi-vim: https://github.com/davidhalter/jedi-vim .. _youcompleteme: http://valloric.github.io/YouCompleteMe/ .. _Jedi.el: https://github.com/tkf/emacs-jedi +.. _company-mode: https://github.com/syohex/emacs-company-jedi .. _elpy: https://github.com/jorgenschaefer/elpy .. _anaconda-mode: https://github.com/proofit404/anaconda-mode .. _ycmd: https://github.com/abingham/emacs-ycmd From 91fb4fd9e1fb66735a0194877ef3a3b0585da833 Mon Sep 17 00:00:00 2001 From: Dmitry Sadovnychyi Date: Mon, 22 Jun 2015 18:09:04 +0800 Subject: [PATCH 07/15] Add link to Atom in README --- README.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.rst b/README.rst index 0fbb1088..774a4d85 100644 --- a/README.rst +++ b/README.rst @@ -39,6 +39,7 @@ Jedi can currently be used with the following editors: - TextMate_ (Not sure if it's actually working) - Kate_ version 4.13+ supports it natively, you have to enable it, though. [`proof `_] +- Atom_ (autocomplete-python_) And it powers the following projects: @@ -186,3 +187,5 @@ For more detailed information visit the `testing documentation .. _wdb: https://github.com/Kozea/wdb .. _TextMate: https://github.com/lawrenceakka/python-jedi.tmbundle .. _Kate: http://kate-editor.org +.. _Atom: https://atom.io/ +.. _autocomplete-python: https://atom.io/packages/autocomplete-python From 81252f0b7d2e399f7e146487734597035505ec79 Mon Sep 17 00:00:00 2001 From: Dmitry Sadovnychyi Date: Tue, 23 Jun 2015 13:49:04 +0800 Subject: [PATCH 08/15] Add link to Atom in docs --- docs/docs/usage.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/docs/usage.rst b/docs/docs/usage.rst index 960ca2ab..12fbf66f 100644 --- a/docs/docs/usage.rst +++ b/docs/docs/usage.rst @@ -47,6 +47,10 @@ Kate: `__, you have to enable it, though. +Atom: + +- autocomplete-python_ + .. _other-software: @@ -86,3 +90,4 @@ Using a custom ``$HOME/.pythonrc.py`` .. _wdb: https://github.com/Kozea/wdb .. _TextMate: https://github.com/lawrenceakka/python-jedi.tmbundle .. _kate: http://kate-editor.org/ +.. _autocomplete-python: https://atom.io/packages/autocomplete-python From 4cc6cb3ac44a526f43561961e7d6ad7ddbbf7da9 Mon Sep 17 00:00:00 2001 From: Tzerjen Wei Date: Wed, 29 Jul 2015 00:08:21 +0800 Subject: [PATCH 09/15] fix a set comprehension issue --- jedi/evaluate/__init__.py | 2 +- test/test_evaluate/test_iterable.py | 31 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/test_evaluate/test_iterable.py diff --git a/jedi/evaluate/__init__.py b/jedi/evaluate/__init__.py index a959d05f..c8dca140 100644 --- a/jedi/evaluate/__init__.py +++ b/jedi/evaluate/__init__.py @@ -242,7 +242,7 @@ class Evaluator(object): except (IndexError, AttributeError): pass else: - if isinstance(comp_for, tree.CompFor): + if isinstance(comp_for, tree.CompFor) and c[0] != '{': return [iterable.Comprehension.from_atom(self, atom)] return [iterable.Array(self, atom)] diff --git a/test/test_evaluate/test_iterable.py b/test/test_evaluate/test_iterable.py new file mode 100644 index 00000000..a8b29f6a --- /dev/null +++ b/test/test_evaluate/test_iterable.py @@ -0,0 +1,31 @@ +from textwrap import dedent + +from jedi import names + + +def get_scope_and_evaluator(source): + d = names(dedent(source))[0] + return d.parent()._definition, d._evaluator + +def find_types(s): + scope, evaluator = get_scope_and_evaluator(s) + return evaluator.find_types(scope, s[0]) + + + +def test_comprehensions(): + """ + test list/set/generator/dict comprehension syntax + """ + + s = "a = [i for i in range(10)]" + assert len(find_types(s)) == 1 + + s = "a = [i for i in range(10)]" + assert len(find_types(s)) == 1 + + s = "a = {i:i for i in range(10)}" + assert len(find_types(s)) == 1 + + s = "a = {i for i in range(10)}" + assert len(find_types(s)) == 1 From fdcf7183172cff01e10321c83ea460fa8f006415 Mon Sep 17 00:00:00 2001 From: Tzerjen Wei Date: Sun, 9 Aug 2015 14:11:42 +0800 Subject: [PATCH 10/15] move set comprehension tests to comletion/comprehensions.py --- test/completion/comprehensions.py | 7 +++++++ test/test_evaluate/test_iterable.py | 31 ----------------------------- 2 files changed, 7 insertions(+), 31 deletions(-) delete mode 100644 test/test_evaluate/test_iterable.py diff --git a/test/completion/comprehensions.py b/test/completion/comprehensions.py index 98a15bcb..a8aaa0e9 100644 --- a/test/completion/comprehensions.py +++ b/test/completion/comprehensions.py @@ -50,6 +50,13 @@ left ##? str() {a-1:b for a,b in {1:'a', 3:1.0}.items()}[0] +# with a set literal +#? int() +[a for a in {1, 2, 3}][0] + +##? int() +{a for a in range(10)}[0] + # list comprehensions should also work in combination with functions def listen(arg): for x in arg: diff --git a/test/test_evaluate/test_iterable.py b/test/test_evaluate/test_iterable.py deleted file mode 100644 index a8b29f6a..00000000 --- a/test/test_evaluate/test_iterable.py +++ /dev/null @@ -1,31 +0,0 @@ -from textwrap import dedent - -from jedi import names - - -def get_scope_and_evaluator(source): - d = names(dedent(source))[0] - return d.parent()._definition, d._evaluator - -def find_types(s): - scope, evaluator = get_scope_and_evaluator(s) - return evaluator.find_types(scope, s[0]) - - - -def test_comprehensions(): - """ - test list/set/generator/dict comprehension syntax - """ - - s = "a = [i for i in range(10)]" - assert len(find_types(s)) == 1 - - s = "a = [i for i in range(10)]" - assert len(find_types(s)) == 1 - - s = "a = {i:i for i in range(10)}" - assert len(find_types(s)) == 1 - - s = "a = {i for i in range(10)}" - assert len(find_types(s)) == 1 From 666cbbf123d8364a603249e7a400b566a6ced0bb Mon Sep 17 00:00:00 2001 From: Tzerjen Wei Date: Sun, 9 Aug 2015 17:58:38 +0800 Subject: [PATCH 11/15] update the test of set comprehension literal --- test/completion/comprehensions.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/completion/comprehensions.py b/test/completion/comprehensions.py index a8aaa0e9..8ba1fbce 100644 --- a/test/completion/comprehensions.py +++ b/test/completion/comprehensions.py @@ -54,8 +54,18 @@ left #? int() [a for a in {1, 2, 3}][0] +#? set() +{a for a in range(10)} + ##? int() -{a for a in range(10)}[0] +[x for x in {a for a in range(10)}][0] + +##? int() +{a for a in range(10)}.pop() + +##? int() +iter({a for a in range(10)}).next() + # list comprehensions should also work in combination with functions def listen(arg): From b9902b22d6b7eb9dcc15fb533d06e358f690b696 Mon Sep 17 00:00:00 2001 From: Jonas Haag Date: Tue, 18 Aug 2015 11:44:01 +0200 Subject: [PATCH 12/15] Fix docstring --- jedi/api/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index 31300974..18ca0fc7 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -62,8 +62,8 @@ class Script(object): :type source: str :param line: The line to perform actions on (starting with 1). :type line: int - :param col: The column of the cursor (starting with 0). - :type col: int + :param column: The column of the cursor (starting with 0). + :type column: int :param path: The path of the file in the file system, or ``''`` if it hasn't been saved yet. :type path: str or None From c2f8e1846e175e931ceec43c2c35cc2d61325a35 Mon Sep 17 00:00:00 2001 From: Ronan Jouchet Date: Wed, 26 Aug 2015 21:58:25 -0400 Subject: [PATCH 13/15] Fix #603 - Fix broken 'Tips for how to use jedi efficiently' link in README.md --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 774a4d85..f465f6b8 100644 --- a/README.rst +++ b/README.rst @@ -96,7 +96,7 @@ You can run Jedi on cPython 2.6, 2.7, 3.2, 3.3 or 3.4, but it should also understand/parse code older than those versions. Tips on how to use Jedi efficiently can be found `here -`_. +`_. API --- From cae88b0f94bd1bb04a775b2fb19bf842792b9e45 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Wed, 30 Sep 2015 14:59:21 +0200 Subject: [PATCH 14/15] Add sourcelair to Jedi usages. --- README.rst | 2 ++ docs/docs/usage.rst | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/README.rst b/README.rst index 774a4d85..635f192d 100644 --- a/README.rst +++ b/README.rst @@ -40,6 +40,7 @@ Jedi can currently be used with the following editors: - Kate_ version 4.13+ supports it natively, you have to enable it, though. [`proof `_] - Atom_ (autocomplete-python_) +- sourcelair_ And it powers the following projects: @@ -189,3 +190,4 @@ For more detailed information visit the `testing documentation .. _Kate: http://kate-editor.org .. _Atom: https://atom.io/ .. _autocomplete-python: https://atom.io/packages/autocomplete-python +.. _sourcelair: https://www.sourcelair.com diff --git a/docs/docs/usage.rst b/docs/docs/usage.rst index 12fbf66f..dbeecc77 100644 --- a/docs/docs/usage.rst +++ b/docs/docs/usage.rst @@ -51,6 +51,10 @@ Atom: - autocomplete-python_ +SourceLair: + +- sourcelair_ + .. _other-software: @@ -91,3 +95,4 @@ Using a custom ``$HOME/.pythonrc.py`` .. _TextMate: https://github.com/lawrenceakka/python-jedi.tmbundle .. _kate: http://kate-editor.org/ .. _autocomplete-python: https://atom.io/packages/autocomplete-python +.. _sourcelair: https://www.sourcelair.com From 317ef333fe1a401676baf2ea51f2768cd41db2e6 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Wed, 30 Sep 2015 15:01:49 +0200 Subject: [PATCH 15/15] Captial letters in SourceLair changed. --- README.rst | 4 ++-- docs/docs/usage.rst | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 97f6f609..7bab1904 100644 --- a/README.rst +++ b/README.rst @@ -40,7 +40,7 @@ Jedi can currently be used with the following editors: - Kate_ version 4.13+ supports it natively, you have to enable it, though. [`proof `_] - Atom_ (autocomplete-python_) -- sourcelair_ +- SourceLair_ And it powers the following projects: @@ -190,4 +190,4 @@ For more detailed information visit the `testing documentation .. _Kate: http://kate-editor.org .. _Atom: https://atom.io/ .. _autocomplete-python: https://atom.io/packages/autocomplete-python -.. _sourcelair: https://www.sourcelair.com +.. _SourceLair: https://www.sourcelair.com diff --git a/docs/docs/usage.rst b/docs/docs/usage.rst index dbeecc77..55d9aca6 100644 --- a/docs/docs/usage.rst +++ b/docs/docs/usage.rst @@ -53,7 +53,7 @@ Atom: SourceLair: -- sourcelair_ +- SourceLair_ .. _other-software: @@ -95,4 +95,4 @@ Using a custom ``$HOME/.pythonrc.py`` .. _TextMate: https://github.com/lawrenceakka/python-jedi.tmbundle .. _kate: http://kate-editor.org/ .. _autocomplete-python: https://atom.io/packages/autocomplete-python -.. _sourcelair: https://www.sourcelair.com +.. _SourceLair: https://www.sourcelair.com