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


C# CodeFixContext.GetFirstNodeWithDiagnostic方法代码示例

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


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

示例1: RegisterCodeFixesAsync

        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
            var method = context.GetFirstNodeWithDiagnostic<MethodDeclarationSyntax>(root);

            // Extracting semantic model and precodition block
            var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
            var preconditionBlock = PreconditionsBlock.GetPreconditions(method, semanticModel);

            Contract.Assert(preconditionBlock.Preconditions.Count != 0, "Precondition block should have at least one statement!");

            // Caching all precondition. This will help to remove or leave them in different methods.
            var preconditionStatements = preconditionBlock.Preconditions.Select(p => p.IfThrowStaement).ToImmutableHashSet();
            
            // Extracting new method: it should contains all statements from the original method
            // but without preconditions.
            var extractedMethodBody = method.Body.Statements.Where(s => !preconditionStatements.Contains(s));

            // Clonning original method by changing it's body and changing the visibility
            var extractedMethod = 
                method.WithStatements(extractedMethodBody)
                        .WithIdentifier(Identifier($"Do{method.Identifier.Text}"))
                        .WithVisibilityModifier(VisibilityModifier.Private);

            // Updating original method: removing everything except preconditions
            var updatedMethodBody = method.Body.Statements.Where(s => preconditionStatements.Contains(s)).ToList();
            
            // Creating an invocation for extracted method
            var originalMethodCallExpression = CreateMethodCallExpression(extractedMethod, method.ParameterList.AsArguments());
            updatedMethodBody.Add(SyntaxFactory.ReturnStatement(originalMethodCallExpression));
            
            // Removing 'async'
            var updatedMethod =
                method.WithStatements(updatedMethodBody)
                    .WithoutModifiers(t => t.IsKind(SyntaxKind.AsyncKeyword));

            // Заменяем метод парой узлов: новым методом и выделенным методом
            var newRoot = root.ReplaceNode(method, new[] {updatedMethod, extractedMethod});
            var codeAction = CodeAction.Create(FixText, ct => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)));
            context.RegisterCodeFix(codeAction, context.Diagnostics.First());
        }
开发者ID:SergeyTeplyakov,项目名称:ErrorProne.NET,代码行数:41,代码来源:AsyncMethodPreconditionCodeFixProvider.cs


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