src/fmt.bend checks
raw source on the hub · import bolt@1.58.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, 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 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 28 · raw
@k:0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.TokKind -> Bool
a newline token?
def is_space source · line 36 · raw
@k:0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.TokKind -> Bool
a space token: a run of spaces and tabs?
def blank source · line 44 · raw
@+ch:Char -> Bool
a space or a tab?
def trim.lead source · line 48 · raw
@cs:List<&2, Char> -> List<&2, Char>
the chars after the blanks they open with
def trim.tail source · line 56 · raw
@t:String -> String
a text without the spaces and tabs it ends in
def trim.text source · line 61 · raw
@k:0x513b777299af6ffc61682bebdb9f09e1/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 69 · raw
@toks:List<&2, 0x513b777299af6ffc61682bebdb9f09e1/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 77 · raw
@drop:Bool -> @+k:0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.TokKind -> @t:String -> @l:U32 -> @c:U32 -> @more:List<&2, 0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.Tok> -> List<&2, 0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.Tok>
a token kept, with its text trimmed, before the rest; or dropped
def trim source · line 85 · raw
@toks:List<&2, 0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.Tok> -> List<&2, 0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.Tok>
no blank before a line's end
def squeeze.most source · line 93 · raw
U32
how many newlines in a row are kept: two, which is one blank line
def squeeze.run source · line 98 · 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 107 · raw
@toks:List<&2, 0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.Tok> -> @+run:U32 -> List<&2, 0x513b777299af6ffc61682bebdb9f09e1/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 118 · raw
@toks:List<&2, 0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.Tok> -> Bool
are these all newlines?
def close.newline source · line 126 · raw
0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.Tok
the newline a file ends in
def close.go source · line 130 · raw
@toks:List<&2, 0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.Tok> -> List<&2, 0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.Tok>
the tokens through their last one that is no newline, then one newline
def close.first source · line 139 · raw
@cs:List<&2, Char> -> Bool
is the first char a line break?
def close.broken source · line 147 · raw
@t:String -> Bool
does a text end in a line break?
def close.shut source · line 153 · raw
@toks:List<&2, 0x513b777299af6ffc61682bebdb9f09e1/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 164 · raw
@+toks:List<&2, 0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.Tok> -> List<&2, 0x513b777299af6ffc61682bebdb9f09e1/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 174 · raw
@ts:List<&2, 0x513b777299af6ffc61682bebdb9f09e1/src/syntax/lex.Tok> -> List<&2, 0x513b777299af6ffc61682bebdb9f09e1/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 178 · raw
@source:String -> String
a file's text, formatted