예제 #1
0
        public void AddCodeFix(ICodeFix codeFix)
        {
            Contract.Requires(codeFix != null);

            if (!this.hasCodeFix)
            {
                this.hasCodeFix = true;
                ProofObligationsWithCodeFix++;
            }

            this.codeFixes.Add(codeFix);
        }
예제 #2
0
        /// <summary>
        /// Applies the <see cref="ICodeFix.Action"/> operations (retrieved from its
        /// <see cref="CodeAction.GetOperationsAsync(CancellationToken)"/>) to the
        /// given <see cref="Workspace"/>.
        /// </summary>
        /// <param name="codeFix">The code fix to apply to the <paramref name="workspace"/>.</param>
        /// <param name="workspace">The <see cref="Workspace"/> to apply the code fix to.</param>
        /// <param name="cancellationToken">Optional <see cref="CancellationToken"/> for the operation.</param>
        public static async Task ApplyAsync(this ICodeFix codeFix, Workspace workspace, CancellationToken cancellationToken = default(CancellationToken))
        {
            var operations = await codeFix.Action.GetOperationsAsync(cancellationToken);

            var solution = workspace.CurrentSolution;

            foreach (var operation in operations)
            {
                if (operation is ApplyChangesOperation applyChanges)
                {
                    applyChanges.Apply(workspace, cancellationToken);
                }
            }
        }
예제 #3
0
        // ---------------- Constructor ----------------

        public SethCodeFixProvider()
        {
            // Use reflection so we don't need to manuall register everything
            // like we're forced to do with rules.
            fixes = new Dictionary <string, ICodeFix>();
            Assembly assm = typeof(SethCodeFixProvider).Assembly;

            foreach (Type type in assm.GetTypes())
            {
                if (
                    typeof(ICodeFix).IsAssignableFrom(type) &&
                    (type.IsAbstract == false) &&
                    (type.IsInterface == false)
                    )
                {
                    ICodeFix codeFix = (ICodeFix)Activator.CreateInstance(type);
                    fixes.Add(codeFix.Rule.Id, codeFix);
                }
            }

            fixableDiagnosticIds = ImmutableArray.Create(
                fixes.Keys.ToArray()
                );
        }