diff --git a/jedi/evaluate/pep0484.py b/jedi/evaluate/pep0484.py index f5476af3..84bd772d 100644 --- a/jedi/evaluate/pep0484.py +++ b/jedi/evaluate/pep0484.py @@ -210,7 +210,7 @@ def _find_type_from_comment_hint(context, node, varlist, name): else: return [] - comment = node.get_following_comment_same_line() + comment = parser_utils.get_following_comment_same_line(node) if comment is None: return [] match = re.match(r"^#\s*type:\s*([^#]*)", comment) diff --git a/jedi/parser/tree.py b/jedi/parser/tree.py index 87d14ff1..e02fd8ca 100644 --- a/jedi/parser/tree.py +++ b/jedi/parser/tree.py @@ -223,33 +223,6 @@ class BaseNode(_NodeOrLeaf): def get_last_leaf(self): return self.children[-1].get_last_leaf() - def get_following_comment_same_line(self): - """ - returns (as string) any comment that appears on the same line, - after the node, including the # - """ - try: - if self.type == 'for_stmt': - whitespace = self.children[5].get_first_leaf().prefix - elif self.type == 'with_stmt': - whitespace = self.children[3].get_first_leaf().prefix - else: - whitespace = self.get_last_leaf().get_next_leaf().prefix - except AttributeError: - return None - except ValueError: - # TODO in some particular cases, the tree doesn't seem to be linked - # correctly - return None - if "#" not in whitespace: - return None - comment = whitespace[whitespace.index("#"):] - if "\r" in comment: - comment = comment[:comment.index("\r")] - if "\n" in comment: - comment = comment[:comment.index("\n")] - return comment - @utf8_repr def __repr__(self): code = self.get_code().replace('\n', ' ').strip() diff --git a/jedi/parser_utils.py b/jedi/parser_utils.py index e1776b25..dd7836cb 100644 --- a/jedi/parser_utils.py +++ b/jedi/parser_utils.py @@ -189,3 +189,31 @@ def move(node, line_offset): for c in children: move(c, line_offset) + +def get_following_comment_same_line(node): + """ + returns (as string) any comment that appears on the same line, + after the node, including the # + """ + try: + if node.type == 'for_stmt': + whitespace = node.children[5].get_first_leaf().prefix + elif node.type == 'with_stmt': + whitespace = node.children[3].get_first_leaf().prefix + else: + whitespace = node.get_last_leaf().get_next_leaf().prefix + except AttributeError: + return None + except ValueError: + # TODO in some particular cases, the tree doesn't seem to be linked + # correctly + return None + if "#" not in whitespace: + return None + comment = whitespace[whitespace.index("#"):] + if "\r" in comment: + comment = comment[:comment.index("\r")] + if "\n" in comment: + comment = comment[:comment.index("\n")] + return comment +