本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.RemoveNodes方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.RemoveNodes方法的具体用法?C# SyntaxNode.RemoveNodes怎么用?C# SyntaxNode.RemoveNodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.RemoveNodes方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Remove
private static bool Remove(Label label, ref SyntaxNode tree)
{
var members = tree.GetMembers()
.Where(node => node.Identifier().ValueText == label.Name)
.ToList();
if (!members.Any())
return false;
if (label.OnlyBody)
tree = tree.RemoveNodes(members.SelectMany(syntax => syntax.GetBody()), SyntaxRemoveOptions.KeepNoTrivia);
else
tree = tree.RemoveNodes(members, SyntaxRemoveOptions.KeepExteriorTrivia);
return true;
}
示例2: 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;
}
示例3: CreateFiles
private static async Task<Solution> CreateFiles(Document document, SyntaxNode root,
SemanticModel model, ImmutableArray<TypeToRemove> typesToRemove,
Func<string, string> typeFolderGenerator,
CancellationToken token)
{
var project = document.Project;
var workspace = project.Solution.Workspace;
project = ExtractTypesToFilesCodeRefactoringProvider.MoveTypeNodes(
model, typesToRemove, typeFolderGenerator, project, token);
var newRoot = root.RemoveNodes(
typesToRemove.Select(_ => _.Declaration),
SyntaxRemoveOptions.AddElasticMarker);
var newSolution = project.Solution;
var projectId = project.Id;
newSolution = newSolution.WithDocumentSyntaxRoot(document.Id, newRoot);
var newDocument = newSolution.GetProject(project.Id).GetDocument(document.Id);
newRoot = await newDocument.GetSyntaxRootAsync(token);
var newModel = await newDocument.GetSemanticModelAsync(token);
var newUsings = newRoot.GenerateUsingDirectives(newModel);
newRoot = newRoot.RemoveNodes(
newRoot.DescendantNodes(_ => true).OfType<UsingDirectiveSyntax>(),
SyntaxRemoveOptions.AddElasticMarker);
newRoot = (newRoot as CompilationUnitSyntax)?.WithUsings(newUsings);
return newSolution.WithDocumentSyntaxRoot(document.Id, newRoot);
}
示例4: ProcessAsync
public async Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken)
{
var model = await document.GetSemanticModelAsync(cancellationToken);
var diagnostics = model.GetDiagnostics().Where(s => s.Id == "CS8019");
var usingDirectives = new List<SyntaxNode>();
var newDocument = document;
foreach (var diagnostic in diagnostics)
{
var text = diagnostic.Location.SourceSpan;
var token = syntaxNode.FindToken(text.Start); // do I need to get the root from the document?
usingDirectives.Add(token.Parent);
}
var newNode = syntaxNode.RemoveNodes(usingDirectives, SyntaxRemoveOptions.KeepNoTrivia);
if (newNode != syntaxNode)
{
newDocument = document.WithSyntaxRoot(newNode);
}
return await newDocument.GetSyntaxRootAsync(cancellationToken);
}
示例5: GetNewDocumentSyntaxTree
private static SyntaxNode GetNewDocumentSyntaxTree(SyntaxNode syntaxTree, List<SyntaxNode> otherTypeDeclarationsInFile)
{
return syntaxTree.RemoveNodes(otherTypeDeclarationsInFile, SyntaxRemoveOptions.KeepNoTrivia);
}
示例6: RemoveUnusedImportDirectives
private static SyntaxNode RemoveUnusedImportDirectives(SemanticModel semanticModel, SyntaxNode root, CancellationToken cancellationToken)
{
var oldUsings = root.DescendantNodesAndSelf().Where(s => s is UsingDirectiveSyntax);
var unusedUsings = GetUnusedImportDirectives(semanticModel, cancellationToken);
SyntaxTriviaList leadingTrivia = root.GetLeadingTrivia();
root = root.RemoveNodes(oldUsings, SyntaxRemoveOptions.KeepNoTrivia);
var newUsings = SyntaxFactory.List(oldUsings.Except(unusedUsings));
root = ((CompilationUnitSyntax)root)
.WithUsings(newUsings)
.NormalizeWhitespace()
.WithLeadingTrivia(leadingTrivia);
return root;
}
示例7: RemoveUsingStatement
private Task<Document> RemoveUsingStatement(Document document, SyntaxNode root, IEnumerable<SyntaxNode> usingDirectiveNodes)
{
return Task.FromResult(
document.WithSyntaxRoot(root.RemoveNodes(usingDirectiveNodes, SyntaxRemoveOptions.KeepLeadingTrivia)));
}