当前位置: 首页>>代码示例>>C#>>正文


C# SyntaxNode.RemoveNodes方法代码示例

本文整理汇总了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;
		}
开发者ID:andgein,项目名称:uLearn,代码行数:13,代码来源:CsMembersRemover.cs

示例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;
 }
开发者ID:haroldhues,项目名称:code-cracker,代码行数:35,代码来源:SwitchToAutoPropCodeFixProvider.cs

示例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);
		}
开发者ID:JasonBock,项目名称:CompilerAPIBook,代码行数:31,代码来源:ExtractTypesToFilesCodeRefactoringProvider.cs

示例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);
        }
开发者ID:nlockhart,项目名称:codeformatter,代码行数:22,代码来源:RemovedUnusedUsingStatementRule.cs

示例5: GetNewDocumentSyntaxTree

 private static SyntaxNode GetNewDocumentSyntaxTree(SyntaxNode syntaxTree, List<SyntaxNode> otherTypeDeclarationsInFile)
 {
     return syntaxTree.RemoveNodes(otherTypeDeclarationsInFile, SyntaxRemoveOptions.KeepNoTrivia);
 }
开发者ID:GrahamTheCoder,项目名称:RoslynSpike,代码行数:4,代码来源:CodeFixProvider.cs

示例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;
        }
开发者ID:joymon,项目名称:joyful-visualstudio,代码行数:15,代码来源:MoveClassToFile.cs

示例7: RemoveUsingStatement

 private Task<Document> RemoveUsingStatement(Document document, SyntaxNode root, IEnumerable<SyntaxNode> usingDirectiveNodes)
 {     
     return Task.FromResult(
         document.WithSyntaxRoot(root.RemoveNodes(usingDirectiveNodes, SyntaxRemoveOptions.KeepLeadingTrivia)));
 }
开发者ID:michaelcfanning,项目名称:codeformatter,代码行数:5,代码来源:OptimizeNamespaceImportsFixer.cs


注:本文中的Microsoft.CodeAnalysis.SyntaxNode.RemoveNodes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。