-
Notifications
You must be signed in to change notification settings - Fork 55
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
Darker ignores blank lines re-inserted by Black where user had only deleted lines #371
Comments
That's actually not how Darker works. It simply runs Black on the entire new version of the file, and only applies those Black reformattings which fall onto lines which were modified according to But sure enough, I can reproduce this. Debug output from Darker is: $ darker -vv --diff demo.py
[...]
DEBUG:darker.git:[/tmp/drkdbg]$ git rev-parse --is-inside-work-tree
DEBUG:darker.git:[/tmp/drkdbg]$ git diff --name-only --relative HEAD -- demo.py
DEBUG:darker.git:[/tmp/drkdbg]$ git ls-files --others --exclude-standard -- demo.py
DEBUG:darker.__main__:Read 34 lines from edited file /tmp/drkdbg/demo.py
DEBUG:darker.__main__:Black reformat resulted in 31 lines
DEBUG:darker.diff:Diff between edited and reformatted has 5 opcodes
DEBUG:darker.git:[/tmp/drkdbg]$ git show HEAD:./demo.py
DEBUG:darker.git:[/tmp/drkdbg]$ git log -1 --format=%ct HEAD -- demo.py
DEBUG:darker.diff:Diff between edited and reformatted has 7 opcodes
DEBUG:darker.chooser:Found no edits on lines 1-6
DEBUG:darker.chooser:Using 6 original lines at line 1
DEBUG:darker.chooser:Found no edits on line 7
DEBUG:darker.chooser:Using 0 original lines at line 7
DEBUG:darker.chooser:Found no edits on lines 7-27
DEBUG:darker.chooser:Using 21 original lines at line 7
DEBUG:darker.chooser:Found no edits on lines 28-31
DEBUG:darker.chooser:Using 4 original lines at line 28
DEBUG:darker.chooser:Found no edits on lines 32-34
DEBUG:darker.chooser:Using 3 original lines at line 32
DEBUG:darker.__main__:Verifying that the 34 original edited lines and 34 reformatted lines parse into an identical abstract syntax tree So the problem is that since the lines were removed, in Darker's world there are really no "changed lines" in the new version of Indeed this is an edge case Darker doesn't handle at all. If Black inserts new lines in place of lines the user has removed, Darker will miss the reformatting. The reformatting-applying algorithm needs to support this special case e.g. by
I'm not sure it's that straightforward though before looking into the details. |
Hm. What's about to expand the change code block and include all white spaces? |
Can you give a concrete example of that idea? I don't quite follow yet. |
Sorry for bad english ;) I'll try again:
I have named this |
But, you see, there is no "diff code block" that may be reformatted. If I have this file (with line numbers): import os # first line in original file
"Fourth line in original file" and I delete the empty lines: import os # first line in original file
"Fourth line in original file" Now @@ -1,2 +1,3 @@
import os # first line in original file
+
"Fourth line in original file" If I run choose_lines(
black_chunks=[
(
1,
('import os # first line in original file',),
('import os # first line in original file',)
),
(
2,
(),
('',)
),
(
2,
('"Fourth line in original file"',),
('"Fourth line in original file"',)
)
],
edit_linenums=[],
) So import os # first line in original file # <-- line 1 was not edited
"Fourth line in original file" # <-- line 2 was not edited in the newer version of the file. I hope you see that with Darker's current diff logic, it's not quite that simple to implement this change. We would somehow need to keep track not only of the line numbers in the new file which have been modified, but also "half-line-numbers", or consecutive line number pairs, between which lines have been removed. In the example above, choose_lines(
black_chunks=...,
edit_linenums=[],
deletions_between_linenums=[(1, 2)],
) So this is of course possible, but requires a bit of a larger design effort. Could you help by
I could then make sure the rest of Darker finds out the correct value for |
Oh, and what I tend to do when I get stuck with English is let deepl.com do a translation suggestion for me. It is unbelievably good. |
It seems to me that changes in newlines will not fix always.
e.g.: A wrong formatted code part:
git diff looks like:
After darker run only one fix was applied:
Running black, results in:
Black is also not perfect... Think it should look like:
black issues are full with "blank lines" stuff, see: https://github.com/psf/black/issues?q=is%3Aissue+is%3Aopen+blank+lines So maybe it's fixed sometimes...
The darker bug is that it seems not all blank line changes will be pass to black, isn't it?
The text was updated successfully, but these errors were encountered: