Ejemplo n.º 1
0
 public void Undo()
 {
     if (layer != null)
     {
         layer.Dispose();
     }
     layer = null;
     if (tcs != null)
     {
         tcs.SetCanceled();
     }
     tcs = null;
 }
Ejemplo n.º 2
0
        void InsertWithCursorOnLayer(EditorScript currentScript, InsertionCursorLayer layer, TaskCompletionSource <Script> tcs, IList <AstNode> nodes, IDocument target)
        {
            var doc = target as TextDocument;
            var op  = new UndoOperation(layer, tcs);

            if (doc != null)
            {
                doc.UndoStack.Push(op);
            }
            layer.ScrollToInsertionPoint();
            layer.Exited += delegate(object s, InsertionCursorEventArgs args) {
                doc.UndoStack.StartContinuedUndoGroup();
                try {
                    if (args.Success)
                    {
                        if (args.InsertionPoint.LineAfter == NewLineInsertion.None &&
                            args.InsertionPoint.LineBefore == NewLineInsertion.None && nodes.Count > 1)
                        {
                            args.InsertionPoint.LineAfter = NewLineInsertion.BlankLine;
                        }

                        var insertionPoint = args.InsertionPoint;
                        if (nodes.All(n => n is EnumMemberDeclaration))
                        {
                            insertionPoint.LineAfter  = NewLineInsertion.Eol;
                            insertionPoint.LineBefore = NewLineInsertion.None;
                        }

                        int offset      = currentScript.GetCurrentOffset(insertionPoint.Location);
                        int indentLevel = currentScript.GetIndentLevelAt(Math.Max(0, offset - 1));

                        foreach (var node in nodes.Reverse())
                        {
                            var output = currentScript.OutputNode(indentLevel, node);
                            var text   = output.Text;
                            if (node is EnumMemberDeclaration)
                            {
                                if (insertionPoint != layer.InsertionPoints.Last())
                                {
                                    text += ",";
                                }
                                else
                                {
                                    var parentEnum = currentScript.context.RootNode.GetNodeAt(insertionPoint.Location, n => (n is TypeDeclaration) && ((TypeDeclaration)n).ClassType == ClassType.Enum) as TypeDeclaration;
                                    if (parentEnum != null)
                                    {
                                        var lastMember = parentEnum.Members.LastOrDefault();
                                        if (lastMember != null)
                                        {
                                            var segment = currentScript.GetSegment(lastMember);
                                            currentScript.InsertText(segment.EndOffset, ",");
                                        }
                                    }
                                }
                            }
                            int delta = insertionPoint.Insert(target, text);
                            output.RegisterTrackedSegments(currentScript, delta + offset);
                        }
                        currentScript.FormatText(nodes);
                        tcs.SetResult(currentScript);
                    }
                    layer.Dispose();
                    DisposeOnClose();
                } finally {
                    doc.UndoStack.EndUndoGroup();
                }
                op.Reset();
            };
        }