本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.InsertNodesBefore方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.InsertNodesBefore方法的具体用法?C# SyntaxNode.InsertNodesBefore怎么用?C# SyntaxNode.InsertNodesBefore使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.InsertNodesBefore方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Visit
public override SyntaxNode Visit(SyntaxNode node)
{
// Check each node until we find the property's parent. When we've found it, let's add a private field to it
if (_propertyParent.IsEquivalentTo(node))
{
var variableDeclarator = SyntaxFactory.VariableDeclarator(
SyntaxFactory.Identifier(this.PrivateFieldName));
if (_crunchedProperty.IsExpressionProperty())
{
variableDeclarator = variableDeclarator.WithInitializer(
SyntaxFactory.EqualsValueClause(
SyntaxFactory.Token(SyntaxKind.EqualsToken),
_crunchedProperty.ExpressionBody.Expression));
}
// Create the private "backing field" of the property
var privateField = SyntaxFactory.FieldDeclaration(
SyntaxFactory.VariableDeclaration(_crunchedProperty.Type, // Property Type
SyntaxFactory.SeparatedList(
new[] { variableDeclarator }))) // Field Name
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword)); // Modifier (always private)
// Insert the backing field just ahead of the original property
var newParent = node.InsertNodesBefore(_crunchedProperty, new[] { privateField });
return base.Visit(newParent); // Return the new parent which replaces the original.
}
return base.Visit(node);
}
示例2: MoveMember
private static SyntaxNode MoveMember(SyntaxNode syntaxRoot, MemberDeclarationSyntax member, MemberDeclarationSyntax targetMember, IndentationOptions indentationOptions)
{
var firstToken = syntaxRoot.GetFirstToken();
var fileHeader = GetFileHeader(firstToken.LeadingTrivia);
syntaxRoot = syntaxRoot.TrackNodes(member, targetMember, firstToken.Parent);
var memberToMove = syntaxRoot.GetCurrentNode(member);
var targetMemberTracked = syntaxRoot.GetCurrentNode(targetMember);
if (!memberToMove.HasLeadingTrivia)
{
var targetIndentationLevel = IndentationHelper.GetIndentationSteps(indentationOptions, targetMember);
var indentationString = IndentationHelper.GenerateIndentationString(indentationOptions, targetIndentationLevel);
memberToMove = memberToMove.WithLeadingTrivia(SyntaxFactory.Whitespace(indentationString));
}
if (!HasLeadingBlankLines(targetMember)
&& HasLeadingBlankLines(member))
{
memberToMove = memberToMove.WithTrailingTrivia(memberToMove.GetTrailingTrivia().Add(SyntaxFactory.CarriageReturnLineFeed));
memberToMove = memberToMove.WithLeadingTrivia(GetLeadingTriviaWithoutLeadingBlankLines(memberToMove));
}
syntaxRoot = syntaxRoot.InsertNodesBefore(targetMemberTracked, new[] { memberToMove });
var fieldToMoveTracked = syntaxRoot.GetCurrentNodes(member).Last();
syntaxRoot = syntaxRoot.RemoveNode(fieldToMoveTracked, SyntaxRemoveOptions.KeepNoTrivia);
if (fileHeader.Any())
{
var oldFirstToken = syntaxRoot.GetCurrentNode(firstToken.Parent).ChildTokens().First();
syntaxRoot = syntaxRoot.ReplaceToken(oldFirstToken, oldFirstToken.WithLeadingTrivia(StripFileHeader(oldFirstToken.LeadingTrivia)));
var newFirstToken = syntaxRoot.GetFirstToken();
syntaxRoot = syntaxRoot.ReplaceToken(newFirstToken, newFirstToken.WithLeadingTrivia(fileHeader.AddRange(newFirstToken.LeadingTrivia)));
}
return syntaxRoot;
}
示例3: AddBefore
internal static SyntaxNode AddBefore(SyntaxNode root, SyntaxNode loationToAddBefore, SyntaxNode nodeToAdd)
{
return root.InsertNodesBefore(
root.GetCurrentNode(loationToAddBefore)
, new List<SyntaxNode> { nodeToAdd });
}
示例4: ProcessObjectInitializer
//.........这里部分代码省略.........
}
else
{
newObjectCreation =
objectCreation.WithInitializer(null).WithArgumentList(objectCreation.ArgumentList);
}
var local = SyntaxFactory.LocalDeclarationStatement(
SyntaxFactory.VariableDeclaration(
target.Type,
SyntaxFactory.SeparatedList(new[]
{
SyntaxFactory.VariableDeclarator(
SyntaxFactory.Identifier(tempName),
null,
SyntaxFactory.EqualsValueClause(newObjectCreation))
})
)
).NormalizeWhitespace();
statements.Add(local);
statements.AddRange(assignments.Cast<StatementSyntax>());
var listS = SyntaxFactory.List(statements);
var newList2 = new List<ObjectCreationExpressionSyntax>();
var union2 = originalObjectExpressionSyntax.Union(new SyntaxNode[] { target });
foreach (var statementSyntax in union2)
{
try
{
newList2.Add((ObjectCreationExpressionSyntax)newRoot.GetCurrentNode(statementSyntax));
}
catch (Exception ex)
{
}
}
if (!newList2.Contains(target))
{
//
originalObjectExpressionSyntax =
originalObjectExpressionSyntax.Union(new SyntaxNode[] { target }).ToArray();
}
originalObjectExpressionSyntax = originalObjectExpressionSyntax.Where(l => l != null).ToArray();
newRoot = newRoot.TrackNodes(originalObjectExpressionSyntax);
try
{
var currentNode = newRoot.GetCurrentNode(target);
var lastStatement = currentNode.Ancestors().OfType<StatementSyntax>().First();
newRoot = newRoot.InsertNodesBefore(lastStatement, listS);
// var currentNodes = newRoot.GetCurrentNodes<SyntaxNode>(originalObjectExpressionSyntax);
newRoot = newRoot.TrackNodes(originalObjectExpressionSyntax);
currentNode = newRoot.GetCurrentNode(target);
var newName = SyntaxFactory.ParseExpression(tempName);
var nDict = new Dictionary<SyntaxNode, SyntaxNode>();
foreach (var replacedNode in replacedNodes)
nDict[replacedNode.Key] = replacedNode.Value;
nDict[currentNode] = newName;
replacedNodes = nDict;
// newRoot = newRoot;
newRoot = newRoot.ReplaceNode(currentNode, newName);
var newList = new List<ObjectCreationExpressionSyntax>();
var union = originalObjectExpressionSyntax.Union(nDict.Values); //.Union(new SyntaxNode[]{newName});
foreach (var statementSyntax in union)
{
try
{
// if(statementSyntax==newName)
// {
// newList.Add((ObjectCreationExpressionSyntax) (newName));
// continue;
// }
var syntaxNode = newRoot.GetCurrentNode(statementSyntax);
newList.Add((ObjectCreationExpressionSyntax)syntaxNode);
}
catch (Exception ex)
{
}
}
originalObjectExpressionSyntax = newList.ToArray();
newRoot = newRoot.TrackNodes(originalObjectExpressionSyntax);
}
catch (Exception ex)
{
}
}
return lastTemporaryIndex;
}