Previous: , Up: Software Objects   [Contents][Index]


2.1.17 Inherited Indentation

The insertion and removal of ASTs in a software object often results in indentation that doesn’t match the surrounding code. The indentation mix-in can be used to address these situations and help prevent undesirable indentation by maintaining the expected indentation when an AST is inserted into a new position. This is accomplished by storing the current line’s indentation, and the amount of indentation that should be propagated to children and the difference between the indentation provided by all of the AST’s parents. To simplify the implementation, all tabs are treated as spaces.

Mix-ins

The ability to reinsert tabs is provided through the software-indentation mix-in. The following slots are provided:

The ast-indentation mix-in is used to add indentation information to an AST. It provided the following slots:

Parsing

When using the indentation mix-in for software, the parser for the language needs to be modified to not insert whitespace after newlines, instead opting for storing this information in the indentation slots of an AST. This is accomplished by tracking the indentation that each AST provides to its children. When a newline is reached for a child, the current indentation provided by all of its parents is compared to the indentation provided for the child. If the indentation is different, the difference is used to to set either the parent’s indent-children slot when it hasn’t been changed from the default value or the indent-adjustment slot of the child otherwise.

Converting to Text

The source-text method also needs to be modified when using the indentation mix-in. While converting an AST to text, the interleaved text needs to be examined for newlines. When a newline is found in a string and nothing follows it, the next AST reached determines the amount of indentation needed. This will be the indentation provided by the indentation slots of all of its parents plus the indentation provided by its indent-adjustment slot. If a newline is found in a string and other text follows it, indentation is added that matches the current indentation of its parent.


Previous: , Up: Software Objects   [Contents][Index]