From a8d9550632510a169e7485ef3b86021b6cd69bc4 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Fri, 17 Feb 2023 07:40:12 +0800 Subject: [PATCH] - enhancement: Toggle comment for asm/makefile/lua files. --- NEWS.md | 1 + libs/qsynedit/qsynedit/qsynedit.cpp | 91 +++++++++++--------- libs/qsynedit/qsynedit/syntaxer/asm.cpp | 26 ++++++ libs/qsynedit/qsynedit/syntaxer/asm.h | 6 ++ libs/qsynedit/qsynedit/syntaxer/cpp.cpp | 15 ++++ libs/qsynedit/qsynedit/syntaxer/cpp.h | 6 ++ libs/qsynedit/qsynedit/syntaxer/glsl.cpp | 15 ++++ libs/qsynedit/qsynedit/syntaxer/glsl.h | 6 ++ libs/qsynedit/qsynedit/syntaxer/lua.cpp | 15 ++++ libs/qsynedit/qsynedit/syntaxer/lua.h | 6 ++ libs/qsynedit/qsynedit/syntaxer/makefile.cpp | 5 ++ libs/qsynedit/qsynedit/syntaxer/makefile.h | 4 + libs/qsynedit/qsynedit/syntaxer/syntaxer.cpp | 15 ++++ libs/qsynedit/qsynedit/syntaxer/syntaxer.h | 4 + 14 files changed, 172 insertions(+), 43 deletions(-) diff --git a/NEWS.md b/NEWS.md index b2f1c7d4..e5df6e0a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/libs/qsynedit/qsynedit/qsynedit.cpp b/libs/qsynedit/qsynedit/qsynedit.cpp index 533cf0a0..03a9ec4b 100644 --- a/libs/qsynedit/qsynedit/qsynedit.cpp +++ b/libs/qsynedit/qsynedit/qsynedit.cpp @@ -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); - mDocument->putLine(i,s); - mUndoList->addChange(ChangeReason::Delete, - BufferCoord{j+1, i + 1}, - BufferCoord{j + 3, i + 1}, - changeText, SelectionMode::Normal); - // Move begin of selection - if ((i == origBlockBegin.line - 1) && (origBlockBegin.ch > 1)) - origBlockBegin.ch-=2; - // Move end of selection - if ((i == origBlockEnd.line - 1) && (origBlockEnd.ch > 1)) - origBlockEnd.ch-=2; - // Move caret - if ((i == origCaret.line - 1) && (origCaret.ch > 1)) - origCaret.ch-=2; - } + s.remove(j,symbolLen); + mDocument->putLine(i,s); + mUndoList->addChange(ChangeReason::Delete, + BufferCoord{j+1, 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-=symbolLen; + // Move end of selection + if ((i == origBlockEnd.line - 1) && (origBlockEnd.ch > 1)) + origBlockEnd.ch-=symbolLen; + // Move caret + if ((i == origCaret.line - 1) && (origCaret.ch > 1)) + 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); } diff --git a/libs/qsynedit/qsynedit/syntaxer/asm.cpp b/libs/qsynedit/qsynedit/syntaxer/asm.cpp index a81b08dc..76a1cf1e 100644 --- a/libs/qsynedit/qsynedit/syntaxer/asm.cpp +++ b/libs/qsynedit/qsynedit/syntaxer/asm.cpp @@ -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 ""; +} + } diff --git a/libs/qsynedit/qsynedit/syntaxer/asm.h b/libs/qsynedit/qsynedit/syntaxer/asm.h index b379e89c..c3f6f682 100644 --- a/libs/qsynedit/qsynedit/syntaxer/asm.h +++ b/libs/qsynedit/qsynedit/syntaxer/asm.h @@ -119,6 +119,12 @@ public: bool isATT() const; void setATT(bool newATT); + + // Syntaxer interface +public: + QString commentSymbol() override; + QString blockCommentBeginSymbol() override; + QString blockCommentEndSymbol() override; }; } diff --git a/libs/qsynedit/qsynedit/syntaxer/cpp.cpp b/libs/qsynedit/qsynedit/syntaxer/cpp.cpp index 4128a92e..7b22b4fb 100644 --- a/libs/qsynedit/qsynedit/syntaxer/cpp.cpp +++ b/libs/qsynedit/qsynedit/syntaxer/cpp.cpp @@ -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 diff --git a/libs/qsynedit/qsynedit/syntaxer/cpp.h b/libs/qsynedit/qsynedit/syntaxer/cpp.h index e47ad954..609636ee 100644 --- a/libs/qsynedit/qsynedit/syntaxer/cpp.h +++ b/libs/qsynedit/qsynedit/syntaxer/cpp.h @@ -208,6 +208,12 @@ public: // Highlighter interface public: bool supportBraceLevel() override; + + // Syntaxer interface +public: + QString commentSymbol() override; + QString blockCommentBeginSymbol() override; + QString blockCommentEndSymbol() override; }; } diff --git a/libs/qsynedit/qsynedit/syntaxer/glsl.cpp b/libs/qsynedit/qsynedit/syntaxer/glsl.cpp index 8af7f39c..a3a505b8 100644 --- a/libs/qsynedit/qsynedit/syntaxer/glsl.cpp +++ b/libs/qsynedit/qsynedit/syntaxer/glsl.cpp @@ -1448,4 +1448,19 @@ bool GLSLSyntaxer::supportBraceLevel() { return true; } + +QString GLSLSyntaxer::commentSymbol() +{ + return "//"; +} + +QString GLSLSyntaxer::blockCommentBeginSymbol() +{ + return "/*"; +} + +QString GLSLSyntaxer::blockCommentEndSymbol() +{ + return "*/"; +} } diff --git a/libs/qsynedit/qsynedit/syntaxer/glsl.h b/libs/qsynedit/qsynedit/syntaxer/glsl.h index 2c09bb7d..f0778802 100644 --- a/libs/qsynedit/qsynedit/syntaxer/glsl.h +++ b/libs/qsynedit/qsynedit/syntaxer/glsl.h @@ -196,6 +196,12 @@ public: // Highlighter interface public: bool supportBraceLevel() override; + + // Syntaxer interface +public: + QString commentSymbol() override; + QString blockCommentBeginSymbol() override; + QString blockCommentEndSymbol() override; }; } diff --git a/libs/qsynedit/qsynedit/syntaxer/lua.cpp b/libs/qsynedit/qsynedit/syntaxer/lua.cpp index 3a320a55..97450bb9 100644 --- a/libs/qsynedit/qsynedit/syntaxer/lua.cpp +++ b/libs/qsynedit/qsynedit/syntaxer/lua.cpp @@ -969,6 +969,21 @@ void LuaSyntaxer::setUseXMakeLibs(bool newUseXMakeLibs) } } +QString LuaSyntaxer::commentSymbol() +{ + return "--"; +} + +QString LuaSyntaxer::blockCommentBeginSymbol() +{ + return "--["; +} + +QString LuaSyntaxer::blockCommentEndSymbol() +{ + return "]"; +} + const QSet &LuaSyntaxer::customTypeKeywords() const { return mCustomTypeKeywords; diff --git a/libs/qsynedit/qsynedit/syntaxer/lua.h b/libs/qsynedit/qsynedit/syntaxer/lua.h index 3619e874..a67ed6dd 100644 --- a/libs/qsynedit/qsynedit/syntaxer/lua.h +++ b/libs/qsynedit/qsynedit/syntaxer/lua.h @@ -183,6 +183,12 @@ public: QMap > scopedKeywords() override; bool useXMakeLibs() const; void setUseXMakeLibs(bool newUseXMakeLibs); + + // Syntaxer interface +public: + QString commentSymbol() override; + QString blockCommentBeginSymbol() override; + QString blockCommentEndSymbol() override; }; } diff --git a/libs/qsynedit/qsynedit/syntaxer/makefile.cpp b/libs/qsynedit/qsynedit/syntaxer/makefile.cpp index 705e58f0..f74ac3d8 100644 --- a/libs/qsynedit/qsynedit/syntaxer/makefile.cpp +++ b/libs/qsynedit/qsynedit/syntaxer/makefile.cpp @@ -691,4 +691,9 @@ QSet MakefileSyntaxer::keywords() return Directives; } +QString MakefileSyntaxer::commentSymbol() +{ + return "#"; +} + } diff --git a/libs/qsynedit/qsynedit/syntaxer/makefile.h b/libs/qsynedit/qsynedit/syntaxer/makefile.h index 66831a10..1a6c00eb 100644 --- a/libs/qsynedit/qsynedit/syntaxer/makefile.h +++ b/libs/qsynedit/qsynedit/syntaxer/makefile.h @@ -156,6 +156,10 @@ public: public: QSet keywords() override; + + // Syntaxer interface +public: + QString commentSymbol() override; }; } diff --git a/libs/qsynedit/qsynedit/syntaxer/syntaxer.cpp b/libs/qsynedit/qsynedit/syntaxer/syntaxer.cpp index d41d6f07..e1f583bb 100644 --- a/libs/qsynedit/qsynedit/syntaxer/syntaxer.cpp +++ b/libs/qsynedit/qsynedit/syntaxer/syntaxer.cpp @@ -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; diff --git a/libs/qsynedit/qsynedit/syntaxer/syntaxer.h b/libs/qsynedit/qsynedit/syntaxer/syntaxer.h index 75be957c..84916609 100644 --- a/libs/qsynedit/qsynedit/syntaxer/syntaxer.h +++ b/libs/qsynedit/qsynedit/syntaxer/syntaxer.h @@ -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;