Insert() public method

Inserts text.
Anchors positioned exactly at the insertion offset will move according to their movement type. For AnchorMovementType.Default, they will move behind the inserted text. The caret will also move behind the inserted text.
public Insert ( int offset, ITextSource text ) : void
offset int The offset at which the text is inserted.
text ITextSource The new text.
return void
		public void ContinueUndoGroup()
		{
			var doc = new TextDocument();
			doc.Insert(0, "a");
			doc.UndoStack.StartContinuedUndoGroup();
			doc.Insert(1, "b");
			doc.UndoStack.EndUndoGroup();
			doc.UndoStack.Undo();
			Assert.AreEqual("", doc.Text);
		}
        public void ContinueUndoGroup()
        {
            var doc = new TextDocument();

            doc.Insert(0, "a");
            doc.UndoStack.StartContinuedUndoGroup();
            doc.Insert(1, "b");
            doc.UndoStack.EndUndoGroup();
            doc.UndoStack.Undo();
            Assert.AreEqual("", doc.Text);
        }
		public void ContinueEmptyUndoGroup_WithOptionalEntries()
		{
			var doc = new TextDocument();
			doc.Insert(0, "a");
			doc.UndoStack.StartUndoGroup();
			doc.UndoStack.PushOptional(new StubUndoableAction());
			doc.UndoStack.EndUndoGroup();
			doc.UndoStack.StartContinuedUndoGroup();
			doc.Insert(1, "b");
			doc.UndoStack.EndUndoGroup();
			doc.UndoStack.Undo();
			Assert.AreEqual("a", doc.Text);
		}
Esempio n. 4
0
        public void AnchorInEmptyDocument()
        {
            TextAnchor a1 = document.CreateAnchor(0);
            TextAnchor a2 = document.CreateAnchor(0);

            a1.MovementType = AnchorMovementType.BeforeInsertion;
            a2.MovementType = AnchorMovementType.AfterInsertion;
            Assert.AreEqual(0, a1.Offset);
            Assert.AreEqual(0, a2.Offset);
            document.Insert(0, "x");
            Assert.AreEqual(0, a1.Offset);
            Assert.AreEqual(1, a2.Offset);
        }
        /// <inheritdoc cref="IIndentationStrategy.IndentLine"/>
        public override void IndentLine(TextDocument document, DocumentLine line)
        {
            if (line?.PreviousLine == null)
                return;

            var prevLine = document.GetText(line.PreviousLine.Offset, line.PreviousLine.Length);
            var curLine = document.GetText(line.Offset, line.Length);
            int prev = CalcSpace(prevLine);

            var previousIsComment = prevLine.TrimStart().StartsWith("--", StringComparison.CurrentCulture);

            if (Regex.IsMatch(curLine, patternFull) && !previousIsComment)
            {
                var ind = new string(' ', prev);
                document.Insert(line.Offset, ind);
            }
            else if (Regex.IsMatch(prevLine, patternStart) && !previousIsComment)
            {
                var ind = new string(' ', prev + indent_space_count);
                document.Insert(line.Offset, ind);

                var found = false;
                for (int i = line.LineNumber; i < document.LineCount; ++i)
                {
                    var text = document.GetText(document.Lines[i].Offset, document.Lines[i].Length);

                    if (string.IsNullOrWhiteSpace(text) || text.TrimStart().StartsWith("--", StringComparison.CurrentCulture))
                        continue;

                    var sps = CalcSpace(text);

                    if (sps == prev && Regex.IsMatch(text, patternEnd))
                        found = true;
                }

                if (!found)
                {
                    var ntext = Environment.NewLine + new string(' ', prev) + "end";
                    var point = textEditor.SelectionStart;
                    document.Insert(line.Offset + ind.Length, ntext);
                    textEditor.SelectionStart = point;
                }
            }
            else
            {
                var ind = new string(' ', prev);
                if (line != null)
                    document.Insert(line.Offset, ind);
            }
        }
        public void ContinueEmptyUndoGroup_WithOptionalEntries()
        {
            var doc = new TextDocument();

            doc.Insert(0, "a");
            doc.UndoStack.StartUndoGroup();
            doc.UndoStack.PushOptional(new StubUndoableAction());
            doc.UndoStack.EndUndoGroup();
            doc.UndoStack.StartContinuedUndoGroup();
            doc.Insert(1, "b");
            doc.UndoStack.EndUndoGroup();
            doc.UndoStack.Undo();
            Assert.AreEqual("a", doc.Text);
        }
        /// <inheritdoc cref="IIndentationStrategy.IndentLine"/>
        public override void IndentLine(TextDocument document, DocumentLine line)
        {
            if (line.PreviousLine == null)
                return;

            int prev = CalcSpace(line.PreviousLine.Text);

            var previousIsComment = line.PreviousLine.Text.TrimStart().StartsWith("--");

            if (Regex.IsMatch(line.Text, @"\b(?<start>function|while(.+)do|if(.+)then|elseif(.+)then|for(.+)do|{{|end|}})\b") && !previousIsComment)
            {
                var ind = new string(' ', prev);
                document.Insert(line.Offset, ind);
            }
            else if (Regex.IsMatch(line.PreviousLine.Text, @"\b(?<start>function|while(.+)do|if(.+)then|elseif(.+)then|for(.+)do|{{)\b") && !previousIsComment)
            {
                var ind = new string(' ', prev + indent_space_count);
                document.Insert(line.Offset, ind);

                var found = false;
                for (int i = line.LineNumber; i < document.LineCount; ++i)
                {
                    var text = document.Lines[i].Text;

                    if (string.IsNullOrWhiteSpace(text) || text.TrimStart().StartsWith("--"))
                        continue;

                    var sps = CalcSpace(text);

                    if (sps == prev && Regex.IsMatch(text, @"\b(?<start>end)\b"))
                        found = true;
                }

                if (!found)
                {
                    var ntext = Environment.NewLine + new string(' ', prev) + "end";
                    var point = textEditor.SelectionStart;
                    document.Insert(line.Offset + ind.Length, ntext);
                    textEditor.SelectionStart = point;
                }
            }
            else
            {
                var ind = new string(' ', prev);
                if (line != null)
                    document.Insert(line.Offset, ind);
            }
        }
Esempio n. 8
0
        public void RawlyIndentLine(string indentString, ICSharpCode.AvalonEdit.Document.TextDocument document, DocumentLine line)
        {
            if (!_doBeginUpdateManually)
            {
                document.BeginUpdate();
            }

            // 1)
            int prevInd = 0;
            int curOff  = line.Offset;

            if (curOff < document.TextLength)
            {
                char curChar = '\0';
                while (curOff < document.TextLength && ((curChar = document.GetCharAt(curOff)) == ' ' || curChar == '\t'))
                {
                    prevInd++;
                    curOff++;
                }

                document.Remove(line.Offset, prevInd);
            }

            document.Insert(line.Offset, indentString);
            if (!_doBeginUpdateManually)
            {
                document.EndUpdate();
            }
        }
Esempio n. 9
0
 public void TestLinesInserted()
 {
     document.Insert(0, "x\ny\n");
     heightTree.SetHeight(document.Lines[0], 100);
     heightTree.SetHeight(document.Lines[1], 1000);
     heightTree.SetHeight(document.Lines[2], 10000);
     CheckHeights();
 }
Esempio n. 10
0
		public void BackwardChanges()
		{
			TextDocument document = new TextDocument("initial text");
			ITextSource snapshot1 = document.CreateSnapshot();
			document.Replace(0, 7, "nw");
			document.Insert(1, "e");
			ITextSource snapshot2 = document.CreateSnapshot();
			Assert.AreEqual(1, snapshot2.Version.CompareAge(snapshot1.Version));
			TextChangeEventArgs[] arr = snapshot2.Version.GetChangesTo(snapshot1.Version).ToArray();
			Assert.AreEqual(2, arr.Length);
			Assert.AreEqual("", arr[0].InsertedText.Text);
			Assert.AreEqual("initial", arr[1].InsertedText.Text);
			
			Assert.AreEqual("initial text", snapshot1.Text);
			Assert.AreEqual("new text", snapshot2.Text);
		}
Esempio n. 11
0
		public void BackwardChanges()
		{
			TextDocument document = new TextDocument("initial text");
			ChangeTrackingCheckpoint checkpoint1, checkpoint2;
			ITextSource snapshot1 = document.CreateSnapshot(out checkpoint1);
			document.Replace(0, 7, "nw");
			document.Insert(1, "e");
			ITextSource snapshot2 = document.CreateSnapshot(out checkpoint2);
			Assert.AreEqual(1, checkpoint2.CompareAge(checkpoint1));
			DocumentChangeEventArgs[] arr = checkpoint2.GetChangesTo(checkpoint1).ToArray();
			Assert.AreEqual(2, arr.Length);
			Assert.AreEqual("", arr[0].InsertedText);
			Assert.AreEqual("initial", arr[1].InsertedText);
			
			Assert.AreEqual("initial text", snapshot1.Text);
			Assert.AreEqual("new text", snapshot2.Text);
		}
Esempio n. 12
0
        public void BackwardChanges()
        {
            TextDocument document  = new TextDocument("initial text");
            ITextSource  snapshot1 = document.CreateSnapshot();

            document.Replace(0, 7, "nw");
            document.Insert(1, "e");
            ITextSource snapshot2 = document.CreateSnapshot();

            Assert.AreEqual(1, snapshot2.Version.CompareAge(snapshot1.Version));
            TextChangeEventArgs[] arr = snapshot2.Version.GetChangesTo(snapshot1.Version).ToArray();
            Assert.AreEqual(2, arr.Length);
            Assert.AreEqual("", arr[0].InsertedText.Text);
            Assert.AreEqual("initial", arr[1].InsertedText.Text);

            Assert.AreEqual("initial text", snapshot1.Text);
            Assert.AreEqual("new text", snapshot2.Text);
        }
Esempio n. 13
0
        public void InsertInCollapsedSection()
        {
            CollapsedLineSection sec1 = heightTree.CollapseText(document.GetLineByNumber(4), document.GetLineByNumber(6));

            document.Insert(document.GetLineByNumber(5).Offset, "a\nb\nc");
            for (int i = 1; i < 4; i++)
            {
                Assert.IsFalse(heightTree.GetIsCollapsed(i));
            }
            for (int i = 4; i <= 8; i++)
            {
                Assert.IsTrue(heightTree.GetIsCollapsed(i));
            }
            for (int i = 9; i <= 12; i++)
            {
                Assert.IsFalse(heightTree.GetIsCollapsed(i));
            }
            CheckHeights();
        }
Esempio n. 14
0
        public void BackwardChanges()
        {
            TextDocument             document = new TextDocument("initial text");
            ChangeTrackingCheckpoint checkpoint1, checkpoint2;
            ITextSource snapshot1 = document.CreateSnapshot(out checkpoint1);

            document.Replace(0, 7, "nw");
            document.Insert(1, "e");
            ITextSource snapshot2 = document.CreateSnapshot(out checkpoint2);

            Assert.AreEqual(1, checkpoint2.CompareAge(checkpoint1));
            DocumentChangeEventArgs[] arr = checkpoint2.GetChangesTo(checkpoint1).ToArray();
            Assert.AreEqual(2, arr.Length);
            Assert.AreEqual("", arr[0].InsertedText);
            Assert.AreEqual("initial", arr[1].InsertedText);

            Assert.AreEqual("initial text", snapshot1.Text);
            Assert.AreEqual("new text", snapshot2.Text);
        }
Esempio n. 15
0
        public void RawlyIndentLine(int tabsToInsert, ICSharpCode.AvalonEdit.Document.TextDocument document, DocumentLine line)
        {
            if (!_doBeginUpdateManually)
            {
                document.BeginUpdate();
            }

            /*
             * 1) Remove old indentation
             * 2) Insert new one
             */

            // 1)
            int prevInd = 0;
            int curOff  = line.Offset;

            if (curOff < document.TextLength)
            {
                char curChar = '\0';
                while (curOff < document.TextLength && ((curChar = document.GetCharAt(curOff)) == ' ' || curChar == '\t'))
                {
                    prevInd++;
                    curOff++;
                }

                document.Remove(line.Offset, prevInd);
            }

            // 2)
            string indentString = "";

            for (int i = 0; i < tabsToInsert; i++)
            {
                indentString += dEditor.Editor.Options.IndentationString;
            }

            document.Insert(line.Offset, indentString);
            if (!_doBeginUpdateManually)
            {
                document.EndUpdate();
            }
        }
		public void DocumentDoesNotHoldReferenceToTextView()
		{
			TextDocument textDocument = new TextDocument();
			Assert.AreEqual(0, textDocument.LineTrackers.Count);
			
			TextView textView = new TextView();
			WeakReference wr = new WeakReference(textView);
			textView.Document = textDocument;
			Assert.AreEqual(1, textDocument.LineTrackers.Count);
			textView = null;
			
			GarbageCollect();
			Assert.IsFalse(wr.IsAlive);
			// document cannot immediately clear the line tracker
			Assert.AreEqual(1, textDocument.LineTrackers.Count);
			
			// but it should clear it on the next change
			textDocument.Insert(0, "a");
			Assert.AreEqual(0, textDocument.LineTrackers.Count);
		}
Esempio n. 17
0
 public override void IndentLine(TextDocument document, DocumentLine line)
 {
     base.IndentLine(document, line);
     if (document.GetText(line.PreviousLine).Trim().EndsWith("{"))
         document.Insert(line.Offset, _options.IndentationString);
 }
Esempio n. 18
0
        public void InsertBold(int start, int length, TextDocument document)
        {
            var chs = document.GetCharAt(start);
            var che = document.GetCharAt(start + length - 1);

            document.Insert(start + length - 1, che.ToString());
            document.Replace(start + length, 1, "]");   //trick to keep anchors

            document.Insert(start + 1, chs.ToString());
            document.Replace(start, 1, "["); //trick to keep anchors

            Blocks.Add(new TextBlockBold()
            {
                OriginallyLength = length + 2,
                OriginallyOffset = start,
                MyAnchor = new AnchorSegment(document, start, length + 2)
            });
        }
Esempio n. 19
0
 private bool CommentAtBeginOfText(TextDocument document, DocumentLine documentLine)
 {
     var text = document.GetText(documentLine);
     if (string.IsNullOrEmpty(text))
     {
         return false;
     }
     if (!AllowMultipleComments && text.TrimStart(Whitespaces.ToArray()).StartsWith(commentMarker))
     {
         return false;
     }
     var num = documentLine.Offset;
     var text2 = text;
     var i = 0;
     while (i < text2.Length)
     {
         var letter = text2[i];
         if (IsWhitespace(letter))
         {
             num++;
             i++;
         }
         else
         {
             if (num >= documentLine.EndOffset)
             {
                 return false;
             }
             break;
         }
     }
     document.Insert(num, CommentMarker);
     return true;
 }
Esempio n. 20
0
        public void InsertInEmptyDocument()
        {
            document.Insert(0, "a");
            Assert.AreEqual(document.LineCount, 1);
            DocumentLine line = document.GetLineByNumber(1);

            Assert.AreEqual("a", document.GetText(line));
        }
Esempio n. 21
0
        public void DocumentDoesNotHoldReferenceToTextView()
        {
            bool collectedTextView = false;
            TextDocument textDocument = new TextDocument();
            Assert.AreEqual(0, textDocument.LineTrackers.Count);

            TextView textView = new TextViewWithGCCallback(delegate { collectedTextView = true; });
            textView.Document = textDocument;
            Assert.AreEqual(1, textDocument.LineTrackers.Count);
            textView = null;

            GarbageCollect();
            Assert.IsTrue(collectedTextView);
            // document cannot immediately clear the line tracker
            Assert.AreEqual(1, textDocument.LineTrackers.Count);

            // but it should clear it on the next change
            textDocument.Insert(0, "a");
            Assert.AreEqual(0, textDocument.LineTrackers.Count);
        }
Esempio n. 22
0
        public void RawlyIndentLine(int tabsToInsert, TextDocument document, DocumentLine line)
        {
            if (!_doBeginUpdateManually)
                document.BeginUpdate();
            /*
             * 1) Remove old indentation
             * 2) Insert new one
             */

            // 1)
            int prevInd = 0;
            int curOff = line.Offset;
            if (curOff < document.TextLength)
            {
                char curChar = '\0';
                while (curOff < document.TextLength && ((curChar = document.GetCharAt(curOff)) == ' ' || curChar == '\t'))
                {
                    prevInd++;
                    curOff++;
                }

                document.Remove(line.Offset, prevInd);
            }

            // 2)
            string indentString = "";
            for (int i = 0; i < tabsToInsert; i++)
                indentString += dEditor.Editor.Options.IndentationString;

            document.Insert(line.Offset, indentString);
            if (!_doBeginUpdateManually)
                document.EndUpdate();
        }
Esempio n. 23
0
 private bool CommentAtBeginOfLine(TextDocument document, DocumentLine documentLine)
 {
     var text = document.GetText(documentLine).TrimStart(Whitespaces.ToArray());
     if (string.IsNullOrEmpty(text))
     {
         return false;
     }
     if (!AllowMultipleComments && text.StartsWith(CommentMarker))
     {
         return false;
     }
     document.Insert(documentLine.Offset, CommentMarker);
     return true;
 }
 private void InsertComment(TextDocument doc, DocumentLine line)
 {
     if (line.TotalLength == 0) return;
     doc.Insert(line.Offset, FbCommentString);
 }