- fix: After spaces in comments and strings, symbol completion for '{' and '(' are wrong.

This commit is contained in:
Roy Qu 2024-03-01 19:30:37 +08:00
parent 25453e28eb
commit cb66c7f340
16 changed files with 48 additions and 95 deletions

View File

@ -24,6 +24,7 @@ Red Panda C++ Version 2.27
- fix: Selected lines doesn't draw line break glyphs. - fix: Selected lines doesn't draw line break glyphs.
- fix: issue #215 (Caret may be drawn in the gutter.) - fix: issue #215 (Caret may be drawn in the gutter.)
- change: Force use utf8 as the exec encoding for fmtlib in the auto link options page. - change: Force use utf8 as the exec encoding for fmtlib in the auto link options page.
- fix: After spaces in comments and strings, symbol completion for '{' and '(' are wrong.
Red Panda C++ Version 2.26 Red Panda C++ Version 2.26
- enhancement: Code suggestion for embedded std::vectors. - enhancement: Code suggestion for embedded std::vectors.

View File

@ -605,15 +605,15 @@ void Editor::undoSymbolCompletion(int pos)
{ {
QSynedit::PTokenAttribute attr; QSynedit::PTokenAttribute attr;
QString token; QString token;
bool tokenFinished; QSynedit::SyntaxState syntaxState;
if (!syntaxer()) if (!syntaxer())
return; return;
if (!pSettings->editor().removeSymbolPairs()) if (!pSettings->editor().removeSymbolPairs())
return; return;
if (!getTokenAttriAtRowCol(caretXY(), token, tokenFinished, attr)) if (!getTokenAttriAtRowCol(caretXY(), token, attr, syntaxState))
return; return;
if ((attr->tokenType() == QSynedit::TokenType::Comment) && (!tokenFinished)) if (syntaxer()->isCommentNotFinished(syntaxState.state))
return ; return ;
//convert caret x to string index; //convert caret x to string index;
pos--; pos--;
@ -745,7 +745,7 @@ void Editor::keyPressEvent(QKeyEvent *event)
syntaxer()->setLine(s,caretY()); syntaxer()->setLine(s,caretY());
syntaxer()->nextToEol(); syntaxer()->nextToEol();
int state = syntaxer()->getState().state; int state = syntaxer()->getState().state;
if (syntaxer()->isLastLineCommentNotFinished(state)) { if (syntaxer()->isCommentNotFinished(state)) {
if (s=="/**") { //javadoc style docstring if (s=="/**") { //javadoc style docstring
s = lineText().mid(caretX()-1).trimmed(); s = lineText().mid(caretX()-1).trimmed();
if (s=="*/") { if (s=="*/") {
@ -2543,9 +2543,9 @@ bool Editor::handleSymbolCompletion(QChar key)
if (syntaxer()) { if (syntaxer()) {
if (caretX() <= 1) { if (caretX() <= 1) {
if (caretY()>1) { if (caretY()>1) {
if (syntaxer()->isLastLineCommentNotFinished(document()->getSyntaxState(caretY() - 2).state)) if (syntaxer()->isCommentNotFinished(document()->getSyntaxState(caretY() - 2).state))
return false; return false;
if (syntaxer()->isLastLineStringNotFinished(document()->getSyntaxState(caretY() - 2).state) if (syntaxer()->isStringNotFinished(document()->getSyntaxState(caretY() - 2).state)
&& (key!='\"') && (key!='\'')) && (key!='\"') && (key!='\''))
return false; return false;
} }
@ -2554,11 +2554,11 @@ bool Editor::handleSymbolCompletion(QChar key)
// Check if that line is highlighted as comment // Check if that line is highlighted as comment
QSynedit::PTokenAttribute attr; QSynedit::PTokenAttribute attr;
QString token; QString token;
bool tokenFinished; QSynedit::SyntaxState syntaxState;
if (getTokenAttriAtRowCol(HighlightPos, token, tokenFinished, attr)) { if (getTokenAttriAtRowCol(HighlightPos, token, attr, syntaxState)) {
if ((attr->tokenType() == QSynedit::TokenType::Comment) && (!tokenFinished)) if (syntaxer()->isCommentNotFinished(syntaxState.state))
return false; return false;
if ((attr->tokenType() == QSynedit::TokenType::String) && (!tokenFinished) if (syntaxer()->isStringNotFinished(syntaxState.state)
&& (key!='\'') && (key!='\"') && (key!='(') && (key!=')')) && (key!='\'') && (key!='\"') && (key!='(') && (key!=')'))
return false; return false;
if (( key=='<' || key =='>') && (mParser && !mParser->isIncludeLine(lineText()))) if (( key=='<' || key =='>') && (mParser && !mParser->isIncludeLine(lineText())))
@ -3080,7 +3080,7 @@ Editor::QuoteStatus Editor::getQuoteStatus()
QuoteStatus Result = QuoteStatus::NotQuote; QuoteStatus Result = QuoteStatus::NotQuote;
if (!syntaxer()) if (!syntaxer())
return Result; return Result;
if ((caretY()>1) && syntaxer()->isLastLineStringNotFinished(document()->getSyntaxState(caretY() - 2).state)) if ((caretY()>1) && syntaxer()->isStringNotFinished(document()->getSyntaxState(caretY() - 2).state))
Result = QuoteStatus::DoubleQuote; Result = QuoteStatus::DoubleQuote;
QString Line = document()->getLine(caretY()-1); QString Line = document()->getLine(caretY()-1);
@ -3470,11 +3470,10 @@ void Editor::showCompletion(const QString& preWord,bool autoComplete, CodeComple
QString s; QString s;
QSynedit::PTokenAttribute attr; QSynedit::PTokenAttribute attr;
bool tokenFinished;
QSynedit::BufferCoord pBeginPos, pEndPos; QSynedit::BufferCoord pBeginPos, pEndPos;
if (getTokenAttriAtRowCol( if (getTokenAttriAtRowCol(
QSynedit::BufferCoord{caretX() - 1, QSynedit::BufferCoord{caretX() - 1,
caretY()}, s, tokenFinished, attr)) { caretY()}, s, attr)) {
if (attr->tokenType() == QSynedit::TokenType::Preprocessor) {//Preprocessor if (attr->tokenType() == QSynedit::TokenType::Preprocessor) {//Preprocessor
word = getWordAtPosition(this,caretXY(),pBeginPos,pEndPos, WordPurpose::wpDirective); word = getWordAtPosition(this,caretXY(),pBeginPos,pEndPos, WordPurpose::wpDirective);
if (!word.startsWith('#')) { if (!word.startsWith('#')) {

View File

@ -128,7 +128,7 @@ namespace QSynedit {
SyntaxState range; SyntaxState range;
while (commentStartLine>=1) { while (commentStartLine>=1) {
range = editor->document()->getSyntaxState(commentStartLine-1); range = editor->document()->getSyntaxState(commentStartLine-1);
if (!editor->syntaxer()->isLastLineCommentNotFinished(range.state)){ if (!editor->syntaxer()->isCommentNotFinished(range.state)){
commentStartLine++; commentStartLine++;
break; break;
} }

View File

@ -341,7 +341,9 @@ bool QSynEdit::getTokenAttriAtRowCol(const BufferCoord &pos, QString &token, PTo
return getTokenAttriAtRowColEx(pos, token, tmpStart, attri); return getTokenAttriAtRowColEx(pos, token, tmpStart, attri);
} }
bool QSynEdit::getTokenAttriAtRowCol(const BufferCoord &pos, QString &token, bool &tokenFinished, PTokenAttribute &attri) bool QSynEdit::getTokenAttriAtRowCol(
const BufferCoord &pos, QString &token,
PTokenAttribute &attri, SyntaxState &syntaxState)
{ {
int posX, posY, endPos, start; int posX, posY, endPos, start;
QString line; QString line;
@ -362,10 +364,7 @@ bool QSynEdit::getTokenAttriAtRowCol(const BufferCoord &pos, QString &token, boo
endPos = start + token.length()-1; endPos = start + token.length()-1;
if ((posX >= start) && (posX <= endPos)) { if ((posX >= start) && (posX <= endPos)) {
attri = mSyntaxer->getTokenAttribute(); attri = mSyntaxer->getTokenAttribute();
if (posX == endPos) syntaxState = mSyntaxer->getState();
tokenFinished = mSyntaxer->getTokenFinished();
else
tokenFinished = false;
return true; return true;
} }
mSyntaxer->next(); mSyntaxer->next();
@ -374,7 +373,6 @@ bool QSynEdit::getTokenAttriAtRowCol(const BufferCoord &pos, QString &token, boo
} }
token = ""; token = "";
attri = PTokenAttribute(); attri = PTokenAttribute();
tokenFinished = false;
return false; return false;
} }
@ -2321,9 +2319,9 @@ void QSynEdit::insertLine(bool moveCaret)
mSyntaxer->setLine(leftLineText, mCaretY-1); mSyntaxer->setLine(leftLineText, mCaretY-1);
mSyntaxer->nextToEol(); mSyntaxer->nextToEol();
mDocument->setSyntaxState(mCaretY-1,mSyntaxer->getState()); mDocument->setSyntaxState(mCaretY-1,mSyntaxer->getState());
notInComment = !mSyntaxer->isLastLineCommentNotFinished( notInComment = !mSyntaxer->isCommentNotFinished(
mSyntaxer->getState().state) mSyntaxer->getState().state)
&& !mSyntaxer->isLastLineStringNotFinished( && !mSyntaxer->isStringNotFinished(
mSyntaxer->getState().state); mSyntaxer->getState().state);
} }
int indentSpaces = 0; int indentSpaces = 0;
@ -5465,9 +5463,9 @@ int QSynEdit::doInsertTextByNormalMode(const BufferCoord& pos, const QStringList
for (int i=1;i<text.length();i++) { for (int i=1;i<text.length();i++) {
bool notInComment = true; bool notInComment = true;
// if (mHighlighter) { // if (mHighlighter) {
// notInComment = !mHighlighter->isLastLineCommentNotFinished( // notInComment = !mHighlighter->isCommentNotFinished(
// mHighlighter->getRangeState().state) // mHighlighter->getRangeState().state)
// && !mHighlighter->isLastLineStringNotFinished( // && !mHighlighter->isStringNotFinished(
// mHighlighter->getRangeState().state); // mHighlighter->getRangeState().state);
// } // }
caretY=pos.line+i; caretY=pos.line+i;

View File

@ -264,7 +264,7 @@ public:
bool getTokenAttriAtRowCol(const BufferCoord& pos, QString& token, bool getTokenAttriAtRowCol(const BufferCoord& pos, QString& token,
PTokenAttribute& attri); PTokenAttribute& attri);
bool getTokenAttriAtRowCol(const BufferCoord& pos, QString& token, bool getTokenAttriAtRowCol(const BufferCoord& pos, QString& token,
bool& tokenFinished, PTokenAttribute& attri); PTokenAttribute& attri, SyntaxState &syntaxState);
bool getTokenAttriAtRowColEx(const BufferCoord& pos, QString& token, bool getTokenAttriAtRowColEx(const BufferCoord& pos, QString& token,
int &start, PTokenAttribute& attri); int &start, PTokenAttribute& attri);

View File

@ -344,7 +344,7 @@ void ASMSyntaxer::UnknownProc()
mTokenID = TokenId::Unknown; mTokenID = TokenId::Unknown;
} }
bool ASMSyntaxer::isIdentStartChar(const QChar &ch) bool ASMSyntaxer::isIdentStartChar(const QChar &ch) const
{ {
if (ch == '_') { if (ch == '_') {
return true; return true;
@ -1718,17 +1718,12 @@ void ASMSyntaxer::setLine(const QString &newLine, int lineNumber)
next(); next();
} }
bool ASMSyntaxer::getTokenFinished() const bool ASMSyntaxer::isCommentNotFinished(int /*state*/) const
{ {
return true; return true;
} }
bool ASMSyntaxer::isLastLineCommentNotFinished(int /*state*/) const bool ASMSyntaxer::isStringNotFinished(int /*state*/) const
{
return true;
}
bool ASMSyntaxer::isLastLineStringNotFinished(int /*state*/) const
{ {
return true; return true;
} }

View File

@ -91,7 +91,7 @@ private:
void StringProc(); void StringProc();
void SymbolProc(); void SymbolProc();
void UnknownProc(); void UnknownProc();
bool isIdentStartChar(const QChar& ch); bool isIdentStartChar(const QChar& ch) const override;
static void initData(); static void initData();
// SynHighlighter interface // SynHighlighter interface
@ -108,9 +108,8 @@ public:
// SynHighlighter interface // SynHighlighter interface
public: public:
bool getTokenFinished() const override; bool isCommentNotFinished(int state) const override;
bool isLastLineCommentNotFinished(int state) const override; bool isStringNotFinished(int state) const override;
bool isLastLineStringNotFinished(int state) const override;
SyntaxState getState() const override; SyntaxState getState() const override;
void setState(const SyntaxState& rangeState) override; void setState(const SyntaxState& rangeState) override;
void resetState() override; void resetState() override;

View File

@ -1505,17 +1505,7 @@ QString CppSyntaxer::blockCommentEndSymbol()
return "*/"; return "*/";
} }
bool CppSyntaxer::getTokenFinished() const bool CppSyntaxer::isCommentNotFinished(int state) const
{
if (mTokenId == TokenId::Comment
|| mTokenId == TokenId::String
|| mTokenId == TokenId::RawString) {
return mRange.state == RangeState::rsUnknown;
}
return true;
}
bool CppSyntaxer::isLastLineCommentNotFinished(int state) const
{ {
return (state == RangeState::rsAnsiC || return (state == RangeState::rsAnsiC ||
state == RangeState::rsDirectiveComment|| state == RangeState::rsDirectiveComment||
@ -1523,7 +1513,7 @@ bool CppSyntaxer::isLastLineCommentNotFinished(int state) const
state == RangeState::rsCppComment); state == RangeState::rsCppComment);
} }
bool CppSyntaxer::isLastLineStringNotFinished(int state) const bool CppSyntaxer::isStringNotFinished(int state) const
{ {
return state == RangeState::rsString; return state == RangeState::rsString;
} }

View File

@ -177,9 +177,8 @@ private:
// SynHighligterBase interface // SynHighligterBase interface
public: public:
bool getTokenFinished() const override; bool isCommentNotFinished(int state) const override;
bool isLastLineCommentNotFinished(int state) const override; bool isStringNotFinished(int state) const override;
bool isLastLineStringNotFinished(int state) const override;
bool isDocstringNotFinished(int state) const override; bool isDocstringNotFinished(int state) const override;
bool eol() const override; bool eol() const override;
QString getToken() const override; QString getToken() const override;

View File

@ -1236,24 +1236,14 @@ void GLSLSyntaxer::pushIndents(IndentType indentType, int line)
mRange.indents.push_back(IndentInfo{indentType,line}); mRange.indents.push_back(IndentInfo{indentType,line});
} }
bool GLSLSyntaxer::getTokenFinished() const bool GLSLSyntaxer::isCommentNotFinished(int state) const
{
if (mTokenId == TokenId::Comment
|| mTokenId == TokenId::String
|| mTokenId == TokenId::RawString) {
return mRange.state == RangeState::rsUnknown;
}
return true;
}
bool GLSLSyntaxer::isLastLineCommentNotFinished(int state) const
{ {
return (state == RangeState::rsAnsiC || return (state == RangeState::rsAnsiC ||
state == RangeState::rsDirectiveComment|| state == RangeState::rsDirectiveComment||
state == RangeState::rsCppComment); state == RangeState::rsCppComment);
} }
bool GLSLSyntaxer::isLastLineStringNotFinished(int state) const bool GLSLSyntaxer::isStringNotFinished(int state) const
{ {
return state == RangeState::rsMultiLineString; return state == RangeState::rsMultiLineString;
} }

View File

@ -161,9 +161,8 @@ private:
// SynHighligterBase interface // SynHighligterBase interface
public: public:
bool getTokenFinished() const override; bool isCommentNotFinished(int state) const override;
bool isLastLineCommentNotFinished(int state) const override; bool isStringNotFinished(int state) const override;
bool isLastLineStringNotFinished(int state) const override;
bool eol() const override; bool eol() const override;
QString getToken() const override; QString getToken() const override;
const PTokenAttribute &getTokenAttribute() const override; const PTokenAttribute &getTokenAttribute() const override;

View File

@ -1005,22 +1005,13 @@ QMap<QString, QSet<QString> > LuaSyntaxer::scopedKeywords()
return StdLibTables; return StdLibTables;
} }
bool LuaSyntaxer::getTokenFinished() const bool LuaSyntaxer::isCommentNotFinished(int state) const
{
if (mTokenId == TokenId::Comment
|| mTokenId == TokenId::String) {
return mRange.state == RangeState::rsUnknown;
}
return true;
}
bool LuaSyntaxer::isLastLineCommentNotFinished(int state) const
{ {
return (state == RangeState::rsComment || return (state == RangeState::rsComment ||
state == RangeState::rsLongComment); state == RangeState::rsLongComment);
} }
bool LuaSyntaxer::isLastLineStringNotFinished(int state) const bool LuaSyntaxer::isStringNotFinished(int state) const
{ {
return state == RangeState::rsString; return state == RangeState::rsString;
} }

View File

@ -140,9 +140,8 @@ private:
// SynHighligterBase interface // SynHighligterBase interface
public: public:
bool getTokenFinished() const override; bool isCommentNotFinished(int state) const override;
bool isLastLineCommentNotFinished(int state) const override; bool isStringNotFinished(int state) const override;
bool isLastLineStringNotFinished(int state) const override;
bool eol() const override; bool eol() const override;
QString getToken() const override; QString getToken() const override;
const PTokenAttribute &getTokenAttribute() const override; const PTokenAttribute &getTokenAttribute() const override;

View File

@ -649,17 +649,12 @@ void MakefileSyntaxer::setLine(const QString &newLine, int lineNumber)
next(); next();
} }
bool MakefileSyntaxer::getTokenFinished() const bool MakefileSyntaxer::isCommentNotFinished(int /*state*/) const
{
return true;
}
bool MakefileSyntaxer::isLastLineCommentNotFinished(int /*state*/) const
{ {
return false; return false;
} }
bool MakefileSyntaxer::isLastLineStringNotFinished(int /*state*/) const bool MakefileSyntaxer::isStringNotFinished(int /*state*/) const
{ {
return false; return false;
} }

View File

@ -108,7 +108,7 @@ private:
void pushState(); void pushState();
void popState(); void popState();
bool isIdentStartChar(const QChar& ch) { bool isIdentStartChar(const QChar& ch) const override{
if ((ch>='a') && (ch <= 'z')) { if ((ch>='a') && (ch <= 'z')) {
return true; return true;
} }
@ -145,9 +145,8 @@ public:
// SynHighlighter interface // SynHighlighter interface
public: public:
bool getTokenFinished() const override; bool isCommentNotFinished(int state) const override;
bool isLastLineCommentNotFinished(int state) const override; bool isStringNotFinished(int state) const override;
bool isLastLineStringNotFinished(int state) const override;
SyntaxState getState() const override; SyntaxState getState() const override;
void setState(const SyntaxState& rangeState) override; void setState(const SyntaxState& rangeState) override;
void resetState() override; void resetState() override;

View File

@ -141,9 +141,8 @@ public:
virtual bool isIdentChar(const QChar& ch) const; virtual bool isIdentChar(const QChar& ch) const;
virtual bool isIdentStartChar(const QChar& ch) const; virtual bool isIdentStartChar(const QChar& ch) const;
virtual bool getTokenFinished() const = 0; virtual bool isCommentNotFinished(int state) const = 0;
virtual bool isLastLineCommentNotFinished(int state) const = 0; virtual bool isStringNotFinished(int state) const = 0;
virtual bool isLastLineStringNotFinished(int state) const = 0;
virtual bool isDocstringNotFinished(int /* state */) const { return false; } virtual bool isDocstringNotFinished(int /* state */) const { return false; }
virtual bool eol() const = 0; virtual bool eol() const = 0;
virtual SyntaxState getState() const = 0; virtual SyntaxState getState() const = 0;