RedPanda-CPP/RedPandaIDE/HighlighterManager.cpp

157 lines
7.2 KiB
C++
Raw Normal View History

2021-12-26 23:18:28 +08:00
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-05-26 00:04:20 +08:00
#include "HighlighterManager.h"
#include <QFileInfo>
#include <QObject>
2022-09-26 11:18:43 +08:00
#include "qsynedit/highlighter/cpp.h"
#include "qsynedit/highlighter/asm.h"
#include "qsynedit/highlighter/glsl.h"
#include "qsynedit/Constants.h"
2021-06-20 22:54:16 +08:00
#include "colorscheme.h"
2021-05-26 00:04:20 +08:00
HighlighterManager highlighterManager;
HighlighterManager::HighlighterManager()
{
}
2022-09-25 09:55:18 +08:00
QSynedit::PSynHighlighter HighlighterManager::getHighlighter(const QString &filename)
2021-05-26 00:04:20 +08:00
{
QFileInfo info(filename);
QString suffix = info.suffix();
if (suffix.isEmpty() || suffix == "c" || suffix == "cpp" || suffix == "cxx"
|| suffix == "cc" || suffix == "h" || suffix == "hpp"
|| suffix == "hxx" || suffix == "hh" || suffix == "C"
|| suffix == "CPP" || suffix =="H" || suffix == "c++"
|| suffix == "h++") {
2021-06-07 11:02:03 +08:00
return getCppHighlighter();
} else if (suffix == "vs" || suffix == "fs" || suffix == "frag") {
return getGLSLHighlighter();
2021-05-26 00:04:20 +08:00
}
2022-09-25 09:55:18 +08:00
return QSynedit::PSynHighlighter();
2021-05-26 00:04:20 +08:00
}
2022-09-25 09:55:18 +08:00
QSynedit::PSynHighlighter HighlighterManager::copyHighlighter(QSynedit::PSynHighlighter highlighter)
2021-06-20 22:54:16 +08:00
{
if (!highlighter)
2022-09-25 09:55:18 +08:00
return QSynedit::PSynHighlighter();
2021-06-20 22:54:16 +08:00
if (highlighter->getName() == SYN_HIGHLIGHTER_CPP)
return getCppHighlighter();
2022-09-25 09:55:18 +08:00
else if (highlighter->getName() == SYN_HIGHLIGHTER_ASM)
return getAsmHighlighter();
else if (highlighter->getName() == SYN_HIGHLIGHTER_GLSL)
return getGLSLHighlighter();
2021-06-24 20:43:09 +08:00
//todo
2022-09-25 09:55:18 +08:00
return QSynedit::PSynHighlighter();
2021-06-20 22:54:16 +08:00
}
2022-09-25 09:55:18 +08:00
QSynedit::PSynHighlighter HighlighterManager::getCppHighlighter()
2021-05-26 00:04:20 +08:00
{
2022-09-25 09:55:18 +08:00
QSynedit::SynEditCppHighlighter* highlighter = new QSynedit::SynEditCppHighlighter();
2022-01-04 16:50:54 +08:00
highlighter->asmAttribute()->setForeground(Qt::blue);
highlighter->charAttribute()->setForeground(Qt::black);
2021-05-26 00:04:20 +08:00
highlighter->commentAttribute()->setForeground(0x8C8C8C);
2022-09-25 09:55:18 +08:00
highlighter->commentAttribute()->setStyles(QSynedit::SynFontStyle::fsItalic);
2021-05-26 00:04:20 +08:00
highlighter->classAttribute()->setForeground(0x008080);
2022-01-04 16:50:54 +08:00
highlighter->floatAttribute()->setForeground(Qt::darkMagenta);
2021-05-26 00:04:20 +08:00
highlighter->functionAttribute()->setForeground(0x00627A);
highlighter->globalVarAttribute()->setForeground(0x660E7A);
2022-01-04 16:50:54 +08:00
highlighter->hexAttribute()->setForeground(Qt::darkMagenta);
2021-05-26 00:04:20 +08:00
highlighter->identifierAttribute()->setForeground(0x080808);
2022-01-04 16:50:54 +08:00
highlighter->invalidAttribute()->setForeground(Qt::red);
highlighter->localVarAttribute()->setForeground(Qt::black);
2021-05-26 00:04:20 +08:00
highlighter->numberAttribute()->setForeground(0x1750EB);
2022-01-04 16:50:54 +08:00
highlighter->octAttribute()->setForeground(Qt::darkMagenta);
highlighter->preprocessorAttribute()->setForeground(0x1f542e);
2021-07-02 10:32:29 +08:00
highlighter->keywordAttribute()->setForeground(0x0033b3);
2022-01-04 16:50:54 +08:00
highlighter->whitespaceAttribute()->setForeground(Qt::lightGray);
2021-05-26 00:04:20 +08:00
highlighter->stringAttribute()->setForeground(0x007d17);
2022-01-04 16:50:54 +08:00
highlighter->stringEscapeSequenceAttribute()->setForeground(Qt::red);
2021-05-26 00:04:20 +08:00
highlighter->symbolAttribute()->setForeground(0xc10000);
highlighter->variableAttribute()->setForeground(0x400080);
2022-09-25 09:55:18 +08:00
QSynedit::PSynHighlighter pHighlighter=std::make_shared<QSynedit::SynEditCppHighlighter>();
2021-05-26 00:04:20 +08:00
return pHighlighter;
}
2021-06-20 22:54:16 +08:00
2022-09-25 09:55:18 +08:00
QSynedit::PSynHighlighter HighlighterManager::getAsmHighlighter()
2021-07-02 10:32:29 +08:00
{
2022-09-25 09:55:18 +08:00
QSynedit::SynEditASMHighlighter* highlighter = new QSynedit::SynEditASMHighlighter();
QSynedit::PSynHighlighter pHighlighter(highlighter);
2021-07-02 10:32:29 +08:00
highlighter->commentAttribute()->setForeground(0x8C8C8C);
2022-09-25 09:55:18 +08:00
highlighter->commentAttribute()->setStyles(QSynedit::SynFontStyle::fsItalic);
2021-07-02 10:32:29 +08:00
highlighter->identifierAttribute()->setForeground(0x080808);
highlighter->keywordAttribute()->setForeground(0x0033b3);
highlighter->numberAttribute()->setForeground(0x1750EB);
2022-01-04 16:50:54 +08:00
highlighter->whitespaceAttribute()->setForeground(Qt::lightGray);
2021-07-02 10:32:29 +08:00
highlighter->stringAttribute()->setForeground(0x007d17);
highlighter->symbolAttribute()->setForeground(0xc10000);
return pHighlighter;
}
2022-09-25 09:55:18 +08:00
QSynedit::PSynHighlighter HighlighterManager::getGLSLHighlighter()
{
2022-09-25 09:55:18 +08:00
QSynedit::SynEditGLSLHighlighter* highlighter = new QSynedit::SynEditGLSLHighlighter();
QSynedit::PSynHighlighter pHighlighter(highlighter);
highlighter->asmAttribute()->setForeground(Qt::blue);
highlighter->charAttribute()->setForeground(Qt::black);
highlighter->commentAttribute()->setForeground(0x8C8C8C);
2022-09-25 09:55:18 +08:00
highlighter->commentAttribute()->setStyles(QSynedit::SynFontStyle::fsItalic);
highlighter->classAttribute()->setForeground(0x008080);
highlighter->floatAttribute()->setForeground(Qt::darkMagenta);
highlighter->functionAttribute()->setForeground(0x00627A);
highlighter->globalVarAttribute()->setForeground(0x660E7A);
highlighter->hexAttribute()->setForeground(Qt::darkMagenta);
highlighter->identifierAttribute()->setForeground(0x080808);
highlighter->invalidAttribute()->setForeground(Qt::red);
highlighter->localVarAttribute()->setForeground(Qt::black);
highlighter->numberAttribute()->setForeground(0x1750EB);
highlighter->octAttribute()->setForeground(Qt::darkMagenta);
highlighter->preprocessorAttribute()->setForeground(0x1f542e);
highlighter->keywordAttribute()->setForeground(0x0033b3);
highlighter->whitespaceAttribute()->setForeground(Qt::lightGray);
highlighter->stringAttribute()->setForeground(0x007d17);
highlighter->stringEscapeSequenceAttribute()->setForeground(Qt::red);
highlighter->symbolAttribute()->setForeground(0xc10000);
highlighter->variableAttribute()->setForeground(0x400080);
return pHighlighter;
}
2022-09-25 09:55:18 +08:00
void HighlighterManager::applyColorScheme(QSynedit::PSynHighlighter highlighter, const QString &schemeName)
2021-06-20 22:54:16 +08:00
{
if (!highlighter)
return;
2021-07-02 10:32:29 +08:00
if ( (highlighter->getName() == SYN_HIGHLIGHTER_CPP)
|| (highlighter->getName() == SYN_HIGHLIGHTER_ASM)
) {
2021-06-20 22:54:16 +08:00
for (QString name: highlighter->attributes().keys()) {
PColorSchemeItem item = pColorManager->getItem(schemeName,name);
if (item) {
2022-09-25 09:55:18 +08:00
QSynedit::PSynHighlighterAttribute attr = highlighter->attributes()[name];
2021-06-20 22:54:16 +08:00
attr->setBackground(item->background());
attr->setForeground(item->foreground());
2022-09-25 09:55:18 +08:00
QSynedit::SynFontStyles styles = QSynedit::SynFontStyle::fsNone;
styles.setFlag(QSynedit::SynFontStyle::fsBold, item->bold());
styles.setFlag(QSynedit::SynFontStyle::fsItalic, item->italic());
styles.setFlag(QSynedit::SynFontStyle::fsUnderline, item->underlined());
styles.setFlag(QSynedit::SynFontStyle::fsStrikeOut, item->strikeout());
attr->setStyles(styles);
2021-06-20 22:54:16 +08:00
}
}
}
}