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


C# SyntaxNode.RemoveNode方法代码示例

本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.RemoveNode方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.RemoveNode方法的具体用法?C# SyntaxNode.RemoveNode怎么用?C# SyntaxNode.RemoveNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.CodeAnalysis.SyntaxNode的用法示例。


在下文中一共展示了SyntaxNode.RemoveNode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetNewRoot

        static SyntaxNode GetNewRoot(SyntaxNode root, SyntaxNode node)
        {
            var decl = node.AncestorsAndSelf().OfType<LocalDeclarationStatementSyntax>().FirstOrDefault();
            if (decl != null)
                return root.RemoveNode(decl, SyntaxRemoveOptions.KeepNoTrivia);
            if (node.Parent.IsKind(SyntaxKind.ElseClause))
                return root.RemoveNode(node.Parent, SyntaxRemoveOptions.KeepNoTrivia);

            var statement = node as StatementSyntax;
            if (statement != null)
                return root.RemoveNode(statement, SyntaxRemoveOptions.KeepNoTrivia);

            return root.RemoveNode(node.Parent, SyntaxRemoveOptions.KeepNoTrivia);
        }
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:14,代码来源:CS0162UnreachableCodeDetectedCodeFixProvider.cs

示例2: CalculateNewRoot

        private static SyntaxNode CalculateNewRoot(SyntaxNode root, IfStatementSyntax ifStatement)
        {
            SyntaxNode newRoot;
            var isTrue = ifStatement.Condition.IsKind(SyntaxKind.TrueLiteralExpression);

            if (isTrue)
            {
                var block = ifStatement.Statement as BlockSyntax;
                newRoot = block == null
                    ? root.ReplaceNode(ifStatement, ifStatement.Statement)
                    : root.ReplaceNode(ifStatement, block.Statements);
            }
            else
            {
                if (ifStatement.Else == null)
                {
                    newRoot = root.RemoveNode(ifStatement, SyntaxRemoveOptions.KeepNoTrivia);
                }
                else
                {
                    var block = ifStatement.Else.Statement as BlockSyntax;
                    newRoot = block == null
                        ? root.ReplaceNode(ifStatement, ifStatement.Else.Statement)
                        : root.ReplaceNode(ifStatement, block.Statements);
                }
            }

            return newRoot.WithAdditionalAnnotations(Formatter.Annotation);
        }
开发者ID:peterstevens130561,项目名称:sonarlint-vs,代码行数:29,代码来源:IfConditionalAlwaysTrueOrFalseCodeFixProvider.cs

示例3: 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

示例4: CalculateNewRoot

        private static SyntaxNode CalculateNewRoot(SyntaxNode root, SyntaxNode currentNode, TryStatementSyntax tryStatement)
        {
            var isTryRemovable = tryStatement.Catches.Count == 1 && tryStatement.Finally == null;

            return isTryRemovable
                ? root.ReplaceNode(
                    tryStatement,
                    tryStatement.Block.Statements.Select(st => st.WithAdditionalAnnotations(Formatter.Annotation)))
                : root.RemoveNode(currentNode, SyntaxRemoveOptions.KeepNoTrivia);
        }
开发者ID:ozgurkayaist,项目名称:sonarlint-vs,代码行数:10,代码来源:CatchRethrowCodeFixProvider.cs

示例5: RegisterActionForDestructor

 private static void RegisterActionForDestructor(CodeFixContext context, SyntaxNode root, BaseMethodDeclarationSyntax method)
 {
     context.RegisterCodeFix(
         CodeAction.Create(
             TitleRemoveDestructor,
             c =>
             {
                 var newRoot = root.RemoveNode(
                     method,
                     SyntaxRemoveOptions.KeepNoTrivia);
                 return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
             },
             TitleRemoveDestructor),
         context.Diagnostics);
 }
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:15,代码来源:RedundancyInConstructorDestructorDeclarationCodeFixProvider.cs

示例6: RegisterCodeFixesAsync

        protected sealed override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
        {
            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var syntaxNode = root.FindNode(diagnosticSpan);

            context.RegisterCodeFix(
                CodeAction.Create(
                    Title,
                    c =>
                    {
                        var newRoot = root.RemoveNode(syntaxNode, SyntaxRemoveOptions.KeepExteriorTrivia | SyntaxRemoveOptions.KeepEndOfLine);
                        return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
                    }),
                context.Diagnostics);
        }
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:16,代码来源:RedundantParenthesesCodeFixProvider.cs

示例7: GetDocumentWithClassDeclarationRemoved

 private static Solution GetDocumentWithClassDeclarationRemoved(Project project, Document document, SyntaxNode syntaxTree, TypeDeclarationSyntax declaration, IEnumerable<SyntaxNode> otherTypeDeclarationsInFile)
 {
     if (otherTypeDeclarationsInFile.Any() || !RoslynBug857331Fixed)
     {
         var newSyntaxTree = syntaxTree.RemoveNode(declaration, SyntaxRemoveOptions.KeepNoTrivia);
         return document.WithSyntaxRoot(newSyntaxTree).Project.Solution;
     }
     else
     {
         var emptyDocumentId = document.Id;
         if (project.Solution.GetDocument(emptyDocumentId) != null)
         {
             var projectWithFileRemoved = project.Solution.RemoveDocument(emptyDocumentId);
             return projectWithFileRemoved;
         }
         return project.Solution;
     }
 }
开发者ID:GrahamTheCoder,项目名称:RoslynSpike,代码行数:18,代码来源:CodeFixProvider.cs

示例8: RegisterCodeFixesAsync

        protected sealed override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
        {
            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var syntaxNode = root.FindNode(diagnosticSpan);

            if (syntaxNode.Parent is ExpressionStatementSyntax)
            {
                context.RegisterCodeFix(
                    CodeAction.Create(
                        Title,
                        c =>
                        {
                            var newRoot = root.RemoveNode(syntaxNode.Parent, SyntaxRemoveOptions.KeepNoTrivia);
                            return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
                        }),
                    context.Diagnostics);
            }
        }
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:19,代码来源:SuppressFinalizeUselessCodeFixProvider.cs

示例9: GetTransformedDocumentAsync

        private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxNode node)
        {
            // The first token is the open parenthesis token. This token has all the inner trivia
            var firstToken = node.GetFirstToken();
            var lastToken = node.GetLastToken();

            var previousToken = firstToken.GetPreviousToken();

            // We want to keep all trivia. The easiest way to do that is by doing it manually
            var newSyntaxRoot = root.RemoveNode(node, SyntaxRemoveOptions.KeepNoTrivia);

            // The removing operation has not changed the location of the previous token
            var newPreviousToken = newSyntaxRoot.FindToken(previousToken.Span.Start);

            var newTrailingTrivia = newPreviousToken.TrailingTrivia.AddRange(firstToken.GetAllTrivia()).AddRange(lastToken.GetAllTrivia());

            newSyntaxRoot = newSyntaxRoot.ReplaceToken(newPreviousToken, newPreviousToken.WithTrailingTrivia(newTrailingTrivia));

            return Task.FromResult(document.WithSyntaxRoot(newSyntaxRoot));
        }
开发者ID:Noryoko,项目名称:StyleCopAnalyzers,代码行数:20,代码来源:SA1410SA1411CodeFixProvider.cs

示例10: RegisterCodeFixesAsync

        protected sealed override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
        {
            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var syntaxNode = root.FindNode(diagnosticSpan) as NamespaceDeclarationSyntax;

            if (syntaxNode == null)
            {
                return;
            }

            context.RegisterCodeFix(
                CodeAction.Create(
                    Title,
                    c =>
                    {
                        var newRoot = root.RemoveNode(syntaxNode, SyntaxRemoveOptions.KeepNoTrivia);
                        return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
                    }),
                context.Diagnostics);
        }
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:21,代码来源:EmptyNamespaceCodeFixProvider.cs

示例11: CalculateNewRoot

        private static SyntaxNode CalculateNewRoot(SyntaxNode root, Diagnostic diagnostic,
            StatementSyntax currentAsStatement, AssignmentExpressionSyntax currentAsAssignment,
            BinaryExpressionSyntax currentAsBinary)
        {
            if (currentAsStatement != null)
            {
                return root.RemoveNode(currentAsStatement, SyntaxRemoveOptions.KeepNoTrivia);
            }

            if (currentAsAssignment != null)
            {
                return root.ReplaceNode(
                    currentAsAssignment,
                    currentAsAssignment.Left.WithAdditionalAnnotations(Formatter.Annotation));
            }

            var isReportingOnLeft = bool.Parse(diagnostic.Properties[SillyBitwiseOperation.IsReportingOnLeftKey]);
            return root.ReplaceNode(
                currentAsBinary,
                (isReportingOnLeft ? currentAsBinary.Right : currentAsBinary.Left).WithAdditionalAnnotations(Formatter.Annotation));
        }
开发者ID:ozgurkayaist,项目名称:sonarlint-vs,代码行数:21,代码来源:SillyBitwiseOperationCodeFixProvider.cs

示例12: RegisterCodeFixesAsync

        protected sealed override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
        {
            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var nodeToRemove = root.FindNode(diagnosticSpan);

            var attributeList = nodeToRemove.Parent as AttributeListSyntax;
            if (attributeList != null && attributeList.Attributes.Count == 1)
            {
                nodeToRemove = attributeList;
            }

            context.RegisterCodeFix(
                CodeAction.Create(
                    Title,
                    c =>
                    {
                        var newRoot = root.RemoveNode(nodeToRemove, SyntaxRemoveOptions.KeepNoTrivia);
                        return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
                    }),
                context.Diagnostics);
        }
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:22,代码来源:ThreadStaticNonStaticFieldCodeFixProvider.cs

示例13: RegisterCodeFixesAsync

        protected sealed override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
        {
            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var parameter = root.FindNode(diagnosticSpan, getInnermostNodeForTie: true) as ParameterSyntax;

            if (!bool.Parse(diagnostic.Properties[MethodParameterUnused.IsRemovableKey]))
            {
                return;
            }

            context.RegisterCodeFix(
                CodeAction.Create(
                    Title,
                    c =>
                    {
                        var newRoot = root.RemoveNode(
                            parameter,
                            SyntaxRemoveOptions.KeepLeadingTrivia);
                        return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
                    }),
                context.Diagnostics);
        }
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:23,代码来源:MethodParameterUnusedCodeFixProvider.cs

示例14: RemoveUnreachableStatement

 public static SyntaxNode RemoveUnreachableStatement(SyntaxNode root, SyntaxNode node)
 {
     if (node.Parent.IsKind(SyntaxKind.IfStatement, SyntaxKind.WhileStatement))
         return root.ReplaceNode(node, SyntaxFactory.Block());
     if (node.Parent.IsKind(SyntaxKind.ElseClause))
         return root.RemoveNode(node.Parent, SyntaxRemoveOptions.KeepNoTrivia);
     var statement = node as StatementSyntax;//for, while, foreach, if, throw, var, etc
     if (statement != null)
         return root.RemoveNode(statement, SyntaxRemoveOptions.KeepNoTrivia);
     var localDeclaration = node.FirstAncestorOfType<LocalDeclarationStatementSyntax>();
     if (localDeclaration != null)
         return root.RemoveNode(localDeclaration, SyntaxRemoveOptions.KeepNoTrivia);
     var expression = GetExpression(node);
     if (expression.Parent.IsKind(SyntaxKind.ForStatement))
         return root.RemoveNode(expression, SyntaxRemoveOptions.KeepNoTrivia);
     var expressionStatement = expression.FirstAncestorOfType<ExpressionStatementSyntax>();
     if (expressionStatement.Parent.IsKind(SyntaxKind.IfStatement, SyntaxKind.WhileStatement))
         return root.ReplaceNode(expressionStatement, SyntaxFactory.Block());
     if (expressionStatement.Parent.IsKind(SyntaxKind.ElseClause))
         return root.RemoveNode(expressionStatement.Parent, SyntaxRemoveOptions.KeepNoTrivia);
     return root.RemoveNode(expressionStatement, SyntaxRemoveOptions.KeepNoTrivia);
 }
开发者ID:JeanLLopes,项目名称:code-cracker,代码行数:22,代码来源:RemoveUnreachableCodeCodeFixProvider.cs

示例15: RegisterCodeFixesAsync

        protected sealed override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
        {
            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var memberAccess = (MemberAccessExpressionSyntax)root.FindNode(diagnosticSpan);

            var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
            var fieldSymbol = (IFieldSymbol)semanticModel.GetSymbolInfo(memberAccess.Expression).Symbol;
            var typeParameterSymbol = (ITypeParameterSymbol)fieldSymbol.Type;
            var genericType = typeParameterSymbol.ContainingType;

            var classDeclarationTasks = genericType.DeclaringSyntaxReferences
                .Select(reference => reference.GetSyntaxAsync(context.CancellationToken))
                .ToList();

            await Task.WhenAll(classDeclarationTasks);

            var classDeclarations = classDeclarationTasks
                .Select(task => task.Result as ClassDeclarationSyntax)
                .Where(cl => cl != null)
                .ToList();

            if (classDeclarations.Any())
            {
                context.RegisterCodeFix(
                    CodeAction.Create(
                        TitleAddClassConstraint,
                        async c =>
                        {
                            var currentSolution = context.Document.Project.Solution;
                            var mapping = GetDocumentIdClassDeclarationMapping(classDeclarations, currentSolution);

                            foreach (var classes in mapping)
                            {
                                var document = currentSolution.GetDocument(classes.Key);
                                var docRoot = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
                                var newDocRoot = GetNewDocumentRoot(docRoot, typeParameterSymbol, classes);
                                currentSolution = currentSolution.WithDocumentSyntaxRoot(classes.Key, newDocRoot);
                            }

                            return currentSolution;
                        }),
                    context.Diagnostics);
            }

            var expression = memberAccess.Parent as ExpressionSyntax;
            var statement = expression?.Parent as StatementSyntax;
            if (statement == null)
            {
                return;
            }

            context.RegisterCodeFix(
                CodeAction.Create(
                    TitleRemove,
                    c =>
                    {
                        var newRoot = root.RemoveNode(statement, SyntaxRemoveOptions.KeepNoTrivia);
                        return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
                    }),
                context.Diagnostics);
        }
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:62,代码来源:GenericReadonlyFieldPropertyAssignmentCodeFixProvider.cs


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