void CodeDocument::remove (const int startPos, const int endPos, const bool undoable) { if (endPos <= startPos) return; if (undoable && !undoDisabled) { undoManager.perform (new DeleteAction (*this, startPos, endPos)); } else { Position startPosition (*this, startPos); Position endPosition (*this, endPos); maximumLineLength = -1; auto firstAffectedLine = startPosition.getLineNumber(); auto endLine = endPosition.getLineNumber(); auto& firstLine = *lines.getUnchecked (firstAffectedLine); if (firstAffectedLine == endLine) { firstLine.line = firstLine.line.substring (0, startPosition.getIndexInLine()) + firstLine.line.substring (endPosition.getIndexInLine()); firstLine.updateLength(); } else { auto& lastLine = *lines.getUnchecked (endLine); firstLine.line = firstLine.line.substring (0, startPosition.getIndexInLine()) + lastLine.line.substring (endPosition.getIndexInLine()); firstLine.updateLength(); int numLinesToRemove = endLine - firstAffectedLine; lines.removeRange (firstAffectedLine + 1, numLinesToRemove); } for (int i = firstAffectedLine + 1; i < lines.size(); ++i) { auto& l = *lines.getUnchecked (i); auto& previousLine = *lines.getUnchecked (i - 1); l.lineStartInFile = previousLine.lineStartInFile + previousLine.lineLength; } checkLastLineStatus(); auto totalChars = getNumCharacters(); for (auto* p : positionsToMaintain) { if (p->getPosition() > startPosition.getPosition()) p->setPosition (jmax (startPos, p->getPosition() + startPos - endPos)); if (p->getPosition() > totalChars) p->setPosition (totalChars); } listeners.call (&CodeDocument::Listener::codeDocumentTextDeleted, startPos, endPos); } }
bool CodeDocument::loadFromStream (InputStream& stream) { remove (0, getNumCharacters(), false); insert (stream.readEntireStreamAsString(), 0, false); setSavePoint(); clearUndoHistory(); return true; }
void CodeDocument::remove (const int startPos, const int endPos, const bool undoable) { if (endPos <= startPos) return; if (undoable) { undoManager.perform (new CodeDocumentDeleteAction (*this, startPos, endPos)); } else { Position startPosition (this, startPos); Position endPosition (this, endPos); maximumLineLength = -1; const int firstAffectedLine = startPosition.getLineNumber(); const int endLine = endPosition.getLineNumber(); int lastAffectedLine = firstAffectedLine + 1; CodeDocumentLine* const firstLine = lines.getUnchecked (firstAffectedLine); if (firstAffectedLine == endLine) { firstLine->line = firstLine->line.substring (0, startPosition.getIndexInLine()) + firstLine->line.substring (endPosition.getIndexInLine()); firstLine->updateLength(); } else { lastAffectedLine = lines.size(); CodeDocumentLine* const lastLine = lines.getUnchecked (endLine); jassert (lastLine != nullptr); firstLine->line = firstLine->line.substring (0, startPosition.getIndexInLine()) + lastLine->line.substring (endPosition.getIndexInLine()); firstLine->updateLength(); int numLinesToRemove = endLine - firstAffectedLine; lines.removeRange (firstAffectedLine + 1, numLinesToRemove); } int i; for (i = firstAffectedLine + 1; i < lines.size(); ++i) { CodeDocumentLine* const l = lines.getUnchecked (i); const CodeDocumentLine* const previousLine = lines.getUnchecked (i - 1); l->lineStartInFile = previousLine->lineStartInFile + previousLine->lineLength; } checkLastLineStatus(); const int totalChars = getNumCharacters(); for (i = 0; i < positionsToMaintain.size(); ++i) { CodeDocument::Position* p = positionsToMaintain.getUnchecked(i); if (p->getPosition() > startPosition.getPosition()) p->setPosition (jmax (startPos, p->getPosition() + startPos - endPos)); if (p->getPosition() > totalChars) p->setPosition (totalChars); } sendListenerChangeMessage (firstAffectedLine, lastAffectedLine); } }
void CodeDocument::replaceAllContent (const String& newContent) { remove (0, getNumCharacters(), true); insert (newContent, 0, true); }