Table of Contents

Class DeclarationEdits

Namespace
RDCore.SDK.Semantics.Refactoring
Assembly
RDCore.SDK.dll

The edits that add declarations to the declaration section of a module, remove one variable of a declaration statement, and add a member to a user-defined type. They keep the comments, the line continuations and the other variables of a statement.

public static class DeclarationEdits
Inheritance
DeclarationEdits
Inherited Members

Remarks

The edits are of the text as it is, as LSP 3.17 TextEdits of one document are. What they write ends its lines as the text does: with what ends the text's first line, and with a carriage return and a line feed when the text has a single line.

Methods

AddTypeMember(string, MemberDeclarationNode, string)

The edit that adds a member to a user-defined type (MS-VBAL §5.2.3.3): on a line of its own before its End Type, indented as its last member is, or by four spaces more than its End Type when it has none.

public static RefactorTextEdit AddTypeMember(string text, MemberDeclarationNode type, string member)

Parameters

text string

The text of the module.

type MemberDeclarationNode

The type, a node of the module's syntax tree.

member string

The text of the member, as Name As Long.

Returns

RefactorTextEdit

The edit, an insertion at the start of the line of the End Type.

Exceptions

ArgumentException

type is not a user-defined type.

ApplyTo(string, IReadOnlyList<RefactorTextEdit>)

The text that edits of a text leave: each edit's range replaced by what it writes.

public static string ApplyTo(string text, IReadOnlyList<RefactorTextEdit> edits)

Parameters

text string

The text, its lines ended by a carriage return and a line feed, a line feed, or a carriage return.

edits IReadOnlyList<RefactorTextEdit>

The edits, in any order, their ranges positions of text before any edit is made; they do not overlap (LSP 3.17 §TextEdit).

Returns

string

The text with every edit made.

Exceptions

ArgumentException

Two of the edits overlap.

ArgumentOutOfRangeException

An edit is of a line the text does not have.

IndentOf(string, int)

The spaces and tabs a line of a text starts with: what a line written in its place, or next to it at the same level, is indented with.

public static string IndentOf(string text, int line)

Parameters

text string

The text, its lines ended by a carriage return and a line feed, a line feed, or a carriage return.

line int

The line, from 0.

Returns

string

Exceptions

ArgumentOutOfRangeException

line is not a line of the text.

Insert(ModuleParseResult, string, string)

The edit that writes declarations at the end of the declaration section of a module (SectionEnd(ModuleParseResult, string)), on lines of their own, with a blank line between them and a procedure written directly after them.

public static RefactorTextEdit? Insert(ModuleParseResult parse, string text, string declarations)

Parameters

parse ModuleParseResult

The module, parsed.

text string

The text the module was parsed from.

declarations string

The text of the declarations, one or more lines.

Returns

RefactorTextEdit

The edit, or null when the end of the declaration section is in a branch of an #If block.

Exceptions

ArgumentException

The module has no syntax tree.

RangeAfter(SourceRange, IReadOnlyList<RefactorTextEdit>)

Where a range of a text is after edits of that text: the edits written before it move it by the lines and characters they add or remove, and the edits written after it leave it where it is. A refactoring finds there what it left of the text, such as a name it did not edit.

public static SourceRange? RangeAfter(SourceRange range, IReadOnlyList<RefactorTextEdit> edits)

Parameters

range SourceRange

The range, in the text before the edits.

edits IReadOnlyList<RefactorTextEdit>

The edits, in the text before them, in any order; they do not overlap (LSP 3.17 §TextEdit). Text inserted where the range starts is written before it, and text inserted where it ends is written after it.

Returns

SourceRange?

The range in the text after the edits, or null when an edit replaces any of it, or inserts text inside it: what is written there is the edit's.

Exceptions

ArgumentException

Two of the edits overlap.

RemoveVariable(ModuleParseResult, string, VariableDeclarationNode)

The edits that remove one variable of a declaration statement, at module level (MS-VBAL §5.2.3.1) or in a procedure, and keep the others it declares:

  • the only variable of its statement takes the statement with it, and its lines when nothing else is written on them; a comment after it stays;
  • the first variable of several leaves the keywords in front of it to the next one (Dim a As Long, b As String becomes Dim b As String);
  • any other takes the comma in front of it with it.

The Attribute lines of the variable written directly after its statement (Attribute Name.VB_VarDescription) are removed with it.

public static IReadOnlyList<RefactorTextEdit>? RemoveVariable(ModuleParseResult parse, string text, VariableDeclarationNode variable)

Parameters

parse ModuleParseResult

The module, parsed.

text string

The text the module was parsed from.

variable VariableDeclarationNode

The variable, a node of the module's syntax tree.

Returns

IReadOnlyList<RefactorTextEdit>

The edits, in the order they are in the text; or null when the variable is the only one of a statement that shares its line with another statement (Dim a As Long: a = 1), which is left to the caller to write as it wants.

Exceptions

ArgumentException

The module has no syntax tree, or variable is not in it.

SectionEnd(ModuleParseResult, string)

Where declarations are added to the declaration section of a module (MS-VBAL §5.2): at the start of the line after the last line that is not blank before the whole text of the first procedure (Extent(ModuleParseResult, string, MemberDeclarationNode)), so the comments directly above that procedure stay its own. In a module with no procedure, it is the end of the text.

public static SourcePosition? SectionEnd(ModuleParseResult parse, string text)

Parameters

parse ModuleParseResult

The module, parsed: its syntax tree, its trivia (the comments) and its precompiler trivia (the #If blocks).

text string

The text the module was parsed from.

Returns

SourcePosition?

The position, or null when it is in a branch of an #If block (MS-VBAL §3.4), where a declaration would be compiled only when that branch is live.

Exceptions

ArgumentException

The module has no syntax tree.

WholeLines(string, int, int)

The range of whole lines of a text, from the start of the first to the start of the line after the last, so that an edit that removes it leaves no blank line. When the last line is the last of a text that has no line end after it, the range starts where the line before the first ends instead, and so removes that line end; it is all of the text when the lines are all of it.

public static SourceRange WholeLines(string text, int first, int last)

Parameters

text string

The text, its lines ended by a carriage return and a line feed, a line feed, or a carriage return.

first int

The first line, from 0.

last int

The last line, from first.

Returns

SourceRange

Exceptions

ArgumentOutOfRangeException

The lines are not lines of the text, or last is before first.