Beispiel #1
0
        public override async Task <bool> IsAvailableAsync(EditorRefactoringContext context, CancellationToken cancellationToken)
        {
            SyntaxTree st = await context.GetSyntaxTreeAsync().ConfigureAwait(false);

            Identifier identifier = (Identifier)st.GetNodeAt(context.CaretLocation, node => node.Role == Roles.Identifier);

            if (identifier == null)
            {
                return(false);
            }
            if (MakeValidFileName(identifier.Name).Equals(Path.GetFileName(context.FileName), StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }
            if (identifier.Parent.Parent is TypeDeclaration)
            {
                return(false);
            }
            ParseInformation info = await context.GetParseInformationAsync().ConfigureAwait(false);

            if (info.UnresolvedFile.TopLevelTypeDefinitions.Count == 1)
            {
                return(false);
            }
            return(identifier.Parent is TypeDeclaration || identifier.Parent is DelegateDeclaration);
        }
        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();
            }
        }
Beispiel #3
0
        public static async Task <SyntaxTree> GetSyntaxTreeAsync(this EditorRefactoringContext editorContext)
        {
            var parseInfo = (await editorContext.GetParseInformationAsync().ConfigureAwait(false)) as CSharpFullParseInformation;

            if (parseInfo != null)
            {
                return(parseInfo.SyntaxTree);
            }
            else
            {
                return(new SyntaxTree());
            }
        }
		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();
			}
		}
		public override async Task<bool> IsAvailableAsync(EditorRefactoringContext context, CancellationToken cancellationToken)
		{
			SyntaxTree st = await context.GetSyntaxTreeAsync().ConfigureAwait(false);
			Identifier identifier = (Identifier)st.GetNodeAt(context.CaretLocation, node => node.Role == Roles.Identifier);
			if (identifier == null) return false;
			if (MakeValidFileName(identifier.Name).Equals(Path.GetFileName(context.FileName), StringComparison.OrdinalIgnoreCase))
				return false;
			if (identifier.Parent.Parent is TypeDeclaration)
				return false;
			ParseInformation info = await context.GetParseInformationAsync().ConfigureAwait(false);
			if (info.UnresolvedFile.TopLevelTypeDefinitions.Count == 1)
				return false;
			return identifier.Parent is TypeDeclaration || identifier.Parent is DelegateDeclaration;
		}