forked from qt-creator/qt-creator
Utils: add Text::Range::contains function and Position operators
Change-Id: I8876c4df3ca01dfc8f6a0889a1274c44abb89db0 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -11,11 +11,6 @@
|
||||
|
||||
namespace Utils::Text {
|
||||
|
||||
bool Position::operator==(const Position &other) const
|
||||
{
|
||||
return line == other.line && column == other.column;
|
||||
}
|
||||
|
||||
QTextCursor Position::toTextCursor(QTextDocument *doc) const
|
||||
{
|
||||
QTextCursor result(doc);
|
||||
@@ -85,6 +80,36 @@ int Position::toPositionInDocument(const QTextDocument *document) const
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool operator==(const Position &left, const Position &right)
|
||||
{
|
||||
return left.line == right.line && left.column == right.column;
|
||||
}
|
||||
|
||||
bool operator!=(const Position &left, const Position &right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
bool operator>=(const Position &left, const Position &right)
|
||||
{
|
||||
return !(left < right);
|
||||
}
|
||||
|
||||
bool operator>(const Position &left, const Position &right)
|
||||
{
|
||||
return !(left <= right);
|
||||
}
|
||||
|
||||
bool operator<=(const Position &left, const Position &right)
|
||||
{
|
||||
return left.line < right.line || (left.line == right.line && left.column <= right.column);
|
||||
}
|
||||
|
||||
bool operator<(const Position &left, const Position &right)
|
||||
{
|
||||
return left.line < right.line || (left.line == right.line && left.column < right.column);
|
||||
}
|
||||
|
||||
int Range::length(const QString &text) const
|
||||
{
|
||||
if (end.line < begin.line)
|
||||
@@ -118,6 +143,11 @@ bool Range::operator==(const Range &other) const
|
||||
return begin == other.begin && end == other.end;
|
||||
}
|
||||
|
||||
bool Range::contains(const Position &pos) const
|
||||
{
|
||||
return pos >= begin && pos <= end;
|
||||
}
|
||||
|
||||
QTextCursor Range::toTextCursor(QTextDocument *doc) const
|
||||
{
|
||||
QTextCursor cursor(doc);
|
||||
|
@@ -23,12 +23,6 @@ public:
|
||||
int line = 0; // 1-based
|
||||
int column = -1; // 0-based
|
||||
|
||||
bool operator<(const Position &other) const
|
||||
{ return line < other.line || (line == other.line && column < other.column); }
|
||||
bool operator==(const Position &other) const;
|
||||
|
||||
bool operator!=(const Position &other) const { return !(operator==(other)); }
|
||||
|
||||
bool isValid() const { return line > 0 && column >= 0; }
|
||||
|
||||
int toPositionInDocument(const QTextDocument *doc) const;
|
||||
@@ -39,6 +33,13 @@ public:
|
||||
static Position fromCursor(const QTextCursor &cursor);
|
||||
};
|
||||
|
||||
QTCREATOR_UTILS_EXPORT bool operator<(const Position &left, const Position &right);
|
||||
QTCREATOR_UTILS_EXPORT bool operator<=(const Position &left, const Position &right);
|
||||
QTCREATOR_UTILS_EXPORT bool operator>(const Position &left, const Position &right);
|
||||
QTCREATOR_UTILS_EXPORT bool operator>=(const Position &left, const Position &right);
|
||||
QTCREATOR_UTILS_EXPORT bool operator==(const Position &left, const Position &right);
|
||||
QTCREATOR_UTILS_EXPORT bool operator!=(const Position &left, const Position &right);
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT Range
|
||||
{
|
||||
public:
|
||||
@@ -52,6 +53,8 @@ public:
|
||||
|
||||
bool operator!=(const Range &other) const { return !(operator==(other)); }
|
||||
|
||||
bool contains(const Position &pos) const;
|
||||
|
||||
QTextCursor toTextCursor(QTextDocument *doc) const;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user