RedPanda-CPP/RedPandaIDE/qsynedit/Types.cpp

237 lines
5.1 KiB
C++
Raw Normal View History

2021-09-25 07:51:48 +08:00
#include "Types.h"
#include "SynEdit.h"
#include <QDebug>
ContentsCoord::ContentsCoord(const SynEdit *edit, int ch, int line)
2021-09-25 07:51:48 +08:00
{
Q_ASSERT(edit!=nullptr);
mEdit = edit;
mChar = ch;
mLine = line;
normalize();
}
void ContentsCoord::normalize()
2021-09-25 07:51:48 +08:00
{
if (mEdit->lines()->count()==0) {
mChar = 0;
mLine = 0;
return;
}
int aLine = mLine;
int aChar = mChar;
int line = aLine-1;
int lineCount = mEdit->lines()->count();
if (line>=lineCount) {
mChar = mEdit->lines()->getString(lineCount-1).length()+1;
mLine = lineCount;
return;
}
if (line<0) {
mChar = 0;
mLine = 0;
return;
}
if (aChar<1) {
while (true) {
line--;
if (line < 0) {
mChar = 0;
mLine = 0;
return;
}
QString s = mEdit->lines()->getString(line);
int len = s.length();
aChar+=len+1;
if (aChar>=1) {
break;
}
}
} else {
while (true) {
QString s =mEdit->lines()->getString(line);
int len = s.length();
if (aChar<=len+1) {
break;
}
if (line == lineCount-1) {
mChar = 1;
mLine = lineCount+1;
return;
}
aChar -= len+1;
line++;
}
}
mChar = aChar;
mLine = line+1;
return;
}
int ContentsCoord::line() const
2021-09-25 07:51:48 +08:00
{
return mLine;
}
void ContentsCoord::setLine(int newLine)
2021-09-25 07:51:48 +08:00
{
mLine = newLine;
}
bool ContentsCoord::atStart()
2021-09-25 07:51:48 +08:00
{
return mLine<1;
}
bool ContentsCoord::atEnd()
2021-09-25 07:51:48 +08:00
{
Q_ASSERT(mEdit!=nullptr);
return mLine>mEdit->lines()->count();
}
const SynEdit *ContentsCoord::edit() const
2021-09-25 07:51:48 +08:00
{
return mEdit;
}
const ContentsCoord &ContentsCoord::operator=(const ContentsCoord &coord)
2021-09-25 07:51:48 +08:00
{
mEdit = coord.mEdit;
mChar = coord.mChar;
mLine = coord.mLine;
return *this;
}
const ContentsCoord &ContentsCoord::operator=(const ContentsCoord &&coord)
2021-09-25 07:51:48 +08:00
{
if (this!=&coord) {
mEdit = coord.mEdit;
mChar = coord.mChar;
mLine = coord.mLine;
}
return *this;
}
bool ContentsCoord::operator==(const ContentsCoord &coord) const
2021-09-25 07:51:48 +08:00
{
Q_ASSERT(mEdit == coord.mEdit);
return (mLine == coord.mLine)
&& (mChar == coord.mChar);
}
bool ContentsCoord::operator<(const ContentsCoord &coord) const
2021-09-25 07:51:48 +08:00
{
Q_ASSERT(mEdit == coord.mEdit);
return (mLine < coord.mLine) || (mLine == coord.mLine && mChar < coord.mChar);
}
bool ContentsCoord::operator<=(const ContentsCoord &coord) const
2021-09-25 07:51:48 +08:00
{
Q_ASSERT(mEdit == coord.mEdit);
return (mLine < coord.mLine) || (mLine == coord.mLine && mChar <= coord.mChar);
}
bool ContentsCoord::operator>(const ContentsCoord &coord) const
2021-09-25 07:51:48 +08:00
{
Q_ASSERT(mEdit == coord.mEdit);
return (mLine > coord.mLine) || (mLine == coord.mLine && mChar > coord.mChar);
}
bool ContentsCoord::operator>=(const ContentsCoord &coord) const
2021-09-25 07:51:48 +08:00
{
Q_ASSERT(mEdit == coord.mEdit);
return (mLine > coord.mLine) || (mLine == coord.mLine && mChar >= coord.mChar);
}
size_t ContentsCoord::operator-(const ContentsCoord& coord) const
2021-09-25 07:51:48 +08:00
{
Q_ASSERT(mEdit == coord.mEdit);
if (mLine == coord.mLine) {
return mChar - coord.mChar;
} else if (mLine > coord.mLine) {
size_t result = mEdit->lines()->getString(coord.mLine-1).length()+1-coord.mChar;
int line = coord.mLine+1;
while (line<=mLine-1) {
result += mEdit->lines()->getString(line-1).length()+1;
line++;
}
if (mLine<=mEdit->lines()->count()) {
result += mChar;
}
return result;
} else {
return coord - (*this);
}
}
const ContentsCoord &ContentsCoord::operator+=(int delta)
2021-09-25 07:51:48 +08:00
{
mChar+=delta;
normalize();
return *this;
}
const ContentsCoord &ContentsCoord::operator-=(int delta)
2021-09-25 07:51:48 +08:00
{
mChar-=delta;
normalize();
return *this;
}
BufferCoord ContentsCoord::toBufferCoord() const
2021-09-25 07:51:48 +08:00
{
return BufferCoord{mChar,mLine};
}
ContentsCoord ContentsCoord::operator-(int delta) const
2021-09-25 07:51:48 +08:00
{
Q_ASSERT(mEdit != nullptr);
return ContentsCoord(mEdit,mChar-delta,mLine);
2021-09-25 07:51:48 +08:00
}
ContentsCoord ContentsCoord::operator+(int delta) const
2021-09-25 07:51:48 +08:00
{
Q_ASSERT(mEdit != nullptr);
return ContentsCoord(mEdit,mChar+delta,mLine);
2021-09-25 07:51:48 +08:00
}
QChar ContentsCoord::operator*() const
2021-09-25 07:51:48 +08:00
{
Q_ASSERT(mEdit != nullptr);
if (mLine < 1) {
return QChar('\0');
}
if (mLine > mEdit->lines()->count()) {
return QChar('\0');
}
QString s = mEdit->lines()->getString(mLine-1);
2021-09-25 21:34:10 +08:00
if (mChar >= s.length()+1 ) {
2021-09-25 07:51:48 +08:00
return QChar('\n');
}
2021-09-25 21:34:10 +08:00
//qDebug()<<mLine<<":"<<mChar<<" '"<<s<<"'";
2021-09-25 07:51:48 +08:00
return s[mChar-1];
}
ContentsCoord::ContentsCoord()
2021-09-25 07:51:48 +08:00
{
mEdit = nullptr;
mLine = 0;
mEdit = 0;
}
ContentsCoord::ContentsCoord(const ContentsCoord &coord):
ContentsCoord(coord.mEdit,
2021-09-25 07:51:48 +08:00
coord.mChar,
coord.mLine)
{
}
int ContentsCoord::ch() const
2021-09-25 07:51:48 +08:00
{
return mChar;
}
void ContentsCoord::setCh(int newChar)
2021-09-25 07:51:48 +08:00
{
mChar = newChar;
}