public final class CodeBuilder extends Object
It can also be used to track the origins of some of the
regions of the generated code, typically in order to keep
track of pieces of code that are copied verbatim
from some other source file into the code builder
(see withTracker(String, int)
).
Modifier and Type | Field and Description |
---|---|
static int |
PER_LEVEL
Amount of indentation per level
|
Constructor and Description |
---|
CodeBuilder(int level)
Returns a fresh code builder starting with
the given indentation level, and using a
fresh instance of
StringBuilder as
the internal target of emitted code |
Modifier and Type | Method and Description |
---|---|
CodeBuilder |
closeBlock()
Closes a curly-braced block with decremented
indentation, i.e. is equivalent to:
decrIndent();
newline();
emit("}");
newline();
|
CodeBuilder |
closeBlock0()
Closes a curly-braced block with decremented
indentation and without breaking the line after
the closing brace, i.e. is equivalent to:
decrIndent();
newline();
emit("}");
It is useful when closing several blocks in a row
to avoid empty lines.
|
String |
contents()
Returns the current contents of this code builder buffer
|
CodeBuilder |
decrIndent()
Decrements the indentation level by 1
(and the indentation amount by
PER_LEVEL |
CodeBuilder |
emit(char c)
Appends the given character, which should not
be a newline characters unless indentation
is not required
|
CodeBuilder |
emit(CharSequence code)
Appends the given characters, which should not
contain newline characters unless indentation
of the given piece of code is not required
|
CodeBuilder |
emitIf(boolean cond,
String code)
Appends the given code, which should not
contain newline characters, provided the condition
cond holds |
CodeBuilder |
emitln(String line)
Appends the given line of code and emits a terminal
newline() |
CodeBuilder |
emitlnIf(boolean cond,
String line)
Appends the given line of code and emits a terminal
newline() , provided the condition
cond holds |
CodeBuilder |
emitTracked(CExtent extent)
Convenient helper which emits the content of some
verbatim piece of code described by the given extent,
and maps it to the extent's original position.
|
CodeBuilder |
emitTrackedIf(boolean cond,
CExtent extent)
Same as
emitTracked(CExtent) but is only
performed if the given condition holds. |
CodeBuilder |
endTrackedRange(@Nullable CExtent extent)
Closes the currently tracked region and maps it to
the source position given in the corresponding call
to
startTrackedRange(LexBuffer.Position) . |
int |
getCurrentLevel() |
SourceMapping |
getSourceMapping() |
CodeBuilder |
incrIndent()
Increments the indentation level by 1
(and the indentation amount by
PER_LEVEL ) |
CodeBuilder |
newline()
Starts a new line with the current indentation amount
|
CodeBuilder |
openBlock()
Opens a new curly-braced block with incremented
indentation, i.e. is equivalent to:
emit(" {");
incrIndent();
newline();
|
void |
print(Appendable appendable)
Prints all code emitted thus far in this
code builder into the provided
appendable |
CodeBuilder |
startTrackedRange(LexBuffer.Position pos)
Starts tracking a new region from the current position
in the emitted code.
|
void |
withTracker(String generated,
int offset)
Configures this
CodeBuilder to be ready
to start tracking some regions of the emitted code. |
public static final int PER_LEVEL
public CodeBuilder(int level)
StringBuilder
as
the internal target of emitted codelevel
- public int getCurrentLevel()
public CodeBuilder incrIndent()
PER_LEVEL
)public CodeBuilder newline()
public CodeBuilder decrIndent()
PER_LEVEL
public CodeBuilder emit(CharSequence code)
code
- public CodeBuilder emit(char c)
c
- public CodeBuilder emitln(String line)
newline()
line
- should not contain newline characterspublic CodeBuilder emitIf(boolean cond, String code)
cond
holdscond
- code
- public CodeBuilder emitlnIf(boolean cond, String line)
newline()
, provided the condition
cond
holdscond
- line
- should not contain newline characterspublic CodeBuilder openBlock()
emit(" {"); incrIndent(); newline();
public CodeBuilder closeBlock0()
decrIndent(); newline(); emit("}");It is useful when closing several blocks in a row to avoid empty lines.
public CodeBuilder closeBlock()
decrIndent(); newline(); emit("}"); newline();
public String contents()
public void print(Appendable appendable) throws IOException
appendable
appendable
- IOException
public void withTracker(String generated, int offset)
CodeBuilder
to be ready
to start tracking some regions of the emitted code.
Should only be called once: extra calls will be ignored.
Once this instance is configured, the various methods that take advantage of region tracking can be used:
getSourceMapping()
startTrackedRange(LexBuffer.Position)
endTrackedRange(CExtent)
emitTracked(CExtent)
emitTrackedIf(boolean, CExtent)
The tracked regions will refer to their positions in the
emitted code relative to the current amount of code
already emitted when this method is called, plus the
given offset
.
generated
- the name describing the code being generatedoffset
- a non-negative offset describing where this code
is going to be emitted in the actual generated filepublic SourceMapping getSourceMapping()
IllegalArgumentException
- if tracking has
not been enabled with withTracker(String, int)
public CodeBuilder startTrackedRange(LexBuffer.Position pos)
pos
in the source file.
Only one region can be tracked at any given time.
Successive calls to startTrackedRange(LexBuffer.Position)
without any calls to endTrackedRange(CExtent)
will be
ignored.
pos
- IllegalArgumentException
- if tracking has
not been enabled with withTracker(String, int)
endTrackedRange(CExtent)
public CodeBuilder endTrackedRange(@Nullable CExtent extent)
startTrackedRange(LexBuffer.Position)
. It will now
be registered into the source mappings
.
If no region was being tracked, this call is ignored.
extent
- if non-null
, describes how the emitted
contents were obtained by instantiating placeholders
in the corresponding source regionIllegalArgumentException
- if tracking has
not been enabled with withTracker(String, int)
public CodeBuilder emitTracked(CExtent extent)
extent
- IllegalArgumentException
- if tracking has
not been enabled with withTracker(String, int)
public CodeBuilder emitTrackedIf(boolean cond, CExtent extent)
emitTracked(CExtent)
but is only
performed if the given condition holds. Convenient
for conditional output based on preferences/debug/etc.cond
- extent
-