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)
This commit is contained in:
Stephan Aßmus
2014-01-22 00:16:44 +01:00
parent 5aabe5e88f
commit 04b0e36ac0
2 changed files with 41 additions and 3 deletions
+38 -2
View File
@@ -1,11 +1,12 @@
/*
* Copyright 2013, Stephan Aßmus <[email protected]>.
* Copyright 2013-2014, Stephan Aßmus <[email protected]>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "TextDocument.h"
#include <algorithm>
#include <stdio.h>
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("<document/>\n");
return;
}
printf("<document>\n");
for (int32 i = 0; i < paragraphCount; i++) {
fParagraphs.ItemAtFast(i).PrintToStream();
}
printf("</document>\n");
}
+3 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2013, Stephan Aßmus <[email protected]>.
* Copyright 2013-2014, Stephan Aßmus <[email protected]>.
* 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;