Пример #1
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);
        }
        public override void Run(Document document, TextLocation loc)
        {
            var context = new MDRefactoringContext(document, loc);

            using (var script = context.StartScript())
                act.Run(script);
        }
Пример #3
0
 public MDRefactoringScript(MDRefactoringContext context, Document document, CSharpFormattingOptions formattingOptions) : base(document.Editor.Document, formattingOptions, document.Editor.CreateNRefactoryTextEditorOptions())
 {
     this.context      = context;
     this.document     = document;
     undoGroup         = this.document.Editor.OpenUndoGroup();
     this.startVersion = this.document.Editor.Version;
 }
Пример #4
0
 internal static string GetCorrectFileName(MDRefactoringContext context, EntityDeclaration type)
 {
     if (type == null)
     {
         return(context.Document.FileName);
     }
     return(Path.Combine(Path.GetDirectoryName(context.Document.FileName), type.Name + Path.GetExtension(context.Document.FileName)));
 }
Пример #5
0
        public override void DoGlobalOperationOn(IEntity entity, Action <RefactoringContext, Script, AstNode> callback, string operationName = null)
        {
            using (var monitor = IdeApp.Workbench.ProgressMonitors.GetBackgroundProgressMonitor(operationName ?? GettextCatalog.GetString("Performing refactoring task..."), null)) {
                var col = ReferenceFinder.FindReferences(entity, true, monitor);

                string oldFileName          = null;
                MDRefactoringContext ctx    = null;
                MDRefactoringScript  script = null;
                TextEditorData       data   = null;
                bool hadBom = false;
                System.Text.Encoding encoding = null;
                bool isOpen = true;

                foreach (var r in col)
                {
                    var memberRef = r as CSharpReferenceFinder.CSharpMemberReference;
                    if (memberRef == null)
                    {
                        continue;
                    }

                    if (oldFileName != memberRef.FileName)
                    {
                        if (oldFileName != null && !isOpen)
                        {
                            script.Dispose();
                            Mono.TextEditor.Utils.TextFileUtility.WriteText(oldFileName, data.Text, encoding, hadBom);
                        }

                        data = TextFileProvider.Instance.GetTextEditorData(memberRef.FileName, out hadBom, out encoding, out isOpen);
                        var project = memberRef.Project;

                        ParsedDocument parsedDocument;
                        using (var reader = new StreamReader(data.OpenStream()))
                            parsedDocument = new MonoDevelop.PlayScript.Parser.TypeSystemParser().Parse(true, memberRef.FileName, reader, project);
                        var resolver = new CSharpAstResolver(TypeSystemService.GetCompilation(project), memberRef.SyntaxTree, parsedDocument.ParsedFile as CSharpUnresolvedFile);

                        ctx         = new MDRefactoringContext(project as DotNetProject, data, parsedDocument, resolver, memberRef.AstNode.StartLocation, this.context.CancellationToken);
                        script      = new MDRefactoringScript(ctx, FormattingOptions);
                        oldFileName = memberRef.FileName;
                    }

                    callback(ctx, script, memberRef.AstNode);
                }

                if (oldFileName != null && !isOpen)
                {
                    script.Dispose();
                    Mono.TextEditor.Utils.TextFileUtility.WriteText(oldFileName, data.Text, encoding, hadBom);
                }
            }
        }
Пример #6
0
        TypeDeclaration GetTypeDeclaration(MDRefactoringContext context)
        {
            var result = context.GetNode <TypeDeclaration> ();

            if (result == null || result.Parent is TypeDeclaration)
            {
                return(null);
            }
            if (result != null && result.NameToken.Contains(context.Location))
            {
                return(result);
            }
            return(null);
        }
Пример #7
0
        protected IEnumerable <MonoDevelop.CodeActions.CodeAction> GetActions(MDRefactoringContext context)
        {
            if (context.IsInvalid)
            {
                yield break;
            }
            var type = GetTypeDeclaration(context);

            if (type == null)
            {
                yield break;
            }
            if (Path.GetFileNameWithoutExtension(context.Document.FileName) == type.Name)
            {
                yield break;
            }
            string title;

            if (IsSingleType(context))
            {
                title = String.Format(GettextCatalog.GetString("_Rename file to '{0}'"), Path.GetFileName(GetCorrectFileName(context, type)));
            }
            else
            {
                title = String.Format(GettextCatalog.GetString("_Move type to file '{0}'"), Path.GetFileName(GetCorrectFileName(context, type)));
            }
            yield return(new MonoDevelop.CodeActions.DefaultCodeAction(title, (d, l) => {
                var ctx = new MDRefactoringContext(d, l);
                string correctFileName = GetCorrectFileName(ctx, type);
                if (IsSingleType(ctx))
                {
                    FileService.RenameFile(ctx.Document.FileName, correctFileName);
                    if (ctx.Document.Project != null)
                    {
                        ctx.Document.Project.Save(new NullProgressMonitor());
                    }
                    return;
                }

                CreateNewFile(ctx, type, correctFileName);
                using (var script = ctx.StartScript()) {
                    script.Remove(type);
                }
            }));
        }
Пример #8
0
 bool IsSingleType(MDRefactoringContext context)
 {
     return(context.Unit.GetTypes().Count() == 1);
 }