Example #1
0
        public bool Equals(SuggestedAction otherSuggestedAction)
        {
            if (otherSuggestedAction == null)
            {
                return(false);
            }

            if (ReferenceEquals(this, otherSuggestedAction))
            {
                return(true);
            }

            if (!ReferenceEquals(Provider, otherSuggestedAction.Provider))
            {
                return(false);
            }

            var otherCodeAction = otherSuggestedAction.CodeAction;

            if (CodeAction.EquivalenceKey == null || otherCodeAction.EquivalenceKey == null)
            {
                return(false);
            }

            return(CodeAction.EquivalenceKey == otherCodeAction.EquivalenceKey);
        }
Example #2
0
        internal bool Equals(SuggestedAction otherSuggestedAction)
        {
            if (otherSuggestedAction == null)
            {
                return(false);
            }

            if (this == otherSuggestedAction)
            {
                return(true);
            }

            if (Provider != otherSuggestedAction.Provider)
            {
                return(false);
            }

            var otherCodeAction = otherSuggestedAction.CodeAction;

            if (CodeAction.EquivalenceKey == null || otherCodeAction.EquivalenceKey == null)
            {
                return(false);
            }

            return(CodeAction.EquivalenceKey == otherCodeAction.EquivalenceKey);
        }
            private static void AddFix(CodeFix fix, SuggestedAction suggestedAction, IDictionary <Diagnostic, IList <SuggestedAction> > map, IList <Diagnostic> order)
            {
                var diag = fix.PrimaryDiagnostic;

                if (!map.ContainsKey(diag))
                {
                    // Remember the order of the keys for the 'map' dictionary.
                    order.Add(diag);
                    map[diag] = ImmutableArray.CreateBuilder <SuggestedAction>();
                }

                map[diag].Add(suggestedAction);
            }
            private static void AddFix(CodeFix fix, SuggestedAction suggestedAction, IDictionary <CodeFixGroupKey, IList <SuggestedAction> > map, IList <CodeFixGroupKey> order)
            {
                var diag = fix.GetPrimaryDiagnosticData();

                var groupKey = new CodeFixGroupKey(diag, fix.Action.Priority);

                if (!map.ContainsKey(groupKey))
                {
                    order.Add(groupKey);
                    map[groupKey] = ImmutableArray.CreateBuilder <SuggestedAction>();
                }

                map[groupKey].Add(suggestedAction);
            }
            /// <summary>
            /// Groups fixes by the diagnostic being addressed by each fix.
            /// </summary>
            private void GroupFixes(Workspace workspace, IEnumerable <CodeFixCollection> fixCollections, IDictionary <Diagnostic, IList <SuggestedAction> > map, IList <Diagnostic> order, bool hasSuppressionFixes)
            {
                foreach (var fixCollection in fixCollections)
                {
                    var fixes    = fixCollection.Fixes;
                    var fixCount = fixes.Length;

                    Func <CodeAction, SuggestedActionSet> getFixAllSuggestedActionSet = codeAction =>
                                                                                        CodeFixSuggestedAction.GetFixAllSuggestedActionSet(codeAction, fixCount, fixCollection.FixAllContext,
                                                                                                                                           workspace, _subjectBuffer, _owner._editHandler);

                    foreach (var fix in fixes)
                    {
                        // Suppression fixes are handled below.
                        if (!(fix.Action is SuppressionCodeAction))
                        {
                            SuggestedAction suggestedAction;
                            if (fix.Action.HasCodeActions)
                            {
                                var nestedActions = new List <SuggestedAction>();
                                foreach (var nestedAction in fix.Action.GetCodeActions())
                                {
                                    nestedActions.Add(new CodeFixSuggestedAction(workspace, _subjectBuffer, _owner._editHandler,
                                                                                 fix, nestedAction, fixCollection.Provider, getFixAllSuggestedActionSet(nestedAction)));
                                }

                                var diag = fix.Diagnostics[0];
                                var set  = new SuggestedActionSet(nestedActions, SuggestedActionSetPriority.Medium, GetApplicableToSpan(diag));

                                suggestedAction = new SuggestedAction(workspace, _subjectBuffer, _owner._editHandler,
                                                                      fix.Action, fixCollection.Provider, new[] { set });
                            }
                            else
                            {
                                suggestedAction = new CodeFixSuggestedAction(workspace, _subjectBuffer, _owner._editHandler,
                                                                             fix, fix.Action, fixCollection.Provider, getFixAllSuggestedActionSet(fix.Action));
                            }

                            AddFix(fix, suggestedAction, map, order);
                        }
                    }

                    if (hasSuppressionFixes)
                    {
                        // Add suppression fixes to the end of a given SuggestedActionSet so that they always show up last in a group.
                        foreach (var fix in fixes)
                        {
                            if (fix.Action is SuppressionCodeAction)
                            {
                                SuggestedAction suggestedAction;
                                if (fix.Action.HasCodeActions)
                                {
                                    suggestedAction = new SuppressionSuggestedAction(workspace, _subjectBuffer, _owner._editHandler,
                                                                                     fix, fixCollection.Provider, getFixAllSuggestedActionSet);
                                }
                                else
                                {
                                    suggestedAction = new CodeFixSuggestedAction(workspace, _subjectBuffer, _owner._editHandler,
                                                                                 fix, fix.Action, fixCollection.Provider, getFixAllSuggestedActionSet(fix.Action));
                                }

                                AddFix(fix, suggestedAction, map, order);
                            }
                        }
                    }
                }
            }
            /// <summary>
            /// Groups fixes by the diagnostic being addressed by each fix.
            /// </summary>
            private void GroupFixes(
                Workspace workspace,
                IEnumerable <CodeFixCollection> fixCollections,
                IDictionary <CodeFixGroupKey, IList <SuggestedAction> > map,
                IList <CodeFixGroupKey> order,
                bool hasSuppressionFixes)
            {
                foreach (var fixCollection in fixCollections)
                {
                    var fixes    = fixCollection.Fixes;
                    var fixCount = fixes.Length;

                    Func <CodeAction, SuggestedActionSet> getFixAllSuggestedActionSet =
                        codeAction => CodeFixSuggestedAction.GetFixAllSuggestedActionSet(
                            codeAction, fixCount, fixCollection.FixAllState,
                            fixCollection.SupportedScopes, fixCollection.FirstDiagnostic,
                            workspace, _subjectBuffer, _owner._editHandler,
                            _owner._waitIndicator, _owner._listener);

                    foreach (var fix in fixes)
                    {
                        // Suppression fixes are handled below.
                        if (!(fix.Action is SuppressionCodeAction))
                        {
                            SuggestedAction suggestedAction;
                            if (fix.Action.HasCodeActions)
                            {
                                var nestedActions = new List <SuggestedAction>();
                                foreach (var nestedAction in fix.Action.GetCodeActions())
                                {
                                    nestedActions.Add(new CodeFixSuggestedAction(workspace, _subjectBuffer,
                                                                                 _owner._editHandler, _owner._waitIndicator, fix,
                                                                                 nestedAction, fixCollection.Provider, getFixAllSuggestedActionSet(nestedAction), _owner._listener));
                                }

                                var diag = fix.PrimaryDiagnostic;
                                var set  = new SuggestedActionSet(nestedActions, SuggestedActionSetPriority.Medium, diag.Location.SourceSpan.ToSpan());

                                suggestedAction = new SuggestedAction(workspace, _subjectBuffer,
                                                                      _owner._editHandler, _owner._waitIndicator, fix.Action,
                                                                      fixCollection.Provider, _owner._listener, new[] { set });
                            }
                            else
                            {
                                suggestedAction = new CodeFixSuggestedAction(
                                    workspace, _subjectBuffer, _owner._editHandler, _owner._waitIndicator, fix,
                                    fix.Action, fixCollection.Provider, getFixAllSuggestedActionSet(fix.Action), _owner._listener);
                            }

                            AddFix(fix, suggestedAction, map, order);
                        }
                    }

                    if (hasSuppressionFixes)
                    {
                        // Add suppression fixes to the end of a given SuggestedActionSet so that they always show up last in a group.
                        foreach (var fix in fixes)
                        {
                            if (fix.Action is SuppressionCodeAction)
                            {
                                SuggestedAction suggestedAction;
                                if (fix.Action.HasCodeActions)
                                {
                                    suggestedAction = new SuppressionSuggestedAction(
                                        workspace, _subjectBuffer, _owner._editHandler, _owner._waitIndicator,
                                        fix, fixCollection.Provider, getFixAllSuggestedActionSet, _owner._listener);
                                }
                                else
                                {
                                    suggestedAction = new CodeFixSuggestedAction(
                                        workspace, _subjectBuffer, _owner._editHandler, _owner._waitIndicator, fix,
                                        fix.Action, fixCollection.Provider, getFixAllSuggestedActionSet(fix.Action), _owner._listener);
                                }

                                AddFix(fix, suggestedAction, map, order);
                            }
                        }
                    }
                }
            }