From 97e37bfd623ecc386cd4ccadc5f1e51f1f5d2cf9 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Sat, 25 Sep 2021 07:51:48 +0800 Subject: [PATCH] refactor normailizedbuffercoord --- RedPandaIDE/RedPandaIDE.pro | 1 + RedPandaIDE/editor.cpp | 70 ++++----- RedPandaIDE/qsynedit/SynEdit.cpp | 106 ++++---------- RedPandaIDE/qsynedit/SynEdit.h | 9 +- RedPandaIDE/qsynedit/Types.cpp | 235 +++++++++++++++++++++++++++++++ RedPandaIDE/qsynedit/Types.h | 39 ++++- 6 files changed, 330 insertions(+), 130 deletions(-) create mode 100644 RedPandaIDE/qsynedit/Types.cpp diff --git a/RedPandaIDE/RedPandaIDE.pro b/RedPandaIDE/RedPandaIDE.pro index 8e7a168e..2c858dd2 100644 --- a/RedPandaIDE/RedPandaIDE.pro +++ b/RedPandaIDE/RedPandaIDE.pro @@ -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 \ diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index 55fc6f77..7a7d70db 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -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=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)) { diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index 8f15d0c7..5a5a43ab 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -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;icount()); + for (int i=pStart.line();igetString(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); +} diff --git a/RedPandaIDE/qsynedit/SynEdit.h b/RedPandaIDE/qsynedit/SynEdit.h index fd0ee461..baf0fe65 100644 --- a/RedPandaIDE/qsynedit/SynEdit.h +++ b/RedPandaIDE/qsynedit/SynEdit.h @@ -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); diff --git a/RedPandaIDE/qsynedit/Types.cpp b/RedPandaIDE/qsynedit/Types.cpp new file mode 100644 index 00000000..d1dcd70d --- /dev/null +++ b/RedPandaIDE/qsynedit/Types.cpp @@ -0,0 +1,235 @@ +#include "Types.h" +#include "SynEdit.h" +#include +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; +} diff --git a/RedPandaIDE/qsynedit/Types.h b/RedPandaIDE/qsynedit/Types.h index e8ae0a88..2a39977c 100644 --- a/RedPandaIDE/qsynedit/Types.h +++ b/RedPandaIDE/qsynedit/Types.h @@ -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 {