public override void Run(RefactoringOptions options)
        {
            DocumentLocation location      = options.GetTextEditorData().Caret.Location;
            IType            interfaceType = options.Dom.GetType(options.ResolveResult.ResolvedType);
            IType            declaringType = options.Document.CompilationUnit.GetTypeAt(location.Line + 1, location.Column + 1);

            var editor = options.GetTextEditorData().Parent;

            InsertionCursorEditMode mode       = new InsertionCursorEditMode(editor, HelperMethods.GetInsertionPoints(editor.Document, declaringType));
            ModeHelpWindow          helpWindow = new ModeHelpWindow();

            helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
            helpWindow.TitleText    = GettextCatalog.GetString("<b>Implement Interface -- Targeting</b>");
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Key</b>"), GettextCatalog.GetString("<b>Behavior</b>")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Up</b>"), GettextCatalog.GetString("Move to <b>previous</b> target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Down</b>"), GettextCatalog.GetString("Move to <b>next</b> target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Enter</b>"), GettextCatalog.GetString("<b>Declare interface implementation</b> at target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Esc</b>"), GettextCatalog.GetString("<b>Cancel</b> this refactoring.")));
            mode.HelpWindow = helpWindow;
            mode.CurIndex   = mode.InsertionPoints.Count - 1;
            mode.StartMode();
            mode.Exited += delegate(object s, InsertionCursorEventArgs args) {
                if (args.Success)
                {
                    CodeGenerator generator = CodeGenerator.CreateGenerator(options.GetTextEditorData().Document.MimeType);
                    args.InsertionPoint.Insert(editor, generator.CreateInterfaceImplementation(declaringType, interfaceType, false));
                }
            };
        }
Пример #2
0
        void OnOKClicked(object sender, EventArgs e)
        {
            try {
                StringBuilder code          = new StringBuilder();
                CodeGenerator generator     = CodeGenerator.CreateGenerator(editor.Editor.Document.MimeType, editor.Editor.TabsToSpaces, editor.Editor.Options.TabSize, editor.Editor.EolMarker);
                IType         declaringType = editor.GetType(cls.Location.Line, cls.Location.Column) ?? cls;

                foreach (KeyValuePair <IType, IEnumerable <TreeIter> > kvp in GetAllClasses())
                {
                    if (code.Length > 0)
                    {
                        code.AppendLine();
                        code.AppendLine();
                    }

                    //update the target class so that new members don't get inserted in weird locations
                    StringBuilder curImpl = new StringBuilder();
                    foreach (var pair in YieldImpls(kvp))
                    {
                        if (curImpl.Length > 0)
                        {
                            curImpl.AppendLine();
                            curImpl.AppendLine();
                        }
                        curImpl.Append(generator.CreateMemberImplementation(declaringType, pair.Key, pair.Value != null).Code);
                    }
                    if (kvp.Key.ClassType == ClassType.Interface)
                    {
                        code.Append(generator.WrapInRegions(kvp.Key.Name + " implementation", curImpl.ToString()));
                    }
                    else
                    {
                        code.Append(curImpl.ToString());
                    }
                }

                var mode       = new InsertionCursorEditMode(editor.Editor.Parent, CodeGenerationService.GetInsertionPoints(editor, this.cls));
                var helpWindow = new ModeHelpWindow();
                helpWindow.Shown       += (s, a) => DesktopService.RemoveWindowShadow(helpWindow);
                helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
                helpWindow.TitleText    = GettextCatalog.GetString("<b>Override -- Targeting</b>");
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Key</b>"), GettextCatalog.GetString("<b>Behavior</b>")));
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Up</b>"), GettextCatalog.GetString("Move to <b>previous</b> target point.")));
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Down</b>"), GettextCatalog.GetString("Move to <b>next</b> target point.")));
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Enter</b>"), GettextCatalog.GetString("<b>Declare overrides</b> at target point.")));
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Esc</b>"), GettextCatalog.GetString("<b>Cancel</b> this refactoring.")));
                mode.HelpWindow = helpWindow;
                mode.CurIndex   = mode.InsertionPoints.Count - 1;
                mode.StartMode();
                mode.Exited += delegate(object s, InsertionCursorEventArgs args) {
                    if (args.Success)
                    {
                        args.InsertionPoint.Insert(editor.Editor, code.ToString());
                    }
                };
            } finally {
                ((Widget)this).Destroy();
            }
        }
Пример #3
0
            public override void InsertWithCursor(string operation, AstNode node, InsertPosition defaultPosition)
            {
                var editor     = ctx.Document.Editor;
                var mode       = new InsertionCursorEditMode(editor.Parent, MonoDevelop.Ide.CodeGenerationService.GetInsertionPoints(ctx.Document, ctx.Document.CompilationUnit.GetTypeAt(ctx.Location.Line, ctx.Location.Column)));
                var helpWindow = new Mono.TextEditor.PopupWindow.ModeHelpWindow();

                helpWindow.TransientFor = MonoDevelop.Ide.IdeApp.Workbench.RootWindow;
                helpWindow.TitleText    = string.Format(GettextCatalog.GetString("<b>{0} -- Targeting</b>"), operation);
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Key</b>"), GettextCatalog.GetString("<b>Behavior</b>")));
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Up</b>"), GettextCatalog.GetString("Move to <b>previous</b> target point.")));
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Down</b>"), GettextCatalog.GetString("Move to <b>next</b> target point.")));
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Enter</b>"), GettextCatalog.GetString("<b>Accept</b> target point.")));
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Esc</b>"), GettextCatalog.GetString("<b>Cancel</b> this operation.")));
                mode.HelpWindow = helpWindow;

                switch (defaultPosition)
                {
                case InsertPosition.Start:
                    mode.CurIndex = 0;
                    break;

                case InsertPosition.End:
                    mode.CurIndex = mode.InsertionPoints.Count - 1;
                    break;

                case InsertPosition.Before:
                    for (int i = 0; i < mode.InsertionPoints.Count; i++)
                    {
                        if (mode.InsertionPoints [i].Location < new DocumentLocation(ctx.Location.Line, ctx.Location.Column))
                        {
                            mode.CurIndex = i;
                        }
                    }
                    break;

                case InsertPosition.After:
                    for (int i = 0; i < mode.InsertionPoints.Count; i++)
                    {
                        if (mode.InsertionPoints [i].Location > new DocumentLocation(ctx.Location.Line, ctx.Location.Column))
                        {
                            mode.CurIndex = i;
                            break;
                        }
                    }
                    break;
                }

                mode.StartMode();
                mode.Exited += delegate(object s, InsertionCursorEventArgs iCArgs) {
                    if (iCArgs.Success)
                    {
                        var output = OutputNode(GetIndentLevelAt(editor.LocationToOffset(iCArgs.InsertionPoint.Location)), node);
                        iCArgs.InsertionPoint.Insert(editor, output.Text);
                    }
                };
            }
        public static void Implement(RefactoringOptions options, IType abstractType)
        {
            var        document = options.Document;
            TextEditor editor   = document.Editor.Parent;

            var loc           = document.Editor.Caret.Location;
            var declaringType = document.ParsedDocument.GetInnermostTypeDefinition(loc);
            var type          = options.ResolveResult.Type;

            var mode = new InsertionCursorEditMode(editor, CodeGenerationService.GetInsertionPoints(document, declaringType));

            if (mode.InsertionPoints.Count == 0)
            {
                MessageService.ShowError(
                    GettextCatalog.GetString("No valid insertion point can be found in type '{0}'.", declaringType.Name));
                return;
            }
            var helpWindow = new InsertionCursorLayoutModeHelpWindow();

            helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
            helpWindow.TitleText    = GettextCatalog.GetString("Implement Abstract Members");
            mode.HelpWindow         = helpWindow;
            mode.CurIndex           = mode.InsertionPoints.Count - 1;
            mode.StartMode();
            mode.Exited += delegate(object s, InsertionCursorEventArgs args) {
                if (args.Success)
                {
                    var generator = options.CreateCodeGenerator();
                    if (generator == null)
                    {
                        return;
                    }

                    var def = type.GetDefinition();
                    var missingAbstractMembers = def.Members.Where(member => member.IsAbstract && !declaringType.Members.Any(m => member.Name == m.Name));
                    var sb = new StringBuilder();
                    foreach (var member in missingAbstractMembers)
                    {
                        if (sb.Length > 0)
                        {
                            sb.Append(editor.EolMarker);
                            sb.Append(editor.EolMarker);
                        }
                        var resolvedType = declaringType.Resolve(options.Document.ParsedDocument.GetTypeResolveContext(options.Document.Compilation, loc)).GetDefinition();
                        var impl         = generator.CreateMemberImplementation(resolvedType, declaringType, member, false);
                        if (impl != null)
                        {
                            sb.Append(impl.Code);
                        }
                    }
                    args.InsertionPoint.Insert(document.Editor, generator.WrapInRegions("implemented abstract members of " + abstractType.FullName, sb.ToString()));
                }
            };
        }
Пример #5
0
        void OnOKClicked(object sender, EventArgs e)
        {
            TextEditorData data = options.GetTextEditorData();

            Mono.TextEditor.TextEditor editor = data.Parent;
// Insertion cursor mode test:
            if (editor != null)
            {
                IType type = properties.DeclaringMember.DeclaringType;

                InsertionCursorEditMode mode = new InsertionCursorEditMode(editor, CodeGenerationService.GetInsertionPoints(options.Document, type));
                for (int i = 0; i < mode.InsertionPoints.Count; i++)
                {
                    var point = mode.InsertionPoints[i];
                    if (point.Location < editor.Caret.Location)
                    {
                        mode.CurIndex = i;
                    }
                    else
                    {
                        break;
                    }
                }
                ModeHelpWindow helpWindow = new ModeHelpWindow();
                helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
                helpWindow.TitleText    = GettextCatalog.GetString("<b>Extract Method -- Targeting</b>");
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Key</b>"), GettextCatalog.GetString("<b>Behavior</b>")));
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Up</b>"), GettextCatalog.GetString("Move to <b>previous</b> target point.")));
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Down</b>"), GettextCatalog.GetString("Move to <b>next</b> target point.")));
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Enter</b>"), GettextCatalog.GetString("<b>Declare new method</b> at target point.")));
                helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Esc</b>"), GettextCatalog.GetString("<b>Cancel</b> this refactoring.")));
                mode.HelpWindow = helpWindow;
                mode.StartMode();
                methodName       = entry.Text;
                activeModifier   = comboboxModifiers.Active;
                generateComments = checkbuttonGenerateComment.Active;

                mode.Exited += delegate(object s, InsertionCursorEventArgs args) {
                    if (args.Success)
                    {
                        SetProperties();
                        properties.InsertionPoint = args.InsertionPoint;
                        List <Change>    changes = extractMethod.PerformChanges(options, properties);
                        IProgressMonitor monitor = IdeApp.Workbench.ProgressMonitors.GetBackgroundProgressMonitor(this.Title, null);
                        RefactoringService.AcceptChanges(monitor, options.Dom, changes);
                    }
                };
            }

            ((Widget)this).Destroy();
        }
Пример #6
0
        internal static void InternalRun(RefactoringOptions options, bool implementExplicit)
        {
            IType interfaceType;

            if (!InternalIsValid(options, out interfaceType))
            {
                return;
            }
            var loc           = options.Document.Editor.Caret.Location;
            var declaringType = options.Document.ParsedDocument.GetInnermostTypeDefinition(loc);

            if (declaringType == null)
            {
                return;
            }

            var editor = options.GetTextEditorData().Parent;

            var mode = new InsertionCursorEditMode(
                editor,
                CodeGenerationService.GetInsertionPoints(options.Document, declaringType));

            if (mode.InsertionPoints.Count == 0)
            {
                MessageService.ShowError(
                    GettextCatalog.GetString("No valid insertion point can be found in type '{0}'.", declaringType.Name)
                    );
                return;
            }

            var helpWindow = new InsertionCursorLayoutModeHelpWindow();

            helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
            helpWindow.TitleText    = GettextCatalog.GetString("Implement Interface");
            mode.HelpWindow         = helpWindow;
            mode.CurIndex           = mode.InsertionPoints.Count - 1;
            mode.StartMode();
            mode.Exited += delegate(object s, InsertionCursorEventArgs args) {
                if (args.Success)
                {
                    var generator = options.CreateCodeGenerator();
                    if (generator == null)
                    {
                        return;
                    }
                    var type = declaringType.Resolve(options.Document.ParsedDocument.GetTypeResolveContext(options.Document.Compilation, loc)).GetDefinition();
                    args.InsertionPoint.Insert(options.GetTextEditorData(), generator.CreateInterfaceImplementation(type, declaringType, interfaceType, implementExplicit));
                }
            };
        }
Пример #7
0
        public override void InsertWithCursor(string operation, ITypeDefinition parentType, IEnumerable <AstNode> nodes)
        {
            var part = parentType.Parts.FirstOrDefault();

            if (part == null)
            {
                return;
            }

            var loadedDocument = Ide.IdeApp.Workbench.OpenDocument(part.Region.FileName);

            loadedDocument.RunWhenLoaded(delegate {
                var editor         = loadedDocument.Editor;
                var loc            = part.Region.Begin;
                var parsedDocument = loadedDocument.UpdateParseDocument();
                var declaringType  = parsedDocument.GetInnermostTypeDefinition(loc);
                var mode           = new InsertionCursorEditMode(
                    editor.Parent,
                    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;
                }
                var helpWindow          = new Mono.TextEditor.PopupWindow.InsertionCursorLayoutModeHelpWindow();
                helpWindow.TransientFor = MonoDevelop.Ide.IdeApp.Workbench.RootWindow;
                helpWindow.TitleText    = operation;
                mode.HelpWindow         = helpWindow;

                mode.CurIndex = 0;

                mode.StartMode();
                mode.Exited += delegate(object s, InsertionCursorEventArgs iCArgs) {
                    if (iCArgs.Success)
                    {
                        foreach (var node in nodes)
                        {
                            var output = OutputNode(CodeGenerationService.CalculateBodyIndentLevel(declaringType), node);
                            output.RegisterTrackedSegments(this, loadedDocument.Editor.LocationToOffset(iCArgs.InsertionPoint.Location));
                            iCArgs.InsertionPoint.Insert(editor, output.Text);
                        }
                    }
                };
            });
        }
        void CreateTestMethod(string className, string methodName)
        {
            var methodText = TestMethodTemplate.Replace("METHODNAME", "Test_" + methodName);
            var document   = IdeApp.Workbench.ActiveDocument;
            var editor     = document.Editor;

            var declaringType = MemberExtensionsHelper.Instance.GetDeclaringTypeWithName(className);

            if (declaringType == null)
            {
                return;
            }
            var mode = new InsertionCursorEditMode(
                editor.Parent,
                CodeGenerationService.GetInsertionPoints(document, declaringType));

            if (mode.InsertionPoints.Count == 0)
            {
                MessageService.ShowError(
                    GettextCatalog.GetString("No valid insertion point can be found in type '{0}'.", declaringType.Name)
                    );
                return;
            }
            var helpWindow = new Mono.TextEditor.PopupWindow.InsertionCursorLayoutModeHelpWindow();

//				helpWindow.Window = MonoDevelop.Ide.IdeApp.Workbench.RootWindow;
            helpWindow.TitleText = "Insert test method";
            mode.HelpWindow      = helpWindow;

            mode.CurIndex = 0;
            mode.StartMode();
            mode.Exited += delegate(object s, InsertionCursorEventArgs iCArgs) {
                if (iCArgs.Success)
                {
                    iCArgs.InsertionPoint.Insert(document.Editor, methodText);

                    editor.SetCaretTo(iCArgs.InsertionPoint.Location.Line, iCArgs.InsertionPoint.Location.Column, true, false);
                }
            };
        }
        public static void Implement(MonoDevelop.Ide.Gui.Document document, IType abstractType)
        {
            TextEditor editor = document.Editor.Parent;

            DocumentLocation location      = editor.Caret.Location;
            IType            declaringType = document.CompilationUnit.GetTypeAt(location.Line, location.Column);

            InsertionCursorEditMode mode       = new InsertionCursorEditMode(editor, CodeGenerationService.GetInsertionPoints(document, declaringType));
            ModeHelpWindow          helpWindow = new ModeHelpWindow();

            helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
            helpWindow.TitleText    = GettextCatalog.GetString("<b>Implement abstract members -- Targeting</b>");
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Key</b>"), GettextCatalog.GetString("<b>Behavior</b>")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Up</b>"), GettextCatalog.GetString("Move to <b>previous</b> target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Down</b>"), GettextCatalog.GetString("Move to <b>next</b> target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Enter</b>"), GettextCatalog.GetString("<b>Declare interface implementation</b> at target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Esc</b>"), GettextCatalog.GetString("<b>Cancel</b> this refactoring.")));
            mode.HelpWindow = helpWindow;
            mode.CurIndex   = mode.InsertionPoints.Count - 1;
            mode.StartMode();
            mode.Exited += delegate(object s, InsertionCursorEventArgs args) {
                if (args.Success)
                {
                    CodeGenerator generator = document.CreateCodeGenerator();
                    var           missingAbstractMembers = abstractType.Members.Where(member => member.IsAbstract && !member.IsSpecialName && !declaringType.Members.Any(m => member.Name == m.Name));
                    StringBuilder sb = new StringBuilder();
                    foreach (var member in missingAbstractMembers)
                    {
                        if (sb.Length > 0)
                        {
                            sb.Append(editor.EolMarker);
                            sb.Append(editor.EolMarker);
                        }

                        sb.Append(generator.CreateMemberImplementation(declaringType, member, false).Code);
                    }
                    args.InsertionPoint.Insert(document.Editor, generator.WrapInRegions("implemented abstract members of " + abstractType.FullName, sb.ToString()));
                }
            };
        }
Пример #10
0
        public override void InsertWithCursor(string operation, ITypeDefinition parentType, IEnumerable <AstNode> nodes, Action continuation)
        {
            if (parentType == null)
            {
                return;
            }
            var part = parentType.Parts.FirstOrDefault();

            if (part == null)
            {
                return;
            }

            var loadedDocument = Ide.IdeApp.Workbench.OpenDocument(part.Region.FileName);

            loadedDocument.RunWhenLoaded(delegate {
                var editor         = loadedDocument.Editor;
                var loc            = part.Region.Begin;
                var parsedDocument = loadedDocument.UpdateParseDocument();
                var declaringType  = parsedDocument.GetInnermostTypeDefinition(loc);
                var mode           = new InsertionCursorEditMode(
                    editor.Parent,
                    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(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(this, delta + offset);
                    }
                    if (continuation != null)
                    {
                        continuation();
                    }
                    return;
                }


                var helpWindow          = new Mono.TextEditor.PopupWindow.InsertionCursorLayoutModeHelpWindow();
                helpWindow.TransientFor = MonoDevelop.Ide.IdeApp.Workbench.RootWindow;
                helpWindow.TitleText    = operation;
                helpWindow.Shown       += (s, a) => DesktopService.RemoveWindowShadow(helpWindow);
                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(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(this, delta + offset);
                        }
                        if (continuation != null)
                        {
                            continuation();
                        }
                    }
                    else
                    {
                        Rollback();
                    }
                    DisposeOnClose();
                };
            });
        }
Пример #11
0
        public static Task <bool> InsertMemberWithCursor(
            string operation, ITypeDefinition parentType, IUnresolvedTypeDefinition part,
            IUnresolvedMember newMember, bool implementExplicit = false)
        {
            var tcs = new TaskCompletionSource <bool>();

            if (parentType == null)
            {
                return(tcs.Task);
            }
            part = part ?? FindCurrentPart(parentType);
            if (part == null)
            {
                return(tcs.Task);
            }
            var loadedDocument = IdeApp.Workbench.OpenDocument(part.Region.FileName);

            loadedDocument.RunWhenLoaded(delegate {
                var editor         = loadedDocument.Editor;
                var loc            = part.Region.Begin;
                var parsedDocument = loadedDocument.UpdateParseDocument();
                var declaringType  = parsedDocument.GetInnermostTypeDefinition(loc);
                var mode           = new InsertionCursorEditMode(
                    editor.Parent,
                    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;
                }
                var suitableInsertionPoint = GetSuitableInsertionPoint(mode.InsertionPoints, part, newMember);
                if (suitableInsertionPoint != null)
                {
                    mode.CurIndex = mode.InsertionPoints.IndexOf(suitableInsertionPoint);
                }
                else
                {
                    mode.CurIndex = 0;
                }

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

                mode.StartMode();
                mode.Exited += delegate(object s, InsertionCursorEventArgs iCArgs) {
                    if (!iCArgs.Success)
                    {
                        tcs.SetResult(false);
                        return;
                    }
                    var generator         = CreateCodeGenerator(editor, parentType.Compilation);
                    generator.IndentLevel = CalculateBodyIndentLevel(declaringType);
                    var generatedCode     = generator.CreateMemberImplementation(parentType, part, newMember, implementExplicit);
                    mode.InsertionPoints[mode.CurIndex].Insert(editor, generatedCode.Code);
                    tcs.SetResult(true);
                };
            });

            return(tcs.Task);
        }
        void OnOKClicked(object sender, EventArgs e)
        {
            TreeIter iter;

            if (!store.GetIterFirst(out iter))
            {
                return;
            }

            List <FieldData> data = new List <FieldData> ();

            do
            {
                bool selected = (bool)store.GetValue(iter, colCheckedIndex);
                if (!selected)
                {
                    continue;
                }

                string    propertyName = (string)store.GetValue(iter, colPropertyNameIndex);
                string    visibility   = (string)store.GetValue(iter, colVisibilityIndex);
                bool      read_only    = (bool)store.GetValue(iter, colReadOnlyIndex);
                IField    field        = (IField)store.GetValue(iter, colFieldIndex);
                Modifiers mod          = Modifiers.None;
                if (visibility.ToUpper() == "PUBLIC")
                {
                    mod = Modifiers.Public;
                }
                if (visibility.ToUpper() == "PRIVATE")
                {
                    mod = Modifiers.Private;
                }
                if (visibility.ToUpper() == "PROTECTED")
                {
                    mod = Modifiers.Protected;
                }
                if (visibility.ToUpper() == "INTERNAL")
                {
                    mod = Modifiers.Internal;
                }
                data.Add(new FieldData(field, propertyName, read_only, mod));
            } while (store.IterNext(ref iter));

            InsertionCursorEditMode mode       = new InsertionCursorEditMode(editor, HelperMethods.GetInsertionPoints(editor.Document, declaringType));
            ModeHelpWindow          helpWindow = new ModeHelpWindow();

            helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
            helpWindow.TitleText    = GettextCatalog.GetString("<b>Encapsulate Field -- Targeting</b>");
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Key</b>"), GettextCatalog.GetString("<b>Behavior</b>")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Up</b>"), GettextCatalog.GetString("Move to <b>previous</b> target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Down</b>"), GettextCatalog.GetString("Move to <b>next</b> target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Enter</b>"), GettextCatalog.GetString("<b>Declare new property</b> at target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Esc</b>"), GettextCatalog.GetString("<b>Cancel</b> this refactoring.")));
            mode.HelpWindow = helpWindow;
            mode.CurIndex   = mode.InsertionPoints.Count - 1;
            int         idx = -1, i = 0;
            DomLocation lastLocation = DomLocation.Empty;

            foreach (IMember member in declaringType.Members)
            {
                if (lastLocation != member.Location && data.Any(d => d.Field.Location == member.Location))
                {
                    idx = i;
                }
                lastLocation = member.Location;
                i++;
            }
            if (idx >= 0)
            {
                mode.CurIndex = idx + 1;
            }
            mode.StartMode();
            mode.Exited += delegate(object s, InsertionCursorEventArgs args) {
                if (args.Success)
                {
                    CodeGenerator generator = CodeGenerator.CreateGenerator(editor.Document.MimeType);
                    StringBuilder code      = new StringBuilder();
                    for (int j = 0; j < data.Count; j++)
                    {
                        if (j > 0)
                        {
                            code.AppendLine();
                            code.AppendLine();
                        }
                        var f = data[j];
                        code.Append(generator.CreateFieldEncapsulation(declaringType, f.Field, f.PropertyName, f.Modifiers, f.ReadOnly));
                    }
                    args.InsertionPoint.Insert(editor, code.ToString());
                }
            };
            ((Widget)this).Destroy();
        }
Пример #13
0
        public override Task <Script> InsertWithCursor(string operation, ITypeDefinition parentType, Func <Script, RefactoringContext, IEnumerable <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 = Ide.IdeApp.Workbench.OpenDocument(part.Region.FileName);

            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 != this.context.TextEditor)
                {
                    script = new MDRefactoringScript(new MDRefactoringContext(loadedDocument, loc, context.CancellationToken), FormattingOptions);
                    startedScripts.Add(script);
                }
                else
                {
                    script = this;
                }
                var nodes = nodeCallback(script, script.context);
                var mode  = new InsertionCursorEditMode(
                    editor.Parent,
                    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(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(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);
        }
Пример #14
0
        public override Task <Script> InsertWithCursor(string operation, InsertPosition defaultPosition, IEnumerable <AstNode> nodes)
        {
            var tcs              = new TaskCompletionSource <Script> ();
            var editor           = context.TextEditor;
            DocumentLocation loc = context.TextEditor.Caret.Location;
            var declaringType    = context.ParsedDocument.GetInnermostTypeDefinition(loc);
            var mode             = new InsertionCursorEditMode(
                editor.Parent,
                CodeGenerationService.GetInsertionPoints(context.TextEditor, context.ParsedDocument, declaringType));

            if (mode.InsertionPoints.Count == 0)
            {
                MessageService.ShowError(
                    GettextCatalog.GetString("No valid insertion point can be found in type '{0}'.", declaringType.Name)
                    );
                return(tcs.Task);
            }
            var helpWindow = new Mono.TextEditor.PopupWindow.InsertionCursorLayoutModeHelpWindow();

            helpWindow.TitleText = operation;
            mode.HelpWindow      = helpWindow;

            switch (defaultPosition)
            {
            case InsertPosition.Start:
                mode.CurIndex = 0;
                break;

            case InsertPosition.End:
                mode.CurIndex = mode.InsertionPoints.Count - 1;
                break;

            case InsertPosition.Before:
                for (int i = 0; i < mode.InsertionPoints.Count; i++)
                {
                    if (mode.InsertionPoints [i].Location < loc)
                    {
                        mode.CurIndex = i;
                    }
                }
                break;

            case InsertPosition.After:
                for (int i = 0; i < mode.InsertionPoints.Count; i++)
                {
                    if (mode.InsertionPoints [i].Location > loc)
                    {
                        mode.CurIndex = i;
                        break;
                    }
                }
                break;
            }
            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(CodeGenerationService.CalculateBodyIndentLevel(declaringType), node);
                        var offset = context.TextEditor.LocationToOffset(iCArgs.InsertionPoint.Location);
                        var delta  = iCArgs.InsertionPoint.Insert(editor, output.Text);
                        output.RegisterTrackedSegments(this, delta + offset);
                    }
                    tcs.SetResult(this);
                }
                else
                {
                    Rollback();
                }
                DisposeOnClose();
            };
            return(tcs.Task);
        }
Пример #15
0
        public override void Run(RefactoringOptions options)
        {
            IResolver resolver = options.GetResolver();
            INRefactoryASTProvider provider = options.GetASTProvider();
            TextEditorData         data     = options.GetTextEditorData();
            IType type = options.ResolveResult.CallingType;

            if (invoke.TargetObject is IdentifierExpression)
            {
                fileName      = options.Document.FileName;
                newMethodName = ((IdentifierExpression)invoke.TargetObject).Identifier;
                indent        = options.GetIndent(options.ResolveResult.CallingMember);
                if (options.ResolveResult.CallingMember.IsStatic)
                {
                    modifiers |= ICSharpCode.NRefactory.Ast.Modifiers.Static;
                }
            }
            else
            {
                newMethodName = ((MemberReferenceExpression)invoke.TargetObject).MemberName;
                string        callingObject = provider.OutputNode(options.Dom, ((MemberReferenceExpression)invoke.TargetObject).TargetObject);
                ResolveResult resolveResult = resolver.Resolve(new ExpressionResult(callingObject), resolvePosition);
                type     = options.Dom.GetType(resolveResult.ResolvedType);
                fileName = type.CompilationUnit.FileName;
                if (resolveResult.StaticResolve)
                {
                    modifiers |= ICSharpCode.NRefactory.Ast.Modifiers.Static;
                }

                if (fileName == options.Document.FileName)
                {
                    indent = options.GetIndent(options.ResolveResult.CallingMember);
//					insertNewMethod.Offset = options.Document.TextEditor.GetPositionFromLineColumn (options.ResolveResult.CallingMember.BodyRegion.End.Line, options.ResolveResult.CallingMember.BodyRegion.End.Column);
                }
                else
                {
                    var openDocument = IdeApp.Workbench.OpenDocument(fileName);
                    data = openDocument.TextEditorData;
                    if (data == null)
                    {
                        return;
                    }
                    modifiers |= ICSharpCode.NRefactory.Ast.Modifiers.Public;
                    bool isInInterface = type.ClassType == MonoDevelop.Projects.Dom.ClassType.Interface;
                    if (isInInterface)
                    {
                        modifiers = ICSharpCode.NRefactory.Ast.Modifiers.None;
                    }
                    if (data == null)
                    {
                        throw new InvalidOperationException("Can't open file:" + modifiers);
                    }
                    try {
                        indent = data.Document.GetLine(type.Location.Line - 1).GetIndentation(data.Document) ?? "";
                    } catch (Exception) {
                        indent = "";
                    }
                    indent += "\t";
//					insertNewMethod.Offset = otherFile.Document.LocationToOffset (type.BodyRegion.End.Line - 1, 0);
                }
            }

            InsertionCursorEditMode mode = new InsertionCursorEditMode(data.Parent, HelperMethods.GetInsertionPoints(data.Document, type));

            if (fileName == options.Document.FileName)
            {
                for (int i = 0; i < mode.InsertionPoints.Count; i++)
                {
                    var point = mode.InsertionPoints[i];
                    if (point.Location < data.Caret.Location)
                    {
                        mode.CurIndex = i;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            ModeHelpWindow helpWindow = new ModeHelpWindow();

            helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
            helpWindow.TitleText    = GettextCatalog.GetString("<b>Create Method -- Targeting</b>");
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Key</b>"), GettextCatalog.GetString("<b>Behavior</b>")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Up</b>"), GettextCatalog.GetString("Move to <b>previous</b> target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Down</b>"), GettextCatalog.GetString("Move to <b>next</b> target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Enter</b>"), GettextCatalog.GetString("<b>Declare new method</b> at target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Esc</b>"), GettextCatalog.GetString("<b>Cancel</b> this refactoring.")));
            mode.HelpWindow = helpWindow;
            mode.StartMode();
            mode.Exited += delegate(object s, InsertionCursorEventArgs args) {
                if (args.Success)
                {
                    insertionPoint  = args.InsertionPoint;
                    insertionOffset = data.Document.LocationToOffset(args.InsertionPoint.Location);
                    base.Run(options);
                    if (string.IsNullOrEmpty(fileName))
                    {
                        return;
                    }
                    MonoDevelop.Ide.Gui.Document document = IdeApp.Workbench.OpenDocument(fileName);
                    TextEditorData docData = document.TextEditorData;
                    if (docData != null)
                    {
                        docData.ClearSelection();
                        docData.Caret.Offset = selectionEnd;
                        docData.SetSelection(selectionStart, selectionEnd);
                    }
                }
            };
        }
Пример #16
0
        public override void Run(RefactoringOptions options)
        {
            TextEditorData data = options.GetTextEditorData();

            fileName = declaringType.CompilationUnit.FileName;

            var openDocument = IdeApp.Workbench.OpenDocument(fileName);

            if (openDocument == null)
            {
                MessageService.ShowError(string.Format(GettextCatalog.GetString("Can't open file {0}."), fileName));
                return;
            }
            data = openDocument.Editor;
            if (data == null)
            {
                return;
            }

            if (data == null)
            {
                throw new InvalidOperationException("Can't open file:" + modifiers);
            }
            try {
                indent = data.Document.GetLine(declaringType.Location.Line).GetIndentation(data.Document) ?? "";
            } catch (Exception) {
                indent = "";
            }
            indent += "\t";

            InsertionCursorEditMode mode = new InsertionCursorEditMode(data.Parent, CodeGenerationService.GetInsertionPoints(options.Document, declaringType));

            if (fileName == options.Document.FileName)
            {
                for (int i = 0; i < mode.InsertionPoints.Count; i++)
                {
                    var point = mode.InsertionPoints [i];
                    if (point.Location < data.Caret.Location)
                    {
                        mode.CurIndex = i;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            ModeHelpWindow helpWindow = new ModeHelpWindow();

            helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
            helpWindow.TitleText    = GettextCatalog.GetString("<b>Create Method -- Targeting</b>");
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Key</b>"), GettextCatalog.GetString("<b>Behavior</b>")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Up</b>"), GettextCatalog.GetString("Move to <b>previous</b> target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Down</b>"), GettextCatalog.GetString("Move to <b>next</b> target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Enter</b>"), GettextCatalog.GetString("<b>Declare new method</b> at target point.")));
            helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Esc</b>"), GettextCatalog.GetString("<b>Cancel</b> this refactoring.")));
            mode.HelpWindow = helpWindow;
            mode.StartMode();
            mode.Exited += delegate(object s, InsertionCursorEventArgs args) {
                if (args.Success)
                {
                    SetInsertionPoint(args.InsertionPoint);
                    BaseRun(options);
                    if (string.IsNullOrEmpty(fileName))
                    {
                        return;
                    }
                    MonoDevelop.Ide.Gui.Document document = IdeApp.Workbench.OpenDocument(fileName);
                    TextEditorData docData = document.Editor;
                    if (docData != null)
                    {
                        docData.ClearSelection();
                        docData.Caret.Offset = selectionEnd;
                        docData.SetSelection(selectionStart, selectionEnd);
                    }
                }
            };
        }
Пример #17
0
        public override void InsertWithCursor(string operation, InsertPosition defaultPosition, IEnumerable <AstNode> nodes)
        {
            var editor           = document.Editor;
            DocumentLocation loc = document.Editor.Caret.Location;
            var declaringType    = document.ParsedDocument.GetInnermostTypeDefinition(loc);
            var mode             = new InsertionCursorEditMode(
                editor.Parent,
                CodeGenerationService.GetInsertionPoints(document, declaringType));

            if (mode.InsertionPoints.Count == 0)
            {
                MessageService.ShowError(
                    GettextCatalog.GetString("No valid insertion point can be found in type '{0}'.", declaringType.Name)
                    );
                return;
            }
            var helpWindow = new Mono.TextEditor.PopupWindow.InsertionCursorLayoutModeHelpWindow();

            helpWindow.TransientFor = MonoDevelop.Ide.IdeApp.Workbench.RootWindow;
            helpWindow.TitleText    = operation;
            mode.HelpWindow         = helpWindow;

            switch (defaultPosition)
            {
            case InsertPosition.Start:
                mode.CurIndex = 0;
                break;

            case InsertPosition.End:
                mode.CurIndex = mode.InsertionPoints.Count - 1;
                break;

            case InsertPosition.Before:
                for (int i = 0; i < mode.InsertionPoints.Count; i++)
                {
                    if (mode.InsertionPoints [i].Location < loc)
                    {
                        mode.CurIndex = i;
                    }
                }
                break;

            case InsertPosition.After:
                for (int i = 0; i < mode.InsertionPoints.Count; i++)
                {
                    if (mode.InsertionPoints [i].Location > loc)
                    {
                        mode.CurIndex = i;
                        break;
                    }
                }
                break;
            }

            mode.StartMode();
            mode.Exited += delegate(object s, InsertionCursorEventArgs iCArgs) {
                if (iCArgs.Success)
                {
                    foreach (var node in nodes)
                    {
                        var output = OutputNode(CodeGenerationService.CalculateBodyIndentLevel(declaringType), node);
                        output.RegisterTrackedSegments(this, document.Editor.LocationToOffset(iCArgs.InsertionPoint.Location));
                        iCArgs.InsertionPoint.Insert(editor, output.Text);
                    }
                }
            };
        }