Insert() public method

public Insert ( int position, string text ) : ITextSnapshot
position int
text string
return ITextSnapshot
コード例 #1
0
        public static EditorTree ApplyTextChange(string expression, int start, int oldLength, int newLength, string newText)
        {
            TextBufferMock textBuffer = new TextBufferMock(expression, RContentTypeDefinition.ContentType);

            EditorTree tree = new EditorTree(textBuffer);
            tree.Build();

            TextChange tc = new TextChange();
            tc.OldRange = new TextRange(start, oldLength);
            tc.NewRange = new TextRange(start, newLength);
            tc.OldTextProvider = new TextProvider(textBuffer.CurrentSnapshot);

            if (oldLength == 0 && newText.Length > 0)
            {
                textBuffer.Insert(start, newText);
            }
            else if (oldLength > 0 && newText.Length > 0)
            {
                textBuffer.Replace(new Span(start, oldLength), newText);
            }
            else
            {
                textBuffer.Delete(new Span(start, oldLength));
            }

            return tree;
        }
コード例 #2
0
ファイル: RoxygenTest.cs プロジェクト: Microsoft/RTVS
        public void InsertRoxygen01() {
            var tb = new TextBufferMock(string.Empty, RContentTypeDefinition.ContentType);
            AstRoot ast = RParser.Parse(tb.CurrentSnapshot.GetText());
            var result = RoxygenBlock.TryInsertBlock(tb, ast, 0);
            result.Should().BeFalse();

            tb = new TextBufferMock("x <- 1\r\ny <- 2", RContentTypeDefinition.ContentType);
            ast = RParser.Parse(tb.CurrentSnapshot.GetText());
            RoxygenBlock.TryInsertBlock(tb, ast, 0).Should().BeFalse();
            RoxygenBlock.TryInsertBlock(tb, ast, 8).Should().BeFalse();

            tb = new TextBufferMock("##\r\nx <- function(a) { }", RContentTypeDefinition.ContentType);
            ast = RParser.Parse(tb.CurrentSnapshot.GetText());
            RoxygenBlock.TryInsertBlock(tb, ast, 4).Should().BeTrue();
            string actual = tb.CurrentSnapshot.GetText();
            actual.Should().Be(
@"#' Title
#'
#' @param a
#'
#' @return
#' @export
#'
#' @examples
x <- function(a) { }");

            int funcStart = tb.CurrentSnapshot.GetText().IndexOf("x <-");
            tb.Insert(funcStart, "\r\n");
            RoxygenBlock.TryInsertBlock(tb, ast, funcStart - 2).Should().BeFalse();
        }
コード例 #3
0
        public void ClassifyRContent() {
            string expected1 =
@"[0:9] keyword
[9:1] RD Braces
[16:2] number
[19:1] number
[32:5] string";

            string expected2 =
@"[0:9] keyword
[9:1] RD Braces
[16:2] number
[19:1] number
[32:6] string";

            string s1 = "\\examples{ x <- -9:9 plot(col = \"";
            string s2 = "red\")";
            string original = s1 + s2;

            TextBufferMock textBuffer = new TextBufferMock(original, RdContentTypeDefinition.ContentType);
            ClassificationTypeRegistryServiceMock ctrs = new ClassificationTypeRegistryServiceMock();
            RdClassifier cls = new RdClassifier(textBuffer, ctrs);

            IList<ClassificationSpan> spans = cls.GetClassificationSpans(new SnapshotSpan(textBuffer.CurrentSnapshot, new Span(0, textBuffer.CurrentSnapshot.Length)));
            string actual = ClassificationWriter.WriteClassifications(spans);
            BaselineCompare.CompareStringLines(expected1, actual);

            textBuffer.Insert(s1.Length, "%");
            spans = cls.GetClassificationSpans(new SnapshotSpan(textBuffer.CurrentSnapshot, new Span(0, textBuffer.CurrentSnapshot.Length)));
            actual = ClassificationWriter.WriteClassifications(spans);
            BaselineCompare.CompareStringLines(expected2, actual);

            textBuffer.Delete(new Span(s1.Length, 1));
            spans = cls.GetClassificationSpans(new SnapshotSpan(textBuffer.CurrentSnapshot, new Span(0, textBuffer.CurrentSnapshot.Length)));
            actual = ClassificationWriter.WriteClassifications(spans);
            BaselineCompare.CompareStringLines(expected1, actual);
        }
コード例 #4
0
ファイル: OutlineBuilderTest.cs プロジェクト: Microsoft/RTVS
        public void Sections() {
            string content =
@"# NAME1 -----
x <- 1


# NAME2 -----


";
            TextBufferMock textBuffer = null;
            int calls = 0;
            OutlineRegionsChangedEventArgs args = null;

            textBuffer = new TextBufferMock(content, RContentTypeDefinition.ContentType);
            using (var tree = new EditorTree(textBuffer, _editorShell)) {
                tree.Build();
                using (var editorDocument = new EditorDocumentMock(tree)) {
                    using (var ob = new ROutlineRegionBuilder(editorDocument, _editorShell)) {
                        var rc1 = new OutlineRegionCollection(0);
                        ob.BuildRegions(rc1);

                        rc1.Should().HaveCount(2);
                        rc1[0].DisplayText.Should().Be("# NAME1");
                        rc1[1].DisplayText.Should().Be("# NAME2");

                        rc1[0].Length.Should().Be(21);
                        rc1[1].Length.Should().Be(13);

                        ob.RegionsChanged += (s, e) => {
                            calls++;
                            args = e;
                        };

                        textBuffer.Insert(2, "A");
                        editorDocument.EditorTree.EnsureTreeReady();

                        // Wait for background/idle tasks to complete
                        var start = DateTime.Now;
                        while (calls == 0 && (DateTime.Now - start).TotalMilliseconds < 2000) {
                            _editorShell.DoIdle();
                        }

                        calls.Should().Be(1);
                        args.Should().NotBeNull();
                        args.ChangedRange.Start.Should().Be(0);
                        args.ChangedRange.End.Should().Be(textBuffer.CurrentSnapshot.Length);
                        args.Regions.Should().HaveCount(2);

                        args.Regions[0].DisplayText.Should().Be("# ANAME1");
                        args.Regions[1].DisplayText.Should().Be("# NAME2");

                        args.Regions[0].Length.Should().Be(22);
                        args.Regions[1].Length.Should().Be(13);
                    }
                }
            }
        }
コード例 #5
0
        public void TextChange_Test() {
            TextChange tc = new TextChange();
            tc.IsEmpty.Should().BeTrue();
            tc.TextChangeType.Should().Be(TextChangeType.Trivial);

            string content = "23456789";
            TextBufferMock textBuffer = new TextBufferMock(content, RContentTypeDefinition.ContentType);
            ITextSnapshot oldSnapshot = textBuffer.CurrentSnapshot;

            textBuffer.Insert(0, "1");
            ITextSnapshot newSnapshot1 = textBuffer.CurrentSnapshot;

            textBuffer.Insert(0, "0");
            ITextSnapshot newSnapshot2 = textBuffer.CurrentSnapshot;

            tc.OldTextProvider = new TextProvider(oldSnapshot);
            tc.NewTextProvider = new TextProvider(newSnapshot1);

            tc.OldRange = new TextRange(0, 0);
            tc.NewRange = new TextRange(0, 1);

            var tc1 = new TextChange(tc, new TextProvider(newSnapshot2));

            tc1.ShouldBeEquivalentTo(new {
                OldRange = new { Length = 0 },
                NewRange = new { Length = 2 },
                Version = 2,
                FullParseRequired = false,
                IsEmpty = false,
                IsSimpleChange = true
            }, o => o.ExcludingMissingMembers());

            var tc2 = tc1.Clone() as TextChange;

            tc2.ShouldBeEquivalentTo(new {
                OldRange = new { Length = 0 },
                NewRange = new { Length = 2 },
                Version = 2,
                FullParseRequired = false,
                IsEmpty = false,
                IsSimpleChange = true
            }, o => o.ExcludingMissingMembers());

            tc1.Clear();

            tc1.ShouldBeEquivalentTo(new {
                OldRange = new { Length = 0 },
                NewRange = new { Length = 0 },
                OldTextProvider = (ITextProvider)null,
                NewTextProvider = (ITextProvider)null,
                IsEmpty = true,
                IsSimpleChange = true
            }, o => o.ExcludingMissingMembers());

            tc2.ShouldBeEquivalentTo(new {
                OldRange = new { Length = 0 },
                NewRange = new { Length = 2 },
                Version = 2,
                FullParseRequired = false,
                IsEmpty = false,
                IsSimpleChange = true
            }, o => o.ExcludingMissingMembers());
        }