Skip to content

Commit 0dc70b3

Browse files
authored
Merge pull request #566 from akaihola/readme-limitations
Update "How does it work?" and explain limitations in README
2 parents dc059ae + 0c41d4b commit 0dc70b3

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

Diff for: README.rst

+45-11
Original file line numberDiff line numberDiff line change
@@ -927,18 +927,51 @@ See issue `#382`_ and PR `#430`_ for more information.
927927
How does it work?
928928
=================
929929

930-
Darker takes a ``git diff`` of your Python files,
931-
records which lines of current files have been edited or added since the last commit.
932-
It then runs Black_ and notes which chunks of lines were reformatted.
933-
Finally, only those reformatted chunks on which edited lines fall (even partially)
934-
are applied to the edited file.
930+
To apply Black reformatting and to modernize format strings on changed lines,
931+
Darker does the following:
935932

936-
Also, in case the ``--isort`` option was specified,
937-
isort_ is run on each edited file before applying Black_.
938-
Similarly, each linter requested using the `--lint <command>` option is run,
939-
and only those linting messages are displayed which appeared after the modifications to
940-
the source code files,
941-
or which were there already before but now fall on modified lines.
933+
- take a ``git diff`` of Python files between ``REV1`` and ``REV2`` as specified using
934+
the ``--revision=REV1..REV2`` option
935+
- record current line numbers of lines edited or added between those revisions
936+
- run flynt_ on edited and added files (if Flynt is enabled by the user)
937+
- run Black_ on edited and added files
938+
- compare before and after reformat, noting each continuous chunk of reformatted lines
939+
- discard reformatted chunks on which no edited/added line falls on
940+
- keep reformatted chunks on which some edited/added lines fall on
941+
942+
To sort imports when the ``--isort`` option was specified, Darker proceeds like this:
943+
944+
- run isort_ on each edited and added file before applying Black_
945+
- only if any of the edited or added lines falls between the first and last line
946+
modified by isort_, are those modifications kept
947+
- if all lines between the first and last line modified by isort_ were unchanged between
948+
the revisions, discard import sorting modifications for that file
949+
950+
For details on how linting support works, see Graylint_ documentation.
951+
952+
953+
Limitations and work-arounds
954+
=============================
955+
956+
Black doesn't support partial formatting natively.
957+
Because of this, Darker lets Black reformat complete files.
958+
Darker then accepts or rejects chunks of contiguous lines touched by Black,
959+
depending on whether any of the lines in a chunk were edited or added
960+
between the two revisions.
961+
962+
Due to the nature of this algorithm,
963+
Darker is often unable to minimize the number of changes made by Black
964+
as carefully as a developer could do by hand.
965+
Also, depending on what kind of changes were made to the code,
966+
diff results may lead to Darker applying reformatting in an invalid way.
967+
Fortunately, Darker always checks that the result of reformatting
968+
converts to the same AST as the original code.
969+
If that's not the case, Darker expands the chunk until it finds a valid reformatting.
970+
As a result, a much larger block of code may be reformatted than necessary.
971+
972+
The most reasonable work-around to these limitations
973+
is to review the changes made by Darker before committing them to the repository
974+
and unstaging any changes that are not desired.
942975

943976

944977
License
@@ -983,6 +1016,7 @@ __ https://github.com/asottile/pyupgrade
9831016
__ https://github.com/google/yapf/blob/main/yapf/third_party/yapf_diff/yapf_diff.py
9841017
.. _yapf: https://github.com/google/yapf
9851018
.. _#51: https://github.com/akaihola/darker/pull/51
1019+
.. _Graylint: https://github.com/akaihola/graylint
9861020

9871021

9881022
Contributors ✨

0 commit comments

Comments
 (0)