Skip to content

Commit 101b2b6

Browse files
committed
add comments and a couple of tests
1 parent 911c348 commit 101b2b6

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

pylsp/_utils.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,19 @@ def format_docstring(contents):
149149
"""
150150
contents = contents.replace('\t', u'\u00A0' * 4)
151151
contents = contents.replace(' ', u'\u00A0' * 2)
152-
example_snippets = re.findall(EX_SNIPPET_RE, contents)
153-
for snippet in example_snippets:
154-
contents = contents.replace(snippet, f"```python\n{snippet}\n```")
152+
153+
# If examples exist in the docstring wrap them in backticks
154+
if ">>>" in contents:
155+
# add an additional newline just in case the end of the
156+
# docstring doesn't end in a blank line.
157+
if contents[-2:] != "\n\n":
158+
contents = f"{contents}\n"
159+
# search for the example block regex
160+
example_snippets = re.findall(EX_SNIPPET_RE, contents)
161+
# wrap the snippets that were found in backticks
162+
for snippet in example_snippets:
163+
contents = contents.replace(snippet, f"```python\n{snippet}\n```")
164+
155165
return contents
156166

157167

test/test_utils.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,82 @@ def test_clip_column():
9494
assert _utils.clip_column(2, ['123\n', '123'], 0) == 2
9595
assert _utils.clip_column(3, ['123\n', '123'], 0) == 3
9696
assert _utils.clip_column(4, ['123\n', '123'], 1) == 3
97+
98+
99+
def test_format_docstring():
100+
teststr = """\
101+
Examples
102+
--------
103+
Abc
104+
105+
>>> a = 'test'
106+
>>> b = 5
107+
>>> a
108+
test
109+
>>> b + 3
110+
8
111+
112+
another
113+
114+
>>> x = np.array([1, 2, 3])
115+
>>> y = np.array([4, 5, 6])
116+
>>> x + y
117+
array([5, 7, 9])
118+
119+
"""
120+
resultstr = _utils.format_docstring(teststr)
121+
assert resultstr == """\
122+
Examples
123+
--------
124+
Abc
125+
126+
```python
127+
>>> a = 'test'
128+
>>> b = 5
129+
>>> a
130+
test
131+
>>> b + 3
132+
8
133+
```
134+
135+
another
136+
137+
```python
138+
>>> x = np.array([1, 2, 3])
139+
>>> y = np.array([4, 5, 6])
140+
>>> x + y
141+
array([5, 7, 9])
142+
```
143+
144+
"""
145+
146+
147+
def test_format_docstring_missing_newline():
148+
teststr = """\
149+
Examples
150+
--------
151+
Abc
152+
153+
>>> a = 'test'
154+
>>> b = 5
155+
>>> a
156+
test
157+
>>> b + 3
158+
8
159+
"""
160+
resultstr = _utils.format_docstring(teststr)
161+
assert resultstr == """\
162+
Examples
163+
--------
164+
Abc
165+
166+
```python
167+
>>> a = 'test'
168+
>>> b = 5
169+
>>> a
170+
test
171+
>>> b + 3
172+
8
173+
```
174+
175+
"""

0 commit comments

Comments
 (0)