Skip to content

Commit

Permalink
fix: handle index error in link wrapping (#141)
Browse files Browse the repository at this point in the history
* fix: index error on short in-line links

* test: add test for short in-line links

* fix: retain spaces between words

* test: add test for retain spaces between words
  • Loading branch information
weibullguy committed Dec 29, 2022
1 parent 5bae271 commit ee4bfe3
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/docformatter/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def do_preserve_links(
subsequent_indent=indentation,
)

# There is nothing to do if the input wasn't wrapped.
if len(lines) < 2:
return lines

url = next(
(
line
Expand All @@ -77,16 +81,23 @@ def do_preserve_links(
# Is this an in-line link (i.e., enclosed in <>)? We want to keep
# the '<' and '>' part of the link.
if re.search(r"<", url):
lines[url_idx] = f"{indentation}" + url.split(sep="<")[0].strip()
if len(url.split(sep="<")[0].strip()) > 0:
lines[url_idx] = (
f"{indentation}" + url.split(sep="<")[0].strip()
)

url = f"{indentation}<" + url.split(sep="<")[1]
url = url + lines[url_idx + 1].strip()
lines[url_idx + 1] = url
if len(url.split(sep=">")) < 2:
url = url + lines[url_idx + 1].strip()
lines[url_idx + 1] = url

# Is this a link target definition (i.e., .. a link: https://)? We
# want to keep the .. a link: on the same line as the url.
elif re.search(r"(\.\. )", url):
url = url + lines[url_idx + 1].strip()
lines[url_idx] = url
lines.pop(url_idx + 1)

# Is this a simple link (i.e., just a link in the text) that should
# be unwrapped? We want to break the url out from the rest of the
# text.
Expand Down
76 changes: 76 additions & 0 deletions tests/test_format_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,82 @@ def test_format_docstring_with_inline_links(
INDENTATION, docstring.strip()
)

@pytest.mark.unit
@pytest.mark.parametrize(
"args",
[["--wrap-descriptions", "72", ""]],
)
def test_format_docstring_with_short_inline_link(
self,
test_args,
args,
):
"""Short in-line links will remain untouched.
See issue #140. See requirement docformatter_10.1.3.1.
"""
uut = Formatter(
test_args,
sys.stderr,
sys.stdin,
sys.stdout,
)

docstring = '''\
"""This is yanf with a short link.
See `the link <https://www.link.com`_ for more details.
"""\
'''

assert '''\
"""This is yanf with a short link.
See `the link <https://www.link.com`_ for more details.
"""\
''' == uut._do_format_docstring(
INDENTATION, docstring.strip()
)

@pytest.mark.unit
@pytest.mark.parametrize(
"args",
[["--wrap-descriptions", "72", ""]],
)
def test_format_docstring_with_inline_link_retain_spaces(
self,
test_args,
args,
):
"""In-line links shouldn't remove blank spaces between words.
See issue #140.
"""
uut = Formatter(
test_args,
sys.stderr,
sys.stdin,
sys.stdout,
)

docstring = '''\
"""This is a docstring with a link that causes a wrap.
See `the link <https://www.link.com/a/long/link/that/causes/line/break>`_ for more details.
"""\
'''

assert '''\
"""This is a docstring with a link that causes a wrap.
See `the link
<https://www.link.com/a/long/link/that/causes/line/break>`_ for more
details.
"""\
''' == uut._do_format_docstring(
INDENTATION, docstring.strip()
)

@pytest.mark.unit
@pytest.mark.parametrize(
"args",
Expand Down

0 comments on commit ee4bfe3

Please sign in to comment.