Skip to content

Commit

Permalink
refactor: build options for the Black call in a helper method [black-…
Browse files Browse the repository at this point in the history
…as-plugin]

Thanks @clintonsteiner!
  • Loading branch information
akaihola committed Oct 12, 2024
1 parent f7d3c5a commit ff047ac
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/darker/formatters/black_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ def run(self, content: TextDocument) -> TextDocument:
:return: The reformatted content
"""
contents_for_black = content.string_with_newline("\n")
if contents_for_black.strip():
dst_contents = format_str(
contents_for_black, mode=self._make_black_options()
)
else:
# The custom handling of empty and all-whitespace files was needed until
# Black 22.12.0. See https:/psf/black/pull/2484
dst_contents = "\n" if "\n" in content.string else ""
return TextDocument.from_str(
dst_contents,
encoding=content.encoding,
override_newline=content.newline,
)

def _make_black_options(self) -> Mode:
"""Create a Black ``Mode`` object from the configuration options."""
# Collect relevant Black configuration options from ``self.config`` in order to
# pass them to Black's ``format_str()``. File exclusion options aren't needed
# since at this point we already have a single file's content to work on.
Expand Down Expand Up @@ -182,19 +199,7 @@ def run(self, content: TextDocument) -> TextDocument:
mode["string_normalization"] = not self.config["skip_string_normalization"]
if "preview" in self.config:
mode["preview"] = self.config["preview"]

contents_for_black = content.string_with_newline("\n")
if contents_for_black.strip():
dst_contents = format_str(contents_for_black, mode=Mode(**mode))
else:
# The custom handling of empty and all-whitespace files was needed until
# Black 22.12.0. See https:/psf/black/pull/2484
dst_contents = "\n" if "\n" in content.string else ""
return TextDocument.from_str(
dst_contents,
encoding=content.encoding,
override_newline=content.newline,
)
return Mode(**mode)

def get_config_path(self) -> str | None:
"""Get the path of the Black configuration file."""
Expand Down

0 comments on commit ff047ac

Please sign in to comment.