- enhancement: Syntax highlighting for c++ attributes.

fix: mingw build errors
This commit is contained in:
Roy Qu 2024-03-21 21:28:12 +08:00
parent 86cc3e170e
commit e72b780dc3
5 changed files with 57 additions and 11 deletions

View File

@ -73,6 +73,7 @@ Red Panda C++ Version 2.27
- fix: Option 'Pause after run in console' for tools doesn't work.
- fix: Filename that contains '&' doesn't correctly displayed in the editor tab.
- enhancement: Type induction for "auto &&" vars.
- enhancement: Syntax highlighting for c++ attributes.
Red Panda C++ Version 2.26
- enhancement: Code suggestion for embedded std::vectors.

View File

@ -86,6 +86,7 @@
#include <QMimeType>
#include <QToolTip>
#include <QCompleter>
#include <QUuid>
#include "mainwindow.h"
#include <QScrollBar>

View File

@ -20,6 +20,9 @@
#include <QFont>
#include <QDebug>
#define DATA_KEY_INITIAL_DCHAR_SEQ "initialDCharSeq"
#define DATA_KEY_IN_ATTRIBUTE "inAttribute"
namespace QSynedit {
static const QSet<QString> CppStatementKeyWords {
@ -152,6 +155,21 @@ const QSet<QString> CppSyntaxer::Keywords {
"nullptr",
};
const QSet<QString> CppSyntaxer::StandardAttributes {
"noreturn",
"carries_dependency",
"deprecated",
"fallthrough",
"nodiscard",
"maybe_unused",
"likely",
"unlikely",
"no_unique_address",
"assume",
"optimize_for_synchronized"
};
CppSyntaxer::CppSyntaxer(): Syntaxer()
{
mCharAttribute = std::make_shared<TokenAttribute>(SYNS_AttrCharacter,
@ -293,6 +311,12 @@ bool CppSyntaxer::isCharEscaping(int state)
return state == RangeState::rsCharEscaping;
}
bool CppSyntaxer::isInAttribute(const SyntaxState &state)
{
return state.extraData.contains(DATA_KEY_IN_ATTRIBUTE)
&& state.extraData[DATA_KEY_IN_ATTRIBUTE].toBool();
}
CppSyntaxer::TokenId CppSyntaxer::getTokenId()
{
return mTokenId;
@ -652,6 +676,8 @@ void CppSyntaxer::procIdentifier()
if (CppStatementKeyWords.contains(word)) {
pushIndents(IndentType::Statement);
}
} else if (isInAttribute(mRange) && StandardAttributes.contains(word)) {
mTokenId = TokenId::Key;
} else {
mTokenId = TokenId::Identifier;
}
@ -1002,7 +1028,7 @@ void CppSyntaxer::procRawString()
mTokenId = TokenId::RawString;
QString rawStringInitialDCharSeq;
if (mRange.state == RangeState::rsRawString)
mRange.extraData = std::make_shared<QVariant>("");
mRange.extraData[DATA_KEY_INITIAL_DCHAR_SEQ] = "";
while (mRun<mLineSize) {
if (mRange.state!=RangeState::rsRawStringNotEscaping &&
(mLine[mRun].isSpace()
@ -1019,16 +1045,16 @@ void CppSyntaxer::procRawString()
case '(':
if (mRange.state==RangeState::rsRawString) {
mRange.state = RangeState::rsRawStringNotEscaping;
mRange.extraData = std::make_shared<QVariant>(rawStringInitialDCharSeq);
mRange.extraData[DATA_KEY_INITIAL_DCHAR_SEQ] = rawStringInitialDCharSeq;
}
break;
case ')':
if (mRange.state == RangeState::rsRawStringNotEscaping) {
rawStringInitialDCharSeq = mRange.extraData->toString();
rawStringInitialDCharSeq = mRange.extraData[DATA_KEY_INITIAL_DCHAR_SEQ].toString();
if ( mLine.mid(mRun+1,rawStringInitialDCharSeq.length()) == rawStringInitialDCharSeq) {
mRun = mRun+rawStringInitialDCharSeq.length();
mRange.state = RangeState::rsRawStringEnd;
mRange.extraData = nullptr;
mRange.extraData.remove(DATA_KEY_INITIAL_DCHAR_SEQ);
}
}
break;
@ -1137,7 +1163,16 @@ void CppSyntaxer::procSpace()
void CppSyntaxer::procSquareClose()
{
mRun+=1;
mRun++;
if (mRun < mLineSize && mLine[mRun]==']') {
if (mRange.extraData.contains(DATA_KEY_IN_ATTRIBUTE)
&& mRange.extraData[DATA_KEY_IN_ATTRIBUTE].toBool()) {
mTokenId = TokenId::Symbol;
mRun++;
mRange.extraData.remove(DATA_KEY_IN_ATTRIBUTE);
return;
}
}
mTokenId = TokenId::Symbol;
mRange.bracketLevel--;
if (mRange.bracketLevel<0)
@ -1147,10 +1182,16 @@ void CppSyntaxer::procSquareClose()
void CppSyntaxer::procSquareOpen()
{
mRun+=1;
mTokenId = TokenId::Symbol;
mRange.bracketLevel++;
pushIndents(IndentType::Bracket);
mRun++;
if (mRun < mLineSize && mLine[mRun]=='[') {
mRun++;
mTokenId = TokenId::Symbol;
mRange.extraData[DATA_KEY_IN_ATTRIBUTE]=true;
} else{
mTokenId = TokenId::Symbol;
mRange.bracketLevel++;
pushIndents(IndentType::Bracket);
}
}
void CppSyntaxer::procStar()

View File

@ -90,12 +90,15 @@ public:
static const QSet<QString> ValidIntegerSuffixes;
static const QSet<QString> StandardAttributes;
bool isStringToNextLine(int state);
bool isRawStringStart(int state);
bool isRawStringNoEscape(int state);
bool isRawStringEnd(int state);
bool isCharNotFinished(int state);
bool isCharEscaping(int state);
bool isInAttribute(const SyntaxState &state);
TokenId getTokenId();
private:

View File

@ -23,7 +23,7 @@
#include <QMap>
#include <QSet>
#include <QVector>
#include <QVector>
#include <QVariant>
#include "../types.h"
namespace QSynedit {
@ -57,7 +57,7 @@ struct SyntaxState {
QVector<IndentInfo> indents; // indents stack (needed by auto indent)
IndentInfo lastUnindent;
bool hasTrailingSpaces;
std::shared_ptr<QVariant> extraData;
QMap<QString,QVariant> extraData;
bool operator==(const SyntaxState& s2);
IndentInfo getLastIndent();