Compare commits

...

2 commits

Author SHA1 Message Date
71b59302fa Mess with wrapping and whatnot 2024-09-14 07:28:24 -07:00
f3a4c4348a Custom indentation 2024-09-14 07:28:18 -07:00
3 changed files with 34 additions and 13 deletions

View file

@ -25,6 +25,8 @@ class FineGrammar(Grammar):
trivia = ["BLANKS", "COMMENT"] trivia = ["BLANKS", "COMMENT"]
pretty_indent = " "
def __init__(self): def __init__(self):
super().__init__( super().__init__(
precedence=[ precedence=[
@ -134,14 +136,23 @@ class FineGrammar(Grammar):
@rule("FunctionDecl") @rule("FunctionDecl")
def function_declaration(self) -> Rule: def function_declaration(self) -> Rule:
return seq( return seq(
group(
group(
group( group(
self.FUN, self.FUN,
sp, sp,
mark(self.IDENTIFIER, field="name", highlight=highlight.entity.name.function), mark(
sp, self.IDENTIFIER,
group( field="name",
highlight=highlight.entity.name.function,
),
),
nl,
mark(self.function_parameters, field="parameters"), mark(self.function_parameters, field="parameters"),
mark(opt(sp, group(self.ARROW, sp, self.type_expression)), field="return_type"), ),
mark(
opt(indent(sp, group(self.ARROW, sp, self.type_expression))),
field="return_type",
), ),
), ),
sp, sp,

View file

@ -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()
) )

View file

@ -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)