SyntaxNodeExtensions Class

Operations that return the same node type they were given.

public static class SyntaxNodeExtensions

Remarks

These are extensions rather than members so that the result keeps its type: editing a document returns a document, not the abstract node type, and no cast is needed at the call site.

Methods

NameDescription
CopyAnnotationsTo<TNode>(SyntaxNode, TNode)Returns to carrying the annotations of from as well as its own.
InsertNodesAfter<TRoot>(TRoot, SyntaxNode, IEnumerable<SyntaxNode>)Returns root with newNodes added after nodeInList.
InsertNodesBefore<TRoot>(TRoot, SyntaxNode, IEnumerable<SyntaxNode>)Returns root with newNodes added in front of nodeInList.
RemoveNode<TRoot>(TRoot, SyntaxNode, SyntaxRemoveOptions)Returns root without node.
RemoveNodes<TRoot>(TRoot, IEnumerable<SyntaxNode>, SyntaxRemoveOptions)Returns root without nodes.
ReplaceNode<TRoot>(TRoot, SyntaxNode, SyntaxNode)Returns root with newNode in place of oldNode.
ReplaceNode<TRoot>(TRoot, SyntaxNode, IEnumerable<SyntaxNode>)Returns root with newNodes in place of oldNode.
ReplaceNodes<TRoot, TNode>(TRoot, IEnumerable<TNode>, Func<TNode, TNode, SyntaxNode>)Returns root with each of nodes replaced by what computeReplacement returns for it.
ReplaceToken<TRoot>(TRoot, SyntaxToken, SyntaxToken)Returns root with newToken in place of oldToken.
ReplaceTokens<TRoot>(TRoot, IEnumerable<SyntaxToken>, Func<SyntaxToken, SyntaxToken, SyntaxToken>)Returns root with each of tokens replaced by what computeReplacement returns for it.
ReplaceTrivia<TRoot>(TRoot, SyntaxTrivia, SyntaxTrivia)Returns root with newTrivia in place of oldTrivia.
ReplaceTrivia<TRoot>(TRoot, IEnumerable<SyntaxTrivia>, Func<SyntaxTrivia, SyntaxTrivia, SyntaxTrivia>)Returns root with each of trivia replaced by what computeReplacement returns for it.
WithAdditionalAnnotations<TNode>(TNode, SyntaxAnnotation[])Returns node carrying annotations as well as the ones it already has.
WithLeadingTrivia<TNode>(TNode, SyntaxTrivia[])Returns node with trivia in front of its first token.
WithLeadingTrivia<TNode>(TNode, IEnumerable<SyntaxTrivia>?)Returns node with trivia in front of its first token.
WithTrailingTrivia<TNode>(TNode, SyntaxTrivia[])Returns node with trivia after its last token.
WithTrailingTrivia<TNode>(TNode, IEnumerable<SyntaxTrivia>?)Returns node with trivia after its last token.
WithTriviaFrom<TNode>(TNode, SyntaxNode)Returns node surrounded by the trivia that surrounds source.
WithoutAnnotations<TNode>(TNode, SyntaxAnnotation[])Returns node without annotations.
WithoutAnnotations<TNode>(TNode, string)Returns node without any annotation of the given kind.

CopyAnnotationsTo<TNode>(SyntaxNode, TNode)

SyntaxNodeExtensions.cs

Returns to carrying the annotations of from as well as its own.

public static TNode CopyAnnotationsTo<TNode>(this SyntaxNode from, TNode to) where TNode : SyntaxNode

Type parameters

TNode

Parameters

from SyntaxNode
to TNode

Returns

TNode

Exceptions

ArgumentNullException
from or to is null.

InsertNodesAfter<TRoot>(TRoot, SyntaxNode, IEnumerable<SyntaxNode>)

SyntaxNodeExtensions.cs

Returns root with newNodes added after nodeInList.

public static TRoot InsertNodesAfter<TRoot>(this TRoot root, SyntaxNode nodeInList, IEnumerable<SyntaxNode> newNodes) where TRoot : SyntaxNode

Type parameters

TRoot

Parameters

root TRoot
nodeInList SyntaxNode
newNodes IEnumerable<SyntaxNode>

Returns

TRoot

Exceptions

ArgumentNullException
Any argument is null.
ArgumentException
nodeInList is not part of root, or is not in a list.

InsertNodesBefore<TRoot>(TRoot, SyntaxNode, IEnumerable<SyntaxNode>)

SyntaxNodeExtensions.cs

Returns root with newNodes added in front of nodeInList.

public static TRoot InsertNodesBefore<TRoot>(this TRoot root, SyntaxNode nodeInList, IEnumerable<SyntaxNode> newNodes) where TRoot : SyntaxNode

Type parameters

TRoot

Parameters

root TRoot
nodeInList SyntaxNode
newNodes IEnumerable<SyntaxNode>

Returns

TRoot

Exceptions

ArgumentNullException
Any argument is null.
ArgumentException
nodeInList is not part of root, or is not in a list.

RemoveNode<TRoot>(TRoot, SyntaxNode, SyntaxRemoveOptions)

SyntaxNodeExtensions.cs

Returns root without node.

public static TRoot RemoveNode<TRoot>(this TRoot root, SyntaxNode node, SyntaxRemoveOptions options) where TRoot : SyntaxNode

Type parameters

TRoot

Parameters

root TRoot
The tree to rebuild.
node SyntaxNode
The node to take out. Its separator goes with it when it sits in a separated list.
options SyntaxRemoveOptions
What to keep of the trivia around it. Anything kept moves onto what now stands in its place.

Returns

TRoot

Exceptions

ArgumentNullException
Any argument is null.
ArgumentException
node is not part of root.

RemoveNodes<TRoot>(TRoot, IEnumerable<SyntaxNode>, SyntaxRemoveOptions)

SyntaxNodeExtensions.cs

Returns root without nodes.

public static TRoot RemoveNodes<TRoot>(this TRoot root, IEnumerable<SyntaxNode> nodes, SyntaxRemoveOptions options) where TRoot : SyntaxNode

Type parameters

TRoot

Parameters

root TRoot
The tree to rebuild.
nodes IEnumerable<SyntaxNode>
The nodes to take out. Naming both a node and something inside it removes the node, once.
options SyntaxRemoveOptions
What to keep of the trivia around them.

Returns

TRoot

Exceptions

ArgumentNullException
Any argument is null.
ArgumentException
One of nodes is not part of root.

ReplaceNode overloads

ReplaceNode<TRoot>(TRoot, SyntaxNode, SyntaxNode)

SyntaxNodeExtensions.cs

Returns root with newNode in place of oldNode.

public static TRoot ReplaceNode<TRoot>(this TRoot root, SyntaxNode oldNode, SyntaxNode newNode) where TRoot : SyntaxNode
Type parameters
TRoot
Parameters
root TRoot
oldNode SyntaxNode
newNode SyntaxNode
Returns

TRoot

Exceptions
ArgumentNullException
Any argument is null.
ArgumentException
oldNode is not part of root.
Remarks

The result is a new tree that shares every part of the old one that did not change, and it is the same type as root, so editing a document gives back a document. Nothing is re-parsed.

ReplaceNode<TRoot>(TRoot, SyntaxNode, IEnumerable<SyntaxNode>)

SyntaxNodeExtensions.cs

Returns root with newNodes in place of oldNode.

public static TRoot ReplaceNode<TRoot>(this TRoot root, SyntaxNode oldNode, IEnumerable<SyntaxNode> newNodes) where TRoot : SyntaxNode
Type parameters
TRoot
Parameters
root TRoot
oldNode SyntaxNode
newNodes IEnumerable<SyntaxNode>
Returns

TRoot

Exceptions
ArgumentNullException
Any argument is null.
ArgumentException
oldNode is not part of root, or is not somewhere several nodes can go.
Remarks

Replacing one node with several requires that it sits in a list, even one holding nothing but it.

ReplaceNodes<TRoot, TNode>(TRoot, IEnumerable<TNode>, Func<TNode, TNode, SyntaxNode>)

SyntaxNodeExtensions.cs

Returns root with each of nodes replaced by what computeReplacement returns for it.

public static TRoot ReplaceNodes<TRoot, TNode>(this TRoot root, IEnumerable<TNode> nodes, Func<TNode, TNode, SyntaxNode> computeReplacement) where TRoot : SyntaxNode where TNode : SyntaxNode

Type parameters

TRoot
TNode

Parameters

root TRoot
The tree to rebuild.
nodes IEnumerable<TNode>
The nodes to replace.
computeReplacement Func<TNode, TNode, SyntaxNode>
Given the original node twice, returns what to put in its place.

Returns

TRoot

Exceptions

ArgumentNullException
Any argument is null.
ArgumentException
One of nodes is not part of root.

Remarks

An empty sequence asks for nothing, and gives root back unchanged.

ReplaceToken<TRoot>(TRoot, SyntaxToken, SyntaxToken)

SyntaxNodeExtensions.cs

Returns root with newToken in place of oldToken.

public static TRoot ReplaceToken<TRoot>(this TRoot root, SyntaxToken oldToken, SyntaxToken newToken) where TRoot : SyntaxNode

Type parameters

TRoot

Parameters

root TRoot
oldToken SyntaxToken
newToken SyntaxToken

Returns

TRoot

Exceptions

ArgumentNullException
root is null.
ArgumentException
oldToken is not part of root.

ReplaceTokens<TRoot>(TRoot, IEnumerable<SyntaxToken>, Func<SyntaxToken, SyntaxToken, SyntaxToken>)

SyntaxNodeExtensions.cs

Returns root with each of tokens replaced by what computeReplacement returns for it.

public static TRoot ReplaceTokens<TRoot>(this TRoot root, IEnumerable<SyntaxToken> tokens, Func<SyntaxToken, SyntaxToken, SyntaxToken> computeReplacement) where TRoot : SyntaxNode

Type parameters

TRoot

Parameters

root TRoot
tokens IEnumerable<SyntaxToken>
computeReplacement Func<SyntaxToken, SyntaxToken, SyntaxToken>

Returns

TRoot

Exceptions

ArgumentNullException
Any argument is null.
ArgumentException
One of tokens is not part of root.

Remarks

An empty sequence asks for nothing, and gives root back unchanged.

ReplaceTrivia overloads

ReplaceTrivia<TRoot>(TRoot, SyntaxTrivia, SyntaxTrivia)

SyntaxNodeExtensions.cs

Returns root with newTrivia in place of oldTrivia.

public static TRoot ReplaceTrivia<TRoot>(this TRoot root, SyntaxTrivia oldTrivia, SyntaxTrivia newTrivia) where TRoot : SyntaxNode
Type parameters
TRoot
Parameters
root TRoot
oldTrivia SyntaxTrivia
newTrivia SyntaxTrivia
Returns

TRoot

Exceptions
ArgumentNullException
root is null.
ArgumentException
oldTrivia is not part of root.

ReplaceTrivia<TRoot>(TRoot, IEnumerable<SyntaxTrivia>, Func<SyntaxTrivia, SyntaxTrivia, SyntaxTrivia>)

SyntaxNodeExtensions.cs

Returns root with each of trivia replaced by what computeReplacement returns for it.

public static TRoot ReplaceTrivia<TRoot>(this TRoot root, IEnumerable<SyntaxTrivia> trivia, Func<SyntaxTrivia, SyntaxTrivia, SyntaxTrivia> computeReplacement) where TRoot : SyntaxNode
Type parameters
TRoot
Parameters
root TRoot
trivia IEnumerable<SyntaxTrivia>
computeReplacement Func<SyntaxTrivia, SyntaxTrivia, SyntaxTrivia>
Returns

TRoot

Exceptions
ArgumentNullException
Any argument is null.
ArgumentException
One of trivia is not part of root.
Remarks

An empty sequence asks for nothing, and gives root back unchanged.

WithAdditionalAnnotations<TNode>(TNode, SyntaxAnnotation[])

SyntaxNodeExtensions.cs

Returns node carrying annotations as well as the ones it already has.

public static TNode WithAdditionalAnnotations<TNode>(this TNode node, params SyntaxAnnotation[] annotations) where TNode : SyntaxNode

Type parameters

TNode

Parameters

node TNode
annotations SyntaxAnnotation[]

Returns

TNode

Exceptions

ArgumentNullException
node or annotations is null.

WithLeadingTrivia overloads

WithLeadingTrivia<TNode>(TNode, SyntaxTrivia[])

SyntaxNodeExtensions.cs

Returns node with trivia in front of its first token.

public static TNode WithLeadingTrivia<TNode>(this TNode node, params SyntaxTrivia[] trivia) where TNode : SyntaxNode
Type parameters
TNode
Parameters
node TNode
trivia SyntaxTrivia[]
Returns

TNode

Exceptions
ArgumentNullException
node is null.

WithLeadingTrivia<TNode>(TNode, IEnumerable<SyntaxTrivia>?)

SyntaxNodeExtensions.cs

Returns node with trivia in front of its first token.

public static TNode WithLeadingTrivia<TNode>(this TNode node, IEnumerable<SyntaxTrivia>? trivia) where TNode : SyntaxNode
Type parameters
TNode
Parameters
node TNode
trivia IEnumerable<SyntaxTrivia>?
Returns

TNode

Exceptions
ArgumentNullException
node is null.

WithTrailingTrivia overloads

WithTrailingTrivia<TNode>(TNode, SyntaxTrivia[])

SyntaxNodeExtensions.cs

Returns node with trivia after its last token.

public static TNode WithTrailingTrivia<TNode>(this TNode node, params SyntaxTrivia[] trivia) where TNode : SyntaxNode
Type parameters
TNode
Parameters
node TNode
trivia SyntaxTrivia[]
Returns

TNode

Exceptions
ArgumentNullException
node is null.

WithTrailingTrivia<TNode>(TNode, IEnumerable<SyntaxTrivia>?)

SyntaxNodeExtensions.cs

Returns node with trivia after its last token.

public static TNode WithTrailingTrivia<TNode>(this TNode node, IEnumerable<SyntaxTrivia>? trivia) where TNode : SyntaxNode
Type parameters
TNode
Parameters
node TNode
trivia IEnumerable<SyntaxTrivia>?
Returns

TNode

Exceptions
ArgumentNullException
node is null.

WithTriviaFrom<TNode>(TNode, SyntaxNode)

SyntaxNodeExtensions.cs

Returns node surrounded by the trivia that surrounds source.

public static TNode WithTriviaFrom<TNode>(this TNode node, SyntaxNode source) where TNode : SyntaxNode

Type parameters

TNode

Parameters

node TNode
source SyntaxNode

Returns

TNode

Exceptions

ArgumentNullException
node or source is null.

Remarks

Replacing a node does not carry its trivia over, because the replacement may want its own. This is how a caller asks for the trivia of the node being replaced to be kept.

WithoutAnnotations overloads

WithoutAnnotations<TNode>(TNode, SyntaxAnnotation[])

SyntaxNodeExtensions.cs

Returns node without annotations.

public static TNode WithoutAnnotations<TNode>(this TNode node, params SyntaxAnnotation[] annotations) where TNode : SyntaxNode
Type parameters
TNode
Parameters
node TNode
annotations SyntaxAnnotation[]
Returns

TNode

Exceptions
ArgumentNullException
node or annotations is null.

WithoutAnnotations<TNode>(TNode, string)

SyntaxNodeExtensions.cs

Returns node without any annotation of the given kind.

public static TNode WithoutAnnotations<TNode>(this TNode node, string annotationKind) where TNode : SyntaxNode
Type parameters
TNode
Parameters
node TNode
annotationKind string
Returns

TNode

Exceptions
ArgumentNullException
node or annotationKind is null.