Skip to content

Commit

Permalink
feat: adjust for tab when wrapping (#105)
Browse files Browse the repository at this point in the history
* chore: move pycharm image to docs/image

* feat: account for tabbed indentation when wrapping

* test: add test for wrapping tabbed indentation

* fix: remove redundant --force-wrap in parser

* chore: fix README indentation
  • Loading branch information
weibullguy authored Aug 11, 2022
1 parent 83f5b23 commit 3dc1a7e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
8 changes: 5 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ Below is the help output::
(default: False)
--force-wrap
force descriptions to be wrapped even if it may result
in a mess
(default: False)
in a mess (default: False)
--tab_width width
tabs in indentation are this many characters when
wrapping lines (default: 1)
--range start_line end_line
apply docformatter to docstrings between these lines;
line numbers are indexed at 1
Expand Down Expand Up @@ -295,7 +297,7 @@ PyCharm

Head over to ``Preferences > Tools > File Watchers``, click the ``+`` icon and configure *docformatter* as shown below:

.. image:: ./images/pycharm-file-watcher-configurations.png
.. image:: https://github.com/PyCQA/docformatter/blob/master/docs/images/pycharm-file-watcher-configurations.png?raw=true
:alt: PyCharm file watcher configurations

GitHub Actions
Expand Down
41 changes: 30 additions & 11 deletions docformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ def do_parse_arguments(self) -> None:
"set to 0 to disable wrapping "
"(default: %(default)s)",
)
self.parser.add_argument(
"--force-wrap",
action="store_true",
default=bool(self.flargs_dct.get("force-wrap", False)),
help="force descriptions to be wrapped even if it may "
"result in a mess (default: %(default)s)",
)
self.parser.add_argument(
"--tab_width",
type=int,
dest="tab_width",
metavar="width",
default=int(self.flargs_dct.get("tab-width", 1)),
help="tabs in indentation are this many characters when "
"wrapping lines (default: %(default)s)",
)
self.parser.add_argument(
"--blank",
dest="post_description_blank",
Expand Down Expand Up @@ -231,13 +247,6 @@ def do_parse_arguments(self) -> None:
"one-line docstring wraps to two or more lines "
"(default: %(default)s)",
)
self.parser.add_argument(
"--force-wrap",
action="store_true",
default=bool(self.flargs_dct.get("force-wrap", False)),
help="force descriptions to be wrapped even if it may "
"result in a mess (default: %(default)s)",
)
self.parser.add_argument(
"--range",
metavar="line",
Expand Down Expand Up @@ -412,12 +421,14 @@ def _format_code(
source,
summary_wrap_length=79,
description_wrap_length=72,
force_wrap=False,
tab_width=1,
pre_summary_newline=False,
pre_summary_space=False,
make_summary_multi_line=False,
close_quotes_on_newline=False,
post_description_blank=False,
force_wrap=False,

line_range=None,
length_range=None,
strict=True,
Expand Down Expand Up @@ -462,12 +473,13 @@ def _format_code(
token_string,
summary_wrap_length=summary_wrap_length,
description_wrap_length=description_wrap_length,
force_wrap=force_wrap,
tab_width=tab_width,
pre_summary_newline=pre_summary_newline,
pre_summary_space=pre_summary_space,
make_summary_multi_line=make_summary_multi_line,
close_quotes_on_newline=close_quotes_on_newline,
post_description_blank=post_description_blank,
force_wrap=force_wrap,
strict=strict,
)

Expand Down Expand Up @@ -500,12 +512,13 @@ def format_docstring(
docstring,
summary_wrap_length=0,
description_wrap_length=0,
force_wrap=False,
tab_width=1,
pre_summary_newline=False,
pre_summary_space=False,
make_summary_multi_line=False,
close_quotes_on_newline=False,
post_description_blank=False,
force_wrap=False,
strict=True,
):
"""Return formatted version of docstring.
Expand Down Expand Up @@ -542,6 +555,11 @@ def format_docstring(
# Something is probably not right with the splitting.
return docstring

# Compensate for textwrap counting each tab in indentation as 1 character.
tab_compensation = indentation.count('\t') * (tab_width - 1)
summary_wrap_length -= tab_compensation
description_wrap_length -= tab_compensation

if description:
# Compensate for triple quotes by temporarily prepending 3 spaces.
# This temporary prepending is undone below.
Expand Down Expand Up @@ -970,12 +988,13 @@ def _format_code_with_args(source, args):
source,
summary_wrap_length=args.wrap_summaries,
description_wrap_length=args.wrap_descriptions,
force_wrap=args.force_wrap,
tab_width=args.tab_width,
pre_summary_newline=args.pre_summary_newline,
pre_summary_space=args.pre_summary_space,
make_summary_multi_line=args.make_summary_multi_line,
close_quotes_on_newline=args.close_quotes_on_newline,
post_description_blank=args.post_description_blank,
force_wrap=args.force_wrap,
line_range=args.line_range,
strict=not args.non_strict,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_format_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def test_force_wrap(self):
)
)

@pytest.mark.xfail
@pytest.mark.unit
def test_format_docstring_with_summary_only_and_wrap_and_tab_indentation(
self,
):
Expand Down

0 comments on commit 3dc1a7e

Please sign in to comment.