Add diff option to cmake-format

This commit is contained in:
Regen
2019-11-16 22:43:54 +09:00
parent 160f6ea40e
commit 50c4cb34a1

View File

@@ -86,6 +86,7 @@ def main(args: List[str] = None):
from pathlib import Path from pathlib import Path
from argparse import ArgumentParser from argparse import ArgumentParser
from .parser import ListParser from .parser import ListParser
from difflib import unified_diff
parser = ArgumentParser() parser = ArgumentParser()
parser.add_argument('lists', type=Path, nargs='*', help='CMake list files') parser.add_argument('lists', type=Path, nargs='*', help='CMake list files')
@@ -93,6 +94,7 @@ def main(args: List[str] = None):
'--inplace', '--inplace',
action='store_true', action='store_true',
help='inplace edit') help='inplace edit')
parser.add_argument('-d', '--diff', action='store_true', help='show diff')
args = parser.parse_args(args) args = parser.parse_args(args)
@@ -102,15 +104,20 @@ def main(args: List[str] = None):
with listpath.open() as fp: with listpath.open() as fp:
content = fp.read() content = fp.read()
tokens, remain = list_parser.parse(content) tokens, remain = list_parser.parse(content)
if remain: formatted = content if remain else formatter.format(tokens)
if args.inplace:
pass if args.inplace:
else: if not remain:
print(content, end='')
else:
formated = formatter.format(tokens)
if args.inplace:
with listpath.open('w') as fp: with listpath.open('w') as fp:
fp.write(formated) fp.write(formatted)
else:
if args.diff:
diff = unified_diff(content.splitlines(True),
formatted.splitlines(True), str(listpath),
str(listpath), '(before formatting)',
'(after formatting)')
diffstr = ''.join(diff)
if diffstr:
print(diffstr, end='')
else: else:
print(formated, end='') print(formatted, end='')