Composite command that includes a mixin into a child class. A reference to the mixin is already defined as a field in the child class.
Inheritance: CompositeCommand
コード例 #1
0
        public void CreateMixinFromFieldDeclarationCommand_NoMixin()
        {
            var command = new CreateMixinFromFieldDeclarationCommand(null);
            var childClass = NSubstitute.Substitute.For<ClassWithSourceCode>();

            Assert.False(command.CanExecute(childClass));
        }
コード例 #2
0
        public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
            // Find the node at the selection.
            var node = root.FindNode(context.Span);

            var model = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);

            CompositeCommand command = null;
            
            // create mixin from base type
            var baseTypeSyntax = node as SimpleBaseTypeSyntax;
            // create command depending on the selected node
            if (baseTypeSyntax != null)
            {
                command = new CreateMixinFromInterfaceCommand(baseTypeSyntax, model);
            }
            else
            {
                var fieldDeclarationNode = GetContainingFieldDeclaration(node);
                if (fieldDeclarationNode != null)
                    command = new CreateMixinFromFieldDeclarationCommand(fieldDeclarationNode, model);
            }

            if (command == null)
                return;

            // get service provider and read settings from storage
            var serviceProvider = Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider;
            var settings = new Settings(serviceProvider);

            var childClassDeclaration = node.FindContainingClass();
            var childClass = new ClassFactory(model).Create(childClassDeclaration);
            if (!command.CanExecute(childClass, settings))
                return;
            var action = CodeAction.Create(
                command.Title,
                c => CreateMixin(command, context.Document, childClass, c));
            // Register this code action.
            context.RegisterRefactoring(action);
        }