- enhancement: Toggle comment for asm/makefile/lua files.

This commit is contained in:
Roy Qu 2023-02-17 07:40:12 +08:00
parent 3f971b34bb
commit a8d9550632
14 changed files with 172 additions and 43 deletions

View File

@ -30,6 +30,7 @@ Red Panda C++ Version 2.12
- enhancement: Basic code completion for xmake.lua.
- enhancement: Parser not correctly released if save a c file to non-c file.
- enhancement: Improve auto indent for embedding no-brace statements like for-for-if.
- enhancement: Toggle comment for asm/makefile/lua files.
Red Panda C++ Version 2.11

View File

@ -1693,6 +1693,8 @@ void QSynEdit::doComment()
int endLine;
if (mReadOnly)
return;
if (!syntaxer() || syntaxer()->commentSymbol().isEmpty())
return;
mUndoList->beginBlock();
auto action = finally([this]{
mUndoList->endBlock();
@ -1705,11 +1707,13 @@ void QSynEdit::doComment()
endLine = std::max(origBlockBegin.line - 1, origBlockEnd.line - 2);
else
endLine = origBlockEnd.line - 1;
QString commentSymbol = syntaxer()->commentSymbol();
int symbolLen = commentSymbol.length();
for (int i = origBlockBegin.line - 1; i<=endLine; i++) {
mDocument->putLine(i, "//" + mDocument->getLine(i));
mDocument->putLine(i, commentSymbol + mDocument->getLine(i));
mUndoList->addChange(ChangeReason::Insert,
BufferCoord{1, i + 1},
BufferCoord{3, i + 1},
BufferCoord{1+symbolLen, i + 1},
QStringList(), SelectionMode::Normal);
}
// When grouping similar commands, process one comment action per undo/redo
@ -1730,11 +1734,15 @@ void QSynEdit::doUncomment()
{
BufferCoord origBlockBegin, origBlockEnd, origCaret;
int endLine;
QString s;
QString s,s2;
QStringList changeText;
changeText.append("//");
if (mReadOnly)
return;
if (!syntaxer() || syntaxer()->commentSymbol().isEmpty())
return;
QString commentSymbol=syntaxer()->commentSymbol();
int symbolLen = commentSymbol.length();
changeText.append(commentSymbol);
mUndoList->beginBlock();
auto action = finally([this]{
mUndoList->endBlock();
@ -1749,27 +1757,28 @@ void QSynEdit::doUncomment()
endLine = origBlockEnd.line - 1;
for (int i = origBlockBegin.line - 1; i<= endLine; i++) {
s = mDocument->getLine(i);
s2=s.trimmed();
if (!s2.startsWith(commentSymbol))
continue;
// Find // after blanks only
int j = 0;
while ((j+1 < s.length()) && (s[j] == '\n' || s[j] == '\t'))
j++;
if ((j + 1 < s.length()) && (s[j] == '/') && (s[j + 1] == '/')) {
s.remove(j,2);
s.remove(j,symbolLen);
mDocument->putLine(i,s);
mUndoList->addChange(ChangeReason::Delete,
BufferCoord{j+1, i + 1},
BufferCoord{j + 3, i + 1},
BufferCoord{j+1+symbolLen, i + 1},
changeText, SelectionMode::Normal);
// Move begin of selection
if ((i == origBlockBegin.line - 1) && (origBlockBegin.ch > 1))
origBlockBegin.ch-=2;
origBlockBegin.ch-=symbolLen;
// Move end of selection
if ((i == origBlockEnd.line - 1) && (origBlockEnd.ch > 1))
origBlockEnd.ch-=2;
origBlockEnd.ch-=symbolLen;
// Move caret
if ((i == origCaret.line - 1) && (origCaret.ch > 1))
origCaret.ch-=2;
}
origCaret.ch-=symbolLen;
}
// When grouping similar commands, process one uncomment action per undo/redo
mUndoList->addGroupBreak();
@ -1784,6 +1793,10 @@ void QSynEdit::doToggleComment()
bool allCommented = true;
if (mReadOnly)
return;
if (!syntaxer() || syntaxer()->commentSymbol().isEmpty())
return;
QString commentSymbol=syntaxer()->commentSymbol();
mUndoList->beginBlock();
auto action = finally([this]{
mUndoList->endBlock();
@ -1797,22 +1810,8 @@ void QSynEdit::doToggleComment()
else
endLine = origBlockEnd.line - 1;
for (int i = origBlockBegin.line - 1; i<= endLine; i++) {
s = mDocument->getLine(i);
// Find // after blanks only
int j = 0;
while ((j < s.length()) && (s[j] == '\n' || s[j] == '\t'))
j++;
if (j>= s.length())
continue;
if (s[j] != '/'){
allCommented = false;
break;
}
if (j+1>=s.length()) {
allCommented = false;
break;
}
if (s[j + 1] != '/') {
s = mDocument->getLine(i).trimmed();
if (!s.startsWith(commentSymbol)) {
allCommented = false;
break;
}
@ -1828,21 +1827,27 @@ void QSynEdit::doToggleBlockComment()
QString s;
if (mReadOnly)
return;
if (!syntaxer() || syntaxer()->blockCommentBeginSymbol().isEmpty())
return;
QString beginSymbol=syntaxer()->blockCommentBeginSymbol();
QString endSymbol=syntaxer()->blockCommentEndSymbol();
int beginLen = beginSymbol.length();
int endLen = endSymbol.length();
QString text=selText().trimmed();
if (text.length()>4 && text.startsWith("/*") && text.endsWith("*/")) {
if (text.length()>beginLen+endLen && text.startsWith(beginSymbol) && text.endsWith(endSymbol)) {
QString newText=selText();
int pos = newText.indexOf("/*");
int pos = newText.indexOf(beginSymbol);
if (pos>=0) {
newText.remove(pos,2);
newText.remove(pos,beginLen);
}
pos = newText.lastIndexOf("*/");
pos = newText.lastIndexOf(endSymbol);
if (pos>=0) {
newText.remove(pos,2);
newText.remove(pos,endLen);
}
setSelText(newText);
} else {
QString newText="/*"+selText()+"*/";
QString newText=beginSymbol+selText()+endSymbol;
setSelText(newText);
}

View File

@ -639,4 +639,30 @@ const PTokenAttribute &ASMSyntaxer::labelAttribute() const
{
return mLabelAttribute;
}
QString ASMSyntaxer::commentSymbol()
{
if (mATT)
return "#";
else
return ";";
}
QString ASMSyntaxer::blockCommentBeginSymbol()
{
if (mATT)
return "/*";
else
return "";
}
QString ASMSyntaxer::blockCommentEndSymbol()
{
if (mATT)
return "*/";
else
return "";
}
}

View File

@ -119,6 +119,12 @@ public:
bool isATT() const;
void setATT(bool newATT);
// Syntaxer interface
public:
QString commentSymbol() override;
QString blockCommentBeginSymbol() override;
QString blockCommentEndSymbol() override;
};
}

View File

@ -1425,6 +1425,21 @@ bool CppSyntaxer::supportBraceLevel()
return true;
}
QString CppSyntaxer::commentSymbol()
{
return "//";
}
QString CppSyntaxer::blockCommentBeginSymbol()
{
return "/*";
}
QString CppSyntaxer::blockCommentEndSymbol()
{
return "*/";
}
bool CppSyntaxer::getTokenFinished() const
{
if (mTokenId == TokenId::Comment

View File

@ -208,6 +208,12 @@ public:
// Highlighter interface
public:
bool supportBraceLevel() override;
// Syntaxer interface
public:
QString commentSymbol() override;
QString blockCommentBeginSymbol() override;
QString blockCommentEndSymbol() override;
};
}

View File

@ -1448,4 +1448,19 @@ bool GLSLSyntaxer::supportBraceLevel()
{
return true;
}
QString GLSLSyntaxer::commentSymbol()
{
return "//";
}
QString GLSLSyntaxer::blockCommentBeginSymbol()
{
return "/*";
}
QString GLSLSyntaxer::blockCommentEndSymbol()
{
return "*/";
}
}

View File

@ -196,6 +196,12 @@ public:
// Highlighter interface
public:
bool supportBraceLevel() override;
// Syntaxer interface
public:
QString commentSymbol() override;
QString blockCommentBeginSymbol() override;
QString blockCommentEndSymbol() override;
};
}

View File

@ -969,6 +969,21 @@ void LuaSyntaxer::setUseXMakeLibs(bool newUseXMakeLibs)
}
}
QString LuaSyntaxer::commentSymbol()
{
return "--";
}
QString LuaSyntaxer::blockCommentBeginSymbol()
{
return "--[";
}
QString LuaSyntaxer::blockCommentEndSymbol()
{
return "]";
}
const QSet<QString> &LuaSyntaxer::customTypeKeywords() const
{
return mCustomTypeKeywords;

View File

@ -183,6 +183,12 @@ public:
QMap<QString, QSet<QString> > scopedKeywords() override;
bool useXMakeLibs() const;
void setUseXMakeLibs(bool newUseXMakeLibs);
// Syntaxer interface
public:
QString commentSymbol() override;
QString blockCommentBeginSymbol() override;
QString blockCommentEndSymbol() override;
};
}

View File

@ -691,4 +691,9 @@ QSet<QString> MakefileSyntaxer::keywords()
return Directives;
}
QString MakefileSyntaxer::commentSymbol()
{
return "#";
}
}

View File

@ -156,6 +156,10 @@ public:
public:
QSet<QString> keywords() override;
// Syntaxer interface
public:
QString commentSymbol() override;
};
}

View File

@ -189,6 +189,21 @@ PTokenAttribute Syntaxer::getAttribute(const QString& name) const
return mAttributes.value(name,PTokenAttribute());
}
QString Syntaxer::commentSymbol()
{
return QString();
}
QString Syntaxer::blockCommentBeginSymbol()
{
return QString();
}
QString Syntaxer::blockCommentEndSymbol()
{
return QString();
}
bool Syntaxer::enabled() const
{
return mEnabled;

View File

@ -184,6 +184,10 @@ public:
bool enabled() const;
void setEnabled(bool value);
virtual PTokenAttribute getAttribute(const QString& name) const;
virtual QString commentSymbol();
virtual QString blockCommentBeginSymbol();
virtual QString blockCommentEndSymbol();
protected:
PTokenAttribute mCommentAttribute;