src/fmt.bend checks
raw source on the hub · import bolt@1.56.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. Three passes over a file's tokens:
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.
Indentation, the spaces inside a line, and where a line breaks are left as
they are: this is what space reports that has one right answer, and the
blank lines no rule reports.
Pure: text in, text out. src/fmt/plan.bend decides which files change and
src/fmt/run.bend writes them.
4 imports
import Base import ./lazy/lazy.bend as Lazy import ./rchars.bend as Rchars import ./syntax/lex.bend as Lex
Definitions
def is_newline source · line 24 · raw
@kk:0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.TokKind -> Bool
a newline token?
def is_space source · line 32 · raw
@kk:0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.TokKind -> Bool
a space token: a run of spaces and tabs?
def blank source · line 40 · raw
@+ch:Char -> Bool
a space or a tab?
def trim.lead source · line 44 · raw
@cs:List<&2, Char> -> List<&2, Char>
the chars after the blanks they open with
def trim.tail source · line 52 · raw
@tt:String -> String
a text without the spaces and tabs it ends in
def trim.text source · line 57 · raw
@kk:0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.TokKind -> @+tt: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 65 · raw
@toks:List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/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 73 · raw
@drop:Bool -> @+kk:0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.TokKind -> @tt:String -> @ll:U32 -> @cc:U32 -> @more:List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.Tok> -> List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.Tok>
a token kept, with its text trimmed, before the rest; or dropped
def trim source · line 81 · raw
@toks:List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.Tok> -> List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.Tok>
no blank before a line's end
def squeeze.most source · line 89 · raw
U32
how many newlines in a row are kept: two, which is one blank line
def squeeze.run source · line 94 · 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 103 · raw
@toks:List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.Tok> -> @+run:U32 -> List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/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 114 · raw
@toks:List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.Tok> -> Bool
are these all newlines?
def close.newline source · line 122 · raw
0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.Tok
the newline a file ends in
def close.go source · line 126 · raw
@toks:List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.Tok> -> List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.Tok>
the tokens through their last one that is no newline, then one newline
def close source · line 135 · raw
@toks:List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.Tok> -> List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.Tok>
one newline at the end; an empty file stays empty
def toks source · line 145 · raw
@ts:List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/src/syntax/lex.Tok> -> List<&2, 0x9442a37d1ea1cd8e94edbbcf49ca618b/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 149 · raw
@source:String -> String
a file's text, formatted