public class JsCodeBuilder extends Object
Usage example that demonstrates most of the methods:
JsCodeBuilder jcb = new JsCodeBuilder(CodeStyle.STRINGBUILDER);
jcb.appendLine("story.title = function(opt_data) {");
jcb.increaseIndent();
jcb.pushOutputVar("output");
jcb.initOutputVarIfNecessary();
jcb.pushOutputVar("temp");
jcb.addToOutputVar(Lists.newArrayList(
new JsExpr("'Snow White and the '", Integer.MAX_VALUE),
new JsExpr("opt_data.numDwarfs", Integer.MAX_VALUE));
jcb.popOutputVar();
jcb.addToOutputVar(Lists.newArrayList(
new JsExpr("temp", Integer.MAX_VALUE),
new JsExpr("' Dwarfs'", Integer.MAX_VALUE));
jcb.appendLineStart("return ").appendOutputVarName().appendLineEnd(".toString();");
jcb.popOutputVar();
jcb.decreaseIndent();
String THE_END = "the end";
jcb.appendLine("} // ", THE_END);
The above example builds the following JS code:
story.title = function(opt_data) {
var output = new soy.StringBuilder();
var temp = new soy.StringBuilder('Snow White and the ', opt_data.numDwarfs);
output.append(temp, ' Dwarfs');
return output.toString();
} // the end
| Modifier and Type | Field and Description |
|---|---|
protected CodeChunk.WithValue |
currOutputVar
The current output variable.
|
| Modifier | Constructor and Description |
|---|---|
protected |
JsCodeBuilder() |
protected |
JsCodeBuilder(JsCodeBuilder parent) |
| Modifier and Type | Method and Description |
|---|---|
JsCodeBuilder |
addChunksToOutputVar(List<? extends CodeChunk.WithValue> codeChunks)
Appends one or more lines representing the concatenation of the values of the given code chunks
saved to the current output variable.
|
JsCodeBuilder |
addChunkToOutputVar(CodeChunk.WithValue chunk)
Appends the given code chunk to the current output variable.
|
void |
addGoogRequire(GoogRequire require)
Adds a
goog.require |
JsCodeBuilder |
append(CodeChunk codeChunk)
Serializes the given
CodeChunk into the code builder, respecting the code builder's
current indentation level. |
JsCodeBuilder |
append(String... codeFragments)
Appends one or more strings to the generated code.
|
JsCodeBuilder |
appendLine(String... codeFragments)
Appends the current indent, then the given strings, then a newline.
|
JsCodeBuilder |
appendLineEnd(String... codeFragments)
Appends the given strings, then a newline.
|
JsCodeBuilder |
appendLineStart(String... codeFragments)
Appends the current indent, then the given strings.
|
JsCodeBuilder |
decreaseIndent()
Decreases the current indent.
|
JsCodeBuilder |
decreaseIndentTwice()
Decreases the current indent twice.
|
String |
getCode() |
CodeChunk.RequiresCollector |
getRequiresCollector() |
JsCodeBuilder |
increaseIndent()
Increases the current indent.
|
JsCodeBuilder |
increaseIndentTwice()
Increases the current indent twice.
|
void |
initOutputVarIfNecessary() |
JsCodeBuilder |
popOutputVar()
Pops off the current output variable.
|
JsCodeBuilder |
pushOutputVar(String outputVarName)
Pushes on a new current output variable.
|
JsCodeBuilder |
setOutputVarInited()
Tells this CodeBuilder that the current output variable has already been initialized.
|
@Nullable protected CodeChunk.WithValue currOutputVar
TODO(user): this is always an CodeChunk.id(java.lang.String). Consider exposing a subclass of
CodeChunk so we can enforce this invariant at compile time.
protected JsCodeBuilder()
protected JsCodeBuilder(JsCodeBuilder parent)
public void initOutputVarIfNecessary()
public JsCodeBuilder addChunkToOutputVar(CodeChunk.WithValue chunk)
public JsCodeBuilder addChunksToOutputVar(List<? extends CodeChunk.WithValue> codeChunks)
public final JsCodeBuilder increaseIndent()
public final JsCodeBuilder increaseIndentTwice()
public final JsCodeBuilder decreaseIndent()
public final JsCodeBuilder decreaseIndentTwice()
public JsCodeBuilder pushOutputVar(String outputVarName)
outputVarName - The new output variable name.public JsCodeBuilder popOutputVar()
public JsCodeBuilder setOutputVarInited()
initOutputVarIfNecessary and addToOutputVar to not add initialization
code even on the first use of the variable.public JsCodeBuilder append(CodeChunk codeChunk)
CodeChunk into the code builder, respecting the code builder's
current indentation level.public JsCodeBuilder append(String... codeFragments)
codeFragments - The code string(s) to append.public JsCodeBuilder appendLine(String... codeFragments)
codeFragments - The code string(s) to append.public JsCodeBuilder appendLineStart(String... codeFragments)
codeFragments - The code string(s) to append.public JsCodeBuilder appendLineEnd(String... codeFragments)
codeFragments - The code string(s) to append.public CodeChunk.RequiresCollector getRequiresCollector()
public void addGoogRequire(GoogRequire require)
goog.requirerequire - The namespace being requiredpublic String getCode()