Esempio n. 1
0
        private void HandleKeyboardInput(EditInputType type, string detail)
        {
            if (NavigationTypeMap.ContainsKey(type))
            {
                this.pageCollection.NavigateCursor(NavigationTypeMap[type]);
                return;
            }

            if (type == EditInputType.Escape)
            {
                this.keyboardInput.Stop();
                return;
            }

            if (type == EditInputType.Input)
            {
                var buffer = new DocumentBuffer(this.elementFactory, this.textScoper);
                buffer.Append(detail, this.pageCollection.GetCurrentFlags());
                this.pageCollection.Insert(buffer, null);
                return;
            }


            throw new Exception("Unhandled input type: " + type);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        public void TestInitialize()
        {
            this.textScoperMock = new Mock <ITextScoper>();
            var elementFactoryMock = new Mock <IElementFactory>();

            elementFactoryMock.Setup(fac => fac.Create(It.IsAny <Flags>())).Returns <Flags>((f) => new FormattedTextMock()
            {
                Flags = f
            });
            this.bufferUnderTest = new DocumentBuffer(elementFactoryMock.Object, textScoperMock.Object);
        }
        public void NewLines(string s, NewLineLocation[] expected)
        {
            var doc = new DocumentBuffer();

            doc.SetContent(s);
            var nls = doc.GetNewLineLocations().ToArray();

            for (var i = 0; i < nls.Length; i++)
            {
                Assert.AreEqual(nls[i].Kind, expected[i].Kind);
                Assert.AreEqual(nls[i].EndIndex, expected[i].EndIndex);
            }
        }
Esempio n. 5
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 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 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 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);
        }
Esempio 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);
        }
        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);
        }
Esempio n. 13
0
        private DocumentBuffer InternalCreateBuffer(FileInfo file, Encoding enc, Guid id)
        {
            var buf = buffers.FirstOrDefault(b =>
                                             b.File.FullName.Equals(file.FullName, StringComparison.OrdinalIgnoreCase));

            if (buf == null)
            {
                var doc = CreateDocument(file, enc);

                if (doc == null)
                {
                    return(null);
                }

                buf = new DocumentBuffer(doc, file, enc, id);
                buffers.Add(buf);
                var rd = recents.FirstOrDefault(r => r.File.FullName.Equals(file.FullName, StringComparison.OrdinalIgnoreCase));

                if (rd != null)
                {
                    rd.Date = DateTime.Now;
                }
                else
                {
                    recents.Add(new Recent {
                        File = file, Date = DateTime.Now
                    });
                }

                if (recents.Count > MAX_RECENT)
                {
                    recents.RemoveAt(0);
                }
            }

            return((DocumentBuffer)buf);
        }