本文整理汇总了C#中SyntaxNode.ReplaceNode方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.ReplaceNode方法的具体用法?C# SyntaxNode.ReplaceNode怎么用?C# SyntaxNode.ReplaceNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.ReplaceNode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetUpdatedDocumentAsync
internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
{
// if nothing can be fixed, return the unchanged node
var newRoot = root;
var kind = nodeToFix.CSharpKind();
var syntaxFactoryService = document.GetLanguageService<SyntaxGenerator>();
switch (kind)
{
case SyntaxKind.Argument:
// StringComparison.CurrentCulture => StringComparison.Ordinal
// StringComparison.CurrentCultureIgnoreCase => StringComparison.OrdinalIgnoreCase
var argument = (ArgumentSyntax)nodeToFix;
var memberAccess = argument.Expression as MemberAccessExpressionSyntax;
if (memberAccess != null)
{
// preserve the "IgnoreCase" suffix if present
bool isIgnoreCase = memberAccess.Name.GetText().ToString().EndsWith(CA1309DiagnosticAnalyzer.IgnoreCaseText);
var newOrdinalText = isIgnoreCase ? CA1309DiagnosticAnalyzer.OrdinalIgnoreCaseText : CA1309DiagnosticAnalyzer.OrdinalText;
var newIdentifier = syntaxFactoryService.IdentifierName(newOrdinalText);
var newMemberAccess = memberAccess.WithName((SimpleNameSyntax)newIdentifier).WithAdditionalAnnotations(Formatter.Annotation);
newRoot = root.ReplaceNode(memberAccess, newMemberAccess);
}
break;
case SyntaxKind.IdentifierName:
// string.Equals(a, b) => string.Equals(a, b, StringComparison.Ordinal)
// string.Compare(a, b) => string.Compare(a, b, StringComparison.Ordinal)
var identifier = (IdentifierNameSyntax)nodeToFix;
var invokeParent = identifier.GetAncestor<InvocationExpressionSyntax>();
if (invokeParent != null)
{
var methodSymbol = model.GetSymbolInfo(identifier).Symbol as IMethodSymbol;
if (methodSymbol != null && CanAddStringComparison(methodSymbol))
{
// append a new StringComparison.Ordinal argument
var newArg = syntaxFactoryService.Argument(CreateOrdinalMemberAccess(syntaxFactoryService, model))
.WithAdditionalAnnotations(Formatter.Annotation);
var newInvoke = invokeParent.AddArgumentListArguments((ArgumentSyntax)newArg).WithAdditionalAnnotations(Formatter.Annotation);
newRoot = root.ReplaceNode(invokeParent, newInvoke);
}
}
break;
case SyntaxKind.EqualsExpression:
case SyntaxKind.NotEqualsExpression:
// "a == b" => "string.Equals(a, b, StringComparison.Ordinal)"
// "a != b" => "!string.Equals(a, b, StringComparison.Ordinal)"
var binaryExpression = (BinaryExpressionSyntax)nodeToFix;
var invocation = CreateEqualsExpression(syntaxFactoryService, model, binaryExpression.Left, binaryExpression.Right, kind == SyntaxKind.EqualsExpression).WithAdditionalAnnotations(Formatter.Annotation);
newRoot = root.ReplaceNode(nodeToFix, invocation);
break;
}
if (newRoot == root)
{
return Task.FromResult(document);
}
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
示例2: GetNewRoot
protected override Task<SyntaxNode> GetNewRoot(SyntaxNode root, SyntaxNode oldNode, SemanticModel semanticModel, Diagnostic diagnostic, Document document, CancellationToken cancellationToken)
{
var expression = oldNode as ExpressionSyntax;
switch (diagnostic.Id)
{
case CS4014:
if (expression == null)
{
return Task.FromResult<SyntaxNode>(null);
}
return Task.FromResult(root.ReplaceNode(oldNode, ConvertToAwaitExpression(expression)));
case CS4016:
if (expression == null)
{
return Task.FromResult<SyntaxNode>(null);
}
if (!IsCorrectReturnType(expression, semanticModel))
{
return Task.FromResult<SyntaxNode>(null);
}
return Task.FromResult(root.ReplaceNode(oldNode, ConvertToAwaitExpression(expression)));
default:
return Task.FromResult<SyntaxNode>(null);
}
}
示例3: GetUpdatedDocumentAsync
internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var charSetType = WellKnownTypes.CharSet(model.Compilation);
var dllImportType = WellKnownTypes.DllImportAttribute(model.Compilation);
var marshalAsType = WellKnownTypes.MarshalAsAttribute(model.Compilation);
var unmanagedType = WellKnownTypes.UnmanagedType(model.Compilation);
if (charSetType == null || dllImportType == null || marshalAsType == null || unmanagedType == null)
{
return Task.FromResult(document);
}
var syntaxFactoryService = document.Project.LanguageServices.GetService<SyntaxGenerator>();
// return the unchanged root if no fix is available
var newRoot = root;
if (nodeToFix.CSharpKind() == SyntaxKind.Attribute)
{
// could be either a [DllImport] or [MarshalAs] attribute
var attribute = (AttributeSyntax)nodeToFix;
var attributeType = model.GetSymbolInfo(attribute).Symbol;
var arguments = attribute.ArgumentList.Arguments;
if (dllImportType.Equals(attributeType.ContainingType))
{
// [DllImport] attribute, add or replace CharSet named parameter
var argumentValue = CreateCharSetArgument(syntaxFactoryService, charSetType).WithAdditionalAnnotations(Formatter.Annotation);
var namedParameter = arguments.FirstOrDefault(arg => arg.NameEquals != null && arg.NameEquals.Name.Identifier.Text == CharSetText);
if (namedParameter == null)
{
// add the parameter
namedParameter = SyntaxFactory.AttributeArgument(SyntaxFactory.NameEquals(CharSetText), null, (ExpressionSyntax)argumentValue)
.WithAdditionalAnnotations(Formatter.Annotation);
var newArguments = arguments.Add(namedParameter);
var newArgumentList = attribute.ArgumentList.WithArguments(newArguments);
newRoot = root.ReplaceNode(attribute.ArgumentList, newArgumentList);
}
else
{
// replace the parameter
var newNamedParameter = namedParameter.WithExpression((ExpressionSyntax)argumentValue);
newRoot = root.ReplaceNode(namedParameter, newNamedParameter);
}
}
else if (marshalAsType.Equals(attributeType.ContainingType) && arguments.Count == 1)
{
// [MarshalAs] attribute, replace the only argument
var newExpression = CreateMarshalAsArgument(syntaxFactoryService, unmanagedType)
.WithLeadingTrivia(arguments[0].GetLeadingTrivia())
.WithTrailingTrivia(arguments[0].GetTrailingTrivia());
var newArgument = arguments[0].WithExpression((ExpressionSyntax)newExpression);
newRoot = root.ReplaceNode(arguments[0], newArgument);
}
}
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
示例4: GetFixesAsync
internal async override Task<IEnumerable<CodeAction>> GetFixesAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, CancellationToken cancellationToken)
{
IEnumerable<CodeAction> actions = null;
// Fix 1: Add a NonSerialized attribute to the field
var fieldNode = GetFieldDeclarationNode(nodeToFix);
if (fieldNode != null)
{
var attr = CodeGenerationSymbolFactory.CreateAttributeData(WellKnownTypes.NonSerializedAttribute(model.Compilation));
var newNode = CodeGenerator.AddAttributes(fieldNode, document.Project.Solution.Workspace, SpecializedCollections.SingletonEnumerable(attr)).WithAdditionalAnnotations(Formatting.Formatter.Annotation);
var newDocument = document.WithSyntaxRoot(root.ReplaceNode(fieldNode, newNode));
var codeAction = CodeAction.Create(FxCopFixersResources.AddNonSerializedAttribute, newDocument);
actions = SpecializedCollections.SingletonEnumerable(codeAction);
// Fix 2: If the type of the field is defined in source, then add the serializable attribute to the type.
var fieldSymbol = model.GetDeclaredSymbol(nodeToFix) as IFieldSymbol;
var type = fieldSymbol.Type;
if (type.Locations.Any(l => l.IsInSource))
{
var typeDeclNode = type.DeclaringSyntaxReferences.First().GetSyntax();
var serializableAttr = CodeGenerationSymbolFactory.CreateAttributeData(WellKnownTypes.SerializableAttribute(model.Compilation));
var newTypeDeclNode = CodeGenerator.AddAttributes(typeDeclNode, document.Project.Solution.Workspace, SpecializedCollections.SingletonEnumerable(serializableAttr)).WithAdditionalAnnotations(Formatting.Formatter.Annotation);
var documentContainingNode = document.Project.Solution.GetDocument(typeDeclNode.SyntaxTree);
var docRoot = await documentContainingNode.GetSyntaxRootAsync(cancellationToken);
var newDocumentContainingNode = documentContainingNode.WithSyntaxRoot(docRoot.ReplaceNode(typeDeclNode, newTypeDeclNode));
var typeCodeAction = CodeAction.Create(FxCopFixersResources.AddSerializableAttribute, newDocumentContainingNode.Project.Solution);
actions = actions.Concat(typeCodeAction);
}
}
return actions;
}
示例5: AddStaticKeyword
private Task<Document> AddStaticKeyword(Document document, SyntaxNode root, ClassDeclarationSyntax classDeclaration)
{
var staticKeyword = SyntaxFactory.Token(SyntaxKind.StaticKeyword).WithAdditionalAnnotations(Formatter.Annotation);
var newDeclaration = classDeclaration.AddModifiers(staticKeyword);
var newRoot = root.ReplaceNode(classDeclaration, newDeclaration);
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
示例6: HandleSingleIfStatementForm
private Document HandleSingleIfStatementForm(Document document, SyntaxNode root, Diagnostic diagnostic)
{
var ifStatementLocation = diagnostic.AdditionalLocations[0];
var expressionStatementLocation = diagnostic.AdditionalLocations[1];
var ifStatement = (IfStatementSyntax)root.FindNode(ifStatementLocation.SourceSpan);
var expressionStatement = (ExpressionStatementSyntax)root.FindNode(expressionStatementLocation.SourceSpan);
var invocationExpression = (InvocationExpressionSyntax)expressionStatement.Expression;
StatementSyntax newStatement = expressionStatement.WithExpression(
SyntaxFactory.ConditionalAccessExpression(
invocationExpression.Expression,
SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberBindingExpression(SyntaxFactory.IdentifierName(nameof(Action.Invoke))), invocationExpression.ArgumentList)));
newStatement = newStatement.WithPrependedLeadingTrivia(ifStatement.GetLeadingTrivia());
if (ifStatement.Parent.IsKind(SyntaxKind.ElseClause) && ifStatement.Statement.IsKind(SyntaxKind.Block))
{
newStatement = ((BlockSyntax)ifStatement.Statement).WithStatements(SyntaxFactory.SingletonList(newStatement));
}
newStatement = newStatement.WithAdditionalAnnotations(Formatter.Annotation);
var newRoot = root.ReplaceNode(ifStatement, newStatement);
return document.WithSyntaxRoot(newRoot);
}
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:26,代码来源:InvokeDelegateWithConditionalAccessCodeFixProvider.cs
示例7: AddNonSerializedAttribute
private Task<Document> AddNonSerializedAttribute(Document document, SemanticModel model, SyntaxNode root, SyntaxNode fieldNode, SyntaxGenerator generator)
{
var attr = generator.Attribute(generator.TypeExpression(WellKnownTypes.NonSerializedAttribute(model.Compilation)));
var newNode = generator.AddAttributes(fieldNode, attr);
var newDocument = document.WithSyntaxRoot(root.ReplaceNode(fieldNode, newNode));
return Task.FromResult(newDocument);
}
示例8: GetUpdatedDocumentAsync
internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var generator = SyntaxGenerator.GetGenerator(document);
var attr = generator.Attribute(generator.TypeExpression(WellKnownTypes.SerializableAttribute(model.Compilation)));
var newNode = generator.AddAttributes(nodeToFix, attr);
return Task.FromResult(document.WithSyntaxRoot(root.ReplaceNode(nodeToFix, newNode)));
}
示例9: GetUpdatedDocumentAsync
internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
{
//// We are going to add two operators:
////
//// public static bool operator ==(A left, A right)
//// {
//// throw new NotImplementedException();
//// }
////
//// public static bool operator !=(A left, A right)
//// {
//// throw new NotImplementedException();
//// }
var syntaxNode = nodeToFix as StructDeclarationSyntax;
if (syntaxNode == null)
{
return Task.FromResult(document);
}
var statement = CreateThrowNotImplementedStatement(model);
if (statement == null)
{
return Task.FromResult(document);
}
var parameters = new[] { CreateParameter(syntaxNode.Identifier.ValueText, LeftName), CreateParameter(syntaxNode.Identifier.ValueText, RightName) };
var op_equality = CreateOperatorDeclaration(SyntaxKind.EqualsEqualsToken, parameters, statement);
var op_inequality = CreateOperatorDeclaration(SyntaxKind.ExclamationEqualsToken, parameters, statement);
var newNode = syntaxNode.AddMembers(new[] { op_equality, op_inequality }).WithAdditionalAnnotations(Formatter.Annotation);
return Task.FromResult(document.WithSyntaxRoot(root.ReplaceNode(nodeToFix, newNode)));
}
示例10: HandleDeclarationAsync
private async Task<Document> HandleDeclarationAsync(Document document, SyntaxNode root, SyntaxNode node, CancellationToken cancellationToken)
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var declarationContext = node.Parent;
TypeSyntax typeSyntax = null;
if (declarationContext is VariableDeclarationSyntax)
{
typeSyntax = ((VariableDeclarationSyntax)declarationContext).Type;
}
else if (declarationContext is ForEachStatementSyntax)
{
typeSyntax = ((ForEachStatementSyntax)declarationContext).Type;
}
else
{
Contract.Fail($"unhandled kind {declarationContext.Kind().ToString()}");
}
var typeSymbol = semanticModel.GetTypeInfo(typeSyntax).ConvertedType;
var typeName = typeSymbol.GenerateTypeSyntax()
.WithLeadingTrivia(node.GetLeadingTrivia())
.WithTrailingTrivia(node.GetTrailingTrivia());
Debug.Assert(!typeName.ContainsDiagnostics, "Explicit type replacement likely introduced an error in code");
var newRoot = root.ReplaceNode(node, typeName);
return document.WithSyntaxRoot(newRoot);
}
示例11: ReplaceTypeWithVarAsync
private static Task<Document> ReplaceTypeWithVarAsync(Document document, SyntaxNode root, SyntaxNode node)
{
var implicitType = SyntaxFactory.IdentifierName("var")
.WithLeadingTrivia(node.GetLeadingTrivia())
.WithTrailingTrivia(node.GetTrailingTrivia());
var newRoot = root.ReplaceNode(node, implicitType);
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
示例12: GetCodeFixAsync
protected override async Task<CodeAction> GetCodeFixAsync(SyntaxNode root, SyntaxNode node, Document document, Diagnostic diagnostics, CancellationToken cancellationToken)
{
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var methodSymbol = model.GetDeclaredSymbol(node, cancellationToken) as IMethodSymbol;
if (methodSymbol.ReturnsVoid)
{
return null;
}
var ienumerableSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.IEnumerable");
var ienumeratorSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.IEnumerator");
var ienumerableGenericSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.Generic.IEnumerable`1");
var ienumeratorGenericSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.Generic.IEnumerator`1");
if (ienumerableGenericSymbol == null ||
ienumerableSymbol == null ||
ienumeratorGenericSymbol == null ||
ienumeratorSymbol == null)
{
return null;
}
var returnType = methodSymbol.ReturnType;
if (returnType.InheritsFromOrEquals(ienumerableSymbol))
{
if (returnType.GetArity() != 1)
{
return null;
}
var typeArg = returnType.GetTypeArguments().First();
ienumerableGenericSymbol = ienumerableGenericSymbol.Construct(typeArg);
}
else
{
ienumerableGenericSymbol = ienumerableGenericSymbol.Construct(returnType);
}
TypeSyntax oldReturnType;
var newReturnType = ienumerableGenericSymbol.GenerateTypeSyntax();
var newMethodDeclarationNode = WithReturnType(node, newReturnType, out oldReturnType);
if (newMethodDeclarationNode == null)
{
// the language updated and added a new kind of MethodDeclarationSyntax without updating this file
return null;
}
root = root.ReplaceNode(node, newMethodDeclarationNode);
var newDocument = document.WithSyntaxRoot(root);
return new MyCodeAction(
string.Format(CSharpFeaturesResources.ChangeReturnType,
oldReturnType.ToString(),
ienumerableGenericSymbol.ToMinimalDisplayString(model, node.SpanStart)), newDocument);
}
示例13: GetNewRootAsync
private Task<SyntaxNode> GetNewRootAsync(
SyntaxNode root,
SyntaxNode oldNode,
SemanticModel semanticModel,
Diagnostic diagnostic,
Document document,
CancellationToken cancellationToken)
{
var expression = oldNode as ExpressionSyntax;
if (expression == null)
{
return SpecializedTasks.Default<SyntaxNode>();
}
switch (diagnostic.Id)
{
case CS4014:
return Task.FromResult(root.ReplaceNode(oldNode, ConvertToAwaitExpression(expression)));
case CS4016:
if (!DoesExpressionReturnTask(expression, semanticModel))
{
return SpecializedTasks.Default<SyntaxNode>();
}
return Task.FromResult(root.ReplaceNode(oldNode, ConvertToAwaitExpression(expression)));
case CS0029:
if (!DoesExpressionReturnGenericTaskWhoseArgumentsMatchLeftSide(expression, semanticModel, document.Project, cancellationToken))
{
return SpecializedTasks.Default<SyntaxNode>();
}
return Task.FromResult(root.ReplaceNode(oldNode, ConvertToAwaitExpression(expression)));
default:
return SpecializedTasks.Default<SyntaxNode>();
}
}
示例14: GetUpdatedDocumentAsync
internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
{
var attributeSyntax = nodeToFix as ClassDeclarationSyntax;
if (attributeSyntax != null)
{
// TODO : Organize the modifiers list after adding sealed modifier.
var sealedModifier = SyntaxFactory.Token(SyntaxKind.SealedKeyword);
var newAttributeSyntax = attributeSyntax
.WithModifiers(attributeSyntax.Modifiers.Add(sealedModifier))
.WithAdditionalAnnotations(Formatter.Annotation);
document = document.WithSyntaxRoot(root.ReplaceNode(attributeSyntax, newAttributeSyntax));
}
return Task.FromResult(document);
}
示例15: GetNewRoot
protected override async Task<SyntaxNode> GetNewRoot(SyntaxNode root, SyntaxNode oldNode, SemanticModel semanticModel, Diagnostic diagnostic, Document document, CancellationToken cancellationToken)
{
var nodeToModify = GetContainingMember(oldNode);
if (nodeToModify == null)
{
return null;
}
var modifiedNode = await ConvertToAsync(nodeToModify, semanticModel, document, cancellationToken).ConfigureAwait(false);
if (modifiedNode != null)
{
return root.ReplaceNode(nodeToModify, modifiedNode);
}
return null;
}