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