Beispiel #1
0
        public async Task <Document> ImplementInterfaceAsync(
            Document document,
            SyntaxNode node,
            CancellationToken cancellationToken
            )
        {
            using (Logger.LogBlock(FunctionId.Refactoring_ImplementInterface, cancellationToken))
            {
                var model = await document
                            .GetSemanticModelAsync(cancellationToken)
                            .ConfigureAwait(false);

                var state = State.Generate(this, document, model, node, cancellationToken);
                if (state == null)
                {
                    return(document);
                }

                // While implementing just one default action, like in the case of pressing enter after interface name in VB,
                // choose to implement with the dispose pattern as that's the Dev12 behavior.
                var action = ShouldImplementDisposePattern(state, explicitly: false)
                  ? ImplementInterfaceWithDisposePatternCodeAction.CreateImplementWithDisposePatternCodeAction(
                    this,
                    document,
                    state
                    )
                  : ImplementInterfaceCodeAction.CreateImplementCodeAction(this, document, state);

                return(await action
                       .GetUpdatedDocumentAsync(cancellationToken)
                       .ConfigureAwait(false));
            }
        }
Beispiel #2
0
        private IEnumerable <CodeAction> GetActions(Document document, State state)
        {
            if (state == null)
            {
                yield break;
            }

            if (state.MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented.Length > 0)
            {
                yield return(ImplementInterfaceCodeAction.CreateImplementCodeAction(this, document, state));

                if (ShouldImplementDisposePattern(state, explicitly: false))
                {
                    yield return(ImplementInterfaceWithDisposePatternCodeAction.CreateImplementWithDisposePatternCodeAction(this, document, state));
                }

                var delegatableMembers = GetDelegatableMembers(state);
                foreach (var member in delegatableMembers)
                {
                    yield return(ImplementInterfaceCodeAction.CreateImplementThroughMemberCodeAction(this, document, state, member));
                }

                if (state.ClassOrStructType.IsAbstract)
                {
                    yield return(ImplementInterfaceCodeAction.CreateImplementAbstractlyCodeAction(this, document, state));
                }
            }

            if (state.MembersWithoutExplicitImplementation.Length > 0)
            {
                yield return(ImplementInterfaceCodeAction.CreateImplementExplicitlyCodeAction(this, document, state));

                if (ShouldImplementDisposePattern(state, explicitly: true))
                {
                    yield return(ImplementInterfaceWithDisposePatternCodeAction.CreateImplementExplicitlyWithDisposePatternCodeAction(this, document, state));
                }
            }

            if (AnyImplementedImplicitly(state))
            {
                yield return(ImplementInterfaceCodeAction.CreateImplementRemainingExplicitlyCodeAction(this, document, state));
            }
        }
Beispiel #3
0
        private IEnumerable <CodeAction> GetActions(Document document, State state)
        {
            if (state == null)
            {
                yield break;
            }

            if (state.UnimplementedMembers != null && state.UnimplementedMembers.Count > 0)
            {
                yield return(ImplementInterfaceCodeAction.CreateImplementCodeAction(this, document, state));

                if (ShouldImplementDisposePattern(document, state, explicitly: false))
                {
                    yield return(ImplementInterfaceWithDisposePatternCodeAction.CreateImplementWithDisposePatternCodeAction(this, document, state));
                }

                var delegatableMembers = GetDelegatableMembers(state);
                foreach (var member in delegatableMembers)
                {
                    yield return(ImplementInterfaceCodeAction.CreateImplementThroughMemberCodeAction(this, document, state, member));
                }

                if (state.ClassOrStructType.IsAbstract)
                {
                    yield return(ImplementInterfaceCodeAction.CreateImplementAbstractlyCodeAction(this, document, state));
                }
            }

            if (state.UnimplementedExplicitMembers != null && state.UnimplementedExplicitMembers.Count > 0)
            {
                yield return(ImplementInterfaceCodeAction.CreateImplementExplicitlyCodeAction(this, document, state));

                if (ShouldImplementDisposePattern(document, state, explicitly: true))
                {
                    yield return(ImplementInterfaceWithDisposePatternCodeAction.CreateImplementExplicitlyWithDisposePatternCodeAction(this, document, state));
                }
            }
        }
        private IEnumerable <CodeAction> GetActions(Document document, ImplementTypeOptions options, State state)
        {
            if (state == null)
            {
                yield break;
            }

            if (state.MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented.Length > 0)
            {
                var totalMemberCount        = 0;
                var inaccessibleMemberCount = 0;

                foreach (var(_, members) in state.MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented)
                {
                    foreach (var member in members)
                    {
                        totalMemberCount++;

                        if (AccessibilityHelper.IsLessAccessibleThan(member, state.ClassOrStructType))
                        {
                            inaccessibleMemberCount++;
                        }
                    }
                }

                // If all members to implement are inaccessible, then "Implement interface" codeaction
                // will be the same as "Implement interface explicitly", so there is no point in having both of them
                if (totalMemberCount != inaccessibleMemberCount)
                {
                    yield return(ImplementInterfaceCodeAction.CreateImplementCodeAction(this, document, options, state));
                }

                if (ShouldImplementDisposePattern(state, explicitly: false))
                {
                    yield return(ImplementInterfaceWithDisposePatternCodeAction.CreateImplementWithDisposePatternCodeAction(this, document, options, state));
                }

                var delegatableMembers = GetDelegatableMembers(state);
                foreach (var member in delegatableMembers)
                {
                    yield return(ImplementInterfaceCodeAction.CreateImplementThroughMemberCodeAction(this, document, options, state, member));
                }

                if (state.ClassOrStructType.IsAbstract)
                {
                    yield return(ImplementInterfaceCodeAction.CreateImplementAbstractlyCodeAction(this, document, options, state));
                }
            }

            if (state.MembersWithoutExplicitImplementation.Length > 0)
            {
                yield return(ImplementInterfaceCodeAction.CreateImplementExplicitlyCodeAction(this, document, options, state));

                if (ShouldImplementDisposePattern(state, explicitly: true))
                {
                    yield return(ImplementInterfaceWithDisposePatternCodeAction.CreateImplementExplicitlyWithDisposePatternCodeAction(this, document, options, state));
                }
            }

            if (AnyImplementedImplicitly(state))
            {
                yield return(ImplementInterfaceCodeAction.CreateImplementRemainingExplicitlyCodeAction(this, document, options, state));
            }
        }