Ejemplo n.º 1
0
        public void ResetDocumentBuffer()
        {
            var doc = new DocumentBuffer();

            doc.Reset(0, "");

            Assert.AreEqual("", doc.Text.ToString());

            doc.Update(new[] { new DocumentChangeSet(0, 1, new[] {
                    DocumentChange.Insert("text", SourceLocation.MinValue)
                }) });

            Assert.AreEqual("text", doc.Text.ToString());

            try {
                doc.Update(new[] { new DocumentChangeSet(1, 0, new[] {
                        DocumentChange.Delete(SourceLocation.MinValue, SourceLocation.MinValue.AddColumns(4))
                    }) });
                Assert.Fail("expected InvalidOperationException");
            } catch (InvalidOperationException) {
            }
            Assert.AreEqual("text", doc.Text.ToString());
            Assert.AreEqual(1, doc.Version);

            doc.Update(new[] { new DocumentChangeSet(1, 0, new[] {
                    new DocumentChange {
                        WholeBuffer = true
                    }
                }) });

            Assert.AreEqual("", doc.Text.ToString());
            Assert.AreEqual(0, doc.Version);
        }
Ejemplo n.º 2
0
        public void BasicDocumentBuffer()
        {
            var doc = new DocumentBuffer();

            doc.Reset(0, @"def f(x):
    return

def g(y):
    return y * 2
");

            doc.Update(new DocumentChangeSet(0, 1, new[] {
                // We *should* batch adjacent insertions, but we should also
                // work fine even if we don't. Note that the insertion point
                // tracks backwards with previous insertions in the same version.
                // If each of these were in its own array, the location would
                // have to change for each.
                DocumentChange.Insert(")", new SourceLocation(2, 11)),
                DocumentChange.Insert("x", new SourceLocation(2, 11)),
                DocumentChange.Insert("(", new SourceLocation(2, 11)),
                DocumentChange.Insert("g", new SourceLocation(2, 11)),
                DocumentChange.Insert(" ", new SourceLocation(2, 11))
            }));

            doc.Text.Should().Contain("return g(x)");
            Assert.AreEqual(1, doc.Version);

            doc.Update(new[] {
                new DocumentChangeSet(1, 2, new [] {
                    DocumentChange.Delete(new SourceLocation(2, 14), new SourceLocation(2, 15))
                }),
                new DocumentChangeSet(2, 3, new [] {
                    DocumentChange.Insert("x * 2", new SourceLocation(2, 14))
                })
            });

            doc.Text.Should().Contain("return g(x * 2)");

            doc.Update(new DocumentChangeSet(3, 4, new[] {
                DocumentChange.Replace(new SourceLocation(2, 18), new SourceLocation(2, 19), "300")
            }));

            doc.Text.Should().Contain("return g(x * 300)");

            doc.Update(new DocumentChangeSet(4, 5, new[] {
                // Changes are out of order, but we should fix that automatically
                DocumentChange.Delete(new SourceLocation(2, 13), new SourceLocation(2, 22)),
                DocumentChange.Insert("#", new SourceLocation(2, 7))
            }));
            doc.Text.Should().Contain("re#turn g");
        }
        public void InsertTopToBottom()
        {
            var doc = new DocumentBuffer();

            doc.SetContent(@"linelinelineline");
            doc.Update(new[] {
                DocumentChange.Insert("\n", new SourceLocation(1, 1)),
                DocumentChange.Insert("1\n", new SourceLocation(2, 5)),
                DocumentChange.Insert("2\r", new SourceLocation(3, 5)),
                DocumentChange.Insert("3\r\n", new SourceLocation(4, 5)),
                DocumentChange.Insert("4\r\n", new SourceLocation(5, 5)),
            });
            Assert.AreEqual("\nline1\nline2\rline3\r\nline4\r\n", doc.Text);
        }
        public void ResetDocumentBuffer()
        {
            var doc = new DocumentBuffer();

            doc.SetContent(string.Empty);
            Assert.AreEqual(string.Empty, doc.Text);

            doc.Update(new[] {
                DocumentChange.Insert("text", SourceLocation.MinValue)
            });

            Assert.AreEqual("text", doc.Text);
            Assert.AreEqual(1, doc.Version);
        }
        public void ReplaceAllDocumentBuffer()
        {
            var doc = new DocumentBuffer();

            doc.SetContent(string.Empty);
            Assert.AreEqual(string.Empty, doc.Text);

            doc.Update(new[] {
                DocumentChange.ReplaceAll("text")
            });

            Assert.AreEqual("text", doc.Text);
            Assert.AreEqual(1, doc.Version);

            doc.Update(new[] {
                DocumentChange.ReplaceAll("abcdef")
            });

            Assert.AreEqual(@"abcdef", doc.Text);
            Assert.AreEqual(2, doc.Version);

            doc.Update(new[] {
                DocumentChange.Insert("text", SourceLocation.MinValue),
                DocumentChange.ReplaceAll("1234")
            });

            Assert.AreEqual(@"1234", doc.Text);
            Assert.AreEqual(3, doc.Version);

            doc.Update(new[] {
                DocumentChange.ReplaceAll("1234"),
                DocumentChange.Insert("text", SourceLocation.MinValue)
            });

            Assert.AreEqual(@"text1234", doc.Text);
            Assert.AreEqual(4, doc.Version);
        }
Ejemplo n.º 6
0
        public void Update(IEnumerable <DocumentChange> changes)
        {
            lock (AnalysisLock) {
                _parseCts?.Cancel();
                _parseCts = new CancellationTokenSource();

                _linkedParseCts?.Dispose();
                _linkedParseCts = CancellationTokenSource.CreateLinkedTokenSource(_disposeToken.CancellationToken, _parseCts.Token);

                _buffer.Update(changes);
                Parse();
            }

            Services.GetService <IPythonAnalyzer>().InvalidateAnalysis(this);
        }
        public void Update(IEnumerable <DocumentChangeSet> changes)
        {
            lock (AnalysisLock) {
                ExpectedAnalysisVersion++;

                _linkedAnalysisCts?.Cancel();
                _analysisTcs = new TaskCompletionSource <IDocumentAnalysis>();

                _parseCts?.Cancel();
                _parseCts = new CancellationTokenSource();

                _linkedParseCts?.Dispose();
                _linkedParseCts = CancellationTokenSource.CreateLinkedTokenSource(_allProcessingCts.Token, _parseCts.Token);

                _buffer.Update(changes);
                Parse();
            }
        }
        public void SequentialChanges()
        {
            var doc = new DocumentBuffer();

            doc.SetContent(@"
line1
line2
line3
line4
");
            doc.Update(new[] {
                DocumentChange.Delete(new SourceSpan(2, 5, 3, 5)),
                DocumentChange.Delete(new SourceSpan(3, 5, 4, 5))
            });
            Assert.AreEqual(@"
line2
line4
", doc.Text);
        }
Ejemplo n.º 9
0
        public void DeleteAcrossLines()
        {
            var doc = new DocumentBuffer();

            doc.Reset(0, @"
line1
line2
line3
line4
");
            doc.Update(new[] {
                DocumentChange.Delete(new SourceSpan(4, 5, 5, 5)),
                DocumentChange.Delete(new SourceSpan(2, 5, 3, 5)),
            });
            Assert.AreEqual(@"
line2
line4
", doc.Text);
        }
        public void InsertMultipleDisjoint()
        {
            var doc = new DocumentBuffer();

            doc.SetContent(@"
line
line
line
line
");
            doc.Update(new[] {
                DocumentChange.Insert("4", new SourceLocation(5, 5)),
                DocumentChange.Insert("3", new SourceLocation(4, 5)),
                DocumentChange.Insert("2", new SourceLocation(3, 5)),
                DocumentChange.Insert("1", new SourceLocation(2, 5)),
            });
            Assert.AreEqual(@"
line1
line2
line3
line4
", doc.Text);
        }
        public void DeleteMultipleDisjoint()
        {
            var doc = new DocumentBuffer();

            doc.SetContent(@"
line1
line2
line3
line4
");
            doc.Update(new[] {
                DocumentChange.Delete(new SourceSpan(5, 5, 5, 6)),
                DocumentChange.Delete(new SourceSpan(4, 5, 4, 6)),
                DocumentChange.Delete(new SourceSpan(3, 5, 3, 6)),
                DocumentChange.Delete(new SourceSpan(2, 5, 2, 6))
            });
            Assert.AreEqual(@"
line
line
line
line
", doc.Text);
        }