mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 22:14:27 +08:00
Some more clarifications around docstrings, see #860
This commit is contained in:
@@ -445,16 +445,26 @@ class Completion:
|
|||||||
generally in "Python" code in docstrings, we use the following
|
generally in "Python" code in docstrings, we use the following
|
||||||
heuristic:
|
heuristic:
|
||||||
|
|
||||||
- Either
|
- Having an indented block of code
|
||||||
|
- Having some doctest code that starts with `>>>`
|
||||||
"""
|
"""
|
||||||
|
def iter_relevant_lines(lines):
|
||||||
|
include_next_line = False
|
||||||
|
for l in code_lines:
|
||||||
|
if include_next_line or l.startswith('>>>') or l.startswith(' '):
|
||||||
|
yield re.sub(r'^( *>>> ?| +)', '', l)
|
||||||
|
else:
|
||||||
|
yield None
|
||||||
|
|
||||||
|
include_next_line = bool(re.match(' *>>>', l))
|
||||||
|
|
||||||
string = dedent(string)
|
string = dedent(string)
|
||||||
code_lines = split_lines(string, keepends=True)
|
code_lines = split_lines(string, keepends=True)
|
||||||
if code_lines[-1].startswith('>>>') or code_lines[-1].startswith(' '):
|
code_lines = list(iter_relevant_lines(code_lines))
|
||||||
code_lines = [
|
if code_lines[-1] is not None:
|
||||||
re.sub(r'^(>>> ?| +)', '', l)
|
# Some code lines might be None, therefore get rid of that.
|
||||||
for l in code_lines
|
code_lines = [c or '\n' for c in code_lines]
|
||||||
if l.startswith('>>>') or l.startswith(' ')
|
|
||||||
]
|
|
||||||
module_node = self._inference_state.grammar.parse(''.join(code_lines))
|
module_node = self._inference_state.grammar.parse(''.join(code_lines))
|
||||||
module_value = ModuleValue(
|
module_value = ModuleValue(
|
||||||
self._inference_state,
|
self._inference_state,
|
||||||
|
|||||||
@@ -424,3 +424,18 @@ def test_basic_str_init_signature(Script, disable_typeshed):
|
|||||||
Foo(''')
|
Foo(''')
|
||||||
c, = Script(code).find_signatures()
|
c, = Script(code).find_signatures()
|
||||||
assert c.name == 'Foo'
|
assert c.name == 'Foo'
|
||||||
|
|
||||||
|
|
||||||
|
def test_doctest_result_completion(Script):
|
||||||
|
code = '''\
|
||||||
|
"""
|
||||||
|
comment
|
||||||
|
|
||||||
|
>>> something = 3
|
||||||
|
somethi
|
||||||
|
"""
|
||||||
|
something_else = 8
|
||||||
|
'''
|
||||||
|
c1, c2 = Script(code).complete(line=5)
|
||||||
|
assert c1.complete == 'ng'
|
||||||
|
assert c2.complete == 'ng_else'
|
||||||
|
|||||||
Reference in New Issue
Block a user