Repository navigation
Conversation
Previously, cljfmt failed to print a diff when the only formatting change was appending a trailing newline. This occurred because `clojure.string/split` discarded trailing empty strings, causing DiffUtils to see identical string vectors. This commit introduces a step that detects missing newlines and appends a hidden marker to the last line. This makes DiffUtils recognize the modification. A subsequent pass then finds this marker and translates it into the standard GNU diff "\ No newline at end of file" line.
|
Thanks for writing up the issue and the PR. My understanding is that if we have a file --- a/a.txt
+++ b/b.txt
@@ -1 +1 @@
-foo
\ No newline at end of file
+fooAs this is the output of (defn- lines [s] (str/split s #"\n" -1))It produces the output: --- a/foo.txt
+++ b/foo.txt
@@ -1,1 +1,2 @@
foo
+Which may be less clear at a glance than the output produced by git or diff. However, instead of trying to work around this issue in cljfmt, my suggestion is that we instead swap from DiffUtils to borkdude's text-diff library (which is already used for the Babashka variant of cljfmt), and then add this as an option to that library. What do you think, @borkdude ? Would you be amenable to a PR that adds an option to |
|
Sure! |
Previously,
cljfmt checkcommand failed to print a diff when the only formatting change was appending a trailing newline (such as when:normalize-newlines-at-file-end?is set totrue).This occurred because
clojure.string/splitdiscarded trailing empty strings, causing DiffUtils to see identical string vectors.Here is the code demonstrating this effect of the
clojure.string/split(evaled in thecljfmt.diffns):This commit introduces a step that detects missing newlines and appends a hidden marker to the last line of the file which does not contain a newline, so the that line is shown as modified in the diff. The new logic then also adds an empty line to the file which did not contain a newline, so that the green diff shows that a newline is added.
The hidden marker is translated into the standard GNU diff "\ No newline at end of file" line. To closer imitate the diff output of the
git diffcommand, and the platforms like gitlab (perhaps github as well), with only exception of actually showing the added newline in the diff in green.Git:
Gitlab:
Now the diff output of the
cljfmt checkcommand for appended newlines looks like this:cljfmt checkcommand with other formatting rules specified in the cljfmt config file while various code files contained various detected formatting issues, and the diff output related to those issues appears to be unaffected by this change.lein test-allwithin the/cljfmt/cljfmtdirectory produces "0 failures, 0 errors." result.