protected override void Load(ICSharpCode.SharpDevelop.Project.MSBuildBasedProject project, string configuration, string platform)
 {
     base.Load(project, configuration, platform);
     if (persistenceHelper != null)
     {
         persistenceHelper.OptionsContainer.PropertyChanged -= ContainerPropertyChanged;
     }
     persistenceHelper = CSharpFormattingOptionsPersistence.GetProjectOptions(project);
     formattingEditor.OptionsContainer = persistenceHelper.OptionsContainer;
     formattingEditor.AllowPresets     = true;
     persistenceHelper.Load();
     persistenceHelper.OptionsContainer.PropertyChanged += ContainerPropertyChanged;
 }
        public override void Complete(CompletionContext context)
        {
            if (declarationBegin > context.StartOffset)
            {
                base.Complete(context);
                return;
            }

            TypeSystemAstBuilder b = new TypeSystemAstBuilder(contextAtCaret);

            b.ShowTypeParameterConstraints = false;
            b.GenerateBody = true;

            var entityDeclaration = b.ConvertEntity(this.Entity);

            entityDeclaration.Modifiers &= ~(Modifiers.Virtual | Modifiers.Abstract);
            entityDeclaration.Modifiers |= Modifiers.Override;

            if (!this.Entity.IsAbstract)
            {
                // modify body to call the base method
                if (this.Entity.SymbolKind == SymbolKind.Method)
                {
                    var baseCall = new BaseReferenceExpression().Invoke(this.Entity.Name, ParametersToExpressions(this.Entity));
                    var body     = entityDeclaration.GetChildByRole(Roles.Body);
                    body.Statements.Clear();
                    if (((IMethod)this.Entity).ReturnType.IsKnownType(KnownTypeCode.Void))
                    {
                        body.Statements.Add(new ExpressionStatement(baseCall));
                    }
                    else
                    {
                        body.Statements.Add(new ReturnStatement(baseCall));
                    }
                }
                else if (this.Entity.SymbolKind == SymbolKind.Indexer || this.Entity.SymbolKind == SymbolKind.Property)
                {
                    Expression baseCall;
                    if (this.Entity.SymbolKind == SymbolKind.Indexer)
                    {
                        baseCall = new BaseReferenceExpression().Indexer(ParametersToExpressions(this.Entity));
                    }
                    else
                    {
                        baseCall = new BaseReferenceExpression().Member(this.Entity.Name);
                    }
                    var getterBody = entityDeclaration.GetChildByRole(PropertyDeclaration.GetterRole).Body;
                    if (!getterBody.IsNull)
                    {
                        getterBody.Statements.Clear();
                        getterBody.Add(new ReturnStatement(baseCall.Clone()));
                    }
                    var setterBody = entityDeclaration.GetChildByRole(PropertyDeclaration.SetterRole).Body;
                    if (!setterBody.IsNull)
                    {
                        setterBody.Statements.Clear();
                        setterBody.Add(new AssignmentExpression(baseCall.Clone(), new IdentifierExpression("value")));
                    }
                }
            }

            var          document          = context.Editor.Document;
            StringWriter w                 = new StringWriter();
            var          formattingOptions = CSharpFormattingOptionsPersistence.GetProjectOptions(contextAtCaret.Compilation.GetProject());
            var          segmentDict       = SegmentTrackingOutputFormatter.WriteNode(
                w, entityDeclaration, formattingOptions.OptionsContainer.GetEffectiveOptions(), context.Editor.Options);

            using (document.OpenUndoGroup()) {
                string newText = w.ToString().TrimEnd();
                document.Replace(declarationBegin, context.EndOffset - declarationBegin, newText);
                var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement);
                if (throwStatement != null)
                {
                    var segment = segmentDict[throwStatement];
                    context.Editor.Select(declarationBegin + segment.Offset, segment.Length);
                }
                CSharpFormatterHelper.Format(context.Editor, declarationBegin, newText.Length, formattingOptions.OptionsContainer);
            }
        }
示例#3
0
        public override void Complete(CompletionContext context)
        {
            if (declarationBegin > context.StartOffset)
            {
                base.Complete(context);
                return;
            }

            TypeSystemAstBuilder b = new TypeSystemAstBuilder(contextAtCaret);

            b.ShowTypeParameterConstraints = false;
            b.GenerateBody = true;

            var entityDeclaration = b.ConvertEntity(this.Entity);

            entityDeclaration.Modifiers &= ~(Modifiers.Virtual | Modifiers.Abstract);
            entityDeclaration.Modifiers |= Modifiers.Override;

            var       body = entityDeclaration.GetChildByRole(Roles.Body);
            Statement baseCallStatement = body.Children.OfType <Statement>().FirstOrDefault();

            if (!this.Entity.IsAbstract)
            {
                // modify body to call the base method
                if (this.Entity.SymbolKind == SymbolKind.Method)
                {
                    var baseCall = new BaseReferenceExpression().Invoke(this.Entity.Name, new Expression[] { });
                    if (((IMethod)this.Entity).ReturnType.IsKnownType(KnownTypeCode.Void))
                    {
                        baseCallStatement = new ExpressionStatement(baseCall);
                    }
                    else
                    {
                        baseCallStatement = new ReturnStatement(baseCall);
                    }

                    // Clear body of inserted method
                    entityDeclaration.GetChildByRole(Roles.Body).Statements.Clear();
                }
            }

            var          document          = context.Editor.Document;
            StringWriter w                 = new StringWriter();
            var          formattingOptions = CSharpFormattingOptionsPersistence.GetProjectOptions(contextAtCaret.Compilation.GetProject());
            var          segmentDict       = SegmentTrackingOutputFormatter.WriteNode(
                w, entityDeclaration, formattingOptions.OptionsContainer.GetEffectiveOptions(), context.Editor.Options);

            using (document.OpenUndoGroup()) {
                InsertionContext insertionContext = new InsertionContext(context.Editor.GetService(typeof(TextArea)) as TextArea, declarationBegin);
                insertionContext.InsertionPosition = context.Editor.Caret.Offset;

                string newText = w.ToString().TrimEnd();
                document.Replace(declarationBegin, context.EndOffset - declarationBegin, newText);
                var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement);
                if (throwStatement != null)
                {
                    var segment = segmentDict[throwStatement];
                    context.Editor.Select(declarationBegin + segment.Offset, segment.Length);
                }
                CSharpFormatterHelper.Format(context.Editor, declarationBegin, newText.Length, formattingOptions.OptionsContainer);

                var refactoringContext = SDRefactoringContext.Create(context.Editor, CancellationToken.None);
                var typeResolveContext = refactoringContext.GetTypeResolveContext();
                if (typeResolveContext == null)
                {
                    return;
                }
                var resolvedCurrent        = typeResolveContext.CurrentTypeDefinition;
                IEditorUIService uiService = context.Editor.GetService(typeof(IEditorUIService)) as IEditorUIService;

                ITextAnchor endAnchor = context.Editor.Document.CreateAnchor(context.Editor.Caret.Offset);
                endAnchor.MovementType = AnchorMovementType.AfterInsertion;

                ITextAnchor startAnchor = context.Editor.Document.CreateAnchor(context.Editor.Caret.Offset);
                startAnchor.MovementType = AnchorMovementType.BeforeInsertion;

                ITextAnchor insertionPos = context.Editor.Document.CreateAnchor(endAnchor.Offset);
                insertionPos.MovementType = AnchorMovementType.BeforeInsertion;

                var current = typeResolveContext.CurrentTypeDefinition;
                AbstractInlineRefactorDialog dialog = new OverrideEqualsGetHashCodeMethodsDialog(insertionContext, context.Editor, endAnchor, insertionPos, current, Entity as IMethod, baseCallStatement);

                dialog.Element = uiService.CreateInlineUIElement(insertionPos, dialog);

                insertionContext.RegisterActiveElement(new InlineRefactorSnippetElement(cxt => null, ""), dialog);
                insertionContext.RaiseInsertionCompleted(EventArgs.Empty);
            }
        }