Ejemplo n.º 1
0
        public void Insert(CompletionContext context, ICompletionItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            if (!(item is OverrideCompletionItem))
            {
                throw new ArgumentException("item is not an OverrideCompletionItem");
            }

            OverrideCompletionItem completionItem = item as OverrideCompletionItem;

            ITextEditor textEditor = context.Editor;

            IEditorUIService uiService = textEditor.GetService(typeof(IEditorUIService)) as IEditorUIService;

            if (uiService == null)
            {
                return;
            }

            ParseInformation parseInfo = ParserService.GetParseInformation(textEditor.FileName);

            if (parseInfo == null)
            {
                return;
            }

            CodeGenerator generator = parseInfo.CompilationUnit.Language.CodeGenerator;
            IClass        current   = parseInfo.CompilationUnit.GetInnermostClass(textEditor.Caret.Line, textEditor.Caret.Column);
            ClassFinder   finder    = new ClassFinder(current, textEditor.Caret.Line, textEditor.Caret.Column);

            if (current == null)
            {
                return;
            }

            using (textEditor.Document.OpenUndoGroup()) {
                ITextAnchor startAnchor = textEditor.Document.CreateAnchor(textEditor.Caret.Offset);
                startAnchor.MovementType = AnchorMovementType.BeforeInsertion;

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

                MethodDeclaration member = (MethodDeclaration)generator.GetOverridingMethod(completionItem.Member, finder);

                string indent          = DocumentUtilitites.GetWhitespaceBefore(textEditor.Document, textEditor.Caret.Offset);
                string codeForBaseCall = generator.GenerateCode(member.Body.Children.OfType <AbstractNode>().First(), "");
                string code            = generator.GenerateCode(member, indent);
                int    marker          = code.IndexOf(codeForBaseCall);

                textEditor.Document.Insert(startAnchor.Offset, code.Substring(0, marker).TrimStart());

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

                InsertionContext insertionContext = new InsertionContext(textEditor.GetService(typeof(TextArea)) as TextArea, startAnchor.Offset);

                AbstractInlineRefactorDialog dialog = new OverrideEqualsGetHashCodeMethodsDialog(insertionContext, textEditor, startAnchor, endAnchor, insertionPos, current, completionItem.Member as IMethod, codeForBaseCall.Trim());
                dialog.Element = uiService.CreateInlineUIElement(insertionPos, dialog);

                textEditor.Document.InsertNormalized(endAnchor.Offset, Environment.NewLine + code.Substring(marker + codeForBaseCall.Length));

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