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"
|
2022-12-06 22:51:59 +08:00
|
|
|
|
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)
|
2022-11-16 09:24:42 +08:00
|
|
|
{
|
|
|
|
switch(language) {
|
2022-12-11 19:47:43 +08:00
|
|
|
case QSynedit::ProgrammingLanguage::CPP:
|
|
|
|
return std::make_shared<QSynedit::CppSyntaxer>();
|
|
|
|
case QSynedit::ProgrammingLanguage::Assembly:
|
|
|
|
return std::make_shared<QSynedit::ASMSyntaxer>();
|
2023-02-12 18:13:24 +08:00
|
|
|
case QSynedit::ProgrammingLanguage::ATTAssembly:
|
|
|
|
return std::make_shared<QSynedit::ASMSyntaxer>(true);
|
2022-12-10 20:41:07 +08:00
|
|
|
case QSynedit::ProgrammingLanguage::Makefile:
|
2022-12-11 19:47:43 +08:00
|
|
|
return std::make_shared<QSynedit::MakefileSyntaxer>();
|
2022-12-10 20:41:07 +08:00
|
|
|
case QSynedit::ProgrammingLanguage::GLSL:
|
2022-12-11 19:47:43 +08:00
|
|
|
return std::make_shared<QSynedit::GLSLSyntaxer>();
|
2023-02-16 12:26:35 +08:00
|
|
|
case QSynedit::ProgrammingLanguage::LUA:
|
|
|
|
return std::make_shared<QSynedit::LuaSyntaxer>();
|
2022-11-16 09:24:42 +08:00
|
|
|
default:
|
2022-12-10 21:23:49 +08:00
|
|
|
return QSynedit::PSyntaxer();
|
2022-11-16 09:24:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 21:23:49 +08:00
|
|
|
QSynedit::PSyntaxer SyntaxerManager::getSyntaxer(const QString &filename)
|
2021-05-26 00:04:20 +08:00
|
|
|
{
|
2022-04-07 21:35:09 +08:00
|
|
|
QFileInfo info(filename);
|
|
|
|
QString suffix = info.suffix();
|
2022-12-06 22:51:59 +08:00
|
|
|
QString basename = info.baseName();
|
2022-12-07 09:48:10 +08:00
|
|
|
if (suffix == "c" || suffix == "cpp" || suffix == "cxx"
|
2022-04-07 21:35:09 +08:00
|
|
|
|| suffix == "cc" || suffix == "h" || suffix == "hpp"
|
|
|
|
|| suffix == "hxx" || suffix == "hh" || suffix == "C"
|
|
|
|
|| suffix == "CPP" || suffix =="H" || suffix == "c++"
|
|
|
|
|| suffix == "h++") {
|
2022-12-11 19:47:43 +08:00
|
|
|
return getSyntaxer(QSynedit::ProgrammingLanguage::CPP);
|
2022-04-07 21:35:09 +08:00
|
|
|
} else if (suffix == "vs" || suffix == "fs" || suffix == "frag") {
|
2022-12-11 19:47:43 +08:00
|
|
|
return getSyntaxer(QSynedit::ProgrammingLanguage::GLSL);
|
2023-02-12 18:13:24 +08:00
|
|
|
} else if (suffix == "asm") {
|
2022-12-11 19:47:43 +08:00
|
|
|
return getSyntaxer(QSynedit::ProgrammingLanguage::Assembly);
|
2023-02-12 18:13:24 +08:00
|
|
|
} 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);
|
2022-12-07 09:48:10 +08:00
|
|
|
} else if (basename.compare("makefile", Qt::CaseInsensitive)==0) {
|
2022-12-11 19:47:43 +08:00
|
|
|
return getSyntaxer(QSynedit::ProgrammingLanguage::Makefile);
|
2022-12-07 09:48:10 +08:00
|
|
|
} else if (suffix.isEmpty()) {
|
2022-12-11 19:47:43 +08:00
|
|
|
return getSyntaxer(QSynedit::ProgrammingLanguage::CPP);
|
2022-12-07 09:48:10 +08:00
|
|
|
}
|
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-06 22:51:59 +08:00
|
|
|
|
2022-12-10 21:23:49 +08:00
|
|
|
for (QString name: syntaxer->attributes().keys()) {
|
2022-12-06 22:51:59 +08:00
|
|
|
PColorSchemeItem item = pColorManager->getItem(schemeName,name);
|
|
|
|
if (item) {
|
2022-12-10 21:23:49 +08:00
|
|
|
QSynedit::PTokenAttribute attr = syntaxer->attributes()[name];
|
2022-12-06 22:51:59 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|