RedPanda-CPP/RedPandaIDE/syntaxermanager.cpp

144 lines
5.4 KiB
C++
Raw Permalink 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>
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"
2024-03-04 16:01:23 +08:00
#include "qsynedit/syntaxer/textfile.h"
#include "qsynedit/formatter/cppformatter.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
{
}
QSynedit::PSyntaxer SyntaxerManager::getSyntaxer(QSynedit::ProgrammingLanguage language) const
{
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);
case QSynedit::ProgrammingLanguage::MixedAssembly:
return std::make_shared<QSynedit::ASMSyntaxer>(false, true);
case QSynedit::ProgrammingLanguage::MixedATTAssembly:
return std::make_shared<QSynedit::ASMSyntaxer>(true, 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>();
case QSynedit::ProgrammingLanguage::XMAKE: {
auto syntaxer=getSyntaxer(QSynedit::ProgrammingLanguage::LUA);
QSynedit::LuaSyntaxer* pSyntaxer= (QSynedit::LuaSyntaxer*)syntaxer.get();
pSyntaxer->setUseXMakeLibs(true);
return syntaxer;
}
default:
2024-03-04 16:01:23 +08:00
return std::make_shared<QSynedit::TextSyntaxer>();
}
}
QSynedit::PSyntaxer SyntaxerManager::getSyntaxer(const QString &filename) const
{
return getSyntaxer(getLanguage(filename));
}
QSynedit::PFormatter SyntaxerManager::getFormatter(QSynedit::ProgrammingLanguage language) const
{
switch(language) {
case QSynedit::ProgrammingLanguage::CPP:
return std::make_shared<QSynedit::CppFormatter>();
default:
return QSynedit::PFormatter();
}
}
QSynedit::PFormatter SyntaxerManager::getFormatter(const QString &filename) const
{
return getFormatter(getLanguage(filename));
}
QSynedit::ProgrammingLanguage SyntaxerManager::getLanguage(const QString &filename) const
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 QSynedit::ProgrammingLanguage::CPP;
} else if (suffix == "vs" || suffix == "fs" || suffix == "frag") {
return QSynedit::ProgrammingLanguage::GLSL;
} else if (suffix == "asm") {
return QSynedit::ProgrammingLanguage::Assembly;
} else if (suffix == "s" || suffix == "S") {
return QSynedit::ProgrammingLanguage::ATTAssembly;
2023-02-16 12:26:35 +08:00
} else if (suffix == "lua") {
if (basename=="xmake") {
return QSynedit::ProgrammingLanguage::XMAKE;
} else
return QSynedit::ProgrammingLanguage::LUA;
} else if (basename.compare("makefile", Qt::CaseInsensitive)==0) {
return QSynedit::ProgrammingLanguage::Makefile;
} else if (suffix.isEmpty()) {
return QSynedit::ProgrammingLanguage::CPP;
}
2024-03-04 16:01:23 +08:00
return QSynedit::ProgrammingLanguage::Textfile;
2021-05-26 00:04:20 +08:00
}
QSynedit::PSyntaxer SyntaxerManager::copy(QSynedit::PSyntaxer syntaxer) const
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
}
void SyntaxerManager::applyColorScheme(QSynedit::PSyntaxer syntaxer, const QString &schemeName) const
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
}
}
}