• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Kate

kateedit.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2005 Hamish Rodda <rodda@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "kateedit.h"
00020 #include "katedocument.h"
00021 
00022 KateEditInfo::KateEditInfo(KateDocument* doc, Kate::EditSource source, const KTextEditor::Range& oldRange, const QStringList& oldText, const KTextEditor::Range& newRange, const QStringList& newText)
00023   : m_doc(doc)
00024   , m_editSource(source)
00025   , m_oldRange(oldRange)
00026   , m_oldText(oldText)
00027   , m_newRange(newRange)
00028   , m_newText(newText)
00029   , m_revisionTokenCounter(0)
00030 {
00031   m_translate = (m_newRange.end() - m_newRange.start()) - (m_oldRange.end() - m_oldRange.start());
00032 }
00033 
00034 KateEditInfo::~KateEditInfo()
00035 {
00036 }
00037 
00038 Kate::EditSource KateEditInfo::editSource() const
00039 {
00040   return m_editSource;
00041 }
00042 
00043 const KTextEditor::Range & KateEditInfo::oldRange( ) const
00044 {
00045   return m_oldRange;
00046 }
00047 
00048 QString KateEditInfo::oldTextString( const KTextEditor::Range & range ) const
00049 {
00050   Q_ASSERT(m_oldRange.contains(range) && range.start().line() == range.end().line());
00051   QString original = m_oldText[range.start().line() - m_oldRange.start().line()];
00052   int startCol = (range.start().line() == m_oldRange.start().line()) ? m_oldRange.start().column() : 0;
00053   return original.mid(range.start().column() - startCol, range.end().column());
00054 }
00055 
00056 QStringList KateEditInfo::oldText( const KTextEditor::Range & range ) const
00057 {
00058   QStringList ret;
00059   for (int i = range.start().line(); i <= range.end().line(); ++i) {
00060     QString original = m_oldText[range.start().line() - m_oldRange.start().line()];
00061 
00062     int startCol = 0, length = -1;
00063     if (range.start().line() == m_oldRange.start().line())
00064       startCol = range.start().column() - m_oldRange.start().column();
00065     if (range.end().line() == m_oldRange.end().line())
00066       length = range.end().column() - startCol;
00067 
00068     ret << original.mid(startCol, length);
00069   }
00070   return ret;
00071 }
00072 
00073 const QStringList & KateEditInfo::oldText( ) const
00074 {
00075   return m_oldText;
00076 }
00077 
00078 const KTextEditor::Range & KateEditInfo::newRange( ) const
00079 {
00080   return m_newRange;
00081 }
00082 
00083 QString KateEditInfo::newTextString( const KTextEditor::Range & range ) const
00084 {
00085   Q_ASSERT(m_newRange.contains(range) && range.start().line() == range.end().line());
00086   QString original = m_newText[range.start().line() - m_newRange.start().line()];
00087   int startCol = (range.start().line() == m_newRange.start().line()) ? m_newRange.start().column() : 0;
00088   return original.mid(range.start().column() - startCol, range.end().column());
00089 }
00090 
00091 QStringList KateEditInfo::newText( const KTextEditor::Range & range ) const
00092 {
00093   QStringList ret;
00094   for (int i = range.start().line(); i <= range.end().line(); ++i) {
00095     QString original = m_newText[range.start().line() - m_newRange.start().line()];
00096 
00097     int startCol = 0, length = -1;
00098     if (range.start().line() == m_newRange.start().line())
00099       startCol = range.start().column() - m_oldRange.start().column();
00100     if (range.end().line() == m_newRange.end().line())
00101       length = range.end().column() - startCol;
00102 
00103     ret << original.mid(startCol, length);
00104   }
00105   return ret;
00106 }
00107 
00108 KateEditHistory::KateEditHistory( KateDocument * doc )
00109   : QObject(doc)
00110   , m_doc(doc)
00111   , m_buffer(new KateEditInfoGroup())
00112 //  , m_redo(new KateEditInfoGroup())
00113   , m_revision(0)
00114 {
00115 }
00116 
00117 KateEditHistory::~KateEditHistory()
00118 {
00119   delete m_buffer;
00120 }
00121 
00122 void KateEditInfoGroup::addEdit( KateEditInfo * edit )
00123 {
00124   m_edits.append(edit);
00125 }
00126 
00127 KateEditInfoGroup::KateEditInfoGroup( )
00128 {
00129 }
00130 
00131 int KateEditHistory::revision()
00132 {
00133   if (!m_buffer->edits().isEmpty()) {
00134     KateEditInfo* edit = buffer()->edits().last();
00135     if (!edit->isReferenced())
00136       m_revisions.insert(++m_revision, edit);
00137 
00138     edit->referenceRevision();
00139     return m_revision;
00140   }
00141 
00142   return 0;
00143 }
00144 
00145 void KateEditHistory::releaseRevision(int revision)
00146 {
00147   if (m_revisions.contains(revision)) {
00148     KateEditInfo* edit = m_revisions[revision];
00149     edit->dereferenceRevision();
00150     if (!edit->isReferenced())
00151       m_revisions.remove(revision);
00152     return;
00153   }
00154 
00155   kWarning() << "Unknown revision token " << revision;
00156 }
00157 
00158 QList<KateEditInfo*> KateEditHistory::editsBetweenRevisions(int from, int to) const
00159 {
00160   QList<KateEditInfo*> ret;
00161 
00162   if (from == -1)
00163     return ret;
00164 
00165   if (buffer()->edits().isEmpty())
00166     return ret;
00167 
00168   if (to != -1) {
00169     Q_ASSERT(from <= to);
00170     Q_ASSERT(m_revisions.contains(to));
00171   }
00172 
00173   int fromIndex = 0;
00174   if (from != 0) {
00175     Q_ASSERT(m_revisions.contains(from));
00176     KateEditInfo* fromEdit = m_revisions[from];
00177     Q_ASSERT(fromEdit);
00178     fromIndex = buffer()->edits().indexOf(fromEdit);
00179   }
00180 
00181   KateEditInfo* toEdit = to == -1 ? buffer()->edits().last() : m_revisions[to];
00182   Q_ASSERT(toEdit);
00183 
00184   int toIndex = buffer()->edits().indexOf(toEdit);
00185   Q_ASSERT(fromIndex != -1);
00186   Q_ASSERT(toIndex != -1);
00187   Q_ASSERT(fromIndex <= toIndex);
00188 
00189   for (int i = fromIndex; i < toIndex; ++i)
00190     ret.append(buffer()->edits().at(i));
00191 
00192   return ret;
00193 }
00194 
00195 bool KateEditInfo::isReferenced() const
00196 {
00197   return m_revisionTokenCounter;
00198 }
00199 
00200 void KateEditInfo::dereferenceRevision()
00201 {
00202   --m_revisionTokenCounter;
00203 }
00204 
00205 void KateEditInfo::referenceRevision()
00206 {
00207   ++m_revisionTokenCounter;
00208 }
00209 
00210 const QStringList & KateEditInfo::newText() const
00211 {
00212   return m_newText;
00213 }
00214 
00215 bool KateEditInfo::isRemoval() const
00216 {
00217   return !m_oldRange.isEmpty() && m_newRange.isEmpty();
00218 }
00219 
00220 #include "kateedit.moc"

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal