-
Notifications
You must be signed in to change notification settings - Fork 54
Description
Description / Summary
A nice-to-have feature would be for mdformat to show the changes it would make to the file by writing the output to stdout rather than to the original file. This is a feature that black has, using the --diff flag on the command line.
Value / benefit
At present, the only way to find out the changes that mdformat will make (or has made) is to create a copy of the file to be formatted, run mdformat on the copy and compare the two files. Or, if the file is under version control, to compare to the previous version (e.g. by git diff). The extra effort is a bit of a pest, but this feature is really just a nice-to-have.
Implementation details
Black's implementation is in src/black/output.py#L55-L73:
def diff(a: str, b: str, a_name: str, b_name: str) -> str:
"""Return a unified diff string between strings `a` and `b`."""
import difflib
a_lines = [line for line in a.splitlines(keepends=True)]
b_lines = [line for line in b.splitlines(keepends=True)]
diff_lines = []
for line in difflib.unified_diff(
a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5
):
# Work around https://bugs.python.org/issue2142
# See:
# https://www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html
if line[-1] == "\n":
diff_lines.append(line)
else:
diff_lines.append(line + "\n")
diff_lines.append("\\ No newline at end of file\n")
return "".join(diff_lines)There's a short color_diff() function directly after, if anyone wants to be fancy.
Tasks to complete
No response