ICodeBlock is the base interface used to generate code in the FlatRedBall Editor. Typically code generation is performed in methods which have received an ICodeBlock, although plugins may also create their own ICodeBlock for fully-generated files.
ICodeBlocks provide the following benefits:
Handling of indentation and curly brackets
Generation of common code structures such as methods and properties
Fluent interface which can implify the creation of blocks of code with various levels of indentation
The most common place to interact with an ICodeBlock is by creating a class that inherits from ElementComponentCodeGenerator
Game1CodeGenerator
For example, the following code shows how to add code to an entity's Initialize method:
The code above would produce code similar to the following block:
If you need to create a full code file without an existing ICodeBlock, you can use the CodeDocument
class to create a new document. The CodeDocument
class is an ICodeBlock which has no indents, but which can call any of the extension methods to add new code.
For example, the following could be used to create a string that defines a new class:
This produces code similar to the following block:
Notice that blocks do not need to be explicltly ended. Calling End() returns the parent block, allowing for fluent interfaces, but it is not required. Calling ToString() on a block resolves all brackets and returns a properly indented block of code.