Add feature to remove excessive newlines.

pull/12/head
Michał Słomkowski 9 years ago
parent 0c28d599c0
commit 82be152c34

@ -62,7 +62,10 @@ def perform_indentation(lines):
if not line.startswith("#") and line.endswith('}') and current_indent > 0: if not line.startswith("#") and line.endswith('}') and current_indent > 0:
current_indent -= 1 current_indent -= 1
if line != "":
indented_lines.append(current_indent * INDENTATION + line) indented_lines.append(current_indent * INDENTATION + line)
else:
indented_lines.append("")
if not line.startswith("#") and line.endswith('{'): if not line.startswith("#") and line.endswith('{'):
current_indent += 1 current_indent += 1
@ -76,7 +79,12 @@ def format_config_file(contents):
lines = join_opening_bracket(lines) lines = join_opening_bracket(lines)
lines = perform_indentation(lines) lines = perform_indentation(lines)
return '\n'.join(lines) + '\n' text = '\n'.join(lines)
for pattern, substitute in ((r'\n{3,}', '\n\n\n'), (r'^\n', ''), (r'\n$', '')):
text = re.sub(pattern, substitute, text, re.MULTILINE)
return text + '\n'
if __name__ == "__main__": if __name__ == "__main__":

@ -70,15 +70,23 @@ class TestFormatter(unittest.TestCase):
self.assertEqual('lorem ipsum " foo bar zip " or " dd aa " mi', self.assertEqual('lorem ipsum " foo bar zip " or " dd aa " mi',
strip_line(' lorem ipsum " foo bar zip " or \t " dd aa " mi')) strip_line(' lorem ipsum " foo bar zip " or \t " dd aa " mi'))
def test_indentation(self): def test_empty_lines_removal(self):
self._check_formatting( self._check_formatting(
" foo bar {\n" + "\n foo bar {\n" +
" lorem ipsum;\n" + " lorem ipsum;\n" +
"}", "}\n\n\n",
"foo bar {\n" + "foo bar {\n" +
" lorem ipsum;\n" + " lorem ipsum;\n" +
"}\n") "}\n")
self._check_formatting(
"\n foo bar {\n\n\n\n\n\n" +
" lorem ipsum;\n" +
"}\n\n\n",
"foo bar {\n\n\n" +
" lorem ipsum;\n" +
"}\n")
self._check_formatting( self._check_formatting(
" foo bar {\n" + " foo bar {\n" +
" lorem ipsum;\n" + " lorem ipsum;\n" +

Loading…
Cancel
Save