Custom indentation
This commit is contained in:
parent
a1fa03615c
commit
f3a4c4348a
2 changed files with 18 additions and 8 deletions
|
|
@ -580,7 +580,9 @@ class Harness:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
return (
|
return (
|
||||||
wadler.layout_document(self.document, width).apply_to_source(self.source).splitlines()
|
wadler.layout_document(self.document, width, self.load_printer().indent())
|
||||||
|
.apply_to_source(self.source)
|
||||||
|
.splitlines()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,7 @@ class DocumentLayout:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def layout_document(doc: Document, width: int) -> DocumentLayout:
|
def layout_document(doc: Document, width: int, indent: str) -> DocumentLayout:
|
||||||
"""Lay out a document to fit within the given width.
|
"""Lay out a document to fit within the given width.
|
||||||
|
|
||||||
The result of this function is a DocumentLayout which can trivially be
|
The result of this function is a DocumentLayout which can trivially be
|
||||||
|
|
@ -213,13 +213,13 @@ def layout_document(doc: Document, width: int) -> DocumentLayout:
|
||||||
column += len(replace)
|
column += len(replace)
|
||||||
else:
|
else:
|
||||||
# TODO: Custom newline expansion, custom indent segments.
|
# TODO: Custom newline expansion, custom indent segments.
|
||||||
output.append("\n" + (chunk.indent * " "))
|
output.append("\n" + (chunk.indent * indent))
|
||||||
column = chunk.indent
|
column = chunk.indent * len(indent)
|
||||||
|
|
||||||
case ForceBreak():
|
case ForceBreak():
|
||||||
# TODO: Custom newline expansion, custom indent segments.
|
# TODO: Custom newline expansion, custom indent segments.
|
||||||
output.append("\n" + (chunk.indent * " "))
|
output.append("\n" + (chunk.indent * indent))
|
||||||
column = chunk.indent
|
column = chunk.indent * len(indent)
|
||||||
|
|
||||||
case Cons(left, right):
|
case Cons(left, right):
|
||||||
chunks.append(chunk.with_document(right))
|
chunks.append(chunk.with_document(right))
|
||||||
|
|
@ -363,12 +363,20 @@ class Printer:
|
||||||
grammar: parser.Grammar
|
grammar: parser.Grammar
|
||||||
_matchers: dict[str, Matcher]
|
_matchers: dict[str, Matcher]
|
||||||
_nonterminals: dict[str, parser.NonTerminal]
|
_nonterminals: dict[str, parser.NonTerminal]
|
||||||
|
_indent: str
|
||||||
|
|
||||||
def __init__(self, grammar: parser.Grammar):
|
def __init__(self, grammar: parser.Grammar, indent: str | None = None):
|
||||||
self.grammar = grammar
|
self.grammar = grammar
|
||||||
self._nonterminals = {nt.name: nt for nt in grammar.non_terminals()}
|
self._nonterminals = {nt.name: nt for nt in grammar.non_terminals()}
|
||||||
self._matchers = {}
|
self._matchers = {}
|
||||||
|
|
||||||
|
if indent is None:
|
||||||
|
indent = getattr(self.grammar, "pretty_indent", " ")
|
||||||
|
self._indent = indent
|
||||||
|
|
||||||
|
def indent(self) -> str:
|
||||||
|
return self._indent
|
||||||
|
|
||||||
def lookup_nonterminal(self, name: str) -> parser.NonTerminal:
|
def lookup_nonterminal(self, name: str) -> parser.NonTerminal:
|
||||||
return self._nonterminals[name]
|
return self._nonterminals[name]
|
||||||
|
|
||||||
|
|
@ -552,4 +560,4 @@ class Printer:
|
||||||
|
|
||||||
def format_tree(self, tree: runtime.Tree, width: int) -> DocumentLayout:
|
def format_tree(self, tree: runtime.Tree, width: int) -> DocumentLayout:
|
||||||
doc = self.convert_tree_to_document(tree)
|
doc = self.convert_tree_to_document(tree)
|
||||||
return layout_document(doc, width)
|
return layout_document(doc, width, self._indent)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue