refactor normailizedbuffercoord

This commit is contained in:
royqh1979@gmail.com 2021-09-25 07:51:48 +08:00
parent ba8d42716a
commit 97e37bfd62
6 changed files with 330 additions and 130 deletions

View File

@ -39,6 +39,7 @@ SOURCES += \
qsynedit/Search.cpp \
qsynedit/SearchBase.cpp \
qsynedit/SearchRegex.cpp \
qsynedit/Types.cpp \
settingsdialog/compilerautolinkwidget.cpp \
settingsdialog/debuggeneralwidget.cpp \
settingsdialog/editorautosavewidget.cpp \

View File

@ -2247,21 +2247,20 @@ QString Editor::getHintForFunction(const PStatement &statement, const PStatement
void Editor::updateFunctionTip()
{
BufferCoord caretPos = caretXY();
NormalizedBufferCoord curPos = normalizeBufferPos(caretPos);
NormalizedBufferCoord nextPos;
NormalizedBufferCoord curPos = fromBufferCoord(caretPos);
int nBraces = 0;
int nCommas = 0;
int FMaxScanLength = 500;
// Find out where the function ends...
for (int i=0;i<FMaxScanLength;i++) {
nextPos = moveBufferPos(curPos,1);
// Stopping characters...
QChar ch = charAtNormalizedBufferPos(curPos);
QChar nextCh = charAtNormalizedBufferPos(nextPos);
QChar ch = *curPos;
if (ch == '\0' || ch == ';') {
return;
// Opening brace, increase count
} else if (ch == '(') {
}
QChar nextCh = *(curPos+1);
if (ch == '(') {
nBraces++;
// Ending brace, decrease count or success (found ending)!
} else if (ch == ')') {
@ -2273,35 +2272,30 @@ void Editor::updateFunctionTip()
} else if ((ch == '/') && (nextCh == '/')) {
// Walk up to an enter sequence
while (ch!='\0' && ch!='\n') {
curPos = nextPos;
nextPos = moveBufferPos(curPos,1);
ch = charAtNormalizedBufferPos(curPos);
nextCh = charAtNormalizedBufferPos(nextPos);
curPos+=1;
ch = *curPos;
}
// Skip linebreak;
if (ch == '\n') {
curPos = nextPos;
nextPos = moveBufferPos(curPos,1);
curPos += 1;
}
} else if ((ch == '/') && (nextCh == '*')) {
// Walk up to "*/"
while (ch!='\0' && !(ch=='*' && nextCh=='/')) {
curPos = nextPos;
nextPos = moveBufferPos(curPos,1);
ch = charAtNormalizedBufferPos(curPos);
nextCh = charAtNormalizedBufferPos(nextPos);
curPos += 1;
ch = *curPos;
nextCh = *(curPos+1);
}
// Step over
if (ch!='\0') {
curPos = nextPos;
nextPos = moveBufferPos(curPos,1);
curPos+=1;
}
} else
curPos = nextPos;
curPos += 1;
}
// If we couldn't find the closing brace or reached the FMaxScanLength...
@ -2310,24 +2304,21 @@ void Editor::updateFunctionTip()
}
NormalizedBufferCoord FFunctionEnd = curPos;
NormalizedBufferCoord prevPos;
// We've stopped at the ending ), start walking backwards )*here* with nBraces = -1
for (int i=0;i<FMaxScanLength;i++) {
prevPos = moveBufferPos(curPos,-1);
QChar ch = charAtNormalizedBufferPos(curPos);
QChar prevCh = charAtNormalizedBufferPos(prevPos);
QChar ch = *curPos;
QChar prevCh = *(curPos-1);
if (prevCh == '*' && ch == '/' ) {
while (true) {
curPos = prevPos;
prevPos = moveBufferPos(curPos,-1);
ch = charAtNormalizedBufferPos(curPos);
prevCh = charAtNormalizedBufferPos(prevPos);
curPos -= 1;
ch = *(curPos);
prevCh = *(curPos-1);
if (prevCh == '\0')
return;
if (prevCh == '/' && ch == '*' ) {
curPos = prevPos;
prevPos = moveBufferPos(curPos,-1);
break;;
curPos -= 1;
break;
}
}
} else if (ch == ')') {
@ -2340,8 +2331,8 @@ void Editor::updateFunctionTip()
if (nBraces == 0)
nCommas++;
}
curPos = prevPos;
if (curPos.Line<1)
curPos -= 1;
if (curPos.atStart())
break;
}
@ -2353,24 +2344,21 @@ void Editor::updateFunctionTip()
NormalizedBufferCoord FFunctionStart = curPos;
// Skip blanks
while (curPos.Line>=1) {
prevPos = moveBufferPos(curPos,-1);
QChar prevCh = charAtNormalizedBufferPos(prevPos);
while (!curPos.atStart()) {
QChar prevCh = *(curPos-1);
if (prevCh == '\t' || prevCh == ' '
|| prevCh == '\n') {
curPos = prevPos;
curPos-=1;
} else {
break;
}
}
prevPos = moveBufferPos(curPos,-1);
if (prevPos.Line<1)
NormalizedBufferCoord prevPos = curPos-1;
if (prevPos.atStart())
return;
// Get the name of the function we're about to show
BufferCoord FuncStartXY;
FuncStartXY.Line = prevPos.Line;
FuncStartXY.Char = prevPos.Char;
BufferCoord FuncStartXY = prevPos.toBufferCoord();
QString token;
PSynHighlighterAttribute HLAttr;
if (!getHighlighterAttriAtRowCol(FuncStartXY,token,HLAttr)) {

View File

@ -699,82 +699,14 @@ BufferCoord SynEdit::displayToBufferPos(const DisplayCoord &p) const
return Result;
}
NormalizedBufferCoord SynEdit::moveBufferPos(const BufferCoord &p, int delta) const
NormalizedBufferCoord SynEdit::fromBufferCoord(const BufferCoord &p) const
{
return normalizeBufferPos(p.Char+delta,p.Line);
return createNormalizedBufferCoord(p.Char,p.Line);
}
NormalizedBufferCoord SynEdit::moveBufferPos(const NormalizedBufferCoord &p, int delta) const
NormalizedBufferCoord SynEdit::createNormalizedBufferCoord(int aChar, int aLine) const
{
return normalizeBufferPos(p.Char+delta,p.Line);
}
NormalizedBufferCoord SynEdit::normalizeBufferPos(const BufferCoord &p) const
{
return normalizeBufferPos(p.Char,p.Line);
}
NormalizedBufferCoord SynEdit::normalizeBufferPos(int aChar, int aLine) const
{
if (mLines->count()==0) {
return NormalizedBufferCoord{0,0};
}
int line = aLine-1;
int lineCount = mLines->count();
if (line>=lineCount) {
return NormalizedBufferCoord{
mLines->getString(lineCount-1).length()+1
,lineCount};
}
if (line<0) {
return NormalizedBufferCoord{0,0};
}
if (aChar<1) {
while (true) {
line--;
if (line < 0) {
return NormalizedBufferCoord{0,0};
}
QString s =mLines->getString(line);
int len = s.length();
aChar+=len+1;
if (aChar>=1) {
return NormalizedBufferCoord{aChar,line+1};
}
}
}
while (true) {
QString s =mLines->getString(line);
int len = s.length();
if (aChar<=len+1 ) {
return NormalizedBufferCoord{aChar,line+1};
}
if (line == lineCount-1) {
return NormalizedBufferCoord{1,lineCount+1};
}
aChar -= len+1;
line++;
}
}
QChar SynEdit::charAtNormalizedBufferPos(const NormalizedBufferCoord &p) const
{
if (p.Line < 1) {
return QChar('\0');
}
if (p.Line > mLines->count()) {
return QChar('\0');
}
if (p.Char == 0) {
return QChar('\n');
}
QString s = mLines->getString(p.Line-1);
if (p.Char > p.Line+1) {
return QChar('\n');
}
return s[p.Char-1];
return NormalizedBufferCoord(this,aChar,aLine);
}
QStringList SynEdit::getContents(const NormalizedBufferCoord &pStart, const NormalizedBufferCoord &pEnd)
@ -782,16 +714,16 @@ QStringList SynEdit::getContents(const NormalizedBufferCoord &pStart, const Norm
QStringList result;
if (mLines->count()==0)
return result;
if (pStart.Line>0) {
QString s = mLines->getString(pStart.Line-1);
result += s.mid(pStart.Char-1);
if (pStart.line()>0) {
QString s = mLines->getString(pStart.line()-1);
result += s.mid(pStart.ch()-1);
}
int endLine = std::min(pEnd.Line,mLines->count());
for (int i=pStart.Line;i<endLine-1;i++) {
int endLine = std::min(pEnd.line(),mLines->count());
for (int i=pStart.line();i<endLine-1;i++) {
result += mLines->getString(i);
}
if (pEnd.Line<=mLines->count()) {
result += mLines->getString(pEnd.Line-1).mid(0,pEnd.Char-1);
if (pEnd.line()<=mLines->count()) {
result += mLines->getString(pEnd.line()-1).mid(0,pEnd.ch()-1);
}
return result;
}
@ -6076,3 +6008,19 @@ void SynEdit::onScrollTimeout()
}
computeScroll(iMousePos.x(), iMousePos.y());
}
SynEdit::Contents::Contents(const SynEdit *edit)
{
mEdit = edit;
}
QChar SynEdit::Contents::charAt(const NormalizedBufferCoord &coord) const
{
Q_ASSERT(coord.edit() == mEdit);
return *coord;
}
QChar SynEdit::Contents::operator[](const NormalizedBufferCoord &coord) const
{
return charAt(coord);
}

View File

@ -138,8 +138,6 @@ class SynEdit : public QAbstractScrollArea
{
Q_OBJECT
public:
explicit SynEdit(QWidget *parent = nullptr);
/**
* Returns how many rows are there in the editor
* @return
@ -167,11 +165,8 @@ public:
BufferCoord displayToBufferPos(const DisplayCoord& p) const;
//normalized buffer coord operations
NormalizedBufferCoord moveBufferPos(const BufferCoord&p, int delta) const;
NormalizedBufferCoord moveBufferPos(const NormalizedBufferCoord &p, int delta) const;
NormalizedBufferCoord normalizeBufferPos(const BufferCoord& p) const;
NormalizedBufferCoord normalizeBufferPos(int aChar, int aLine) const;
QChar charAtNormalizedBufferPos(const NormalizedBufferCoord& p) const;
NormalizedBufferCoord fromBufferCoord(const BufferCoord& p) const;
NormalizedBufferCoord createNormalizedBufferCoord(int aChar,int aLine) const;
QStringList getContents(const NormalizedBufferCoord& pStart,const NormalizedBufferCoord& pEnd);
QString getJoinedContents(const NormalizedBufferCoord& pStart,const NormalizedBufferCoord& pEnd, const QString& joinStr);

View File

@ -0,0 +1,235 @@
#include "Types.h"
#include "SynEdit.h"
#include <QDebug>
NormalizedBufferCoord::NormalizedBufferCoord(const SynEdit *edit, int ch, int line)
{
Q_ASSERT(edit!=nullptr);
mEdit = edit;
mChar = ch;
mLine = line;
normalize();
}
void NormalizedBufferCoord::normalize()
{
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 NormalizedBufferCoord::line() const
{
return mLine;
}
void NormalizedBufferCoord::setLine(int newLine)
{
mLine = newLine;
}
bool NormalizedBufferCoord::atStart()
{
return mLine<1;
}
bool NormalizedBufferCoord::atEnd()
{
Q_ASSERT(mEdit!=nullptr);
return mLine>mEdit->lines()->count();
}
const SynEdit *NormalizedBufferCoord::edit() const
{
return mEdit;
}
const NormalizedBufferCoord &NormalizedBufferCoord::operator=(const NormalizedBufferCoord &coord)
{
mEdit = coord.mEdit;
mChar = coord.mChar;
mLine = coord.mLine;
return *this;
}
const NormalizedBufferCoord &NormalizedBufferCoord::operator=(const NormalizedBufferCoord &&coord)
{
if (this!=&coord) {
mEdit = coord.mEdit;
mChar = coord.mChar;
mLine = coord.mLine;
}
return *this;
}
bool NormalizedBufferCoord::operator==(const NormalizedBufferCoord &coord) const
{
Q_ASSERT(mEdit == coord.mEdit);
return (mLine == coord.mLine)
&& (mChar == coord.mChar);
}
bool NormalizedBufferCoord::operator<(const NormalizedBufferCoord &coord) const
{
Q_ASSERT(mEdit == coord.mEdit);
return (mLine < coord.mLine) || (mLine == coord.mLine && mChar < coord.mChar);
}
bool NormalizedBufferCoord::operator<=(const NormalizedBufferCoord &coord) const
{
Q_ASSERT(mEdit == coord.mEdit);
return (mLine < coord.mLine) || (mLine == coord.mLine && mChar <= coord.mChar);
}
bool NormalizedBufferCoord::operator>(const NormalizedBufferCoord &coord) const
{
Q_ASSERT(mEdit == coord.mEdit);
return (mLine > coord.mLine) || (mLine == coord.mLine && mChar > coord.mChar);
}
bool NormalizedBufferCoord::operator>=(const NormalizedBufferCoord &coord) const
{
Q_ASSERT(mEdit == coord.mEdit);
return (mLine > coord.mLine) || (mLine == coord.mLine && mChar >= coord.mChar);
}
size_t NormalizedBufferCoord::operator-(const NormalizedBufferCoord& coord) const
{
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 NormalizedBufferCoord &NormalizedBufferCoord::operator+=(int delta)
{
mChar+=delta;
normalize();
return *this;
}
const NormalizedBufferCoord &NormalizedBufferCoord::operator-=(int delta)
{
mChar-=delta;
normalize();
return *this;
}
BufferCoord NormalizedBufferCoord::toBufferCoord() const
{
return BufferCoord{mChar,mLine};
}
NormalizedBufferCoord NormalizedBufferCoord::operator-(int delta) const
{
Q_ASSERT(mEdit != nullptr);
return NormalizedBufferCoord(mEdit,mChar-delta,mLine);
}
NormalizedBufferCoord NormalizedBufferCoord::operator+(int delta) const
{
Q_ASSERT(mEdit != nullptr);
return NormalizedBufferCoord(mEdit,mChar+delta,mLine);
}
QChar NormalizedBufferCoord::operator*() const
{
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);
if (mChar > s.length()+1 ) {
return QChar('\n');
}
return s[mChar-1];
}
NormalizedBufferCoord::NormalizedBufferCoord()
{
mEdit = nullptr;
mLine = 0;
mEdit = 0;
}
NormalizedBufferCoord::NormalizedBufferCoord(const NormalizedBufferCoord &coord):
NormalizedBufferCoord(coord.mEdit,
coord.mChar,
coord.mLine)
{
}
int NormalizedBufferCoord::ch() const
{
return mChar;
}
void NormalizedBufferCoord::setCh(int newChar)
{
mChar = newChar;
}

View File

@ -12,15 +12,48 @@ struct BufferCoord {
int Line;
};
class SynEdit;
/**
* Nomalized buffer posistion:
* (0,0) means at the start of the file ('\0')
* (1,count of lines+1) means at the end of the file ('\0')
* (length of the line+1, line) means at the line break of the line ('\n')
*/
struct NormalizedBufferCoord {
int Char;
int Line;
class NormalizedBufferCoord {
public:
NormalizedBufferCoord();
NormalizedBufferCoord(const NormalizedBufferCoord& coord);
int ch() const;
void setCh(int newChar);
int line() const;
void setLine(int newLine);
bool atStart();
bool atEnd();
const SynEdit *edit() const;
const NormalizedBufferCoord& operator=(const NormalizedBufferCoord& coord);
const NormalizedBufferCoord& operator=(const NormalizedBufferCoord&& coord);
bool operator==(const NormalizedBufferCoord& coord) const;
bool operator<(const NormalizedBufferCoord& coord) const;
bool operator<=(const NormalizedBufferCoord& coord) const;
bool operator>(const NormalizedBufferCoord& coord) const;
bool operator>=(const NormalizedBufferCoord& coord) const;
size_t operator-(const NormalizedBufferCoord& coord) const;
const NormalizedBufferCoord& operator+=(int delta);
const NormalizedBufferCoord& operator-=(int delta);
NormalizedBufferCoord operator+(int delta) const;
NormalizedBufferCoord operator-(int delta) const;
BufferCoord toBufferCoord() const;
QChar operator*() const;
private:
NormalizedBufferCoord(const SynEdit* edit, int ch, int line);
void normalize();
private:
int mChar;
int mLine;
const SynEdit* mEdit;
friend class SynEdit;
};
struct DisplayCoord {