Optional commas in pretty-printer

This commit is contained in:
Tony Garnock-Jones 2022-02-11 11:38:29 +01:00
parent 0f49b8b1b4
commit 32544c544a
2 changed files with 10 additions and 2 deletions

View File

@ -312,10 +312,16 @@ def parse_with_annotations(bs, **kwargs):
return Parser(input_buffer=bs, include_annotations=True, **kwargs).next() return Parser(input_buffer=bs, include_annotations=True, **kwargs).next()
class Formatter(TextCodec): class Formatter(TextCodec):
def __init__(self, format_embedded=lambda x: x, indent=None): def __init__(self,
format_embedded=lambda x: x,
indent=None,
with_commas=False,
trailing_comma=False):
super(Formatter, self).__init__() super(Formatter, self).__init__()
self.indent_delta = 0 if indent is None else indent self.indent_delta = 0 if indent is None else indent
self.indent_distance = 0 self.indent_distance = 0
self.with_commas = with_commas
self.trailing_comma = trailing_comma
self.chunks = [] self.chunks = []
self._format_embedded = format_embedded self._format_embedded = format_embedded
@ -362,9 +368,11 @@ class Formatter(TextCodec):
self.write_indent() self.write_indent()
appender(vs[0]) appender(vs[0])
for v in vs[1:]: for v in vs[1:]:
if self.with_commas: self.chunks.append(',')
self.write_indent_space() self.write_indent_space()
appender(v) appender(v)
self.indent_distance = self.indent_distance - self.indent_delta self.indent_distance = self.indent_distance - self.indent_delta
if self.trailing_comma: self.chunks.append(',')
self.write_indent() self.write_indent()
self.chunks.append(closer) self.chunks.append(closer)

View File

@ -5,7 +5,7 @@ except ImportError:
setup( setup(
name="preserves", name="preserves",
version="0.11.0", version="0.12.0",
author="Tony Garnock-Jones", author="Tony Garnock-Jones",
author_email="tonyg@leastfixedpoint.com", author_email="tonyg@leastfixedpoint.com",
license="Apache Software License", license="Apache Software License",