src/fmt.bend checks
raw source on the hub · import bolt@1.57.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. Four passes over a file's tokens:
heads a def header that breaks across lines, or is too wide, takes
the shape wrap asks for (src/fmt/head.bend);
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 that is no def
header breaks are left as they are.
Pure: text in, text out. src/fmt/plan.bend decides which files change and
src/fmt/run.bend writes them.
5 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
Definitions
def is_newline source · line 26 · raw
@k:0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.TokKind -> Bool
a newline token?
def is_space source · line 34 · raw
@k:0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.TokKind -> Bool
a space token: a run of spaces and tabs?
def blank source · line 42 · raw
@+ch:Char -> Bool
a space or a tab?
def trim.lead source · line 46 · raw
@cs:List<&2, Char> -> List<&2, Char>
the chars after the blanks they open with
def trim.tail source · line 54 · raw
@t:String -> String
a text without the spaces and tabs it ends in
def trim.text source · line 59 · raw
@k:0xf70fc6a217b89edfba89174d2aa39455/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 67 · raw
@toks:List<&2, 0xf70fc6a217b89edfba89174d2aa39455/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 75 · raw
@drop:Bool -> @+k:0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.TokKind -> @t:String -> @l:U32 -> @c:U32 -> @more:List<&2, 0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.Tok> -> List<&2, 0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.Tok>
a token kept, with its text trimmed, before the rest; or dropped
def trim source · line 83 · raw
@toks:List<&2, 0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.Tok> -> List<&2, 0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.Tok>
no blank before a line's end
def squeeze.most source · line 91 · raw
U32
how many newlines in a row are kept: two, which is one blank line
def squeeze.run source · line 96 · 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 105 · raw
@toks:List<&2, 0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.Tok> -> @+run:U32 -> List<&2, 0xf70fc6a217b89edfba89174d2aa39455/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 116 · raw
@toks:List<&2, 0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.Tok> -> Bool
are these all newlines?
def close.newline source · line 124 · raw
0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.Tok
the newline a file ends in
def close.go source · line 128 · raw
@toks:List<&2, 0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.Tok> -> List<&2, 0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.Tok>
the tokens through their last one that is no newline, then one newline
def close source · line 137 · raw
@toks:List<&2, 0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.Tok> -> List<&2, 0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.Tok>
one newline at the end; an empty file stays empty
def toks source · line 147 · raw
@ts:List<&2, 0xf70fc6a217b89edfba89174d2aa39455/src/syntax/lex.Tok> -> List<&2, 0xf70fc6a217b89edfba89174d2aa39455/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 151 · raw
@source:String -> String
a file's text, formatted