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;
                        }

                        int offset      = currentScript.GetCurrentOffset(args.InsertionPoint.Location);
                        int indentLevel = currentScript.GetIndentLevelAt(offset);

                        foreach (var node in nodes.Reverse())
                        {
                            var output = currentScript.OutputNode(indentLevel, node);
                            int delta  = args.InsertionPoint.Insert(target, output.Text);
                            output.RegisterTrackedSegments(currentScript, delta + offset);
                        }
                        currentScript.FormatText(nodes);
                        tcs.SetResult(currentScript);
                    }
                    layer.Dispose();
                    DisposeOnClose();
                } finally {
                    doc.UndoStack.EndUndoGroup();
                }
                op.Reset();
            };
        }
		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.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;
						}
						foreach (var node in nodes.Reverse ()) {
							int indentLevel = currentScript.GetIndentLevelAt(target.GetOffset(args.InsertionPoint.Location));
							var output = currentScript.OutputNode(indentLevel, node);
							var offset = target.GetOffset(args.InsertionPoint.Location);
							var delta = args.InsertionPoint.Insert(target, output.Text);
							output.RegisterTrackedSegments(currentScript, delta + offset);
						}
						tcs.SetResult(currentScript);
					}
					layer.Dispose();
					DisposeOnClose();
				} finally {
					doc.UndoStack.EndUndoGroup();
				}
				op.Reset();
			};
		}
Exemple #3
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();
            };
        }
		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();
			};
		}