diff --git a/jedi/parser/tree.py b/jedi/parser/tree.py index 4387c38e..5cb44fa4 100644 --- a/jedi/parser/tree.py +++ b/jedi/parser/tree.py @@ -65,10 +65,17 @@ class DocstringMixin(object): """ Returns a cleaned version of the docstring token. """ if isinstance(self, Module): stmt = self.children[0] - else: + elif isinstance(self, ClassOrFunc): stmt = self.children[self.children.index(':') + 1] if is_node(stmt, 'suite'): # Normally a suite stmt = stmt.children[2] # -> NEWLINE INDENT stmt + else: + c = self.parent.children + index = c.index(self) - 1 + if not index: + return '' + stmt = c[index - 1] + if is_node(stmt, 'simple_stmt'): stmt = stmt.children[0] diff --git a/test/test_api/test_api_classes.py b/test/test_api/test_api_classes.py index df6e3898..11bf596f 100644 --- a/test/test_api/test_api_classes.py +++ b/test/test_api/test_api_classes.py @@ -119,11 +119,16 @@ def test_completion_docstring(): """ Jedi should follow imports in certain conditions """ + def docstr(src, result): + c = Script(src).completions()[0] + assert c.docstring(raw=True, fast=False) == cleandoc(result) + c = Script('import jedi\njed').completions()[0] assert c.docstring(fast=False) == cleandoc(jedi_doc) - c = Script('import jedi\njedi.Scr').completions()[0] - assert c.docstring(raw=True, fast=False) == cleandoc(Script.__doc__) + docstr('import jedi\njedi.Scr', cleandoc(Script.__doc__)) + + docstr('abcd=3;abcd', '') def test_completion_params():