Merge branch 'diff'

This commit is contained in:
Regen
2019-11-16 22:47:05 +09:00
2 changed files with 22 additions and 10 deletions

View File

@@ -10,6 +10,11 @@ Alpha Stage, work in progress.
- [x] Documentation for commands and variables on hover
- [x] Formatting
## Commands
- cmake-language-server: LSP server
- cmake-format: CLI frontend for formatting
## Installation
### Clients

View File

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