From 04b0e36ac0ac1c71531b61f92271cea93fc9010b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Wed, 22 Jan 2014 00:16:44 +0100 Subject: [PATCH] TextDocument: More fixes. * Handle shifting the contents of the next paragraph into the preceeding paragraph if text up to and including the line break of the preceeding paragraph is removed. * Fixed removing the correct paragraph in the loop when it hits the last paragraph. (though that code is still untested) --- .../haiku-depot/textview/TextDocument.cpp | 40 ++++++++++++++++++- src/apps/haiku-depot/textview/TextDocument.h | 4 +- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/apps/haiku-depot/textview/TextDocument.cpp b/src/apps/haiku-depot/textview/TextDocument.cpp index 7b9a13d5c1..10709e8b59 100644 --- a/src/apps/haiku-depot/textview/TextDocument.cpp +++ b/src/apps/haiku-depot/textview/TextDocument.cpp @@ -1,11 +1,12 @@ /* - * Copyright 2013, Stephan Aßmus . + * Copyright 2013-2014, Stephan Aßmus . * All rights reserved. Distributed under the terms of the MIT License. */ #include "TextDocument.h" #include +#include TextDocument::TextDocument() @@ -192,13 +193,29 @@ TextDocument::Remove(int32 textOffset, int32 length) int32 paragraphLength = resultParagraph.Length(); if (textOffset == 0 && length > paragraphLength) { length -= paragraphLength; + paragraphLength = 0; resultParagraph.Clear(); } else { int32 removeLength = std::min(length, paragraphLength - textOffset); resultParagraph.Remove(textOffset, removeLength); + paragraphLength -= removeLength; length -= removeLength; } + if (textOffset == paragraphLength && length == 0 + && index + 1 < fParagraphs.CountItems()) { + // Line break between paragraphs got removed. Shift the next + // paragraph's text spans into the resulting one. + + const TextSpanList& textSpans = ParagraphAt(index + 1).TextSpans(); + int32 spanCount = textSpans.CountItems(); + for (int32 i = 0; i < spanCount; i++) { + const TextSpan& span = textSpans.ItemAtFast(i); + resultParagraph.Append(span); + } + fParagraphs.Remove(index + 1); + } + textOffset = 0; while (length > 0 && index + 1 < fParagraphs.CountItems()) { @@ -214,7 +231,7 @@ TextDocument::Remove(int32 textOffset, int32 length) // Last paragraph reached int32 removedLength = std::min(length, paragraphLength); Paragraph newParagraph(paragraph); - fParagraphs.Remove(index); + fParagraphs.Remove(index + 1); if (!newParagraph.Remove(0, removedLength)) return B_NO_MEMORY; @@ -406,3 +423,22 @@ TextDocument::GetText(int32 start, int32 length) const return text; } + + +// #pragma mark - + + +void +TextDocument::PrintToStream() const +{ + int32 paragraphCount = fParagraphs.CountItems(); + if (paragraphCount == 0) { + printf("\n"); + return; + } + printf("\n"); + for (int32 i = 0; i < paragraphCount; i++) { + fParagraphs.ItemAtFast(i).PrintToStream(); + } + printf("\n"); +} diff --git a/src/apps/haiku-depot/textview/TextDocument.h b/src/apps/haiku-depot/textview/TextDocument.h index d48d8bfec8..4aa28c3bd0 100644 --- a/src/apps/haiku-depot/textview/TextDocument.h +++ b/src/apps/haiku-depot/textview/TextDocument.h @@ -1,5 +1,5 @@ /* - * Copyright 2013, Stephan Aßmus . + * Copyright 2013-2014, Stephan Aßmus . * All rights reserved. Distributed under the terms of the MIT License. */ #ifndef TEXT_DOCUMENT_H @@ -70,6 +70,8 @@ public: BString GetText(int32 textOffset, int32 length) const; + void PrintToStream() const; + private: ParagraphList fParagraphs; Paragraph fEmptyLastParagraph;