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:
@@ -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.
|
* All rights reserved. Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "TextDocument.h"
|
#include "TextDocument.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
TextDocument::TextDocument()
|
TextDocument::TextDocument()
|
||||||
@@ -192,13 +193,29 @@ TextDocument::Remove(int32 textOffset, int32 length)
|
|||||||
int32 paragraphLength = resultParagraph.Length();
|
int32 paragraphLength = resultParagraph.Length();
|
||||||
if (textOffset == 0 && length > paragraphLength) {
|
if (textOffset == 0 && length > paragraphLength) {
|
||||||
length -= paragraphLength;
|
length -= paragraphLength;
|
||||||
|
paragraphLength = 0;
|
||||||
resultParagraph.Clear();
|
resultParagraph.Clear();
|
||||||
} else {
|
} else {
|
||||||
int32 removeLength = std::min(length, paragraphLength - textOffset);
|
int32 removeLength = std::min(length, paragraphLength - textOffset);
|
||||||
resultParagraph.Remove(textOffset, removeLength);
|
resultParagraph.Remove(textOffset, removeLength);
|
||||||
|
paragraphLength -= removeLength;
|
||||||
length -= 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;
|
textOffset = 0;
|
||||||
|
|
||||||
while (length > 0 && index + 1 < fParagraphs.CountItems()) {
|
while (length > 0 && index + 1 < fParagraphs.CountItems()) {
|
||||||
@@ -214,7 +231,7 @@ TextDocument::Remove(int32 textOffset, int32 length)
|
|||||||
// Last paragraph reached
|
// Last paragraph reached
|
||||||
int32 removedLength = std::min(length, paragraphLength);
|
int32 removedLength = std::min(length, paragraphLength);
|
||||||
Paragraph newParagraph(paragraph);
|
Paragraph newParagraph(paragraph);
|
||||||
fParagraphs.Remove(index);
|
fParagraphs.Remove(index + 1);
|
||||||
|
|
||||||
if (!newParagraph.Remove(0, removedLength))
|
if (!newParagraph.Remove(0, removedLength))
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -406,3 +423,22 @@ TextDocument::GetText(int32 start, int32 length) const
|
|||||||
|
|
||||||
return text;
|
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");
|
||||||
|
}
|
||||||
|
|||||||
@@ -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.
|
* All rights reserved. Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
#ifndef TEXT_DOCUMENT_H
|
#ifndef TEXT_DOCUMENT_H
|
||||||
@@ -70,6 +70,8 @@ public:
|
|||||||
|
|
||||||
BString GetText(int32 textOffset, int32 length) const;
|
BString GetText(int32 textOffset, int32 length) const;
|
||||||
|
|
||||||
|
void PrintToStream() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ParagraphList fParagraphs;
|
ParagraphList fParagraphs;
|
||||||
Paragraph fEmptyLastParagraph;
|
Paragraph fEmptyLastParagraph;
|
||||||
|
|||||||
Reference in New Issue
Block a user