SyntaxNodeExtensions Class
Operations that return the same node type they were given.
public static class SyntaxNodeExtensionsRemarks
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
CopyAnnotationsTo<TNode>(SyntaxNode, TNode)
SyntaxNodeExtensions.csReturns to carrying the annotations of from as well as its own.
public static TNode CopyAnnotationsTo<TNode>(this SyntaxNode from, TNode to) where TNode : SyntaxNodeType parameters
TNode
Parameters
fromSyntaxNodetoTNode
Returns
TNode
Exceptions
ArgumentNullExceptionfromortoisnull.
InsertNodesAfter<TRoot>(TRoot, SyntaxNode, IEnumerable<SyntaxNode>)
SyntaxNodeExtensions.csReturns root with newNodes added after nodeInList.
public static TRoot InsertNodesAfter<TRoot>(this TRoot root, SyntaxNode nodeInList, IEnumerable<SyntaxNode> newNodes) where TRoot : SyntaxNodeType parameters
TRoot
Parameters
rootTRootnodeInListSyntaxNodenewNodesIEnumerable<SyntaxNode>
Returns
TRoot
Exceptions
ArgumentNullException- Any argument is
null. ArgumentExceptionnodeInListis not part ofroot, or is not in a list.
InsertNodesBefore<TRoot>(TRoot, SyntaxNode, IEnumerable<SyntaxNode>)
SyntaxNodeExtensions.csReturns root with newNodes added in front of nodeInList.
public static TRoot InsertNodesBefore<TRoot>(this TRoot root, SyntaxNode nodeInList, IEnumerable<SyntaxNode> newNodes) where TRoot : SyntaxNodeType parameters
TRoot
Parameters
rootTRootnodeInListSyntaxNodenewNodesIEnumerable<SyntaxNode>
Returns
TRoot
Exceptions
ArgumentNullException- Any argument is
null. ArgumentExceptionnodeInListis not part ofroot, or is not in a list.
RemoveNode<TRoot>(TRoot, SyntaxNode, SyntaxRemoveOptions)
SyntaxNodeExtensions.csReturns root without node.
public static TRoot RemoveNode<TRoot>(this TRoot root, SyntaxNode node, SyntaxRemoveOptions options) where TRoot : SyntaxNodeType parameters
TRoot
Parameters
rootTRoot- The tree to rebuild.
nodeSyntaxNode- The node to take out. Its separator goes with it when it sits in a separated list.
optionsSyntaxRemoveOptions- 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. ArgumentExceptionnodeis not part ofroot.
RemoveNodes<TRoot>(TRoot, IEnumerable<SyntaxNode>, SyntaxRemoveOptions)
SyntaxNodeExtensions.csReturns root without nodes.
public static TRoot RemoveNodes<TRoot>(this TRoot root, IEnumerable<SyntaxNode> nodes, SyntaxRemoveOptions options) where TRoot : SyntaxNodeType parameters
TRoot
Parameters
rootTRoot- The tree to rebuild.
nodesIEnumerable<SyntaxNode>- The nodes to take out. Naming both a node and something inside it removes the node, once.
optionsSyntaxRemoveOptions- What to keep of the trivia around them.
Returns
TRoot
Exceptions
ArgumentNullException- Any argument is
null. ArgumentException- One of
nodesis not part ofroot.
ReplaceNode overloads
ReplaceNode<TRoot>(TRoot, SyntaxNode, SyntaxNode)
SyntaxNodeExtensions.csReturns root with newNode in place of oldNode.
public static TRoot ReplaceNode<TRoot>(this TRoot root, SyntaxNode oldNode, SyntaxNode newNode) where TRoot : SyntaxNodeType parameters
TRoot
Parameters
rootTRootoldNodeSyntaxNodenewNodeSyntaxNode
Returns
TRoot
Exceptions
ArgumentNullException- Any argument is
null. ArgumentExceptionoldNodeis not part ofroot.
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.csReturns root with newNodes in place of oldNode.
public static TRoot ReplaceNode<TRoot>(this TRoot root, SyntaxNode oldNode, IEnumerable<SyntaxNode> newNodes) where TRoot : SyntaxNodeType parameters
TRoot
Parameters
rootTRootoldNodeSyntaxNodenewNodesIEnumerable<SyntaxNode>
Returns
TRoot
Exceptions
ArgumentNullException- Any argument is
null. ArgumentExceptionoldNodeis not part ofroot, 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.csReturns 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 : SyntaxNodeType parameters
TRootTNode
Parameters
rootTRoot- The tree to rebuild.
nodesIEnumerable<TNode>- The nodes to replace.
computeReplacementFunc<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
nodesis not part ofroot.
Remarks
An empty sequence asks for nothing, and gives root back unchanged.
ReplaceToken<TRoot>(TRoot, SyntaxToken, SyntaxToken)
SyntaxNodeExtensions.csReturns root with newToken in place of oldToken.
public static TRoot ReplaceToken<TRoot>(this TRoot root, SyntaxToken oldToken, SyntaxToken newToken) where TRoot : SyntaxNodeType parameters
TRoot
Parameters
rootTRootoldTokenSyntaxTokennewTokenSyntaxToken
Returns
TRoot
Exceptions
ArgumentNullExceptionrootisnull.ArgumentExceptionoldTokenis not part ofroot.
ReplaceTokens<TRoot>(TRoot, IEnumerable<SyntaxToken>, Func<SyntaxToken, SyntaxToken, SyntaxToken>)
SyntaxNodeExtensions.csReturns 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 : SyntaxNodeType parameters
TRoot
Parameters
rootTRoottokensIEnumerable<SyntaxToken>computeReplacementFunc<SyntaxToken, SyntaxToken, SyntaxToken>
Returns
TRoot
Exceptions
ArgumentNullException- Any argument is
null. ArgumentException- One of
tokensis not part ofroot.
Remarks
An empty sequence asks for nothing, and gives root back unchanged.
ReplaceTrivia overloads
ReplaceTrivia<TRoot>(TRoot, SyntaxTrivia, SyntaxTrivia)
SyntaxNodeExtensions.csReturns root with newTrivia in place of oldTrivia.
public static TRoot ReplaceTrivia<TRoot>(this TRoot root, SyntaxTrivia oldTrivia, SyntaxTrivia newTrivia) where TRoot : SyntaxNodeType parameters
TRoot
Parameters
rootTRootoldTriviaSyntaxTrivianewTriviaSyntaxTrivia
Returns
TRoot
Exceptions
ArgumentNullExceptionrootisnull.ArgumentExceptionoldTriviais not part ofroot.
ReplaceTrivia<TRoot>(TRoot, IEnumerable<SyntaxTrivia>, Func<SyntaxTrivia, SyntaxTrivia, SyntaxTrivia>)
SyntaxNodeExtensions.csReturns 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 : SyntaxNodeType parameters
TRoot
Parameters
rootTRoottriviaIEnumerable<SyntaxTrivia>computeReplacementFunc<SyntaxTrivia, SyntaxTrivia, SyntaxTrivia>
Returns
TRoot
Exceptions
ArgumentNullException- Any argument is
null. ArgumentException- One of
triviais not part ofroot.
Remarks
An empty sequence asks for nothing, and gives root back unchanged.
WithAdditionalAnnotations<TNode>(TNode, SyntaxAnnotation[])
SyntaxNodeExtensions.csReturns node carrying annotations as well as the ones it already has.
public static TNode WithAdditionalAnnotations<TNode>(this TNode node, params SyntaxAnnotation[] annotations) where TNode : SyntaxNodeType parameters
TNode
Parameters
nodeTNodeannotationsSyntaxAnnotation[]
Returns
TNode
Exceptions
ArgumentNullExceptionnodeorannotationsisnull.
WithLeadingTrivia overloads
WithLeadingTrivia<TNode>(TNode, SyntaxTrivia[])
SyntaxNodeExtensions.csReturns node with trivia in front of its first token.
public static TNode WithLeadingTrivia<TNode>(this TNode node, params SyntaxTrivia[] trivia) where TNode : SyntaxNodeType parameters
TNode
Parameters
nodeTNodetriviaSyntaxTrivia[]
Returns
TNode
Exceptions
ArgumentNullExceptionnodeisnull.
WithLeadingTrivia<TNode>(TNode, IEnumerable<SyntaxTrivia>?)
SyntaxNodeExtensions.csReturns node with trivia in front of its first token.
public static TNode WithLeadingTrivia<TNode>(this TNode node, IEnumerable<SyntaxTrivia>? trivia) where TNode : SyntaxNodeType parameters
TNode
Parameters
nodeTNodetriviaIEnumerable<SyntaxTrivia>?
Returns
TNode
Exceptions
ArgumentNullExceptionnodeisnull.
WithTrailingTrivia overloads
WithTrailingTrivia<TNode>(TNode, SyntaxTrivia[])
SyntaxNodeExtensions.csReturns node with trivia after its last token.
public static TNode WithTrailingTrivia<TNode>(this TNode node, params SyntaxTrivia[] trivia) where TNode : SyntaxNodeType parameters
TNode
Parameters
nodeTNodetriviaSyntaxTrivia[]
Returns
TNode
Exceptions
ArgumentNullExceptionnodeisnull.
WithTrailingTrivia<TNode>(TNode, IEnumerable<SyntaxTrivia>?)
SyntaxNodeExtensions.csReturns node with trivia after its last token.
public static TNode WithTrailingTrivia<TNode>(this TNode node, IEnumerable<SyntaxTrivia>? trivia) where TNode : SyntaxNodeType parameters
TNode
Parameters
nodeTNodetriviaIEnumerable<SyntaxTrivia>?
Returns
TNode
Exceptions
ArgumentNullExceptionnodeisnull.
WithTriviaFrom<TNode>(TNode, SyntaxNode)
SyntaxNodeExtensions.csReturns node surrounded by the trivia that surrounds source.
public static TNode WithTriviaFrom<TNode>(this TNode node, SyntaxNode source) where TNode : SyntaxNodeType parameters
TNode
Parameters
nodeTNodesourceSyntaxNode
Returns
TNode
Exceptions
ArgumentNullExceptionnodeorsourceisnull.
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.csReturns node without annotations.
public static TNode WithoutAnnotations<TNode>(this TNode node, params SyntaxAnnotation[] annotations) where TNode : SyntaxNodeType parameters
TNode
Parameters
nodeTNodeannotationsSyntaxAnnotation[]
Returns
TNode
Exceptions
ArgumentNullExceptionnodeorannotationsisnull.
WithoutAnnotations<TNode>(TNode, string)
SyntaxNodeExtensions.csReturns node without any annotation of the given kind.
public static TNode WithoutAnnotations<TNode>(this TNode node, string annotationKind) where TNode : SyntaxNodeType parameters
TNode
Parameters
nodeTNodeannotationKindstring
Returns
TNode
Exceptions
ArgumentNullExceptionnodeorannotationKindisnull.