Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle index error in link wrapping #141

Merged
merged 4 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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