SyntaxEditor Class

Definition

An editor for making changes to a syntax tree. The editor works by giving a list of changes to perform to a particular tree in order. Changes are given a SyntaxNode they will apply to in the original tree the editor is created for. The semantics of application are as follows:

  1. The original root provided is used as the 'current' root for all operations. This 'current' root will continually be updated, becoming the new 'current' root. The original root is never changed.
  2. Each change has its given SyntaxNode tracked, using a SyntaxAnnotation, producing a 'current' root that tracks all of them. This allows that same node to be found after prior changes are applied which mutate the tree.
  3. Each change is then applied in order it was added to the editor.
  4. A change first attempts to find its SyntaxNode in the 'current' root. If that node cannot be found, the operation will fail with an ArgumentException.
  5. The particular change will run on that node, removing, replacing, or inserting around it according to the change. If the change is passed a delegate as its 'compute' argument, it will be given the SyntaxNode found in the current root. The 'current' root will then be updated by replacing the current node with the new computed node.
  6. The 'current' root is then returned.
public ref class SyntaxEditor
public class SyntaxEditor
type SyntaxEditor = class
Public Class SyntaxEditor
Inheritance
SyntaxEditor
Derived

Remarks

The above editing strategy makes it an error for a client of the editor to add a change that updates a parent node and then adds a change that updates a child node (unless the parent change is certain to contain the child), and attempting this will throw at runtime. If a client ever needs to update both a child and a parent, it should add the child change first, and then the parent change. And the parent change should pass an appropriate 'compute' callback so it will see the results of the child change.

If a client wants to make a replacement, then find the valueSyntaxNode put into the tree, that can be done by adding a dedicated annotation to that node and then looking it back up in the 'current' node passed to a 'compute' callback.

Constructors

SyntaxEditor(SyntaxNode, HostWorkspaceServices)

Creates a new SyntaxEditor instance.

SyntaxEditor(SyntaxNode, SolutionServices)

Creates a new SyntaxEditor instance.

SyntaxEditor(SyntaxNode, Workspace)
Obsolete.

Creates a new SyntaxEditor instance.

Properties

Generator

A SyntaxGenerator to use to create and change SyntaxNode's.

OriginalRoot

The SyntaxNode that was specified when the SyntaxEditor was constructed.

Methods

GetChangedRoot()

Returns the changed root node.

InsertAfter(SyntaxNode, IEnumerable<SyntaxNode>)

Insert the new nodes after the specified node already existing in the tree.

InsertAfter(SyntaxNode, SyntaxNode)

Insert the new node after the specified node already existing in the tree.

InsertBefore(SyntaxNode, IEnumerable<SyntaxNode>)

Insert the new nodes before the specified node already existing in the tree.

InsertBefore(SyntaxNode, SyntaxNode)

Insert the new node before the specified node already existing in the tree.

RemoveNode(SyntaxNode)

Remove the node from the tree.

RemoveNode(SyntaxNode, SyntaxRemoveOptions)

Remove the node from the tree.

ReplaceNode(SyntaxNode, Func<SyntaxNode,SyntaxGenerator,SyntaxNode>)

Replace the specified node with a node produced by the function.

ReplaceNode(SyntaxNode, SyntaxNode)

Replace the specified node with a different node.

TrackNode(SyntaxNode)

Makes sure the node is tracked, even if it is not changed.

Extension Methods

AddAttribute(SyntaxEditor, SyntaxNode, SyntaxNode)
AddAttributeArgument(SyntaxEditor, SyntaxNode, SyntaxNode)
AddBaseType(SyntaxEditor, SyntaxNode, SyntaxNode)
AddInterfaceType(SyntaxEditor, SyntaxNode, SyntaxNode)
AddMember(SyntaxEditor, SyntaxNode, SyntaxNode)
AddParameter(SyntaxEditor, SyntaxNode, SyntaxNode)
AddReturnAttribute(SyntaxEditor, SyntaxNode, SyntaxNode)
InsertMembers(SyntaxEditor, SyntaxNode, Int32, IEnumerable<SyntaxNode>)
InsertParameter(SyntaxEditor, SyntaxNode, Int32, SyntaxNode)
SetAccessibility(SyntaxEditor, SyntaxNode, Accessibility)
SetExpression(SyntaxEditor, SyntaxNode, SyntaxNode)
SetGetAccessorStatements(SyntaxEditor, SyntaxNode, IEnumerable<SyntaxNode>)
SetModifiers(SyntaxEditor, SyntaxNode, DeclarationModifiers)
SetName(SyntaxEditor, SyntaxNode, String)
SetSetAccessorStatements(SyntaxEditor, SyntaxNode, IEnumerable<SyntaxNode>)
SetStatements(SyntaxEditor, SyntaxNode, IEnumerable<SyntaxNode>)
SetType(SyntaxEditor, SyntaxNode, SyntaxNode)
SetTypeConstraint(SyntaxEditor, SyntaxNode, String, SpecialTypeConstraintKind, IEnumerable<SyntaxNode>)
SetTypeParameters(SyntaxEditor, SyntaxNode, IEnumerable<String>)

Applies to