From 4eec185ac4e5dedf103155a66bcc5836eb6ca06a Mon Sep 17 00:00:00 2001 From: royqh1979 Date: Sun, 31 Oct 2021 00:08:20 +0800 Subject: [PATCH] - enhancement: correctly handle auto indents for statement span many lines; - enhancment: only use colors have good contrasts with the background in the class browser and code completion suggestion window --- NEWS.md | 2 ++ RedPandaIDE/mainwindow.cpp | 30 +++++++++++++++++------- RedPandaIDE/qsynedit/SynEdit.cpp | 25 +++++++++++--------- RedPandaIDE/qsynedit/highlighter/base.h | 1 - RedPandaIDE/qsynedit/highlighter/cpp.cpp | 21 +++-------------- RedPandaIDE/qsynedit/highlighter/cpp.h | 1 - RedPandaIDE/widgets/classbrowser.cpp | 4 ++-- 7 files changed, 42 insertions(+), 42 deletions(-) diff --git a/NEWS.md b/NEWS.md index c6dba37a..a7cc3a27 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ Version 0.7.6 - enhancement: the line containing 'public:' / 'private:' / 'protected:' / 'case *:' will use of indents of the surrounding '{' line, instead of just unindent one level - enhancement: correctly handle auto indents for multi-level embedding complex statements like 'for(...) if (...) printf(); - change: Don't use 'pause' in the console pauser, in case of privilege problems. + - enhancement: correctly handle auto indents for statement span many lines; + - enhancment: only use colors have good contrasts with the background in the class browser and code completion suggestion window Version 0.7.5 - enhancement: more accurate auto indent calculation diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index 4c8d14ef..167e9f39 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -425,56 +425,67 @@ void MainWindow::updateCompileActions() || mEditorList->pageCount()>0); } +static bool haveGoodContrast(const QColor& c1, const QColor &c2) { + int lightness1 = c1.lightness(); + int lightness2 = c2.lightness(); + return std::abs(lightness1 - lightness2)>=150; +} void MainWindow::updateEditorColorSchemes() { + if (!mStatementColors) + return; + mStatementColors->clear(); + mEditorList->applyColorSchemes(pSettings->editor().colorScheme()); QString schemeName = pSettings->editor().colorScheme(); //color for code completion popup PColorSchemeItem item; + item = pColorManager->getItem(schemeName, SYNS_AttrFunction); - if (item) { + QColor baseColor = palette().color(QPalette::Base); + if (item && haveGoodContrast(item->foreground(), baseColor)) { mStatementColors->insert(StatementKind::skFunction,item); mStatementColors->insert(StatementKind::skConstructor,item); mStatementColors->insert(StatementKind::skDestructor,item); } item = pColorManager->getItem(schemeName, SYNS_AttrClass); - if (item) { + if (item && haveGoodContrast(item->foreground(), baseColor)) { mStatementColors->insert(StatementKind::skClass,item); mStatementColors->insert(StatementKind::skTypedef,item); mStatementColors->insert(StatementKind::skAlias,item); } item = pColorManager->getItem(schemeName, SYNS_AttrIdentifier); - if (item) { + if (item && haveGoodContrast(item->foreground(), baseColor)) { mStatementColors->insert(StatementKind::skEnumType,item); mStatementColors->insert(StatementKind::skEnumClassType,item); } item = pColorManager->getItem(schemeName, SYNS_AttrVariable); - if (item) { + if (item && haveGoodContrast(item->foreground(), baseColor)) { mStatementColors->insert(StatementKind::skVariable,item); } item = pColorManager->getItem(schemeName, SYNS_AttrLocalVariable); - if (item) { + if (item && haveGoodContrast(item->foreground(), baseColor)) { mStatementColors->insert(StatementKind::skLocalVariable,item); mStatementColors->insert(StatementKind::skParameter,item); } item = pColorManager->getItem(schemeName, SYNS_AttrGlobalVariable); - if (item) { + if (item && haveGoodContrast(item->foreground(), baseColor)) { mStatementColors->insert(StatementKind::skGlobalVariable,item); } item = pColorManager->getItem(schemeName, SYNS_AttrPreprocessor); - if (item) { + if (item && haveGoodContrast(item->foreground(), baseColor)) { mStatementColors->insert(StatementKind::skPreprocessor,item); mStatementColors->insert(StatementKind::skEnum,item); mHeaderCompletionPopup->setSuggestionColor(item->foreground()); } item = pColorManager->getItem(schemeName, SYNS_AttrReservedWord); - if (item) { + if (item && haveGoodContrast(item->foreground(), baseColor)) { mStatementColors->insert(StatementKind::skKeyword,item); mStatementColors->insert(StatementKind::skUserCodeSnippet,item); } item = pColorManager->getItem(schemeName, SYNS_AttrString); - if (item) { + if (item && haveGoodContrast(item->foreground(), baseColor)) { mStatementColors->insert(StatementKind::skNamespace,item); mStatementColors->insert(StatementKind::skNamespaceAlias,item); } @@ -490,6 +501,7 @@ void MainWindow::applySettings() else QApplication::setStyle("fusion"); qApp->setPalette(appTheme->palette()); + updateEditorColorSchemes(); QFont font(pSettings->environment().interfaceFont(), pSettings->environment().interfaceFontSize()); diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index b6fc104a..35eb2fb0 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -1455,13 +1455,6 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent) int len = matchingIndents.length(); while (i= sitStatemntBrace) { - int counts = indent - sitStatemntBrace; - for (int j=0;j=0) { newIndents.remove(idx,newIndents.length()-idx); @@ -1471,10 +1464,20 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent) i++; } if (i>=len) { - indentSpaces = leftSpaces(mLines->getString(l-1)); -// if (newIndents.length()>0) -// indentSpaces+=mTabWidth; - break; + // we found the where the indent started + if (len>0 && !range.matchingIndents.isEmpty() + && + ( matchingIndents.back()== sitBrace + || matchingIndents.back() == sitStatement + ) ) { + // but it's not a complete statement + matchingIndents = range.matchingIndents; + } else { + indentSpaces = leftSpaces(mLines->getString(l-1)); + if (newIndents.length()>0) + indentSpaces+=mTabWidth; + break; + } } else { matchingIndents = range.matchingIndents + matchingIndents.mid(i); } diff --git a/RedPandaIDE/qsynedit/highlighter/base.h b/RedPandaIDE/qsynedit/highlighter/base.h index f8ccd6d9..fe345976 100644 --- a/RedPandaIDE/qsynedit/highlighter/base.h +++ b/RedPandaIDE/qsynedit/highlighter/base.h @@ -15,7 +15,6 @@ enum SynIndentType { sitParenthesis = 1, sitBracket = 2, sitStatement = 3, - sitStatemntBrace = 100 }; struct SynRangeState { diff --git a/RedPandaIDE/qsynedit/highlighter/cpp.cpp b/RedPandaIDE/qsynedit/highlighter/cpp.cpp index 226655d2..187d3654 100644 --- a/RedPandaIDE/qsynedit/highlighter/cpp.cpp +++ b/RedPandaIDE/qsynedit/highlighter/cpp.cpp @@ -393,8 +393,9 @@ void SynEditCppHighlighter::braceOpenProc() if (mRange.getLastIndent() == sitStatement) { // if last indent is started by 'if' 'for' etc // just replace it - int counts = popStatementIndents(); - pushIndents(sitStatemntBrace+counts); + while (mRange.getLastIndent() == sitStatement) + popIndents(sitStatement); + pushIndents(sitBrace); // int idx = mRange.indents.length()-1; // if (idx < mRange.firstIndentThisLine) { // mRange.firstIndentThisLine = idx; @@ -1358,8 +1359,6 @@ void SynEditCppHighlighter::processChar() void SynEditCppHighlighter::popIndents(int indentType) { while (!mRange.indents.isEmpty() && mRange.indents.back()!=indentType) { - if (indentType == sitBrace && mRange.indents.back() >= sitStatemntBrace) - break; mRange.indents.pop_back(); } if (!mRange.indents.isEmpty()) { @@ -1371,20 +1370,6 @@ void SynEditCppHighlighter::popIndents(int indentType) } } -int SynEditCppHighlighter::popStatementIndents() -{ - int counts = 0; - while (!mRange.indents.isEmpty() && mRange.indents.back() == sitStatement) { - int idx = mRange.indents.length()-1; - if (idx < mRange.firstIndentThisLine) { -// mRange.matchingIndents.append(mRange.indents[idx]); - counts++; - } - mRange.indents.pop_back(); - } - return counts; -} - void SynEditCppHighlighter::pushIndents(int indentType) { int idx = mRange.indents.length(); diff --git a/RedPandaIDE/qsynedit/highlighter/cpp.h b/RedPandaIDE/qsynedit/highlighter/cpp.h index fefd2868..4b24524d 100644 --- a/RedPandaIDE/qsynedit/highlighter/cpp.h +++ b/RedPandaIDE/qsynedit/highlighter/cpp.h @@ -126,7 +126,6 @@ private: void xorSymbolProc(); void processChar(); void popIndents(int indentType); - int popStatementIndents(); void pushIndents(int indentType); private: diff --git a/RedPandaIDE/widgets/classbrowser.cpp b/RedPandaIDE/widgets/classbrowser.cpp index 6839c333..2d4181fd 100644 --- a/RedPandaIDE/widgets/classbrowser.cpp +++ b/RedPandaIDE/widgets/classbrowser.cpp @@ -127,7 +127,7 @@ QVariant ClassBrowserModel::data(const QModelIndex &index, int role) const return node->statement->command + node->statement->args; } } else if (role == Qt::ForegroundRole) { - if (node->statement) { + if (mColors && node->statement) { PStatement statement = (node->statement); StatementKind kind; if (mParser) { @@ -143,8 +143,8 @@ QVariant ClassBrowserModel::data(const QModelIndex &index, int role) const if (item) { return item->foreground(); } - return pMainWindow->palette().color(QPalette::Text); } + return pMainWindow->palette().color(QPalette::Text); } else if (role == Qt::DecorationRole) { if (node->statement) { PStatement statement = (node->statement);