本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.GetCurrentNode方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.GetCurrentNode方法的具体用法?C# SyntaxNode.GetCurrentNode怎么用?C# SyntaxNode.GetCurrentNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.GetCurrentNode方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MakeAutoPropertyAsync
public async static Task<Solution> MakeAutoPropertyAsync(Document document, SyntaxNode root, PropertyDeclarationSyntax property, CancellationToken cancellationToken)
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
var getterReturn = (ReturnStatementSyntax)property.AccessorList.Accessors.First(a => a.Keyword.ValueText == "get").Body.Statements.First();
var returnIdentifier = (IdentifierNameSyntax)(getterReturn.Expression is MemberAccessExpressionSyntax ? ((MemberAccessExpressionSyntax)getterReturn.Expression).Name : getterReturn.Expression);
var returnIdentifierSymbol = semanticModel.GetSymbolInfo(returnIdentifier).Symbol;
var variableDeclarator = (VariableDeclaratorSyntax)returnIdentifierSymbol.DeclaringSyntaxReferences.First().GetSyntax();
var fieldDeclaration = variableDeclarator.FirstAncestorOfType<FieldDeclarationSyntax>();
root = root.TrackNodes(returnIdentifier, fieldDeclaration, property);
document = document.WithSyntaxRoot(root);
root = await document.GetSyntaxRootAsync(cancellationToken);
semanticModel = await document.GetSemanticModelAsync(cancellationToken);
returnIdentifier = root.GetCurrentNode(returnIdentifier);
returnIdentifierSymbol = semanticModel.GetSymbolInfo(returnIdentifier).Symbol;
var newProperty = GetSimpleProperty(property, variableDeclarator)
.WithTriviaFrom(property)
.WithAdditionalAnnotations(Formatter.Annotation);
var newSolution = await Renamer.RenameSymbolAsync(document.Project.Solution, returnIdentifierSymbol, property.Identifier.ValueText, document.Project.Solution.Workspace.Options, cancellationToken);
document = newSolution.GetDocument(document.Id);
root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
root = root.InsertNodesAfter(root.GetCurrentNode(property), new[] { newProperty });
var multipleVariableDeclaration = fieldDeclaration.Declaration.Variables.Count > 1;
if (multipleVariableDeclaration)
{
var newfieldDeclaration = fieldDeclaration.WithDeclaration(fieldDeclaration.Declaration.RemoveNode(variableDeclarator, SyntaxRemoveOptions.KeepNoTrivia));
root = root.RemoveNode(root.GetCurrentNode<SyntaxNode>(property), SyntaxRemoveOptions.KeepNoTrivia);
root = root.ReplaceNode(root.GetCurrentNode(fieldDeclaration), newfieldDeclaration);
}
else
{
root = root.RemoveNodes(root.GetCurrentNodes<SyntaxNode>(new SyntaxNode[] { fieldDeclaration, property }), SyntaxRemoveOptions.KeepNoTrivia);
}
document = document.WithSyntaxRoot(root);
return document.Project.Solution;
}
示例2: AddStatementToConstructorBody
internal static SyntaxNode AddStatementToConstructorBody(SyntaxNode root, ConstructorDeclarationSyntax constructor, StatementSyntax statement)
{
var body = constructor.Body ?? SyntaxFactory.Block();
return root.ReplaceNode(root.GetCurrentNode(constructor), constructor.WithBody(
body.WithStatements(SyntaxFactory.List(new[] { statement }.Concat(body.Statements)))
));
}
示例3: 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;
}
示例4: RemoveField
private static SyntaxNode RemoveField(SyntaxNode root, VariableDeclaratorSyntax variableDeclarator, FieldDeclarationSyntax fieldDeclaration)
{
var currentField = root.GetCurrentNode(fieldDeclaration);
var multipleVariableDeclaration = fieldDeclaration.Declaration.Variables.Count > 1;
root = multipleVariableDeclaration
? root.ReplaceNode(currentField, fieldDeclaration
.WithDeclaration(fieldDeclaration.Declaration.RemoveNode(variableDeclarator, SyntaxRemoveOptions.KeepNoTrivia)))
: root.RemoveNode(currentField, SyntaxRemoveOptions.KeepNoTrivia);
return root;
}
示例5: AddBefore
internal static SyntaxNode AddBefore(SyntaxNode root, SyntaxNode loationToAddBefore, SyntaxNode nodeToAdd)
{
return root.InsertNodesBefore(
root.GetCurrentNode(loationToAddBefore)
, new List<SyntaxNode> { nodeToAdd });
}
示例6: ProcessObjectInitializer
private static decimal ProcessObjectInitializer(ObjectCreationExpressionSyntax target,
decimal lastTemporaryIndex, ref SyntaxNode newRoot, ref SyntaxNode[] originalObjectExpressionSyntax,
ref IEnumerable<KeyValuePair<SyntaxNode, SyntaxNode>> replacedNodes, int depth = 0)
{
depth++;
var objectCreation = target;
if (objectCreation.Initializer.IsKind(SyntaxKind.ObjectInitializerExpression))
{
var initializerInfo = GetModel(target).GetTypeInfo(target);
var tempName = initializerInfo.Type.Name + "_helper_" + lastTemporaryIndex++;
var assignments = new List<SyntaxNode>();
foreach (var expression in objectCreation.Initializer.Expressions)
{
var localExpression = (expression as AssignmentExpressionSyntax);
if (localExpression == null)
continue;
var right = localExpression.Right;
var targets =
expression.DescendantNodes()
.OfType<ObjectCreationExpressionSyntax>()
.Where(k => k.Ancestors().OfType<ObjectCreationExpressionSyntax>().First() == target);
var objectCreationExpressionSyntaxs = targets as ObjectCreationExpressionSyntax[] ??
targets.ToArray();
var noriginalObjectExpressionSyntax =
objectCreationExpressionSyntaxs.ToArray<SyntaxNode>().ToArray();
foreach (var ntarget in noriginalObjectExpressionSyntax)
{
lastTemporaryIndex = ProcessObjectInitializer((ObjectCreationExpressionSyntax)ntarget,
lastTemporaryIndex, ref newRoot,
ref originalObjectExpressionSyntax, ref replacedNodes, depth);
}
if (noriginalObjectExpressionSyntax.Any())
{
bool contained;
try
{
newRoot.GetCurrentNode(right);
contained = true;
}
catch (Exception)
{
contained = false;
}
if (contained)
{
if (right is MemberAccessExpressionSyntax) // Since Roslyn 1.0.0, old method doesnt work ...
{
var mar = right as MemberAccessExpressionSyntax;
contained = !(mar.Expression.ToString() == replacedNodes.Last().Key.ToString());
}
else if (right is ObjectCreationExpressionSyntax)
{
var mar = right as ObjectCreationExpressionSyntax;
contained =
!mar.Initializer.DescendantNodes().OfType<ObjectCreationExpressionSyntax>().Any();
//!(mar.ToString () == replacedNodes.Last ().Key.ToString ());
}
}
if (!contained || replacedNodes.Count() == 1) // We been replaced
{
try
{
var str = replacedNodes.Last().Value.ToFullString();
if (right is MemberAccessExpressionSyntax)
{
var mar = right as MemberAccessExpressionSyntax;
right = SyntaxFactory.ParseExpression(str + "." + mar.Name.ToFullString());
}
else
{
// for
// replacedNodes.Any(k => k.Key == right);
right = SyntaxFactory.ParseExpression(str);
// (ExpressionSyntax)replacedNodes.FirstOrDefault(o => o.Key.ToFullString() == right.ToFullString()).Value;
}
replacedNodes = replacedNodes.Except(replacedNodes.Last());
}
catch (Exception)
{
// throw;
}
}
}
//.........这里部分代码省略.........