@@ -927,18 +927,51 @@ See issue `#382`_ and PR `#430`_ for more information.
927
927
How does it work?
928
928
=================
929
929
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:
935
932
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.
942
975
943
976
944
977
License
@@ -983,6 +1016,7 @@ __ https://github.com/asottile/pyupgrade
983
1016
__ https://github.com/google/yapf/blob/main/yapf/third_party/yapf_diff/yapf_diff.py
984
1017
.. _yapf : https://github.com/google/yapf
985
1018
.. _#51 : https://github.com/akaihola/darker/pull/51
1019
+ .. _Graylint : https://github.com/akaihola/graylint
986
1020
987
1021
988
1022
Contributors ✨
0 commit comments