process-text-files.py mk-indent: Add support for left-pad-match

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2019-03-17 17:25:17 +01:00
commit 109179b55e

View file

@ -40,6 +40,7 @@ class Cmd(jwutils.Cmd):
require_lhs_pattern = None
skip_short = None
min_assignments = None
left_pad_match = 1
if context is not None:
if 'indent' in context:
indent = context['indent']
@ -47,6 +48,8 @@ class Cmd(jwutils.Cmd):
pattern = context['pattern']
if 'right-align-match' in context:
right_align_match = context['right-align-match']
if 'left-pad-match' in context:
left_pad_match = context['left-pad-match']
if 'skip-lhs-pattern' in context:
skip_lhs_pattern = context['skip-lhs-pattern']
if 'require-lhs-pattern' in context:
@ -73,11 +76,25 @@ class Cmd(jwutils.Cmd):
#slog(NOTICE, "split into", parts)
if right_align_match > 0:
parts[1] = parts[1].rjust(right_align_match)
lhs = parts[0].rstrip().ljust(indent)
if len(parts) > 2:
p2_stripped = parts[2].strip()
if len(p2_stripped) or len(parts) > 3:
parts[2] = ' ' + p2_stripped
r += parts[0].rstrip().ljust(indent) + ''.join(parts[1:]) + '\n'
if left_pad_match > 0:
pad = ''
#slog(NOTICE, "working off {} >{}<".format(left_pad_match, lhs[-left_pad_match:]))
for c in lhs[-left_pad_match:]:
if c != ' ':
pad += ' '
for c in parts[1]:
if c != ' ':
break
if len(pad):
pad = pad[:-1]
#slog(NOTICE, " pad for {}=>{}<".format(line, pad))
lhs += pad
r += lhs + ''.join(parts[1:]) + '\n'
assignments += 1
if min_assignments is not None and assignments < min_assignments:
return data
@ -398,9 +415,11 @@ class CmdMkIndentEquals(Cmd):
slog(NOTICE, "Beautifying", len(files), "makefiles:")
context = dict()
right_align_match = 2
left_pad_match = 1
context["indent"] = args.equal_pos - right_align_match
context["pattern"] = "([?+:]*=|::=)"
context["right-align-match"] = right_align_match
context["left-pad-match"] = left_pad_match
context["skip-lhs-pattern"] = "[^A-Za-z0-9_# ]"
context["require-lhs-pattern"] = "^[ #]*[A-Za-z0-9_]"
context["skip-short"] = args.skip_short