1
0
forked from VimPlug/jedi

At least functions generate docstrings again.

This commit is contained in:
Dave Halter
2014-11-20 01:37:18 +01:00
parent 22b288fc73
commit ce5d428d22
3 changed files with 39 additions and 16 deletions

View File

@@ -104,7 +104,8 @@ def get_faked(module, obj, name=None):
# Set the docstr which was previously not set (faked modules don't # Set the docstr which was previously not set (faked modules don't
# contain it). # contain it).
doc = '''"""%s"""''' % obj.__doc__ # TODO need escapes. doc = '''"""%s"""''' % obj.__doc__ # TODO need escapes.
result.add_docstr(tokenize.Token(tokenize.STRING, doc, (0, 0))) # TODO We need to add the docstr in a proper way.
#result.add_docstr(tokenize.Token(tokenize.STRING, doc, (0, 0)))
return result return result

View File

@@ -152,8 +152,10 @@ class Parser(object):
arr = self.scope_names_stack[-1].setdefault(name.value, []) arr = self.scope_names_stack[-1].setdefault(name.value, [])
arr.append(name) arr.append(name)
return name return name
elif type in (tokenize.STRING, tokenize.NUMBER): elif type == tokenize.STRING:
return pr.Literal(value, start_pos, prefix) return pr.String(value, start_pos, prefix)
elif type == tokenize.NUMBER:
return pr.Number(value, start_pos, prefix)
elif type in (tokenize.NEWLINE, tokenize.ENDMARKER): elif type in (tokenize.NEWLINE, tokenize.ENDMARKER):
return pr.Whitespace(value, start_pos, prefix) return pr.Whitespace(value, start_pos, prefix)
else: else:

View File

@@ -95,24 +95,36 @@ class GetCodeState(object):
class DocstringMixin(object): class DocstringMixin(object):
__slots__ = () __slots__ = ()
def add_docstr(self, token):
""" Clean up a docstring """
self._doc_token = token
@property @property
def raw_doc(self): def raw_doc(self):
""" Returns a cleaned version of the docstring token. """ """ Returns a cleaned version of the docstring token. """
if isinstance(self, SubModule):
stmt = self.children[0]
else:
stmt = self.children[-1]
if is_node(stmt, 'suite'): # Normally a suite
stmt = stmt.children[2] # -> NEWLINE INDENT stmt
if is_node(stmt, 'simple_stmt'):
stmt = stmt.children[0]
try: try:
# Returns a literal cleaned version of the ``Token``. first = stmt.children[0]
cleaned = cleandoc(literal_eval(self._doc_token.string))
# Since we want the docstr output to be always unicode, just force
# it.
if is_py3 or isinstance(cleaned, unicode):
return cleaned
else:
return unicode(cleaned, 'UTF-8', 'replace')
except AttributeError: except AttributeError:
return u('') pass # Probably a pass Keyword (Leaf).
else:
if isinstance(first, String):
# TODO We have to check next leaves until there are no new
# leaves anymore that might be part of the docstring. A
# docstring can also look like this: ``'foo' 'bar'
# Returns a literal cleaned version of the ``Token``.
cleaned = cleandoc(literal_eval(first.value))
# Since we want the docstr output to be always unicode, just
# force it.
if is_py3 or isinstance(cleaned, unicode):
return cleaned
else:
return unicode(cleaned, 'UTF-8', 'replace')
return ''
class Base(object): class Base(object):
@@ -330,6 +342,14 @@ class Literal(Leaf):
return "<%s: %s>" % (type(self).__name__, self.value) return "<%s: %s>" % (type(self).__name__, self.value)
class Number(Literal):
pass
class String(Literal):
pass
class Operator(Leaf): class Operator(Leaf):
def __str__(self): def __str__(self):
return self.value return self.value