public override void BatchRun(Document document, TextLocation loc)
        {
            base.BatchRun(document, loc);
            var context = MDRefactoringContext.Create(document, loc).Result;

            if (context == null)
            {
                return;
            }
            using (var script = context.StartScript()) {
                foreach (var action in SiblingActions)
                {
                    context.SetLocation(action.DocumentRegion.Begin);
                    action.Run(context, script);
                }
            }
        }
示例#2
0
        public override Task <Script> InsertWithCursor(string operation, ITypeDefinition parentType, Func <Script, RefactoringContext, IList <AstNode> > nodeCallback)
        {
            var tcs = new TaskCompletionSource <Script>();

            if (parentType == null)
            {
                return(tcs.Task);
            }
            var part = parentType.Parts.FirstOrDefault();

            if (part == null)
            {
                return(tcs.Task);
            }

            var loadedDocument = IdeApp.Workbench.OpenDocument(new FileOpenInformation(part.Region.FileName, null));

            loadedDocument.RunWhenLoaded(delegate {
                var editor         = loadedDocument.Editor;
                var loc            = part.Region.Begin;
                var parsedDocument = loadedDocument.UpdateParseDocument();
                var declaringType  = parsedDocument.GetInnermostTypeDefinition(loc);
                MDRefactoringScript script;

                if (loadedDocument.Editor != context.TextEditor)
                {
                    script = new MDRefactoringScript(MDRefactoringContext.Create(loadedDocument, loc, context.CancellationToken).Result, FormattingOptions);
                    startedScripts.Add(script);
                }
                else
                {
                    script = this;
                }
                var nodes = nodeCallback(script, script.context);
                var mode  = new InsertionCursorEditMode(
                    editor.Parent,
                    MonoDevelop.Ide.TypeSystem.CodeGenerationService.GetInsertionPoints(loadedDocument, declaringType));
                if (mode.InsertionPoints.Count == 0)
                {
                    MessageService.ShowError(
                        GettextCatalog.GetString("No valid insertion point can be found in type '{0}'.", declaringType.Name)
                        );
                    return;
                }
                if (declaringType.Kind == TypeKind.Enum)
                {
                    foreach (var node in nodes.Reverse())
                    {
                        var output = OutputNode(MonoDevelop.Ide.TypeSystem.CodeGenerationService.CalculateBodyIndentLevel(declaringType), node);
                        var point  = mode.InsertionPoints.First();
                        var offset = loadedDocument.Editor.LocationToOffset(point.Location);
                        var text   = output.Text + ",";
                        var delta  = point.Insert(editor, text);
                        output.RegisterTrackedSegments(script, delta + offset);
                    }
                    tcs.SetResult(script);
                    return;
                }

                var helpWindow       = new Mono.TextEditor.PopupWindow.InsertionCursorLayoutModeHelpWindow();
                helpWindow.TitleText = operation;
                mode.HelpWindow      = helpWindow;

                mode.CurIndex = 0;
                operationsRunning++;
                mode.StartMode();
                mode.Exited += delegate(object s, InsertionCursorEventArgs iCArgs) {
                    if (iCArgs.Success)
                    {
                        if (iCArgs.InsertionPoint.LineAfter == NewLineInsertion.None &&
                            iCArgs.InsertionPoint.LineBefore == NewLineInsertion.None && nodes.Count > 1)
                        {
                            iCArgs.InsertionPoint.LineAfter = NewLineInsertion.BlankLine;
                        }
                        foreach (var node in nodes.Reverse())
                        {
                            var output = OutputNode(MonoDevelop.Ide.TypeSystem.CodeGenerationService.CalculateBodyIndentLevel(declaringType), node);
                            var offset = loadedDocument.Editor.LocationToOffset(iCArgs.InsertionPoint.Location);
                            var text   = output.Text;
                            var delta  = iCArgs.InsertionPoint.Insert(editor, text);
                            output.RegisterTrackedSegments(script, delta + offset);
                        }
                        tcs.SetResult(script);
                    }
                    else
                    {
                        Rollback();
                    }
                    DisposeOnClose();
                };
            });

            return(tcs.Task);
        }