New CBot syntax highlighting

master
krzys-h 2015-08-18 16:00:44 +02:00
parent 371621ced4
commit c5da68d516
4 changed files with 79 additions and 57 deletions

View File

@ -653,16 +653,48 @@ void CText::DrawString(const std::string &text, std::vector<FontMetaChar>::itera
break; break;
} }
Color c = color;
FontHighlight hl = static_cast<FontHighlight>(format[fmtIndex] & FONT_MASK_HIGHLIGHT); FontHighlight hl = static_cast<FontHighlight>(format[fmtIndex] & FONT_MASK_HIGHLIGHT);
if (hl != FONT_HIGHLIGHT_NONE) if (hl != FONT_HIGHLIGHT_NONE)
{ {
Math::Point charSize; if (hl == FONT_HIGHLIGHT_TOKEN)
charSize.x = GetCharWidth(ch, font, size, offset); {
charSize.y = GetHeight(font, size); c = Color(0.612f, 0.475f, 0.208f, 1.0f); // #9C7935
DrawHighlight(hl, pos, charSize); }
else if (hl == FONT_HIGHLIGHT_TYPE)
{
c = Color(0.365f, 0.498f, 0.251f, 1.0f); // #5D7F40
}
else if (hl == FONT_HIGHLIGHT_CONST)
{
c = Color(0.882f, 0.176f, 0.176f, 1.0f); // #E12D2D
}
else if (hl == FONT_HIGHLIGHT_THIS)
{
c = Color(0.545f, 0.329f, 0.608f, 1.0f); // #8B549B
}
else if (hl == FONT_HIGHLIGHT_COMMENT)
{
c = Color(0.251f, 0.271f, 0.306f, 1.0f); // #40454E
}
else if (hl == FONT_HIGHLIGHT_KEYWORD)
{
c = Color(0.267f, 0.478f, 0.655f, 1.0f); // #447AA7
}
else if (hl == FONT_HIGHLIGHT_STRING)
{
c = Color(0.267f, 0.427f, 0.380f, 1.0f); // #446D61
}
else
{
Math::Point charSize;
charSize.x = GetCharWidth(ch, font, size, offset);
charSize.y = GetHeight(font, size);
DrawHighlight(hl, pos, charSize);
}
} }
DrawCharAndAdjustPos(ch, font, size, pos, color); DrawCharAndAdjustPos(ch, font, size, pos, c);
// increment fmtIndex for each byte in multibyte character // increment fmtIndex for each byte in multibyte character
if ( ch.c1 != 0 ) if ( ch.c1 != 0 )
@ -770,26 +802,6 @@ void CText::DrawHighlight(FontHighlight hl, Math::Point pos, Math::Point size)
grad[0] = grad[1] = grad[2] = grad[3] = Color(0.0f, 0.0f, 1.0f, 0.5f); grad[0] = grad[1] = grad[2] = grad[3] = Color(0.0f, 0.0f, 1.0f, 0.5f);
break; break;
case FONT_HIGHLIGHT_TOKEN:
grad[0] = grad[1] = Color(248.0f / 256.0f, 248.0f / 256.0f, 248.0f / 256.0f, 0.5f);
grad[2] = grad[3] = Color(248.0f / 256.0f, 220.0f / 256.0f, 188.0f / 256.0f, 0.5f);
break;
case FONT_HIGHLIGHT_TYPE:
grad[0] = grad[1] = Color(248.0f / 256.0f, 248.0f / 256.0f, 248.0f / 256.0f, 0.5f);
grad[2] = grad[3] = Color(169.0f / 256.0f, 234.0f / 256.0f, 169.0f / 256.0f, 0.5f);
break;
case FONT_HIGHLIGHT_CONST:
grad[0] = grad[1] = Color(248.0f / 256.0f, 248.0f / 256.0f, 248.0f / 256.0f, 0.5f);
grad[2] = grad[3] = Color(248.0f / 256.0f, 176.0f / 256.0f, 169.0f / 256.0f, 0.5f);
break;
case FONT_HIGHLIGHT_REM:
grad[0] = grad[1] = Color(248.0f / 256.0f, 248.0f / 256.0f, 248.0f / 256.0f, 0.5f);
grad[2] = grad[3] = Color(248.0f / 256.0f, 169.0f / 256.0f, 248.0f / 256.0f, 0.5f);
break;
case FONT_HIGHLIGHT_KEY: case FONT_HIGHLIGHT_KEY:
grad[0] = grad[1] = grad[2] = grad[3] = grad[0] = grad[1] = grad[2] = grad[3] =
Color(192.0f / 256.0f, 192.0f / 256.0f, 192.0f / 256.0f, 0.5f); Color(192.0f / 256.0f, 192.0f / 256.0f, 192.0f / 256.0f, 0.5f);

View File

@ -112,18 +112,21 @@ enum FontTitle
* \enum FontHighlight * \enum FontHighlight
* \brief Type of color highlight for text * \brief Type of color highlight for text
* *
* Bitmask in 3 bits left shifted 6 (mask 0x1c0) * Bitmask in 4 bits left shifted 6 (mask 0x3c0)
*/ */
enum FontHighlight enum FontHighlight
{ {
FONT_HIGHLIGHT_NONE = 0x00 << 6, FONT_HIGHLIGHT_NONE = 0x00 << 6,
FONT_HIGHLIGHT_LINK = 0x01 << 6, FONT_HIGHLIGHT_LINK = 0x01 << 6, //!< link underline
FONT_HIGHLIGHT_TOKEN = 0x02 << 6, FONT_HIGHLIGHT_TABLE = 0x02 << 6, //!< code background in SatCom
FONT_HIGHLIGHT_TYPE = 0x03 << 6, FONT_HIGHLIGHT_KEY = 0x03 << 6, //!< background for keys in documentation in SatCom
FONT_HIGHLIGHT_CONST = 0x04 << 6, FONT_HIGHLIGHT_TOKEN = 0x04 << 6, //!< keywords in CBot scripts
FONT_HIGHLIGHT_REM = 0x05 << 6, FONT_HIGHLIGHT_TYPE = 0x05 << 6, //!< types in CBot scripts
FONT_HIGHLIGHT_KEY = 0x06 << 6, FONT_HIGHLIGHT_CONST = 0x06 << 6, //!< constants in CBot scripts
FONT_HIGHLIGHT_TABLE = 0x07 << 6, FONT_HIGHLIGHT_THIS = 0x07 << 6, //!< "this" keyword in CBot scripts
FONT_HIGHLIGHT_COMMENT = 0x08 << 6, //!< comments in CBot scripts
FONT_HIGHLIGHT_KEYWORD = 0x09 << 6, //!< builtin keywords in CBot scripts
FONT_HIGHLIGHT_STRING = 0x0A << 6, //!< string literals in CBot scripts
}; };
/** /**
@ -137,9 +140,9 @@ enum FontMask
//! Mask for FontTitle //! Mask for FontTitle
FONT_MASK_TITLE = 0x030, FONT_MASK_TITLE = 0x030,
//! Mask for FontHighlight //! Mask for FontHighlight
FONT_MASK_HIGHLIGHT = 0x1c0, FONT_MASK_HIGHLIGHT = 0x3c0,
//! Mask for image bit (TODO: not used?) //! Mask for image bit (TODO: not used?)
FONT_MASK_IMAGE = 0x200 FONT_MASK_IMAGE = 0x400
}; };

View File

@ -654,6 +654,7 @@ void CScript::ColorizeScript(Ui::CEdit* edit)
Gfx::FontHighlight color; Gfx::FontHighlight color;
edit->ClearFormat(); edit->ClearFormat();
edit->SetFormat(0, edit->GetMaxChar(), Gfx::FONT_HIGHLIGHT_COMMENT); // anything not processed is a comment
bt = CBotToken::CompileTokens(edit->GetText(), error); bt = CBotToken::CompileTokens(edit->GetText(), error);
while ( bt != nullptr ) while ( bt != nullptr )
@ -664,39 +665,45 @@ void CScript::ColorizeScript(Ui::CEdit* edit)
cursor1 = bt->GetStart(); cursor1 = bt->GetStart();
cursor2 = bt->GetEnd(); cursor2 = bt->GetEnd();
if (cursor1 < 0 || cursor2 < 0 || cursor1 == cursor2 || type == 0) { bt = bt->GetNext(); continue; } // seems to be a bug in CBot engine (how does it even still work? D:)
color = Gfx::FONT_HIGHLIGHT_NONE; color = Gfx::FONT_HIGHLIGHT_NONE;
if ( type >= TokenKeyWord && type < TokenKeyWord+100 ) if ((type == TokenTypVar || (type >= TokenKeyWord && type < TokenKeyWord+100)) && IsType(token)) // types (basic types are TokenKeyWord, classes are TokenTypVar)
{
color = Gfx::FONT_HIGHLIGHT_TOKEN;
}
if ( type >= TokenKeyDeclare && type < TokenKeyDeclare+100 )
{ {
color = Gfx::FONT_HIGHLIGHT_TYPE; color = Gfx::FONT_HIGHLIGHT_TYPE;
} }
if ( type >= TokenKeyVal && type < TokenKeyVal+100 ) else if (type == TokenTypVar && IsFunction(token)) // functions
{
color = Gfx::FONT_HIGHLIGHT_TOKEN;
}
else if (type == TokenTypVar && strcmp(token, "this") == 0) // this
{
color = Gfx::FONT_HIGHLIGHT_THIS;
}
else if (type >= TokenKeyWord && type < TokenKeyWord+100) // builtin keywords
{
color = Gfx::FONT_HIGHLIGHT_KEYWORD;
}
else if (type >= TokenKeyDeclare && type < TokenKeyDeclare+100) // TODO: no idea :P seems to never happen
{
color = Gfx::FONT_HIGHLIGHT_TYPE;
}
else if (type >= TokenKeyVal && type < TokenKeyVal+100) // true, false, null, nan
{ {
color = Gfx::FONT_HIGHLIGHT_CONST; color = Gfx::FONT_HIGHLIGHT_CONST;
} }
if ( type == TokenTypVar || ( type >= TokenKeyWord && type < TokenKeyWord+100 ) || strcmp(token, "this") == 0 ) else if (type == TokenTypDef) // constants (object types etc.)
{ {
if ( IsType(token) ) color = Gfx::FONT_HIGHLIGHT_CONST;
{
color = Gfx::FONT_HIGHLIGHT_TYPE;
}
else if ( IsFunction(token) || strcmp(token, "this") == 0 )
{
color = Gfx::FONT_HIGHLIGHT_TOKEN;
}
} }
if ( type == TokenTypDef ) else if (type == TokenTypString || type == TokenTypNum) // string literals and numbers
{ {
color =Gfx::FONT_HIGHLIGHT_CONST; color = Gfx::FONT_HIGHLIGHT_STRING;
} }
if ( cursor1 < cursor2 && color != Gfx::FONT_HIGHLIGHT_NONE ) assert(cursor1 < cursor2);
{ edit->SetFormat(cursor1, cursor2, color);
edit->SetFormat(cursor1, cursor2, color);
}
bt = bt->GetNext(); bt = bt->GetNext();
} }

View File

@ -3158,7 +3158,7 @@ bool CEdit::SetFormat(int cursor1, int cursor2, int format)
for ( i=cursor1 ; i<cursor2 ; i++ ) for ( i=cursor1 ; i<cursor2 ; i++ )
{ {
m_format.at(i) |= format; m_format.at(i) = format;
} }
return true; return true;