File indexing completed on 2024-05-12 04:38:05

0001 /*
0002     SPDX-FileCopyrightText: 2010 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef KDEVPLATFORM_CURSORINREVISION_H
0008 #define KDEVPLATFORM_CURSORINREVISION_H
0009 
0010 #include <language/languageexport.h>
0011 
0012 #include <KTextEditor/Cursor>
0013 
0014 namespace KDevelop {
0015 /**
0016  * Represents a cursor (line-number and column-number) within a text document.
0017  *
0018  * In KDevelop, this object is used when referencing a cursor that does _not_ point into the
0019  * most current document revision. Therefore, before applying such a cursor in the text
0020  * documents, it has to be translated into the current document revision explicitly, thereby replaying
0021  * eventual changes (see DUChainBase::translate...)
0022  */
0023 
0024 class KDEVPLATFORMLANGUAGE_EXPORT CursorInRevision
0025 {
0026 public:
0027 
0028     int line = 0, column = 0;
0029 
0030     CursorInRevision()
0031     {
0032     }
0033 
0034     CursorInRevision(int _line, int _column) : line(_line)
0035         , column(_column)
0036     {
0037     }
0038 
0039     static CursorInRevision invalid()
0040     {
0041         return CursorInRevision(-1, -1);
0042     }
0043 
0044     bool isValid() const
0045     {
0046         return line != -1 || column != -1;
0047     }
0048 
0049     bool operator<(const CursorInRevision& rhs) const
0050     {
0051         return line < rhs.line || (line == rhs.line && column < rhs.column);
0052     }
0053 
0054     bool operator<=(const CursorInRevision& rhs) const
0055     {
0056         return line < rhs.line || (line == rhs.line && column <= rhs.column);
0057     }
0058 
0059     bool operator>(const CursorInRevision& rhs) const
0060     {
0061         return line > rhs.line || (line == rhs.line && column > rhs.column);
0062     }
0063 
0064     bool operator>=(const CursorInRevision& rhs) const
0065     {
0066         return line > rhs.line || (line == rhs.line && column >= rhs.column);
0067     }
0068 
0069     bool operator ==(const CursorInRevision& rhs) const
0070     {
0071         return line == rhs.line && column == rhs.column;
0072     }
0073 
0074     bool operator !=(const CursorInRevision& rhs) const
0075     {
0076         return !(*this == rhs);
0077     }
0078 
0079     CursorInRevision operator +(const CursorInRevision& rhs) const
0080     {
0081         return CursorInRevision(line + rhs.line, column + rhs.column);
0082     }
0083 
0084     /// @warning Using this is wrong in most cases! If you want
0085     ///  to transform this cursor to the current revision, you should do a proper
0086     ///  mapping instead through @ref KDevelop::DUChainBase or @ref KDevelop::RevisionReference
0087     ///  or @ref KDevelop::DocumentChangeTracker
0088     KTextEditor::Cursor castToSimpleCursor() const
0089     {
0090         return KTextEditor::Cursor(line, column);
0091     }
0092 
0093     /// @warning Using this is wrong in most cases! If you want
0094     ///  to transform this cursor to the current revision, you should do a proper
0095     ///  mapping instead through @ref KDevelop::DUChainBase or @ref KDevelop::RevisionReference
0096     ///  or @ref KDevelop::DocumentChangeTracker
0097     static CursorInRevision castFromSimpleCursor(const KTextEditor::Cursor& cursor)
0098     {
0099         return CursorInRevision(cursor.line(), cursor.column());
0100     }
0101 
0102     /// qDebug() stream operator.  Writes this cursor to the debug output in a nicely formatted way.
0103     inline friend QDebug operator<<(QDebug s, const CursorInRevision& cursor)
0104     {
0105         s.nospace() << "(" << cursor.line << ", " << cursor.column << ")";
0106         return s.space();
0107     }
0108 };
0109 
0110 inline uint qHash(const KDevelop::CursorInRevision& cursor)
0111 {
0112     return cursor.line * 53 + cursor.column * 47;
0113 }
0114 } // namespace KDevelop
0115 
0116 Q_DECLARE_TYPEINFO(KDevelop::CursorInRevision, Q_MOVABLE_TYPE);
0117 Q_DECLARE_METATYPE(KDevelop::CursorInRevision)
0118 
0119 #endif