Esempio n. 1
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);
        }