コード例 #1
0
        public override async void Execute(EditorRefactoringContext context)
        {
            SyntaxTree st = await context.GetSyntaxTreeAsync().ConfigureAwait(false);

            ICompilation compilation = await context.GetCompilationAsync().ConfigureAwait(false);

            CSharpFullParseInformation info = await context.GetParseInformationAsync().ConfigureAwait(false) as CSharpFullParseInformation;

            EntityDeclaration node     = (EntityDeclaration)st.GetNodeAt(context.CaretLocation, n => n is TypeDeclaration || n is DelegateDeclaration);
            IDocument         document = context.Editor.Document;

            FileName newFileName = FileName.Create(Path.Combine(Path.GetDirectoryName(context.FileName), MakeValidFileName(node.Name)));
            string   header      = CopyFileHeader(document, info);
            string   footer      = CopyFileEnd(document, info);

            AstNode newNode = node.Clone();

            foreach (var ns in node.Ancestors.OfType <NamespaceDeclaration>())
            {
                var newNS = new NamespaceDeclaration(ns.Name);
                newNS.Members.AddRange(ns.Children.Where(ch => ch is UsingDeclaration ||
                                                         ch is UsingAliasDeclaration ||
                                                         ch is ExternAliasDeclaration).Select(usingDecl => usingDecl.Clone()));
                newNS.AddMember(newNode);
                newNode = newNS;
            }

            var topLevelUsings = st.Children.Where(ch => ch is UsingDeclaration ||
                                                   ch is UsingAliasDeclaration ||
                                                   ch is ExternAliasDeclaration);
            StringBuilder       newCode = new StringBuilder(header);
            CSharpOutputVisitor visitor = new CSharpOutputVisitor(new StringWriter(newCode), FormattingOptionsFactory.CreateSharpDevelop());

            foreach (var topLevelUsing in topLevelUsings)
            {
                topLevelUsing.AcceptVisitor(visitor);
            }

            newNode.AcceptVisitor(visitor);

            newCode.AppendLine(footer);

            IViewContent viewContent = FileService.NewFile(newFileName, newCode.ToString());

            viewContent.PrimaryFile.SaveToDisk(newFileName);
            // now that the code is saved in the other file, remove it from the original document
            RemoveExtractedNode(context, node);

            IProject project = (IProject)compilation.GetProject();

            if (project != null)
            {
                FileProjectItem projectItem = new FileProjectItem(project, ItemType.Compile);
                projectItem.FileName = newFileName;
                ProjectService.AddProjectItem(project, projectItem);
                FileService.FireFileCreated(newFileName, false);
                project.Save();
                ProjectBrowserPad.RefreshViewAsync();
            }
        }
        static EntityDeclaration CloneNodeWithoutBodies(EntityDeclaration node)
        {
            EntityDeclaration newNode;
            var custom = node as CustomEventDeclaration;

            if (custom == null)
            {
                newNode = (EntityDeclaration)node.Clone();

                if (newNode is PropertyDeclaration || node is IndexerDeclaration)
                {
                    var getter = newNode.GetChildByRole(PropertyDeclaration.GetterRole);
                    if (!getter.IsNull)
                    {
                        getter.Body.Remove();
                    }
                    var setter = newNode.GetChildByRole(PropertyDeclaration.SetterRole);
                    if (!setter.IsNull)
                    {
                        setter.Body.Remove();
                    }
                }
                else
                {
                    newNode.GetChildByRole(Roles.Body).Remove();
                }
            }
            else
            {
                newNode = new EventDeclaration {
                    Modifiers  = custom.Modifiers,
                    ReturnType = custom.ReturnType.Clone(),
                    Variables  =
                    {
                        new VariableInitializer {
                            Name = custom.Name
                        }
                    }
                };
            }
            return(newNode);
        }