Table of Contents

Class ModuleIndenter

Namespace
RDCore.SDK.Formatting
Assembly
RDCore.SDK.dll

Indents the lines of a module by what encloses them in its syntax tree (CodeFormattingOptions): a line is indented one level for each body it is in - of a procedure, an Enum or a Type, a branch of an If block, a Case, a loop, a With block, a branch of an #If directive.

public static class ModuleIndenter
Inheritance
ModuleIndenter
Inherited Members

Remarks

A level is IndentSpaces spaces: the code of a module is indented with spaces, as VBA code is. A block's opener and its closer, and the ElseIf, Else and Case lines between them, are at the level of the block, and so is a line that has its closer after other code (x = 1: End If); what they guard is a level further in. A procedure's body is indented when IndentEntireProcedureBody says so, the Case lines of a Select Case when IndentCase does, and what an #If guards when IndentCompilerDirectives does. A procedure whose header is written once in each branch of an #If has the #If block as its header: its lines are at the procedure's level, and the body after the #End If is indented once.

A line the syntax tree has no statement of is indented by its keywords instead: code the parser could not build as you type, or kept only as its text (UnbuiltStatementTriviaNode). It is indented from the level of where it is: a block's opener indents the lines after it, and its closer ends that. A block the parser could not find the closer of is open to its last line. The indenter never refuses to indent.

A line number stays at the start of its line, and so does a line label, as the VBE has it: the code after either is where the line's level has it, a space after the number or the label at least. A line continued from the one before (_) lines up with the lines of its statement before it. A comment alone on its line is at the level of the code around it. The header of a class or a form module (VERSION, BEGIN ... END) and its Attribute lines are left as they are written.

Between procedures - a Sub, a Function, a Property, an Enum or a Type, with the comments right above it - there are LinesBetweenProcedures empty lines when VerticallySpaceProcedures says so, and as many after what is above the first; none between the accessors of a property when GroupRelatedProperties groups them.

Methods

Apply(string, IReadOnlyList<IndentationEdit>)

The text of a module with indentation edits made to it (Indent(ModuleParseResult, string, CodeFormattingOptions, int, int)).

public static string Apply(string text, IReadOnlyList<IndentationEdit> edits)

Parameters

text string

The text.

edits IReadOnlyList<IndentationEdit>

The edits, none of them overlapping; in any order.

Returns

string

The text with the edits made, the line ends it keeps as they were.

Remarks

The text is read once, from the first edit to the last: the time it takes grows with the text and the edits, and not with the one times the other.

Exceptions

ArgumentNullException

text or edits is null.

ArgumentOutOfRangeException

An edit is of a line the text does not have, or of characters its line does not have, or two edits overlap.

Indent(ModuleParseResult, string, CodeFormattingOptions, int, int)

The edits that indent the lines of a module, from its first line to its last, or of the lines from firstLine to lastLine; none for a line that is indented as it should be.

public static IReadOnlyList<IndentationEdit> Indent(ModuleParseResult parse, string text, CodeFormattingOptions options, int firstLine = 0, int lastLine = 2147483647)

Parameters

parse ModuleParseResult

The module, as it was parsed from text.

text string

The text of the module. A line ends with a carriage return, a line feed, or both, as the Language Server Protocol counts them.

options CodeFormattingOptions

How code is indented.

firstLine int

The first line to indent, from 0. A line that continues the one before it widens the range to the first line of its statement.

lastLine int

The last line to indent. A line that is continued widens the range to the last line of its statement.

Returns

IReadOnlyList<IndentationEdit>

The edits, in the order of the text: by line, then by character, none of them overlapping, so that each can be made where the text as it is has it.

Exceptions

ArgumentNullException

parse, text or options is null.

IndentWritten(string, IReadOnlyList<RefactorTextEdit>, ModuleParseResult, CodeFormattingOptions)

Edits of a module, with the lines they write indented as Indent(ModuleParseResult, string, CodeFormattingOptions, int, int) indents them in the module the edits leave: a line an edit writes from its start is at the level of what encloses it there, whatever indent the edit gave it. The rest of what the edits write, and the lines they do not write, are as they were.

public static IReadOnlyList<RefactorTextEdit> IndentWritten(string text, IReadOnlyList<RefactorTextEdit> edits, ModuleParseResult edited, CodeFormattingOptions options)

Parameters

text string

The text of the module, which the edits are edits of.

edits IReadOnlyList<RefactorTextEdit>

The edits, in any order; they do not overlap (LSP 3.17 §TextEdit).

edited ModuleParseResult

The module as it is parsed from the text the edits leave (ApplyTo(string, IReadOnlyList<RefactorTextEdit>) of text).

options CodeFormattingOptions

How code is indented.

Returns

IReadOnlyList<RefactorTextEdit>

The edits, in the order of the text. An edit whose last line is indented replaces the spaces and tabs that line goes on with after it, too.

Exceptions

ArgumentException

Two of the edits overlap.