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-14 23:56:43 +08:00
|
|
|
#include "base.h"
|
|
|
|
#include "../Constants.h"
|
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
SynHighlighter::SynHighlighter() :
|
2021-05-14 23:56:43 +08:00
|
|
|
mEnabled(true),
|
2021-05-26 00:04:20 +08:00
|
|
|
mWordBreakChars{ SynWordBreakChars }
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const QMap<QString, PSynHighlighterAttribute>& SynHighlighter::attributes() const
|
|
|
|
{
|
|
|
|
return mAttributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QSet<QChar>& SynHighlighter::wordBreakChars() const
|
|
|
|
{
|
|
|
|
return mWordBreakChars;
|
|
|
|
}
|
|
|
|
|
|
|
|
PSynHighlighterAttribute SynHighlighter::commentAttribute() const
|
|
|
|
{
|
|
|
|
return mCommentAttribute;
|
|
|
|
}
|
|
|
|
|
|
|
|
PSynHighlighterAttribute SynHighlighter::identifierAttribute() const
|
|
|
|
{
|
|
|
|
return mIdentifierAttribute;
|
|
|
|
}
|
|
|
|
|
|
|
|
PSynHighlighterAttribute SynHighlighter::keywordAttribute() const
|
|
|
|
{
|
|
|
|
return mKeywordAttribute;
|
|
|
|
}
|
|
|
|
|
|
|
|
PSynHighlighterAttribute SynHighlighter::stringAttribute() const
|
|
|
|
{
|
|
|
|
return mStringAttribute;
|
|
|
|
}
|
|
|
|
|
|
|
|
PSynHighlighterAttribute SynHighlighter::whitespaceAttribute() const
|
|
|
|
{
|
|
|
|
return mWhitespaceAttribute;
|
|
|
|
}
|
|
|
|
|
|
|
|
PSynHighlighterAttribute SynHighlighter::symbolAttribute() const
|
|
|
|
{
|
|
|
|
return mSymbolAttribute;
|
|
|
|
}
|
|
|
|
|
|
|
|
SynHighlighterTokenType SynHighlighter::getTokenType()
|
|
|
|
{
|
|
|
|
return SynHighlighterTokenType::Default;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SynHighlighter::isKeyword(const QString &)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynHighlighter::nextToEol()
|
|
|
|
{
|
|
|
|
while (!eol())
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
2022-01-23 23:27:48 +08:00
|
|
|
QSet<QString> SynHighlighter::keywords() const
|
|
|
|
{
|
|
|
|
return QSet<QString>();
|
|
|
|
}
|
|
|
|
|
2022-06-28 12:22:40 +08:00
|
|
|
QString SynHighlighter::foldString()
|
|
|
|
{
|
|
|
|
return " ... }";
|
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
bool SynHighlighter::isSpaceChar(const QChar &ch)
|
|
|
|
{
|
|
|
|
return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
|
|
|
|
}
|
|
|
|
|
2021-06-22 23:00:34 +08:00
|
|
|
bool SynHighlighter::isWordBreakChar(const QChar &ch)
|
|
|
|
{
|
|
|
|
switch (ch.unicode()) {
|
|
|
|
case '.':
|
|
|
|
case ',':
|
|
|
|
case ';':
|
|
|
|
case ':':
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
|
|
|
case '!':
|
|
|
|
case '?':
|
|
|
|
case '[':
|
|
|
|
case ']':
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
case '{':
|
|
|
|
case '}':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case '^':
|
|
|
|
case '|':
|
|
|
|
case '&':
|
|
|
|
case '-':
|
|
|
|
case '=':
|
|
|
|
case '+':
|
|
|
|
case '*':
|
|
|
|
case '/':
|
|
|
|
case '\\':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
bool SynHighlighter::isIdentChar(const QChar &ch) const
|
|
|
|
{
|
|
|
|
if (ch == '_') {
|
|
|
|
return true;
|
|
|
|
}
|
2021-06-21 22:01:35 +08:00
|
|
|
if ((ch>='0') && (ch <= '9')) {
|
2021-05-14 23:56:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-06-21 22:01:35 +08:00
|
|
|
if ((ch>='a') && (ch <= 'z')) {
|
2021-05-14 23:56:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-06-21 22:01:35 +08:00
|
|
|
if ((ch>='A') && (ch <= 'Z')) {
|
2021-05-14 23:56:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-06-21 22:17:37 +08:00
|
|
|
return false;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynHighlighter::addAttribute(PSynHighlighterAttribute attribute)
|
|
|
|
{
|
|
|
|
mAttributes[attribute->name()]=attribute;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynHighlighter::clearAttributes()
|
|
|
|
{
|
|
|
|
mAttributes.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynHighlighter::attributesCount() const
|
|
|
|
{
|
|
|
|
return mAttributes.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
PSynHighlighterAttribute SynHighlighter::getAttribute(const QString &name) const
|
|
|
|
{
|
|
|
|
auto search = mAttributes.find(name);
|
|
|
|
if (search!=mAttributes.end()) {
|
|
|
|
return search.value();
|
|
|
|
}
|
|
|
|
return PSynHighlighterAttribute();
|
|
|
|
}
|
|
|
|
|
2021-05-18 15:49:58 +08:00
|
|
|
bool SynHighlighter::enabled() const
|
|
|
|
{
|
|
|
|
return mEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynHighlighter::setEnabled(bool value)
|
|
|
|
{
|
|
|
|
if (value != mEnabled) {
|
|
|
|
mEnabled = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
SynFontStyles SynHighlighterAttribute::styles() const
|
|
|
|
{
|
|
|
|
return mStyles;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynHighlighterAttribute::setStyles(const SynFontStyles &styles)
|
|
|
|
{
|
|
|
|
if (mStyles!=styles) {
|
|
|
|
mStyles = styles;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
QColor SynHighlighterAttribute::foreground() const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2021-05-26 00:04:20 +08:00
|
|
|
return mForeground;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
void SynHighlighterAttribute::setForeground(const QColor &color)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2021-05-26 00:04:20 +08:00
|
|
|
mForeground = color;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
QColor SynHighlighterAttribute::background() const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2021-05-26 00:04:20 +08:00
|
|
|
return mBackground;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
void SynHighlighterAttribute::setBackground(const QColor &background)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2021-05-26 00:04:20 +08:00
|
|
|
mBackground = background;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
QString SynHighlighterAttribute::name() const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2021-05-26 00:04:20 +08:00
|
|
|
return mName;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
void SynHighlighterAttribute::setName(const QString &name)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2021-05-26 00:04:20 +08:00
|
|
|
if (mName!=name) {
|
|
|
|
mName = name;
|
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
SynHighlighterAttribute::SynHighlighterAttribute(const QString &name):
|
|
|
|
mForeground(QColor()),
|
|
|
|
mBackground(QColor()),
|
2021-05-26 18:32:17 +08:00
|
|
|
mName(name),
|
|
|
|
mStyles(SynFontStyle::fsNone)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2021-05-26 00:04:20 +08:00
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
2021-09-23 12:06:26 +08:00
|
|
|
|
|
|
|
bool SynRangeState::operator==(const SynRangeState &s2)
|
|
|
|
{
|
2021-10-29 12:25:04 +08:00
|
|
|
// indents contains the information of brace/parenthesis/brackets embedded levels
|
2021-09-23 12:06:26 +08:00
|
|
|
return (state == s2.state)
|
2021-10-28 23:46:54 +08:00
|
|
|
&& (indents == s2.indents)
|
|
|
|
;
|
2021-09-23 12:06:26 +08:00
|
|
|
}
|
2021-10-29 12:25:04 +08:00
|
|
|
|
2021-10-30 18:28:40 +08:00
|
|
|
int SynRangeState::getLastIndent()
|
2021-10-29 12:25:04 +08:00
|
|
|
{
|
|
|
|
if (indents.isEmpty())
|
2021-10-30 18:28:40 +08:00
|
|
|
return -1;
|
2021-10-29 12:25:04 +08:00
|
|
|
return indents.back();
|
|
|
|
}
|
2022-07-04 11:39:06 +08:00
|
|
|
|
|
|
|
SynRangeState::SynRangeState():
|
|
|
|
state(0),
|
|
|
|
braceLevel(0),
|
|
|
|
bracketLevel(0),
|
|
|
|
parenthesisLevel(0),
|
|
|
|
leftBraces(0),
|
|
|
|
rightBraces(0),
|
|
|
|
firstIndentThisLine(0)
|
|
|
|
{
|
|
|
|
}
|