Formatter: Default to printing diff & add --fix option

This commit is contained in:
gregorni 2023-08-24 14:01:08 +02:00
parent d9fac2897f
commit 35e96964da
2 changed files with 29 additions and 12 deletions

View file

@ -135,6 +135,4 @@ class Format:
last_not_whitespace = item last_not_whitespace = item
print(tokenized_str) # TODO: Remove this when the MR is ready to be merged
return tokenized_str return tokenized_str

View file

@ -19,6 +19,7 @@
import argparse import argparse
import difflib
import os import os
import sys import sys
import typing as T import typing as T
@ -70,8 +71,9 @@ class BlueprintApp:
"format", "Format given blueprint files", self.cmd_format "format", "Format given blueprint files", self.cmd_format
) )
format.add_argument( format.add_argument(
"--check", "-f",
help="don't write to the files, just return whether they would be formatted", "--fix",
help="Format the files in place, don't output the diff",
action="store_true", action="store_true",
) )
format.add_argument( format.add_argument(
@ -196,33 +198,50 @@ class BlueprintApp:
if data != formatted_str: if data != formatted_str:
happened = "Would reformat" happened = "Would reformat"
if not opts.check: if opts.fix:
file.seek(0) file.seek(0)
file.truncate() file.truncate()
file.write(formatted_str) file.write(formatted_str)
happened = "Reformatted" happened = "Reformatted"
print(f"{Colors.BOLD}{happened} {file.name}{Colors.CLEAR}") a_lines = data.splitlines(keepends=True)
b_lines = formatted_str.splitlines(keepends=True)
diff_lines = []
for line in difflib.unified_diff(
a_lines, b_lines, fromfile=file.name, tofile=file.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")
print(
f"\n{Colors.BOLD}{happened} {file.name}{Colors.CLEAR}:\n\n"+ "".join(diff_lines)
)
formatted_files += 1 formatted_files += 1
except PrintableError as e: except PrintableError as e:
e.pretty_print(file.name, data, stream=sys.stderr) e.pretty_print(file.name, data, stream=sys.stderr)
sys.exit(1) sys.exit(1)
print("\n") # This actually prints two newlines
left_files = len(input_files) - formatted_files left_files = len(input_files) - formatted_files
if formatted_files == 0: if formatted_files == 0:
print(f"{Colors.GREEN}{Colors.BOLD}Nothing to do.{Colors.CLEAR}") print(f"{Colors.GREEN}{Colors.BOLD}Nothing to do.{Colors.CLEAR}")
elif opts.check: elif opts.fix:
print(
f"{Colors.RED}{Colors.BOLD}Reformatted {formatted_files} files, {left_files} were left unchanged.{Colors.CLEAR}"
)
else:
print( print(
f"{Colors.RED}{Colors.BOLD}{formatted_files} files would be reformatted, {left_files} would be left unchanged.{Colors.CLEAR}" f"{Colors.RED}{Colors.BOLD}{formatted_files} files would be reformatted, {left_files} would be left unchanged.{Colors.CLEAR}"
) )
sys.exit(1) sys.exit(1)
else:
print(
f"{Colors.RED}{Colors.BOLD}Reformatted {formatted_files} files, {left_files} were left unchanged.{Colors.CLEAR}"
)
def cmd_lsp(self, opts): def cmd_lsp(self, opts):
langserv = LanguageServer() langserv = LanguageServer()