RedPanda-CPP/RedPandaIDE/syntaxermanager.cpp

111 lines
4.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/>.
*/
2022-12-10 21:23:49 +08:00
#include "syntaxermanager.h"
2021-05-26 00:04:20 +08:00
#include <QFileInfo>
#include <QObject>
2023-01-11 16:22:26 +08:00
#include "qsynedit/constants.h"
2022-12-10 21:23:49 +08:00
#include "qsynedit/syntaxer/cpp.h"
#include "qsynedit/syntaxer/asm.h"
#include "qsynedit/syntaxer/glsl.h"
2023-02-16 12:26:35 +08:00
#include "qsynedit/syntaxer/lua.h"
2022-12-10 21:23:49 +08:00
#include "qsynedit/syntaxer/makefile.h"
2023-01-11 16:22:26 +08:00
#include "qsynedit/constants.h"
2021-06-20 22:54:16 +08:00
#include "colorscheme.h"
2021-05-26 00:04:20 +08:00
2022-12-10 21:23:49 +08:00
SyntaxerManager syntaxerManager;
2021-05-26 00:04:20 +08:00
2022-12-10 21:23:49 +08:00
SyntaxerManager::SyntaxerManager()
2021-05-26 00:04:20 +08:00
{
}
2022-12-10 21:23:49 +08:00
QSynedit::PSyntaxer SyntaxerManager::getSyntaxer(QSynedit::ProgrammingLanguage language)
{
switch(language) {
case QSynedit::ProgrammingLanguage::CPP:
return std::make_shared<QSynedit::CppSyntaxer>();
case QSynedit::ProgrammingLanguage::Assembly:
return std::make_shared<QSynedit::ASMSyntaxer>();
case QSynedit::ProgrammingLanguage::ATTAssembly:
return std::make_shared<QSynedit::ASMSyntaxer>(true);
2022-12-10 20:41:07 +08:00
case QSynedit::ProgrammingLanguage::Makefile:
return std::make_shared<QSynedit::MakefileSyntaxer>();
2022-12-10 20:41:07 +08:00
case QSynedit::ProgrammingLanguage::GLSL:
return std::make_shared<QSynedit::GLSLSyntaxer>();
2023-02-16 12:26:35 +08:00
case QSynedit::ProgrammingLanguage::LUA:
return std::make_shared<QSynedit::LuaSyntaxer>();
default:
2022-12-10 21:23:49 +08:00
return QSynedit::PSyntaxer();
}
}
2022-12-10 21:23:49 +08:00
QSynedit::PSyntaxer SyntaxerManager::getSyntaxer(const QString &filename)
2021-05-26 00:04:20 +08:00
{
QFileInfo info(filename);
QString suffix = info.suffix();
QString basename = info.baseName();
if (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++") {
return getSyntaxer(QSynedit::ProgrammingLanguage::CPP);
} else if (suffix == "vs" || suffix == "fs" || suffix == "frag") {
return getSyntaxer(QSynedit::ProgrammingLanguage::GLSL);
} else if (suffix == "asm") {
return getSyntaxer(QSynedit::ProgrammingLanguage::Assembly);
} else if (suffix == "s" || suffix == "S") {
return getSyntaxer(QSynedit::ProgrammingLanguage::ATTAssembly);
2023-02-16 12:26:35 +08:00
} else if (suffix == "lua") {
return getSyntaxer(QSynedit::ProgrammingLanguage::LUA);
} else if (basename.compare("makefile", Qt::CaseInsensitive)==0) {
return getSyntaxer(QSynedit::ProgrammingLanguage::Makefile);
} else if (suffix.isEmpty()) {
return getSyntaxer(QSynedit::ProgrammingLanguage::CPP);
}
2022-12-10 21:23:49 +08:00
return QSynedit::PSyntaxer();
2021-05-26 00:04:20 +08:00
}
2022-12-10 21:23:49 +08:00
QSynedit::PSyntaxer SyntaxerManager::copy(QSynedit::PSyntaxer syntaxer)
2021-06-20 22:54:16 +08:00
{
2022-12-10 21:23:49 +08:00
if (!syntaxer)
return QSynedit::PSyntaxer();
return getSyntaxer(syntaxer->language());
2021-06-20 22:54:16 +08:00
}
2022-12-10 21:23:49 +08:00
void SyntaxerManager::applyColorScheme(QSynedit::PSyntaxer syntaxer, const QString &schemeName)
2021-06-20 22:54:16 +08:00
{
2022-12-10 21:23:49 +08:00
if (!syntaxer)
2021-06-20 22:54:16 +08:00
return;
2022-12-10 21:23:49 +08:00
for (QString name: syntaxer->attributes().keys()) {
PColorSchemeItem item = pColorManager->getItem(schemeName,name);
if (item) {
2022-12-10 21:23:49 +08:00
QSynedit::PTokenAttribute attr = syntaxer->attributes()[name];
attr->setBackground(item->background());
attr->setForeground(item->foreground());
QSynedit::FontStyles styles = QSynedit::FontStyle::fsNone;
styles.setFlag(QSynedit::FontStyle::fsBold, item->bold());
styles.setFlag(QSynedit::FontStyle::fsItalic, item->italic());
styles.setFlag(QSynedit::FontStyle::fsUnderline, item->underlined());
styles.setFlag(QSynedit::FontStyle::fsStrikeOut, item->strikeout());
attr->setStyles(styles);
2021-06-20 22:54:16 +08:00
}
}
}