Пример #1
0
        /// <inheritdoc />
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            var editor = new CodeFixHelper(context);
            var root   = await context.Document.GetSyntaxRootAsync(context.CancellationToken);

            var diagnosedNode = root.FindNode(diagnosticSpan).FirstAncestorOrSelf <QualifiedNameSyntax>() ??
                                (NameSyntax)root.FindNode(diagnosticSpan).FirstAncestorOrSelf <IdentifierNameSyntax>();

            if (diagnosedNode == null)
            {
                return;
            }

            const string usingNamespace        = "CMS.DataEngine";
            var          newIdentifierNameNode = SyntaxFactory.ParseExpression("ISearchDocument");

            context.RegisterCodeFix(
                CodeAction.Create(
                    title: CodeFixMessagesProvider.GetReplaceWithMessage(newIdentifierNameNode),
                    createChangedDocument: c => editor.ReplaceExpressionWith(diagnosedNode, newIdentifierNameNode, c, usingNamespace),
                    equivalenceKey: nameof(LuceneSearchDocumentCodeFixProvider)),
                diagnostic);
        }
Пример #2
0
        /// <inheritdoc />
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            var diagnosedNode = root.FindToken(diagnosticSpan.Start).Parent;
            var elementAccess = diagnosedNode.AncestorsAndSelf().OfType <ElementAccessExpressionSyntax>().FirstOrDefault() ?? diagnosedNode.DescendantNodesAndSelf().OfType <ElementAccessExpressionSyntax>().FirstOrDefault();

            if (elementAccess == null)
            {
                return;
            }

            var usingNamespace = "CMS.Helpers";
            var codeFixHelper  = new CodeFixHelper(context);

            var assignmentExpression = elementAccess.FirstAncestorOrSelf <AssignmentExpressionSyntax>();
            var sessionKey           = GetElementAccessKey(elementAccess);
            var valueToBeAssigned    = assignmentExpression.Right;

            SyntaxNode oldNode = assignmentExpression;
            SyntaxNode newNode = SyntaxFactory.ParseExpression($@"SessionHelper.SetValue({sessionKey}, {valueToBeAssigned})");

            context.RegisterCodeFix(
                CodeAction.Create(
                    title: CodeFixMessagesProvider.GetReplaceWithMessage(newNode),
                    createChangedDocument: c => codeFixHelper.ReplaceExpressionWith(oldNode, newNode, c, usingNamespace),
                    equivalenceKey: nameof(HttpSessionElementAccessSetCodeFixProvider)),
                diagnostic);
        }
 private void RegisterCodeFixVariant(CodeFixContext context, CodeFixHelper codeFixHelper, SimpleNameSyntax oldMethodName, SimpleNameSyntax newMethodName)
 {
     context.RegisterCodeFix(
         CodeAction.Create(
             title: CodeFixMessagesProvider.GetReplaceWithMessage(newMethodName),
             createChangedDocument: c => codeFixHelper.ReplaceExpressionWith(oldMethodName, newMethodName, c),
             equivalenceKey: newMethodName.ToString()),
         codeFixHelper.GetFirstDiagnostic());
 }