コード例 #1
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);
        }
コード例 #2
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);
        }