src/fmt.bend checks
raw source on the hub · import bolt@1.60.0.0/src/fmt.bend as Fmt
src/fmt: bolt's formatter (bolt fmt). It changes blank space and nothing
else: every token that is code is kept as it is, in order, and so is each
comment but for the blanks it ends in. Five passes over a file's tokens:
spaces a comma has a space after it, and a run of spaces between two
tokens of a line is one space (src/fmt/line.bend);
heads a def header that breaks across lines, or is too wide, takes
the shape wrap asks for (src/fmt/head.bend). It comes after
spaces, so it measures a header at the width that pass left;
trim no blank before a line's end: a space token right before a
newline, or before the end of the file, is dropped, and a
comment loses the spaces and tabs it ends in;
squeeze no two blank lines in a row, and none at the top: once two
newlines have run, further ones are dropped, and the file opens
as if two already had;
close one newline at the end: the newlines the tokens end in become
one, and a file that ends without one gains it. An empty file
stays empty, and a file that ends inside a string that never
closes is left as it ends: the string holds its last line
break, so there is nothing to add after it.
Indentation, the spaces before a comment, and where a line that is no def
header breaks are left as they are. So is whether two tokens of a line
have a space between them, a comma and the token after it aside.
Pure: text in, text out. src/fmt/plan.bend decides which files change and
src/fmt/run.bend writes them.
6 imports
import Base import ./lazy/lazy.bend as Lazy import ./rchars.bend as Rchars import ./syntax/lex.bend as Lex import ./fmt/head.bend as Head import ./fmt/line.bend as Line
Definitions
def is_newline source · line 33 · raw
@k:0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.TokKind -> Bool
a newline token?
def is_space source · line 41 · raw
@k:0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.TokKind -> Bool
a space token: a run of spaces and tabs?
def blank source · line 49 · raw
@+ch:Char -> Bool
a space or a tab?
def trim.lead source · line 53 · raw
@cs:List<&2, Char> -> List<&2, Char>
the chars after the blanks they open with
def trim.tail source · line 61 · raw
@t:String -> String
a text without the spaces and tabs it ends in
def trim.text source · line 66 · raw
@k:0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.TokKind -> @+t:String -> String
a token's text as trim leaves it: a comment's without the blanks it ends in, any other as it is
def trim.ends source · line 74 · raw
@toks:List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok> -> Bool
does a line end here: at a newline, or at the end of the tokens?
def trim.keep source · line 82 · raw
@drop:Bool -> @+k:0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.TokKind -> @t:String -> @l:U32 -> @c:U32 -> @more:List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok> -> List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok>
a token kept, with its text trimmed, before the rest; or dropped
def trim source · line 90 · raw
@toks:List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok> -> List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok>
no blank before a line's end
def squeeze.most source · line 98 · raw
U32
how many newlines in a row are kept: two, which is one blank line
def squeeze.run source · line 103 · raw
@nl:Bool -> @run:U32 -> U32
the run of newlines after a token: one longer after a newline, none after anything else
def squeeze source · line 112 · raw
@toks:List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok> -> @+run:U32 -> List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok>
no two blank lines in a row. run: how many newlines the tokens so far end in
def close.only source · line 123 · raw
@toks:List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok> -> Bool
are these all newlines?
def close.newline source · line 131 · raw
0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok
the newline a file ends in
def close.go source · line 135 · raw
@toks:List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok> -> List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok>
the tokens through their last one that is no newline, then one newline
def close.first source · line 144 · raw
@cs:List<&2, Char> -> Bool
is the first char a line break?
def close.broken source · line 152 · raw
@t:String -> Bool
does a text end in a line break?
def close.shut source · line 158 · raw
@toks:List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok> -> Bool
do the tokens end in one that is no newline, yet whose text ends in a line break? A string that never closes does: it runs through every line break to the end of the file
def close source · line 169 · raw
@+toks:List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok> -> List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok>
one newline at the end; an empty file stays empty, and so does one that ends inside a string
def toks source · line 179 · raw
@ts:List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok> -> List<&2, 0x10abb46b8df0409ba5fe4777735d49bf/src/syntax/lex.Tok>
a file's tokens, formatted. The line and column of a token after the first change are the ones it was read at, not where it now sits: only the texts are read again
def text source · line 183 · raw
@source:String -> String
a file's text, formatted