Hack for metadata in the document

This commit is contained in:
John Doty 2024-09-13 16:53:51 -07:00
parent 265f07fd5a
commit 709ba060b4
2 changed files with 23 additions and 2 deletions

View file

@ -506,7 +506,8 @@ class Harness:
lines.extend(node.format_lines(self.source))
def format_document(self, lines: list[str], doc: wadler.Document, indent: int = 0):
def append(x: str):
def append(x: str, i: int = 0):
i += indent
lines.append((" " * indent) + x)
match doc:
@ -540,6 +541,14 @@ class Harness:
self.format_document(lines, doc.left, indent)
self.format_document(lines, doc.right, indent)
case wadler.Marker():
append("Marker")
append("metadata", 1)
for k, v in doc.meta.items():
append(f"{k}={v}", 2)
append("child", 1)
self.format_document(lines, doc.child, indent + 2)
case None:
pass

View file

@ -54,6 +54,12 @@ class Group:
child: "Document"
@dataclasses.dataclass(frozen=True)
class Marker:
child: "Document"
meta: dict
@dataclasses.dataclass
class Lazy:
value: typing.Callable[[], "Document"] | "Document"
@ -68,7 +74,7 @@ class Lazy:
return Lazy(lambda: printer.convert_tree_to_document(tree))
Document = None | Text | Literal | NewLine | ForceBreak | Cons | Indent | Group | Lazy
Document = None | Text | Literal | NewLine | ForceBreak | Cons | Indent | Group | Marker | Lazy
class DocumentLayout:
@ -162,6 +168,9 @@ def layout_document(doc: Document, width: int) -> DocumentLayout:
# well.)
stack.append(chunk.with_document(child))
case Marker():
stack.append(chunk.with_document(chunk.doc.child))
case _:
typing.assert_never(chunk.doc)
@ -216,6 +225,9 @@ def layout_document(doc: Document, width: int) -> DocumentLayout:
else:
chunks.append(Chunk(doc=child, indent=chunk.indent, flat=False))
case Marker():
chunks.append(chunk.with_document(chunk.doc.child))
case _:
typing.assert_never(chunk)