コード例 #1
0
        static void CreateNewFile(MDRefactoringContext context, TypeDeclaration type, string correctFileName)
        {
            var content = context.TextEditor.Text;

            var types = new List <EntityDeclaration> (context.Unit.GetTypes().Where(t => t.StartLocation != type.StartLocation));

            types.Sort((x, y) => y.StartLocation.CompareTo(x.StartLocation));

            foreach (var removeType in types)
            {
                var start = context.GetOffset(removeType.StartLocation);
                var end   = context.GetOffset(removeType.EndLocation);
                content = content.Remove(start, end - start);
            }

            if (context.Project != null)
            {
                string header = StandardHeaderService.GetHeader(context.Project, correctFileName, true);
                if (!string.IsNullOrEmpty(header))
                {
                    content = header + context.TextEditor.EolMarker + StripHeader(content);
                }
            }
            content = StripDoubleBlankLines(content);

            File.WriteAllText(correctFileName, content);
            context.Project.AddFile(correctFileName);
            MonoDevelop.Ide.IdeApp.ProjectOperations.Save(context.Project);
        }
コード例 #2
0
        public override void CreateNewType(AstNode newType, NewTypeContext ntctx = NewTypeContext.CurrentNamespace)
        {
            if (newType == null)
            {
                throw new ArgumentNullException("newType");
            }
            var correctFileName = MoveTypeToFile.GetCorrectFileName(context, (EntityDeclaration)newType);

            var content = context.TextEditor.Text;

            var types = new List <EntityDeclaration> (context.Unit.GetTypes());

            types.Sort((x, y) => y.StartLocation.CompareTo(x.StartLocation));

            foreach (var removeType in types)
            {
                var start = context.GetOffset(removeType.StartLocation);
                var end   = context.GetOffset(removeType.EndLocation);
                content = content.Remove(start, end - start);
            }

            var insertLocation = types.Count > 0 ? context.GetOffset(types.Last().StartLocation) : -1;

            if (insertLocation < 0 || insertLocation > content.Length)
            {
                insertLocation = content.Length;
            }
            content = content.Substring(0, insertLocation) + newType.ToString(FormattingOptions) + content.Substring(insertLocation);

            var project = context.FileContainerProject;

            if (project != null)
            {
                var policy     = project.Policies.Get <CSharpFormattingPolicy> ();
                var textPolicy = project.Policies.Get <TextStylePolicy> ();
                content = MonoDevelop.CSharp.Formatting.CSharpFormatter.FormatText(policy, textPolicy, MonoDevelop.CSharp.Formatting.CSharpFormatter.MimeType, content, 0, content.Length);
            }

            File.WriteAllText(correctFileName, content);

            if (project != null)
            {
                project.AddFile(correctFileName);
                IdeApp.ProjectOperations.Save(project);
            }
            IdeApp.Workbench.OpenDocument(correctFileName, project);
        }
コード例 #3
0
        public override void CreateNewType(AstNode newType, NewTypeContext ntctx)
        {
            if (newType == null)
            {
                throw new System.ArgumentNullException("newType");
            }
            var correctFileName = MoveTypeToFile.GetCorrectFileName(context, (EntityDeclaration)newType);

            var content = context.Document.Editor.Text;

            var types = new List <EntityDeclaration> (context.Unit.GetTypes());

            types.Sort((x, y) => y.StartLocation.CompareTo(x.StartLocation));

            foreach (var removeType in types)
            {
                var start = context.GetOffset(removeType.StartLocation);
                var end   = context.GetOffset(removeType.EndLocation);
                content = content.Remove(start, end - start);
            }

            var insertLocation   = types.Count > 0 ? context.GetOffset(types.Last().StartLocation) : -1;
            var formattingPolicy = this.document.GetFormattingPolicy();

            if (insertLocation < 0 || insertLocation > content.Length)
            {
                insertLocation = content.Length;
            }
            content = content.Substring(0, insertLocation) + newType.GetText(formattingPolicy.CreateOptions()) + content.Substring(insertLocation);

            var formatter = new CSharpFormatter();

            content = formatter.FormatText(formattingPolicy, null, CSharpFormatter.MimeType, content, 0, content.Length);

            File.WriteAllText(correctFileName, content);
            document.Project.AddFile(correctFileName);
            MonoDevelop.Ide.IdeApp.ProjectOperations.Save(document.Project);
            MonoDevelop.Ide.IdeApp.Workbench.OpenDocument(correctFileName);
        }
コード例 #4
0
		static void CreateNewFile (MDRefactoringContext context, TypeDeclaration type, string correctFileName)
		{
			var content = context.Document.Editor.Text;
			
			var types = new List<EntityDeclaration> (context.Unit.GetTypes ().Where (t => t.StartLocation != type.StartLocation));
			types.Sort ((x, y) => y.StartLocation.CompareTo (x.StartLocation));

			foreach (var removeType in types) {
				var start = context.GetOffset (removeType.StartLocation);
				var end = context.GetOffset (removeType.EndLocation);
				content = content.Remove (start, end - start);
			}
			
			if (context.Document.Project is MonoDevelop.Projects.DotNetProject) {
				string header = StandardHeaderService.GetHeader (context.Document.Project, correctFileName, true);
				if (!string.IsNullOrEmpty (header))
					content = header + context.Document.Editor.EolMarker + StripHeader (content);
			}
			content = StripDoubleBlankLines (content);
			
			File.WriteAllText (correctFileName, content);
			context.Document.Project.AddFile (correctFileName);
			MonoDevelop.Ide.IdeApp.ProjectOperations.Save (context.Document.Project);
		}