Class CodeWriter
- Direct Known Subclasses:
CodegenWriter
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionDeprecated.Allows calling out to arbitrary code for things like looping or conditional writes without breaking method chaining.closeBlock
(String textAfterNewline, Object... args) Deprecated.Closes a block of syntax by writing a newline, dedenting, then writing text.void
copySettingsFrom
(CodeWriter other) Deprecated.static CodeWriter
Deprecated.Creates a default instance of a CodeWriter that uses "\n" for newlines, flattens multiple successive blank lines into a single blank line, and adds a trailing new line if needed when converting the CodeWriter to a string.dedent()
Deprecated.Removes one level of indentation from all lines.dedent
(int levels) Deprecated.Removes a specific number of indentations from all lines.Deprecated.Disables the automatic appending of newlines in the current state.Deprecated.Enables the automatic appending of newlines in the current state.Deprecated.Ensures that the last text written to the writer was a newline as defined in the current state and inserts one if necessary.indent()
Deprecated.Indents all text one level.indent
(int levels) Deprecated.Indents all text a specific number of levels.Deprecated.Configures the AbstractCodeWriter to always append a newline at the end of the text if one is not already present.insertTrailingNewline
(boolean trailingNewline) Deprecated.Configures the AbstractCodeWriter to always append a newline at the end of the text if one is not already present.Deprecated.Registers a function that intercepts the contents of a section and writes to theAbstractCodeWriter
with the updated contents.onSectionAppend
(String sectionName, Runnable writeAfter) Deprecated.onSectionPrepend
(String sectionName, Runnable writeBefore) Deprecated.Deprecated.Opens a block of syntax by writing text, a newline, then indenting.Deprecated.Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.openBlock
(String textBeforeNewline, String textAfterNewline, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Runnable f) Deprecated.Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.openBlock
(String textBeforeNewline, String textAfterNewline, Object arg1, Object arg2, Object arg3, Object arg4, Runnable f) Deprecated.Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.openBlock
(String textBeforeNewline, String textAfterNewline, Object arg1, Object arg2, Object arg3, Runnable f) Deprecated.Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.Deprecated.Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.Deprecated.Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.Deprecated.Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.popState()
Deprecated.Pops the current AbstractCodeWriter state from the state stack.pushFilteredState
(Function<String, String> filter) Deprecated.Pushes an anonymous named state that is always passed through the given filter function before being written to the writer.Deprecated.Copies and pushes the current state to the state stack.Deprecated.Copies and pushes the current state to the state stack using a named state that can be intercepted by functions registered withAbstractCodeWriter.onSection(CodeInterceptor)
.putContext
(String key, Object value) Deprecated.Adds a named key-value pair to the context of the current state.putContext
(Map<String, Object> mappings) Deprecated.Adds a map of named key-value pair to the context of the current state.putFormatter
(char identifier, BiFunction<Object, String, String> formatFunction) Deprecated.Adds a custom formatter expression to the current state of theAbstractCodeWriter
.removeContext
(String key) Deprecated.Removes a named key-value pair from the context of the current state.setExpressionStart
(char expressionStart) Deprecated.Sets the character used to start expressions in the current state when callingAbstractCodeWriter.write(java.lang.Object, java.lang.Object...)
,AbstractCodeWriter.writeInline(java.lang.Object, java.lang.Object...)
,AbstractCodeWriter.openBlock(java.lang.String, java.lang.Object...)
, etc.setIndentText
(String indentText) Deprecated.Sets the text used for indentation (defaults to four spaces).setNewline
(char newline) Deprecated.Sets the character used to represent newlines in the current state ("\n" is the default).setNewline
(String newline) Deprecated.Sets the character used to represent newlines in the current state ("\n" is the default).setNewlinePrefix
(String newlinePrefix) Deprecated.Sets a prefix to prepend to every line after a new line is added (except for an inserted trailing newline).Deprecated.Ensures that no more than one blank line occurs in succession.trimBlankLines
(int trimBlankLines) Deprecated.Ensures that no more than the given number of newlines can occur in succession, removing consecutive newlines that exceed the given threshold.Deprecated.Enables the trimming of trailing spaces on a line.trimTrailingSpaces
(boolean trimTrailingSpaces) Deprecated.Configures if trailing spaces on a line are removed.Deprecated.Remove the most recent text written to the AbstractCodeWriter if and only if the last written text is exactly equal to the given expanded content string.Deprecated.Writes text to the AbstractCodeWriter and appends a newline.writeInline
(Object content, Object... args) Deprecated.Writes text to the AbstractCodeWriter without appending a newline.writeOptional
(Object content) Deprecated.Optionally writes text to the AbstractCodeWriter and appends a newline if a value is present.writeWithNoFormatting
(Object content) Deprecated.Writes text to the AbstractCodeWriter and appends a newline.Methods inherited from class software.amazon.smithy.utils.AbstractCodeWriter
consumer, copySettingsFrom, enableStackTraceComments, format, formatLiteral, formatWithStackTraceElement, getContext, getContext, getDebugInfo, getDebugInfo, getExpressionStart, getIndentLevel, getIndentText, getInsertTrailingNewline, getNewline, getNewlinePrefix, getTrimBlankLines, getTrimTrailingSpaces, injectSection, isStackTraceRelevant, onSection, pushState, toString, writeInlineWithNoFormatting
-
Constructor Details
-
CodeWriter
public CodeWriter()Deprecated.
-
-
Method Details
-
createDefault
Deprecated.Creates a default instance of a CodeWriter that uses "\n" for newlines, flattens multiple successive blank lines into a single blank line, and adds a trailing new line if needed when converting the CodeWriter to a string.- Returns:
- Returns the created and configured CodeWriter.
-
onSectionPrepend
Deprecated.Prepends to the contents of a named section.writer.onSectionPrepend("foo", () -> { writer.write("This text is added before the rest of the section."); });
- Parameters:
sectionName
- The name of the section to intercept.writeBefore
- A runnable that prepends to a section by mutating the writer.- Returns:
- Returns the CodeWriter.
- See Also:
-
onSectionAppend
Deprecated.Appends to the contents of a named section.writer.onSectionAppend("foo", () -> { writer.write("This text is added after the rest of the section."); });
- Parameters:
sectionName
- The name of the section to intercept.writeAfter
- A runnable that appends to a section by mutating the writer.- Returns:
- Returns the CodeWriter.
- See Also:
-
copySettingsFrom
Deprecated. -
putFormatter
Deprecated.Description copied from class:AbstractCodeWriter
Adds a custom formatter expression to the current state of theAbstractCodeWriter
.The provided
identifier
string must match the following ABNF:%x21-23 ; ( '!' - '#' ) / %x25-2F ; ( '%' - '/' ) / %x3A-60 ; ( ':' - '`' ) / %x7B-7E ; ( '{' - '~' )
- Overrides:
putFormatter
in classAbstractCodeWriter<CodeWriter>
- Parameters:
identifier
- Formatter identifier to associate with this formatter.formatFunction
- Formatter function that formats the given object as a String. The formatter is give the value to format as an object (use .toString to access the string contents) and the current indentation string of the AbstractCodeWriter.- Returns:
- Returns self.
-
setExpressionStart
Deprecated.Description copied from class:AbstractCodeWriter
Sets the character used to start expressions in the current state when callingAbstractCodeWriter.write(java.lang.Object, java.lang.Object...)
,AbstractCodeWriter.writeInline(java.lang.Object, java.lang.Object...)
,AbstractCodeWriter.openBlock(java.lang.String, java.lang.Object...)
, etc.By default,
$
is used to start expressions (for example$L
. However, some programming languages frequently give syntactic meaning to$
, making this an inconvenient syntactic character for the AbstractCodeWriter. In these cases, the character used to start a AbstractCodeWriter expression can be changed. Just like$
, the custom start character can be escaped using two subsequent start characters (e.g.,$$
).- Overrides:
setExpressionStart
in classAbstractCodeWriter<CodeWriter>
- Parameters:
expressionStart
- Character to use to start expressions.- Returns:
- Returns self.
-
pushState
Deprecated.Description copied from class:AbstractCodeWriter
Copies and pushes the current state to the state stack.This method is used to prepare for a corresponding
AbstractCodeWriter.popState()
operation later. It stores the current state of the AbstractCodeWriter into a stack and keeps it active. After pushing, mutations can be made to the state of the AbstractCodeWriter without affecting the previous state on the stack. Changes to the state of the AbstractCodeWriter can be undone by usingAbstractCodeWriter.popState()
, which Returns self state to the state it was in before callingpushState
.- Overrides:
pushState
in classAbstractCodeWriter<CodeWriter>
- Returns:
- Returns the code writer.
-
pushState
Deprecated.Description copied from class:AbstractCodeWriter
Copies and pushes the current state to the state stack using a named state that can be intercepted by functions registered withAbstractCodeWriter.onSection(CodeInterceptor)
.The text written while in this state is buffered and passed to each state interceptor. If no text is written by the section or an interceptor, nothing is changed on the
AbstractCodeWriter
. This behavior allows for placeholder sections to be added intoAbstractCodeWriter
generators in order to provide extension points that can be otherwise empty.- Overrides:
pushState
in classAbstractCodeWriter<CodeWriter>
- Parameters:
sectionName
- Name of the section to set on the state.- Returns:
- Returns the code writer.
-
pushFilteredState
Deprecated.Description copied from class:AbstractCodeWriter
Pushes an anonymous named state that is always passed through the given filter function before being written to the writer.- Overrides:
pushFilteredState
in classAbstractCodeWriter<CodeWriter>
- Parameters:
filter
- Function that maps over the entire section when popped.- Returns:
- Returns the code writer.
-
popState
Deprecated.Description copied from class:AbstractCodeWriter
Pops the current AbstractCodeWriter state from the state stack.This method is used to reverse a previous
AbstractCodeWriter.pushState()
operation. It configures the current AbstractCodeWriter state to what it was before the last precedingpushState
call.- Overrides:
popState
in classAbstractCodeWriter<CodeWriter>
- Returns:
- Returns self.
-
onSection
Deprecated.Description copied from class:AbstractCodeWriter
Registers a function that intercepts the contents of a section and writes to theAbstractCodeWriter
with the updated contents.The
interceptor
function is expected to have a reference to theAbstractCodeWriter
and to mutate it when they are invoked. Each interceptor is invoked in their own isolated pushed/popped states.The text provided to
interceptor
does not contain a trailing new line. A trailing new line is expected to be injected automatically when the results of intercepting the contents are written to theAbstractCodeWriter
. A result is only written if the interceptors write a non-null, non-empty string, allowing for empty placeholders to be added that don't affect the resulting layout of the code.SimpleCodeWriter = new SimpleCodeWriter(); // Prepend text to a section named "foo". writer.onSectionPrepend("foo", () -> writer.write("A")); // Write text to a section, and ensure that the original // text is written too. writer.onSection("foo", text -> { // Write before the original text. writer.write("A"); // Write the original text of the section. writer.writeWithNoFormatting(text); // Write more text to the section. writer.write("C"); }); // Create the section, write to it, then close the section. writer.pushState("foo").write("B").popState(); assert(writer.toString().equals("A\nB\nC\n"));
Newline handling
This method is a wrapper around
AbstractCodeWriter.onSection(CodeInterceptor)
that has several limitations:- The provided
interceptor
is expected to have a reference to anAbstractCodeWriter
so that write calls can be made. - The handling of newlines is much less precise. If you want to
give interceptors full control over how newlines are injected, then
AbstractCodeWriter.onSection(CodeInterceptor)
must be used directly and careful use ofAbstractCodeWriter.writeInlineWithNoFormatting(Object)
is required when writing the previous contents to the interceptor. - Interceptors do not have access to strongly typed event data
like
CodeInterceptor
s do.
The newline handling functionality provided by this method can be reproduced using a
CodeInterceptor
by removing trailing newlines usingAbstractCodeWriter.removeTrailingNewline(String)
.SimpleCodeWriter = new SimpleCodeWriter(); CodeInterceptor<CodeSection, SimpleCodeWriter> interceptor = CodeInterceptor.forName(sectionName, (w, p) -> { String trimmedContent = removeTrailingNewline(p); interceptor.accept(trimmedContent); }) writer.onSection(interceptor);
- Overrides:
onSection
in classAbstractCodeWriter<CodeWriter>
- Parameters:
sectionName
- The name of the section to intercept.interceptor
- The function to intercept with.- Returns:
- Returns self.
- The provided
-
disableNewlines
Deprecated.Description copied from class:AbstractCodeWriter
Disables the automatic appending of newlines in the current state.Methods like
AbstractCodeWriter.write(java.lang.Object, java.lang.Object...)
,AbstractCodeWriter.openBlock(java.lang.String, java.lang.Object...)
, andAbstractCodeWriter.closeBlock(java.lang.String, java.lang.Object...)
will not automatically append newlines when a state has this flag set.- Overrides:
disableNewlines
in classAbstractCodeWriter<CodeWriter>
- Returns:
- Returns self.
-
enableNewlines
Deprecated.Description copied from class:AbstractCodeWriter
Enables the automatic appending of newlines in the current state.- Overrides:
enableNewlines
in classAbstractCodeWriter<CodeWriter>
- Returns:
- Returns self.
-
setNewline
Deprecated.Description copied from class:AbstractCodeWriter
Sets the character used to represent newlines in the current state ("\n" is the default).When the provided string is empty (""), then newlines are disabled in the current state. This is exactly equivalent to calling
AbstractCodeWriter.disableNewlines()
, and does not actually change the newline character of the current state.Setting the newline character to a non-empty string implicitly enables newlines in the current state.
- Overrides:
setNewline
in classAbstractCodeWriter<CodeWriter>
- Parameters:
newline
- Newline character to use.- Returns:
- Returns self.
-
setNewline
Deprecated.Description copied from class:AbstractCodeWriter
Sets the character used to represent newlines in the current state ("\n" is the default).This call also enables newlines in the current state by calling
AbstractCodeWriter.enableNewlines()
.- Overrides:
setNewline
in classAbstractCodeWriter<CodeWriter>
- Parameters:
newline
- Newline character to use.- Returns:
- Returns self.
-
setIndentText
Deprecated.Description copied from class:AbstractCodeWriter
Sets the text used for indentation (defaults to four spaces).- Overrides:
setIndentText
in classAbstractCodeWriter<CodeWriter>
- Parameters:
indentText
- Indentation text.- Returns:
- Returns self.
-
trimTrailingSpaces
Deprecated.Description copied from class:AbstractCodeWriter
Enables the trimming of trailing spaces on a line.- Overrides:
trimTrailingSpaces
in classAbstractCodeWriter<CodeWriter>
- Returns:
- Returns self.
-
trimTrailingSpaces
Deprecated.Description copied from class:AbstractCodeWriter
Configures if trailing spaces on a line are removed.- Overrides:
trimTrailingSpaces
in classAbstractCodeWriter<CodeWriter>
- Parameters:
trimTrailingSpaces
- Set to true to trim trailing spaces.- Returns:
- Returns self.
-
trimBlankLines
Deprecated.Description copied from class:AbstractCodeWriter
Ensures that no more than one blank line occurs in succession.- Overrides:
trimBlankLines
in classAbstractCodeWriter<CodeWriter>
- Returns:
- Returns self.
-
trimBlankLines
Deprecated.Description copied from class:AbstractCodeWriter
Ensures that no more than the given number of newlines can occur in succession, removing consecutive newlines that exceed the given threshold.- Overrides:
trimBlankLines
in classAbstractCodeWriter<CodeWriter>
- Parameters:
trimBlankLines
- Number of allowed consecutive newlines. Set to -1 to perform no trimming. Set to 0 to allow no blank lines. Set to 1 or more to allow for no more than N consecutive blank lines.- Returns:
- Returns self.
-
insertTrailingNewline
Deprecated.Description copied from class:AbstractCodeWriter
Configures the AbstractCodeWriter to always append a newline at the end of the text if one is not already present.This setting is not captured as part of push/popState.
- Overrides:
insertTrailingNewline
in classAbstractCodeWriter<CodeWriter>
- Returns:
- Returns self.
-
insertTrailingNewline
Deprecated.Description copied from class:AbstractCodeWriter
Configures the AbstractCodeWriter to always append a newline at the end of the text if one is not already present.This setting is not captured as part of push/popState.
- Overrides:
insertTrailingNewline
in classAbstractCodeWriter<CodeWriter>
- Parameters:
trailingNewline
- True if a newline is added.- Returns:
- Returns self.
-
setNewlinePrefix
Deprecated.Description copied from class:AbstractCodeWriter
Sets a prefix to prepend to every line after a new line is added (except for an inserted trailing newline).- Overrides:
setNewlinePrefix
in classAbstractCodeWriter<CodeWriter>
- Parameters:
newlinePrefix
- Newline prefix to use.- Returns:
- Returns self.
-
indent
Deprecated.Description copied from class:AbstractCodeWriter
Indents all text one level.- Overrides:
indent
in classAbstractCodeWriter<CodeWriter>
- Returns:
- Returns self.
-
indent
Deprecated.Description copied from class:AbstractCodeWriter
Indents all text a specific number of levels.- Overrides:
indent
in classAbstractCodeWriter<CodeWriter>
- Parameters:
levels
- Number of levels to indent.- Returns:
- Returns self.
-
dedent
Deprecated.Description copied from class:AbstractCodeWriter
Removes one level of indentation from all lines.- Overrides:
dedent
in classAbstractCodeWriter<CodeWriter>
- Returns:
- Returns self.
-
dedent
Deprecated.Description copied from class:AbstractCodeWriter
Removes a specific number of indentations from all lines.Set to -1 to dedent back to 0 (root).
- Overrides:
dedent
in classAbstractCodeWriter<CodeWriter>
- Parameters:
levels
- Number of levels to remove.- Returns:
- Returns self.
-
openBlock
Deprecated.Description copied from class:AbstractCodeWriter
Opens a block of syntax by writing text, a newline, then indenting.String result = new SimpleCodeWriter() .openBlock("public final class $L {", "Foo") .openBlock("public void main(String[] args) {") .write("System.out.println(args[0]);") .closeBlock("}") .closeBlock("}") .toString();
- Overrides:
openBlock
in classAbstractCodeWriter<CodeWriter>
- Parameters:
textBeforeNewline
- Text to write before writing a newline and indenting.args
- String arguments to use for formatting.- Returns:
- Returns this.
-
openBlock
Deprecated.Description copied from class:AbstractCodeWriter
Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.SimpleCodeWriter = new SimpleCodeWriter(); writer.openBlock("public final class $L {", "}", "Foo", () -> { writer.openBlock("public void main(String[] args) {", "}", () -> { writer.write("System.out.println(args[0]);"); }) });
- Overrides:
openBlock
in classAbstractCodeWriter<CodeWriter>
- Parameters:
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.f
- Runnable function to execute inside of the block.- Returns:
- Returns this.
-
openBlock
public CodeWriter openBlock(String textBeforeNewline, String textAfterNewline, Object arg1, Runnable f) Deprecated.Description copied from class:AbstractCodeWriter
Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.- Overrides:
openBlock
in classAbstractCodeWriter<CodeWriter>
- Parameters:
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.arg1
- First positional argument to substitute intotextBeforeNewline
.f
- Runnable function to execute inside of the block.- Returns:
- Returns this.
-
openBlock
public CodeWriter openBlock(String textBeforeNewline, String textAfterNewline, Object arg1, Object arg2, Runnable f) Deprecated.Description copied from class:AbstractCodeWriter
Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.- Overrides:
openBlock
in classAbstractCodeWriter<CodeWriter>
- Parameters:
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.arg1
- First positional argument to substitute intotextBeforeNewline
.arg2
- Second positional argument to substitute intotextBeforeNewline
.f
- Runnable function to execute inside of the block.- Returns:
- Returns this.
-
openBlock
public CodeWriter openBlock(String textBeforeNewline, String textAfterNewline, Object arg1, Object arg2, Object arg3, Runnable f) Deprecated.Description copied from class:AbstractCodeWriter
Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.- Overrides:
openBlock
in classAbstractCodeWriter<CodeWriter>
- Parameters:
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.arg1
- First positional argument to substitute intotextBeforeNewline
.arg2
- Second positional argument to substitute intotextBeforeNewline
.arg3
- Third positional argument to substitute intotextBeforeNewline
.f
- Runnable function to execute inside of the block.- Returns:
- Returns this.
-
openBlock
public CodeWriter openBlock(String textBeforeNewline, String textAfterNewline, Object arg1, Object arg2, Object arg3, Object arg4, Runnable f) Deprecated.Description copied from class:AbstractCodeWriter
Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.- Overrides:
openBlock
in classAbstractCodeWriter<CodeWriter>
- Parameters:
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.arg1
- First positional argument to substitute intotextBeforeNewline
.arg2
- Second positional argument to substitute intotextBeforeNewline
.arg3
- Third positional argument to substitute intotextBeforeNewline
.arg4
- Fourth positional argument to substitute intotextBeforeNewline
.f
- Runnable function to execute inside of the block.- Returns:
- Returns this.
-
openBlock
public CodeWriter openBlock(String textBeforeNewline, String textAfterNewline, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Runnable f) Deprecated.Description copied from class:AbstractCodeWriter
Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.- Overrides:
openBlock
in classAbstractCodeWriter<CodeWriter>
- Parameters:
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.arg1
- First positional argument to substitute intotextBeforeNewline
.arg2
- Second positional argument to substitute intotextBeforeNewline
.arg3
- Third positional argument to substitute intotextBeforeNewline
.arg4
- Fourth positional argument to substitute intotextBeforeNewline
.arg5
- Fifth positional argument to substitute intotextBeforeNewline
.f
- Runnable function to execute inside of the block.- Returns:
- Returns this.
-
openBlock
public CodeWriter openBlock(String textBeforeNewline, String textAfterNewline, Object[] args, Runnable f) Deprecated.Description copied from class:AbstractCodeWriter
Opens a block of syntax by writingtextBeforeNewline
, a newline, then indenting, then executes the givenRunnable
, then closes the block of syntax by writing a newline, dedenting, then writingtextAfterNewline
.- Overrides:
openBlock
in classAbstractCodeWriter<CodeWriter>
- Parameters:
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.args
- Arguments to substitute intotextBeforeNewline
.f
- Runnable function to execute inside of the block.- Returns:
- Returns this.
-
closeBlock
Deprecated.Description copied from class:AbstractCodeWriter
Closes a block of syntax by writing a newline, dedenting, then writing text.- Overrides:
closeBlock
in classAbstractCodeWriter<CodeWriter>
- Parameters:
textAfterNewline
- Text to write after writing a newline and dedenting.args
- String arguments to use for formatting.- Returns:
- Returns this.
-
writeWithNoFormatting
Deprecated.Description copied from class:AbstractCodeWriter
Writes text to the AbstractCodeWriter and appends a newline.The provided text does not use any kind of expression formatting.
Indentation and the newline prefix is only prepended if the writer's cursor is at the beginning of a newline.
Stack trace comments are written along with the given content if
AbstractCodeWriter.enableStackTraceComments(boolean)
was called withtrue
.- Overrides:
writeWithNoFormatting
in classAbstractCodeWriter<CodeWriter>
- Parameters:
content
- Content to write.- Returns:
- Returns self.
-
call
Deprecated.Description copied from class:AbstractCodeWriter
Allows calling out to arbitrary code for things like looping or conditional writes without breaking method chaining.- Overrides:
call
in classAbstractCodeWriter<CodeWriter>
- Parameters:
task
- Method to invoke.- Returns:
- Returns this.
-
write
Deprecated.Description copied from class:AbstractCodeWriter
Writes text to the AbstractCodeWriter and appends a newline.The provided text is automatically formatted using variadic arguments.
Indentation and the newline prefix is only prepended if the writer's cursor is at the beginning of a newline.
If a subclass overrides this method, it should first perform formatting and then delegate to
AbstractCodeWriter.writeWithNoFormatting(java.lang.Object)
to perform the actual write.- Overrides:
write
in classAbstractCodeWriter<CodeWriter>
- Parameters:
content
- Content to write.args
- String arguments to use for formatting.- Returns:
- Returns self.
-
writeInline
Deprecated.Description copied from class:AbstractCodeWriter
Writes text to the AbstractCodeWriter without appending a newline.The provided text is automatically formatted using variadic arguments.
Indentation and the newline prefix is only prepended if the writer's cursor is at the beginning of a newline.
If newlines are present in the given string, each of those lines will receive proper indentation.
If a subclass overrides this method, it should first perform formatting and then delegate to
AbstractCodeWriter.writeInlineWithNoFormatting(java.lang.Object)
to perform the actual write.- Overrides:
writeInline
in classAbstractCodeWriter<CodeWriter>
- Parameters:
content
- Content to write.args
- String arguments to use for formatting.- Returns:
- Returns self.
-
ensureNewline
Deprecated.Description copied from class:AbstractCodeWriter
Ensures that the last text written to the writer was a newline as defined in the current state and inserts one if necessary.- Overrides:
ensureNewline
in classAbstractCodeWriter<CodeWriter>
- Returns:
- Returns self.
-
writeOptional
Deprecated.Description copied from class:AbstractCodeWriter
Optionally writes text to the AbstractCodeWriter and appends a newline if a value is present.If the provided
content
value isnull
, nothing is written. If the providedcontent
value is an emptyOptional
, nothing is written. If the result of callingtoString
oncontent
results in an empty string, nothing is written. Finally, if the value is a non-empty string, the content is written to theAbstractCodeWriter
at the current level of indentation, and a newline is appended.- Overrides:
writeOptional
in classAbstractCodeWriter<CodeWriter>
- Parameters:
content
- Content to write if present.- Returns:
- Returns self.
-
unwrite
Deprecated.Description copied from class:AbstractCodeWriter
Remove the most recent text written to the AbstractCodeWriter if and only if the last written text is exactly equal to the given expanded content string.This can be useful, for example, for use cases like removing trailing commas from lists of values.
For example, the following will remove ", there." from the end of the AbstractCodeWriter:
SimpleCodeWriter = new SimpleCodeWriter(); writer.writeInline("Hello, there."); writer.unwrite(", there."); assert(writer.toString().equals("Hello\n"));
However, the following call to unwrite will do nothing because the last text written to the AbstractCodeWriter does not match:
SimpleCodeWriter = new SimpleCodeWriter(); writer.writeInline("Hello."); writer.unwrite("there."); assert(writer.toString().equals("Hello.\n"));
- Overrides:
unwrite
in classAbstractCodeWriter<CodeWriter>
- Parameters:
content
- Content to write.args
- String arguments to use for formatting.- Returns:
- Returns self.
-
putContext
Deprecated.Description copied from class:AbstractCodeWriter
Adds a named key-value pair to the context of the current state.These context values can be referenced by named interpolated parameters.
- Overrides:
putContext
in classAbstractCodeWriter<CodeWriter>
- Parameters:
key
- Key to add to the context.value
- Value to associate with the key.- Returns:
- Returns self.
-
putContext
Deprecated.Description copied from class:AbstractCodeWriter
Adds a map of named key-value pair to the context of the current state.These context values can be referenced by named interpolated parameters.
- Overrides:
putContext
in classAbstractCodeWriter<CodeWriter>
- Parameters:
mappings
- Key value pairs to add.- Returns:
- Returns self.
-
removeContext
Deprecated.Description copied from class:AbstractCodeWriter
Removes a named key-value pair from the context of the current state.- Overrides:
removeContext
in classAbstractCodeWriter<CodeWriter>
- Parameters:
key
- Key to add to remove from the current context.- Returns:
- Returns self.
-
SimpleCodeWriter
or a custom subclass ofAbstractCodeWriter
.