Current section
Files
Jump to
Current section
Files
src/sqlode@codegen@builder.erl
-module(sqlode@codegen@builder).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/sqlode/codegen/builder.gleam").
-export([empty/0, line/1, lines/1, blank/0, concat/1, indent/2, render/1]).
-export_type([block/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Minimal line-oriented builder for emitting generated Gleam source code.\n"
"\n"
" A `Block` is an ordered list of already-rendered lines. Callers stitch\n"
" blocks together with `concat`, add blank separators with `blank`, and\n"
" attach leading indentation with `indent`. `render` flattens a block to\n"
" the final `String` by joining on `\"\\n\"`.\n"
"\n"
" The builder intentionally does not try to model reflow (Wadler-style\n"
" `Doc`) — generated Gleam code is layout-deterministic and lists of\n"
" lines are the idiom already used across the codegen modules.\n"
).
-opaque block() :: {block, list(binary())}.
-file("src/sqlode/codegen/builder.gleam", 20).
?DOC(" A block containing no lines at all. Useful for conditional sections.\n").
-spec empty() -> block().
empty() ->
{block, []}.
-file("src/sqlode/codegen/builder.gleam", 25).
?DOC(" A block containing a single line of text.\n").
-spec line(binary()) -> block().
line(Value) ->
{block, [Value]}.
-file("src/sqlode/codegen/builder.gleam", 30).
?DOC(" A block from an explicit list of pre-split lines.\n").
-spec lines(list(binary())) -> block().
lines(Values) ->
{block, Values}.
-file("src/sqlode/codegen/builder.gleam", 35).
?DOC(" A block containing a single empty line, used as a visual separator.\n").
-spec blank() -> block().
blank() ->
{block, [<<""/utf8>>]}.
-file("src/sqlode/codegen/builder.gleam", 40).
?DOC(" Concatenate the lines of several blocks in order.\n").
-spec concat(list(block())) -> block().
concat(Parts) ->
{block, gleam@list:flat_map(Parts, fun(B) -> erlang:element(2, B) end)}.
-file("src/sqlode/codegen/builder.gleam", 46).
?DOC(
" Prepend `by` spaces to every non-empty line in the block.\n"
" Empty lines stay empty so the output never has trailing whitespace.\n"
).
-spec indent(block(), integer()) -> block().
indent(Block, By) ->
Prefix = gleam@string:repeat(<<" "/utf8>>, By),
{block, gleam@list:map(erlang:element(2, Block), fun(L) -> case L of
<<""/utf8>> ->
<<""/utf8>>;
_ ->
<<Prefix/binary, L/binary>>
end end)}.
-file("src/sqlode/codegen/builder.gleam", 59).
?DOC(" Render the block by joining its lines with newlines.\n").
-spec render(block()) -> binary().
render(Block) ->
gleam@string:join(erlang:element(2, Block), <<"\n"/utf8>>).