diff --git a/jedi/api/completion.py b/jedi/api/completion.py index 97e6bf41..7d16b481 100644 --- a/jedi/api/completion.py +++ b/jedi/api/completion.py @@ -93,30 +93,12 @@ class Completion: Analyzes the context that a completion is made in and decides what to return. - Could specialized completions for: - - from/import completions - - as nothing - - statements that start always on new line - 'import', 'class', 'def', 'try', 'except', - 'finally', 'while', with - - statements that start always on new line or after ; or after : - return raise continue break del pass global nonlocal assert - - def/class nothing - - async for/def/with - - \n@/del/return/raise no keyword (after keyword no keyword)? - - after keyword - - continue/break/pass nothing - - global/nonlocal search global - - after operator no keyword: return - - yield like return + after ( and = - - almost always ok - 'and', 'for', 'if', 'else', 'in', 'is', 'lambda', 'not', 'or' - - after operations no keyword: - + = * ** - etc Maybe work with the parser state? + Technically this works by generating a parser stack and analysing the + current stack for possible grammar nodes. - # hard: - - await - - yield from / raise from / from import difference + Possible enhancements: + - global/nonlocal search global + - yield from / raise from <- could be only exceptions/generators - In args: */**: no completion - In params (also lambda): no completion before = """ @@ -164,8 +146,9 @@ class Completion: level, only_modules ) - elif nodes and nodes[-1] == 'as': + elif nodes and nodes[-1] in ('as', 'def', 'class'): # No completions for ``with x as foo`` and ``import x as foo``. + # Also true for defining names as a class or function. return [] else: completion_names += self._simple_complete(completion_parts) diff --git a/test/completion/keywords.py b/test/completion/keywords.py index 5ecaaa90..9631e8d6 100644 --- a/test/completion/keywords.py +++ b/test/completion/keywords.py @@ -26,6 +26,34 @@ a + break #? ['break'] b; break -# Should not complete after as. +# ----------------- +# Keywords should not appear everywhere. +# ----------------- + #? [] with open() as f +#? [] +def i +#? [] +class i + +#? [] +continue i + +# More syntax details, e.g. while only after newline, but not after semicolon, +# continue also after semicolon +#? ['while'] +while +#? [] +x while +#? [] +x; while +#? ['continue'] +x; continue + +#? [] +and +#? ['and'] +x and +#? [] +x * and