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

feature/black command line args #3

Merged
merged 25 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d7e1de8
Add line-length & skip-string-normalization args
CorreyL May 7, 2020
5c606cf
Add support for sending `black` args to `FileMode`
CorreyL May 7, 2020
6b2cdb6
Add supported `black` command line args to README
CorreyL May 7, 2020
df4d812
Use correct block specifier and language
CorreyL Jun 21, 2020
eea6472
Add empty line before closing triple-double-quote
CorreyL Jun 21, 2020
d31ee37
Use help string from black
CorreyL Jun 21, 2020
f5c3b16
Use help string from black
CorreyL Jun 21, 2020
e17e07a
Merge remote-tracking branch 'remotes/original-repo/master' into feat…
CorreyL Jun 21, 2020
74c6c4e
Add CorreyL to list of contributors
CorreyL Jun 21, 2020
5e84d3c
Merge branch 'feature/black-command-line-args' of github.com:CorreyL/…
CorreyL Jun 21, 2020
8b9c0c1
Merge remote-tracking branch 'master'
CorreyL Jun 25, 2020
0e822e9
Remove default values for -l and -S
CorreyL Jun 25, 2020
4422679
Override defaults & toml with cmd line args
CorreyL Jun 25, 2020
588c740
Update version # & add new cmd line arg options
CorreyL Jun 25, 2020
b6434c8
Include black `--config` in `black_args` parameter
akaihola Jun 25, 2020
8b2d205
Only get FileMode options from Black config files
akaihola Jun 25, 2020
58e05a2
Simpler merge of config file and cmdline options
akaihola Jun 25, 2020
be2ddb5
Add unit tests for configfile/cmdline processing
akaihola Jun 25, 2020
8156c79
Typing fixes to make Mypy happy
akaihola Jun 25, 2020
175ae0e
Update CHANGES.rst
CorreyL Jun 25, 2020
9eecb66
Improve suggested typing of black_args parameter
CorreyL Jun 25, 2020
292c71d
Merge branch 'feature/black-command-line-args' into feature/unify-com…
CorreyL Jun 25, 2020
4a2567d
Merge pull request #1 from akaihola/feature/unify-command-line-proces…
CorreyL Jun 25, 2020
15cb524
Update -S and -l help text in README
CorreyL Jun 25, 2020
5833bf2
Add -c/--config to README black cmd line options
CorreyL Jun 25, 2020
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
4 changes: 2 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
0.3.0.dev / 2020-06-22
0.3.0 / 2020-06-22
CorreyL marked this conversation as resolved.
Show resolved Hide resolved
----------------------

- Feature: Add support for black config

- Feature: Add support for ``-l``/``--line-length`` and ``-S``/``--skip-string-normalization``

0.2.0 / 2020-03-11
------------------
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

- Alexander Tishin (@Mystic-Mirage)
- Antti Kaihola (@akaihola)
- Correy Lim (@CorreyL)
14 changes: 14 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ Example:

if False: print('there')

Black Command Line Arguments
============================

``black`` `command line arguments`_ that are currently supported:

.. code-block:: shell

-S, --skip-string-normalization
Sets the `--skip-string-normalization` flag for
`black`
-l LINE_LENGTH, --line-length LINE_LENGTH
Sets the `--line-length` argument for `black`
CorreyL marked this conversation as resolved.
Show resolved Hide resolved

.. _command line arguments: https://black.readthedocs.io/en/stable/installation_and_usage.html#command-line-options

Editor integration
==================
Expand Down
13 changes: 10 additions & 3 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


def format_edited_parts(
srcs: Iterable[Path], isort: bool, config: Optional[str]
srcs: Iterable[Path], isort: bool, black_args: dict, config: Optional[str]
CorreyL marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
"""Black (and optional isort) formatting for chunks with edits since the last commit

Expand All @@ -42,6 +42,7 @@ def format_edited_parts(

:param srcs: Directories and files to re-format
:param isort: ``True`` to also run ``isort`` first on each changed file
:param black_args: Command-line arguments to send to ``black.FileMode``
:param config: Path to the black configuration file (optional)

"""
Expand All @@ -68,7 +69,7 @@ def format_edited_parts(
continue

# 4. run black
edited, formatted = run_black(src, config)
edited, formatted = run_black(src, black_args, config)
logger.debug("Read %s lines from edited file %s", len(edited), src)
logger.debug("Black reformat resulted in %s lines", len(formatted))

Expand Down Expand Up @@ -136,8 +137,14 @@ def main(argv: List[str] = None) -> None:
logger.error(f"{ISORT_INSTRUCTION} to use the `--isort` option.")
exit(1)

black_args = {}
if args.line_length:
black_args["line_length"] = args.line_length
if args.skip_string_normalization:
black_args["skip_string_normalization"] = args.skip_string_normalization

paths = {Path(p) for p in args.src}
format_edited_parts(paths, args.isort, args.config)
format_edited_parts(paths, args.isort, black_args, args.config)


if __name__ == "__main__":
Expand Down
21 changes: 19 additions & 2 deletions src/darker/black_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,31 @@ def read_black_config(src: Path, value: Optional[str]) -> Dict[str, Any]:
return context.default_map or {}


def run_black(src: Path, config: Optional[str]) -> Tuple[List[str], List[str]]:
def run_black(src: Path, black_args: dict, config: Optional[str]) -> Tuple[List[str], List[str]]:
CorreyL marked this conversation as resolved.
Show resolved Hide resolved
"""Run the black formatter for the contents of the given Python file

Return lines of the original file as well as the formatted content.

:param black_args: Command-line arguments to send to ``black.FileMode``
CorreyL marked this conversation as resolved.
Show resolved Hide resolved

"""
defaults = read_black_config(src, config)
mode = FileMode(**defaults)

command_line_args = {}
if "line_length" in black_args:
command_line_args["line_length"] = black_args["line_length"]
if "skip_string_normalization" in black_args:
# The ``black`` command line argument is
# ``--skip-string-normalization``, but the parameter for
# ``black.FileMode`` needs to be the opposite boolean of
# ``skip-string-normalization``, hence the inverse boolean
command_line_args["string_normalization"] = not black_args[
"skip_string_normalization"
]

# Override defaults and pyproject.toml settings if they've been specified
# from the command line arguments
mode = FileMode(**{**defaults, **command_line_args})

src_contents = src.read_text()
dst_contents = format_str(src_contents, mode=mode)
Expand Down
14 changes: 14 additions & 0 deletions src/darker/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ def parse_command_line(argv: List[str]) -> Namespace:
parser.add_argument(
"--version", action="store_true", help="Show the version of `darker`"
)
parser.add_argument(
"-S",
"--skip-string-normalization",
action="store_true",
dest="skip_string_normalization",
help="Don't normalize string quotes or prefixes",
)
parser.add_argument(
"-l",
"--line-length",
type=int,
dest="line_length",
help="How many characters per line to allow [default: 88]",
)
return parser.parse_args(argv)