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-11-23 12:51:23 +08:00
|
|
|
#include "qsynedit/Constants.h"
|
2022-09-26 11:18:43 +08:00
|
|
|
#include "qsynedit/highlighter/cpp.h"
|
|
|
|
#include "qsynedit/highlighter/asm.h"
|
|
|
|
#include "qsynedit/highlighter/glsl.h"
|
2022-12-06 22:51:59 +08:00
|
|
|
#include "qsynedit/highlighter/makefilehighlighter.h"
|
|
|
|
|
2022-09-26 11:18:43 +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
|
|
|
|
|
|
|
HighlighterManager highlighterManager;
|
|
|
|
|
|
|
|
HighlighterManager::HighlighterManager()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-12-10 20:41:07 +08:00
|
|
|
QSynedit::PHighlighter HighlighterManager::getHighlighter(QSynedit::ProgrammingLanguage language)
|
2022-11-16 09:24:42 +08:00
|
|
|
{
|
|
|
|
switch(language) {
|
2022-12-10 20:41:07 +08:00
|
|
|
case QSynedit::ProgrammingLanguage::Cpp:
|
2022-11-16 09:24:42 +08:00
|
|
|
return getCppHighlighter();
|
2022-12-10 20:41:07 +08:00
|
|
|
case QSynedit::ProgrammingLanguage::Asssembly:
|
2022-11-16 09:24:42 +08:00
|
|
|
return getAsmHighlighter();
|
2022-12-10 20:41:07 +08:00
|
|
|
case QSynedit::ProgrammingLanguage::Makefile:
|
2022-12-06 22:51:59 +08:00
|
|
|
return getMakefileHighlighter();
|
2022-12-10 20:41:07 +08:00
|
|
|
case QSynedit::ProgrammingLanguage::GLSL:
|
2022-11-16 09:24:42 +08:00
|
|
|
return getGLSLHighlighter();
|
|
|
|
default:
|
|
|
|
return QSynedit::PHighlighter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:01:38 +08:00
|
|
|
QSynedit::PHighlighter HighlighterManager::getHighlighter(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++") {
|
2021-06-07 11:02:03 +08:00
|
|
|
return getCppHighlighter();
|
2022-04-07 21:35:09 +08:00
|
|
|
} else if (suffix == "vs" || suffix == "fs" || suffix == "frag") {
|
|
|
|
return getGLSLHighlighter();
|
2022-11-16 09:24:42 +08:00
|
|
|
} else if (suffix == "s" || suffix == "asm") {
|
|
|
|
return getAsmHighlighter();
|
2022-12-07 09:48:10 +08:00
|
|
|
} else if (basename.compare("makefile", Qt::CaseInsensitive)==0) {
|
2022-12-06 22:51:59 +08:00
|
|
|
return getMakefileHighlighter();
|
2022-12-07 09:48:10 +08:00
|
|
|
} else if (suffix.isEmpty()) {
|
|
|
|
return getCppHighlighter();
|
|
|
|
}
|
2022-09-27 14:01:38 +08:00
|
|
|
return QSynedit::PHighlighter();
|
2021-05-26 00:04:20 +08:00
|
|
|
}
|
|
|
|
|
2022-09-27 14:01:38 +08:00
|
|
|
QSynedit::PHighlighter HighlighterManager::copyHighlighter(QSynedit::PHighlighter highlighter)
|
2021-06-20 22:54:16 +08:00
|
|
|
{
|
|
|
|
if (!highlighter)
|
2022-09-27 14:01:38 +08:00
|
|
|
return QSynedit::PHighlighter();
|
2022-12-06 22:51:59 +08:00
|
|
|
return getHighlighter(highlighter->language());
|
2021-06-20 22:54:16 +08:00
|
|
|
}
|
|
|
|
|
2022-09-27 14:01:38 +08:00
|
|
|
QSynedit::PHighlighter HighlighterManager::getCppHighlighter()
|
2021-05-26 00:04:20 +08:00
|
|
|
{
|
2022-10-10 18:05:18 +08:00
|
|
|
std::shared_ptr<QSynedit::CppHighlighter> highlighter = std::make_shared<QSynedit::CppHighlighter>();
|
|
|
|
return highlighter;
|
2021-05-26 00:04:20 +08:00
|
|
|
}
|
2021-06-20 22:54:16 +08:00
|
|
|
|
2022-09-27 14:01:38 +08:00
|
|
|
QSynedit::PHighlighter HighlighterManager::getAsmHighlighter()
|
2021-07-02 10:32:29 +08:00
|
|
|
{
|
2022-10-10 18:05:18 +08:00
|
|
|
std::shared_ptr<QSynedit::ASMHighlighter> highlighter=std::make_shared<QSynedit::ASMHighlighter>();
|
|
|
|
return highlighter;
|
2021-07-02 10:32:29 +08:00
|
|
|
}
|
|
|
|
|
2022-09-27 14:01:38 +08:00
|
|
|
QSynedit::PHighlighter HighlighterManager::getGLSLHighlighter()
|
2022-01-23 23:27:48 +08:00
|
|
|
{
|
2022-10-10 18:05:18 +08:00
|
|
|
std::shared_ptr<QSynedit::GLSLHighlighter> highlighter=std::make_shared<QSynedit::GLSLHighlighter>();
|
|
|
|
return highlighter;
|
2022-01-23 23:27:48 +08:00
|
|
|
}
|
|
|
|
|
2022-12-06 22:51:59 +08:00
|
|
|
QSynedit::PHighlighter HighlighterManager::getMakefileHighlighter()
|
|
|
|
{
|
|
|
|
std::shared_ptr<QSynedit::MakefileHighlighter> highlighter=std::make_shared<QSynedit::MakefileHighlighter>();
|
|
|
|
return highlighter;
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:01:38 +08:00
|
|
|
void HighlighterManager::applyColorScheme(QSynedit::PHighlighter highlighter, const QString &schemeName)
|
2021-06-20 22:54:16 +08:00
|
|
|
{
|
|
|
|
if (!highlighter)
|
|
|
|
return;
|
2022-12-06 22:51:59 +08:00
|
|
|
|
|
|
|
for (QString name: highlighter->attributes().keys()) {
|
|
|
|
PColorSchemeItem item = pColorManager->getItem(schemeName,name);
|
|
|
|
if (item) {
|
|
|
|
QSynedit::PHighlighterAttribute attr = highlighter->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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|