File indexing completed on 2024-05-12 16:34:58

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Thomas Zander <zander@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 #include "TextChange.h"
0020 
0021 
0022 TextChange::TextChange()
0023         : m_formerPosition(0), m_position(0), m_previous(0), m_next(0)
0024 {
0025 }
0026 
0027 int TextChange::length() const
0028 {
0029     return m_after.length() - m_before.length();
0030 }
0031 
0032 int TextChange::formerPosition() const
0033 {
0034     return m_formerPosition;
0035 }
0036 
0037 int TextChange::position() const
0038 {
0039     return m_position;
0040 }
0041 
0042 int TextChange::formerLength() const
0043 {
0044     // TODO
0045     return -1;
0046 }
0047 
0048 void TextChange::setPosition(int pos)
0049 {
0050     m_position = pos;
0051     m_formerPosition = pos;
0052 }
0053 
0054 void TextChange::setOldText(const QString &old)
0055 {
0056     m_before = old;
0057 }
0058 
0059 void TextChange::setNewText(const QString &current)
0060 {
0061     m_after = current;
0062 }
0063 
0064 void TextChange::setPrevious(TextChange *item)
0065 {
0066     m_previous = item;
0067 }
0068 
0069 void TextChange::setNext(TextChange *item)
0070 {
0071     m_next = item;
0072 }
0073 
0074 void TextChange::move(int length)
0075 {
0076     m_position += length;
0077     if (m_next)
0078         m_next->move(length);
0079 }
0080 
0081 void TextChange::insertBefore(TextChange *node)
0082 {
0083     move(node->length());
0084     node->setPrevious(previous());
0085     node->setNext(this);
0086     setPrevious(node);
0087     if (node->previous())
0088         node->previous()->setNext(node);
0089 }
0090 
0091 void TextChange::insertAfter(TextChange *node)
0092 {
0093     node->setPrevious(this);
0094     node->setNext(next());
0095     setNext(node);
0096     if (node->next())
0097         node->next()->setPrevious(node);
0098 }
0099 
0100 void TextChange::merge(TextChange *other)
0101 {
0102     // make sure the start of 'other' is within this change instance
0103     Q_ASSERT(other->position() >= position() && other->position() <= position() + length());
0104 
0105     /// this only does very simple merging for now.
0106     m_after.insert(other->position() - m_position, other->after());
0107 }
0108