mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
create KeywordStatement to fit assert, del, global, etc into a more generalized schema, which can also improve the get_code method
This commit is contained in:
@@ -573,6 +573,11 @@ class Parser(object):
|
||||
# add the global to the top, because there it is
|
||||
# important.
|
||||
self.module.add_global(t)
|
||||
elif tok_str in STATEMENT_KEYWORDS:
|
||||
stmt, _ = self._parse_statement()
|
||||
k = pr.KeywordStatement(tok_str, tok.start_pos,
|
||||
use_as_parent_scope, stmt)
|
||||
self._scope.add_statement(k)
|
||||
# decorator
|
||||
elif tok_str == '@':
|
||||
stmt, tok = self._parse_statement()
|
||||
|
||||
@@ -46,7 +46,7 @@ from jedi import cache
|
||||
from jedi.parser import tokenize
|
||||
|
||||
|
||||
SCOPE_CONTENTS = ['asserts', 'subscopes', 'imports', 'statements', 'returns']
|
||||
SCOPE_CONTENTS = 'asserts', 'subscopes', 'imports', 'statements', 'returns'
|
||||
|
||||
|
||||
class GetCodeState(object):
|
||||
@@ -801,6 +801,29 @@ class Import(Simple):
|
||||
return n
|
||||
|
||||
|
||||
class KeywordStatement(object):
|
||||
"""
|
||||
For the following statements: `assert`, `del`, `global`, `nonlocal`,
|
||||
`raise`, `return`, `yield`, `pass`, `continue`, `break`, `return`, `yield`.
|
||||
"""
|
||||
__slots__ = ('name', 'start_pos', '_stmt', 'parent')
|
||||
|
||||
def __init__(self, name, start_pos, parent, stmt=None):
|
||||
self.name = name
|
||||
self.start_pos = start_pos
|
||||
self._stmt = stmt
|
||||
self.parent = parent
|
||||
|
||||
if stmt is not None:
|
||||
stmt.parent = self
|
||||
|
||||
def get_code(self):
|
||||
if self._stmt is None:
|
||||
return self.name
|
||||
else:
|
||||
return '%s %s' % (self.name, self._stmt)
|
||||
|
||||
|
||||
class Statement(Simple, DocstringMixin):
|
||||
"""
|
||||
This is the class for all the possible statements. Which means, this class
|
||||
|
||||
Reference in New Issue
Block a user