From 281ef0d7af3c818976eef6a1f42c297a80fa5c16 Mon Sep 17 00:00:00 2001 From: Doyle Rowland Date: Wed, 28 Dec 2022 21:14:47 -0500 Subject: [PATCH 1/4] fix: index error on short in-line links --- src/docformatter/syntax.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/docformatter/syntax.py b/src/docformatter/syntax.py index 0ef5aca..06e693b 100644 --- a/src/docformatter/syntax.py +++ b/src/docformatter/syntax.py @@ -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 @@ -81,6 +85,7 @@ def do_preserve_links( url = f"{indentation}<" + url.split(sep="<")[1] 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): From a782dc89e3152e18a5f9d61008fd0477823298af Mon Sep 17 00:00:00 2001 From: Doyle Rowland Date: Wed, 28 Dec 2022 21:15:05 -0500 Subject: [PATCH 2/4] test: add test for short in-line links --- tests/test_format_docstring.py | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/test_format_docstring.py b/tests/test_format_docstring.py index 8e8dae6..73eb61c 100644 --- a/tests/test_format_docstring.py +++ b/tests/test_format_docstring.py @@ -797,6 +797,43 @@ 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 Date: Wed, 28 Dec 2022 21:54:21 -0500 Subject: [PATCH 3/4] fix: retain spaces between words --- src/docformatter/syntax.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/docformatter/syntax.py b/src/docformatter/syntax.py index 06e693b..127ea2b 100644 --- a/src/docformatter/syntax.py +++ b/src/docformatter/syntax.py @@ -81,10 +81,15 @@ 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. @@ -92,6 +97,7 @@ def do_preserve_links( 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. From 089e52c69e647ba3db519570dec8f0a40db51e27 Mon Sep 17 00:00:00 2001 From: Doyle Rowland Date: Wed, 28 Dec 2022 21:54:37 -0500 Subject: [PATCH 4/4] test: add test for retain spaces between words --- tests/test_format_docstring.py | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/test_format_docstring.py b/tests/test_format_docstring.py index 73eb61c..99c34e4 100644 --- a/tests/test_format_docstring.py +++ b/tests/test_format_docstring.py @@ -834,6 +834,45 @@ def test_format_docstring_with_short_inline_link( 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 `_ for more details. + """\ +''' + + assert '''\ +"""This is a docstring with a link that causes a wrap. + + See `the link + `_ for more + details. + """\ +''' == uut._do_format_docstring( + INDENTATION, docstring.strip() + ) + @pytest.mark.unit @pytest.mark.parametrize( "args",