Adding ColorTextEdit.

[ColorTextEdit](https://github.com/santaclose/ImGuiColorTextEdit) commit `cd9090d`. This fork is still using C++11, but usage of `boost::regex` is removed.

Let's try Zep next, but this already looks like it could do the job!
This commit is contained in:
Artyom Shalkhakov 2025-02-11 08:43:23 -07:00
parent 0ef7697025
commit 4f592a8675
6 changed files with 4382 additions and 20 deletions

View file

@ -826,6 +826,10 @@ set(src_imgui ${src_imgui}
libs/imgui/imgui_widgets.cpp
libs/imgui/imgui_demo.cpp
libs/ImGuiColorTextEdit/TextEditor.h
libs/ImGuiColorTextEdit/LanguageDefinitions.cpp
libs/ImGuiColorTextEdit/TextEditor.cpp
sys/sys_imgui.h
sys/sys_imgui.cpp
@ -1295,6 +1299,7 @@ if(CORE)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} PREFIX neo FILES ${src_core} ${src_sys_base} ${src_sys_core} ${src_imgui} ${src_editor_tools})
target_include_directories(${DHEWM3BINARY} PRIVATE "${CMAKE_SOURCE_DIR}/libs/imgui")
target_include_directories(${DHEWM3BINARY} PRIVATE "${CMAKE_SOURCE_DIR}/libs/zep/include")
if(HARDLINK_GAME)
set_target_properties(${DHEWM3BINARY} PROPERTIES COMPILE_DEFINITIONS "${TOOLS_DEFINES}")

View file

@ -0,0 +1,937 @@
#include "TextEditor.h"
static bool TokenizeCStyleString(const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end)
{
const char* p = in_begin;
if (*p == '"')
{
p++;
while (p < in_end)
{
// handle end of string
if (*p == '"')
{
out_begin = in_begin;
out_end = p + 1;
return true;
}
// handle escape character for "
if (*p == '\\' && p + 1 < in_end && p[1] == '"')
p++;
p++;
}
}
return false;
}
static bool TokenizeCStyleCharacterLiteral(const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end)
{
const char* p = in_begin;
if (*p == '\'')
{
p++;
// handle escape characters
if (p < in_end && *p == '\\')
p++;
if (p < in_end)
p++;
// handle end of character literal
if (p < in_end && *p == '\'')
{
out_begin = in_begin;
out_end = p + 1;
return true;
}
}
return false;
}
static bool TokenizeCStyleIdentifier(const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end)
{
const char* p = in_begin;
if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || *p == '_')
{
p++;
while ((p < in_end) && ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == '_'))
p++;
out_begin = in_begin;
out_end = p;
return true;
}
return false;
}
static bool TokenizeCStyleNumber(const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end)
{
const char* p = in_begin;
const bool startsWithNumber = *p >= '0' && *p <= '9';
if (*p != '+' && *p != '-' && !startsWithNumber)
return false;
p++;
bool hasNumber = startsWithNumber;
while (p < in_end && (*p >= '0' && *p <= '9'))
{
hasNumber = true;
p++;
}
if (hasNumber == false)
return false;
bool isFloat = false;
bool isHex = false;
bool isBinary = false;
if (p < in_end)
{
if (*p == '.')
{
isFloat = true;
p++;
while (p < in_end && (*p >= '0' && *p <= '9'))
p++;
}
else if (*p == 'x' || *p == 'X')
{
// hex formatted integer of the type 0xef80
isHex = true;
p++;
while (p < in_end && ((*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'f') || (*p >= 'A' && *p <= 'F')))
p++;
}
else if (*p == 'b' || *p == 'B')
{
// binary formatted integer of the type 0b01011101
isBinary = true;
p++;
while (p < in_end && (*p >= '0' && *p <= '1'))
p++;
}
}
if (isHex == false && isBinary == false)
{
// floating point exponent
if (p < in_end && (*p == 'e' || *p == 'E'))
{
isFloat = true;
p++;
if (p < in_end && (*p == '+' || *p == '-'))
p++;
bool hasDigits = false;
while (p < in_end && (*p >= '0' && *p <= '9'))
{
hasDigits = true;
p++;
}
if (hasDigits == false)
return false;
}
// single precision floating point type
if (p < in_end && *p == 'f')
p++;
}
if (isFloat == false)
{
// integer size type
while (p < in_end && (*p == 'u' || *p == 'U' || *p == 'l' || *p == 'L'))
p++;
}
out_begin = in_begin;
out_end = p;
return true;
}
static bool TokenizeCStylePunctuation(const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end)
{
(void)in_end;
switch (*in_begin)
{
case '[':
case ']':
case '{':
case '}':
case '!':
case '%':
case '^':
case '&':
case '*':
case '(':
case ')':
case '-':
case '+':
case '=':
case '~':
case '|':
case '<':
case '>':
case '?':
case ':':
case '/':
case ';':
case ',':
case '.':
out_begin = in_begin;
out_end = in_begin + 1;
return true;
}
return false;
}
static bool TokenizeLuaStyleString(const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end)
{
const char* p = in_begin;
bool is_single_quote = false;
bool is_double_quotes = false;
bool is_double_square_brackets = false;
switch (*p)
{
case '\'':
is_single_quote = true;
break;
case '"':
is_double_quotes = true;
break;
case '[':
p++;
if (p < in_end && *(p) == '[')
is_double_square_brackets = true;
break;
}
if (is_single_quote || is_double_quotes || is_double_square_brackets)
{
p++;
while (p < in_end)
{
// handle end of string
if ((is_single_quote && *p == '\'') || (is_double_quotes && *p == '"') || (is_double_square_brackets && *p == ']' && p + 1 < in_end && *(p + 1) == ']'))
{
out_begin = in_begin;
if (is_double_square_brackets)
out_end = p + 2;
else
out_end = p + 1;
return true;
}
// handle escape character for "
if (*p == '\\' && p + 1 < in_end && (is_single_quote || is_double_quotes))
p++;
p++;
}
}
return false;
}
static bool TokenizeLuaStyleIdentifier(const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end)
{
const char* p = in_begin;
if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || *p == '_')
{
p++;
while ((p < in_end) && ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == '_'))
p++;
out_begin = in_begin;
out_end = p;
return true;
}
return false;
}
static bool TokenizeLuaStyleNumber(const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end)
{
const char* p = in_begin;
const bool startsWithNumber = *p >= '0' && *p <= '9';
if (*p != '+' && *p != '-' && !startsWithNumber)
return false;
p++;
bool hasNumber = startsWithNumber;
while (p < in_end && (*p >= '0' && *p <= '9'))
{
hasNumber = true;
p++;
}
if (hasNumber == false)
return false;
if (p < in_end)
{
if (*p == '.')
{
p++;
while (p < in_end && (*p >= '0' && *p <= '9'))
p++;
}
// floating point exponent
if (p < in_end && (*p == 'e' || *p == 'E'))
{
p++;
if (p < in_end && (*p == '+' || *p == '-'))
p++;
bool hasDigits = false;
while (p < in_end && (*p >= '0' && *p <= '9'))
{
hasDigits = true;
p++;
}
if (hasDigits == false)
return false;
}
}
out_begin = in_begin;
out_end = p;
return true;
}
static bool TokenizeLuaStylePunctuation(const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end)
{
(void)in_end;
switch (*in_begin)
{
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 '.':
out_begin = in_begin;
out_end = in_begin + 1;
return true;
}
return false;
}
const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::Cpp()
{
static bool inited = false;
static LanguageDefinition langDef;
if (!inited)
{
static const char* const cppKeywords[] = {
"alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", "atomic_noexcept", "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "class",
"compl", "concept", "const", "constexpr", "const_cast", "continue", "decltype", "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", "float",
"for", "friend", "goto", "if", "import", "inline", "int", "long", "module", "mutable", "namespace", "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", "public",
"register", "reinterpret_cast", "requires", "return", "short", "signed", "sizeof", "static", "static_assert", "static_cast", "struct", "switch", "synchronized", "template", "this", "thread_local",
"throw", "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq"
};
for (auto& k : cppKeywords)
langDef.mKeywords.insert(k);
static const char* const identifiers[] = {
"abort", "abs", "acos", "asin", "atan", "atexit", "atof", "atoi", "atol", "ceil", "clock", "cosh", "ctime", "div", "exit", "fabs", "floor", "fmod", "getchar", "getenv", "isalnum", "isalpha", "isdigit", "isgraph",
"ispunct", "isspace", "isupper", "kbhit", "log10", "log2", "log", "memcmp", "modf", "pow", "printf", "sprintf", "snprintf", "putchar", "putenv", "puts", "rand", "remove", "rename", "sinh", "sqrt", "srand", "strcat", "strcmp", "strerror", "time", "tolower", "toupper",
"std", "string", "vector", "map", "unordered_map", "set", "unordered_set", "min", "max"
};
for (auto& k : identifiers)
{
Identifier id;
id.mDeclaration = "Built-in function";
langDef.mIdentifiers.insert(std::make_pair(std::string(k), id));
}
langDef.mTokenize = [](const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end, PaletteIndex& paletteIndex) -> bool
{
paletteIndex = PaletteIndex::Max;
while (in_begin < in_end && isascii(*in_begin) && isblank(*in_begin))
in_begin++;
if (in_begin == in_end)
{
out_begin = in_end;
out_end = in_end;
paletteIndex = PaletteIndex::Default;
}
else if (TokenizeCStyleString(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::String;
else if (TokenizeCStyleCharacterLiteral(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::CharLiteral;
else if (TokenizeCStyleIdentifier(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::Identifier;
else if (TokenizeCStyleNumber(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::Number;
else if (TokenizeCStylePunctuation(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::Punctuation;
return paletteIndex != PaletteIndex::Max;
};
langDef.mCommentStart = "/*";
langDef.mCommentEnd = "*/";
langDef.mSingleLineComment = "//";
langDef.mCaseSensitive = true;
langDef.mName = "C++";
inited = true;
}
return langDef;
}
const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::Hlsl()
{
static bool inited = false;
static LanguageDefinition langDef;
if (!inited)
{
static const char* const keywords[] = {
"AppendStructuredBuffer", "asm", "asm_fragment", "BlendState", "bool", "break", "Buffer", "ByteAddressBuffer", "case", "cbuffer", "centroid", "class", "column_major", "compile", "compile_fragment",
"CompileShader", "const", "continue", "ComputeShader", "ConsumeStructuredBuffer", "default", "DepthStencilState", "DepthStencilView", "discard", "do", "double", "DomainShader", "dword", "else",
"export", "extern", "false", "float", "for", "fxgroup", "GeometryShader", "groupshared", "half", "Hullshader", "if", "in", "inline", "inout", "InputPatch", "int", "interface", "line", "lineadj",
"linear", "LineStream", "matrix", "min16float", "min10float", "min16int", "min12int", "min16uint", "namespace", "nointerpolation", "noperspective", "NULL", "out", "OutputPatch", "packoffset",
"pass", "pixelfragment", "PixelShader", "point", "PointStream", "precise", "RasterizerState", "RenderTargetView", "return", "register", "row_major", "RWBuffer", "RWByteAddressBuffer", "RWStructuredBuffer",
"RWTexture1D", "RWTexture1DArray", "RWTexture2D", "RWTexture2DArray", "RWTexture3D", "sample", "sampler", "SamplerState", "SamplerComparisonState", "shared", "snorm", "stateblock", "stateblock_state",
"static", "string", "struct", "switch", "StructuredBuffer", "tbuffer", "technique", "technique10", "technique11", "texture", "Texture1D", "Texture1DArray", "Texture2D", "Texture2DArray", "Texture2DMS",
"Texture2DMSArray", "Texture3D", "TextureCube", "TextureCubeArray", "true", "typedef", "triangle", "triangleadj", "TriangleStream", "uint", "uniform", "unorm", "unsigned", "vector", "vertexfragment",
"VertexShader", "void", "volatile", "while",
"bool1","bool2","bool3","bool4","double1","double2","double3","double4", "float1", "float2", "float3", "float4", "int1", "int2", "int3", "int4", "in", "out", "inout",
"uint1", "uint2", "uint3", "uint4", "dword1", "dword2", "dword3", "dword4", "half1", "half2", "half3", "half4",
"float1x1","float2x1","float3x1","float4x1","float1x2","float2x2","float3x2","float4x2",
"float1x3","float2x3","float3x3","float4x3","float1x4","float2x4","float3x4","float4x4",
"half1x1","half2x1","half3x1","half4x1","half1x2","half2x2","half3x2","half4x2",
"half1x3","half2x3","half3x3","half4x3","half1x4","half2x4","half3x4","half4x4",
};
for (auto& k : keywords)
langDef.mKeywords.insert(k);
static const char* const identifiers[] = {
"abort", "abs", "acos", "all", "AllMemoryBarrier", "AllMemoryBarrierWithGroupSync", "any", "asdouble", "asfloat", "asin", "asint", "asint", "asuint",
"asuint", "atan", "atan2", "ceil", "CheckAccessFullyMapped", "clamp", "clip", "cos", "cosh", "countbits", "cross", "D3DCOLORtoUBYTE4", "ddx",
"ddx_coarse", "ddx_fine", "ddy", "ddy_coarse", "ddy_fine", "degrees", "determinant", "DeviceMemoryBarrier", "DeviceMemoryBarrierWithGroupSync",
"distance", "dot", "dst", "errorf", "EvaluateAttributeAtCentroid", "EvaluateAttributeAtSample", "EvaluateAttributeSnapped", "exp", "exp2",
"f16tof32", "f32tof16", "faceforward", "firstbithigh", "firstbitlow", "floor", "fma", "fmod", "frac", "frexp", "fwidth", "GetRenderTargetSampleCount",
"GetRenderTargetSamplePosition", "GroupMemoryBarrier", "GroupMemoryBarrierWithGroupSync", "InterlockedAdd", "InterlockedAnd", "InterlockedCompareExchange",
"InterlockedCompareStore", "InterlockedExchange", "InterlockedMax", "InterlockedMin", "InterlockedOr", "InterlockedXor", "isfinite", "isinf", "isnan",
"ldexp", "length", "lerp", "lit", "log", "log10", "log2", "mad", "max", "min", "modf", "msad4", "mul", "noise", "normalize", "pow", "printf",
"Process2DQuadTessFactorsAvg", "Process2DQuadTessFactorsMax", "Process2DQuadTessFactorsMin", "ProcessIsolineTessFactors", "ProcessQuadTessFactorsAvg",
"ProcessQuadTessFactorsMax", "ProcessQuadTessFactorsMin", "ProcessTriTessFactorsAvg", "ProcessTriTessFactorsMax", "ProcessTriTessFactorsMin",
"radians", "rcp", "reflect", "refract", "reversebits", "round", "rsqrt", "saturate", "sign", "sin", "sincos", "sinh", "smoothstep", "sqrt", "step",
"tan", "tanh", "tex1D", "tex1D", "tex1Dbias", "tex1Dgrad", "tex1Dlod", "tex1Dproj", "tex2D", "tex2D", "tex2Dbias", "tex2Dgrad", "tex2Dlod", "tex2Dproj",
"tex3D", "tex3D", "tex3Dbias", "tex3Dgrad", "tex3Dlod", "tex3Dproj", "texCUBE", "texCUBE", "texCUBEbias", "texCUBEgrad", "texCUBElod", "texCUBEproj", "transpose", "trunc"
};
for (auto& k : identifiers)
{
Identifier id;
id.mDeclaration = "Built-in function";
langDef.mIdentifiers.insert(std::make_pair(std::string(k), id));
}
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([ \t]*#[ \t]*[a-zA-Z_]+)##", PaletteIndex::Preprocessor));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(L?\"(\\.|[^\"])*\")##", PaletteIndex::String));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(\'\\?[^\']\')##", PaletteIndex::CharLiteral));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[fF]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?[0-9]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(0[0-7]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(0[xX][0-9a-fA-F]+[uU]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([a-zA-Z_][a-zA-Z0-9_]*)##", PaletteIndex::Identifier));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([\[\]\{\}\!\%\^\&\*\(\)\-\+\=\~\|\<\>\?\/\;\,\.])##", PaletteIndex::Punctuation));
langDef.mCommentStart = "/*";
langDef.mCommentEnd = "*/";
langDef.mSingleLineComment = "//";
langDef.mCaseSensitive = true;
langDef.mName = "HLSL";
inited = true;
}
return langDef;
}
const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::Glsl()
{
static bool inited = false;
static LanguageDefinition langDef;
if (!inited)
{
static const char* const keywords[] = {
"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "inline", "int", "long", "register", "restrict", "return", "short",
"signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while", "_Alignas", "_Alignof", "_Atomic", "_Bool", "_Complex", "_Generic", "_Imaginary",
"_Noreturn", "_Static_assert", "_Thread_local"
};
for (auto& k : keywords)
langDef.mKeywords.insert(k);
static const char* const identifiers[] = {
"abort", "abs", "acos", "asin", "atan", "atexit", "atof", "atoi", "atol", "ceil", "clock", "cosh", "ctime", "div", "exit", "fabs", "floor", "fmod", "getchar", "getenv", "isalnum", "isalpha", "isdigit", "isgraph",
"ispunct", "isspace", "isupper", "kbhit", "log10", "log2", "log", "memcmp", "modf", "pow", "putchar", "putenv", "puts", "rand", "remove", "rename", "sinh", "sqrt", "srand", "strcat", "strcmp", "strerror", "time", "tolower", "toupper"
};
for (auto& k : identifiers)
{
Identifier id;
id.mDeclaration = "Built-in function";
langDef.mIdentifiers.insert(std::make_pair(std::string(k), id));
}
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([ \t]*#[ \t]*[a-zA-Z_]+)##", PaletteIndex::Preprocessor));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(L?\"(\\.|[^\"])*\")##", PaletteIndex::String));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(\'\\?[^\']\')##", PaletteIndex::CharLiteral));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[fF]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?[0-9]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(0[0-7]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(0[xX][0-9a-fA-F]+[uU]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([a-zA-Z_][a-zA-Z0-9_]*)##", PaletteIndex::Identifier));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([\[\]\{\}\!\%\^\&\*\(\)\-\+\=\~\|\<\>\?\/\;\,\.])##", PaletteIndex::Punctuation));
langDef.mCommentStart = "/*";
langDef.mCommentEnd = "*/";
langDef.mSingleLineComment = "//";
langDef.mCaseSensitive = true;
langDef.mName = "GLSL";
inited = true;
}
return langDef;
}
const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::Python()
{
static bool inited = false;
static LanguageDefinition langDef;
if (!inited)
{
static const char* const keywords[] = {
"False", "await", "else", "import", "pass", "None", "break", "except", "in", "raise", "True", "class", "finally", "is", "return", "and", "continue", "for", "lambda", "try", "as", "def", "from", "nonlocal", "while", "assert", "del", "global", "not", "with", "async", "elif", "if", "or", "yield"
};
for (auto& k : keywords)
langDef.mKeywords.insert(k);
static const char* const identifiers[] = {
"abs", "aiter", "all", "any", "anext", "ascii", "bin", "bool", "breakpoint", "bytearray", "bytes", "callable", "chr", "classmethod", "compile", "complex", "delattr", "dict", "dir", "divmod", "enumerate", "eval", "exec", "filter", "float", "format", "frozenset", "getattr", "globals", "hasattr", "hash", "help", "hex", "id", "input", "int", "isinstance", "issubclass", "iter", "len", "list", "locals", "map", "max", "memoryview", "min", "next", "object", "oct", "open", "ord", "pow", "print", "property", "range", "repr", "reversed", "round", "set", "setattr", "slice", "sorted", "staticmethod", "str", "sum", "super", "tuple", "type", "vars", "zip", "__import__"
};
for (auto& k : identifiers)
{
Identifier id;
id.mDeclaration = "Built-in function";
langDef.mIdentifiers.insert(std::make_pair(std::string(k), id));
}
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##((b|u|f|r)?\"(\\.|[^\"])*\")##", PaletteIndex::String));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##((b|u|f|r)?'(\\.|[^'])*')##", PaletteIndex::String));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[fF]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?[0-9]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(0[0-7]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(0[xX][0-9a-fA-F]+[uU]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([a-zA-Z_][a-zA-Z0-9_]*)##", PaletteIndex::Identifier));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([\[\]\{\}\!\%\^\&\*\(\)\-\+\=\~\|\<\>\?\/\;\,\.\:])##", PaletteIndex::Punctuation));
langDef.mCommentStart = "\"\"\"";
langDef.mCommentEnd = "\"\"\"";
langDef.mSingleLineComment = "#";
langDef.mCaseSensitive = true;
langDef.mName = "Python";
inited = true;
}
return langDef;
}
const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::C()
{
static bool inited = false;
static LanguageDefinition langDef;
if (!inited)
{
static const char* const keywords[] = {
"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "inline", "int", "long", "register", "restrict", "return", "short",
"signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while", "_Alignas", "_Alignof", "_Atomic", "_Bool", "_Complex", "_Generic", "_Imaginary",
"_Noreturn", "_Static_assert", "_Thread_local"
};
for (auto& k : keywords)
langDef.mKeywords.insert(k);
static const char* const identifiers[] = {
"abort", "abs", "acos", "asin", "atan", "atexit", "atof", "atoi", "atol", "ceil", "clock", "cosh", "ctime", "div", "exit", "fabs", "floor", "fmod", "getchar", "getenv", "isalnum", "isalpha", "isdigit", "isgraph",
"ispunct", "isspace", "isupper", "kbhit", "log10", "log2", "log", "memcmp", "modf", "pow", "putchar", "putenv", "puts", "rand", "remove", "rename", "sinh", "sqrt", "srand", "strcat", "strcmp", "strerror", "time", "tolower", "toupper"
};
for (auto& k : identifiers)
{
Identifier id;
id.mDeclaration = "Built-in function";
langDef.mIdentifiers.insert(std::make_pair(std::string(k), id));
}
langDef.mTokenize = [](const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end, PaletteIndex& paletteIndex) -> bool
{
paletteIndex = PaletteIndex::Max;
while (in_begin < in_end && isascii(*in_begin) && isblank(*in_begin))
in_begin++;
if (in_begin == in_end)
{
out_begin = in_end;
out_end = in_end;
paletteIndex = PaletteIndex::Default;
}
else if (TokenizeCStyleString(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::String;
else if (TokenizeCStyleCharacterLiteral(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::CharLiteral;
else if (TokenizeCStyleIdentifier(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::Identifier;
else if (TokenizeCStyleNumber(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::Number;
else if (TokenizeCStylePunctuation(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::Punctuation;
return paletteIndex != PaletteIndex::Max;
};
langDef.mCommentStart = "/*";
langDef.mCommentEnd = "*/";
langDef.mSingleLineComment = "//";
langDef.mCaseSensitive = true;
langDef.mName = "C";
inited = true;
}
return langDef;
}
const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::Sql()
{
static bool inited = false;
static LanguageDefinition langDef;
if (!inited)
{
static const char* const keywords[] = {
"ADD", "EXCEPT", "PERCENT", "ALL", "EXEC", "PLAN", "ALTER", "EXECUTE", "PRECISION", "AND", "EXISTS", "PRIMARY", "ANY", "EXIT", "PRINT", "AS", "FETCH", "PROC", "ASC", "FILE", "PROCEDURE",
"AUTHORIZATION", "FILLFACTOR", "PUBLIC", "BACKUP", "FOR", "RAISERROR", "BEGIN", "FOREIGN", "READ", "BETWEEN", "FREETEXT", "READTEXT", "BREAK", "FREETEXTTABLE", "RECONFIGURE",
"BROWSE", "FROM", "REFERENCES", "BULK", "FULL", "REPLICATION", "BY", "FUNCTION", "RESTORE", "CASCADE", "GOTO", "RESTRICT", "CASE", "GRANT", "RETURN", "CHECK", "GROUP", "REVOKE",
"CHECKPOINT", "HAVING", "RIGHT", "CLOSE", "HOLDLOCK", "ROLLBACK", "CLUSTERED", "IDENTITY", "ROWCOUNT", "COALESCE", "IDENTITY_INSERT", "ROWGUIDCOL", "COLLATE", "IDENTITYCOL", "RULE",
"COLUMN", "IF", "SAVE", "COMMIT", "IN", "SCHEMA", "COMPUTE", "INDEX", "SELECT", "CONSTRAINT", "INNER", "SESSION_USER", "CONTAINS", "INSERT", "SET", "CONTAINSTABLE", "INTERSECT", "SETUSER",
"CONTINUE", "INTO", "SHUTDOWN", "CONVERT", "IS", "SOME", "CREATE", "JOIN", "STATISTICS", "CROSS", "KEY", "SYSTEM_USER", "CURRENT", "KILL", "TABLE", "CURRENT_DATE", "LEFT", "TEXTSIZE",
"CURRENT_TIME", "LIKE", "THEN", "CURRENT_TIMESTAMP", "LINENO", "TO", "CURRENT_USER", "LOAD", "TOP", "CURSOR", "NATIONAL", "TRAN", "DATABASE", "NOCHECK", "TRANSACTION",
"DBCC", "NONCLUSTERED", "TRIGGER", "DEALLOCATE", "NOT", "TRUNCATE", "DECLARE", "NULL", "TSEQUAL", "DEFAULT", "NULLIF", "UNION", "DELETE", "OF", "UNIQUE", "DENY", "OFF", "UPDATE",
"DESC", "OFFSETS", "UPDATETEXT", "DISK", "ON", "USE", "DISTINCT", "OPEN", "USER", "DISTRIBUTED", "OPENDATASOURCE", "VALUES", "DOUBLE", "OPENQUERY", "VARYING","DROP", "OPENROWSET", "VIEW",
"DUMMY", "OPENXML", "WAITFOR", "DUMP", "OPTION", "WHEN", "ELSE", "OR", "WHERE", "END", "ORDER", "WHILE", "ERRLVL", "OUTER", "WITH", "ESCAPE", "OVER", "WRITETEXT"
};
for (auto& k : keywords)
langDef.mKeywords.insert(k);
static const char* const identifiers[] = {
"ABS", "ACOS", "ADD_MONTHS", "ASCII", "ASCIISTR", "ASIN", "ATAN", "ATAN2", "AVG", "BFILENAME", "BIN_TO_NUM", "BITAND", "CARDINALITY", "CASE", "CAST", "CEIL",
"CHARTOROWID", "CHR", "COALESCE", "COMPOSE", "CONCAT", "CONVERT", "CORR", "COS", "COSH", "COUNT", "COVAR_POP", "COVAR_SAMP", "CUME_DIST", "CURRENT_DATE",
"CURRENT_TIMESTAMP", "DBTIMEZONE", "DECODE", "DECOMPOSE", "DENSE_RANK", "DUMP", "EMPTY_BLOB", "EMPTY_CLOB", "EXP", "EXTRACT", "FIRST_VALUE", "FLOOR", "FROM_TZ", "GREATEST",
"GROUP_ID", "HEXTORAW", "INITCAP", "INSTR", "INSTR2", "INSTR4", "INSTRB", "INSTRC", "LAG", "LAST_DAY", "LAST_VALUE", "LEAD", "LEAST", "LENGTH", "LENGTH2", "LENGTH4",
"LENGTHB", "LENGTHC", "LISTAGG", "LN", "LNNVL", "LOCALTIMESTAMP", "LOG", "LOWER", "LPAD", "LTRIM", "MAX", "MEDIAN", "MIN", "MOD", "MONTHS_BETWEEN", "NANVL", "NCHR",
"NEW_TIME", "NEXT_DAY", "NTH_VALUE", "NULLIF", "NUMTODSINTERVAL", "NUMTOYMINTERVAL", "NVL", "NVL2", "POWER", "RANK", "RAWTOHEX", "REGEXP_COUNT", "REGEXP_INSTR",
"REGEXP_REPLACE", "REGEXP_SUBSTR", "REMAINDER", "REPLACE", "ROUND", "ROWNUM", "RPAD", "RTRIM", "SESSIONTIMEZONE", "SIGN", "SIN", "SINH",
"SOUNDEX", "SQRT", "STDDEV", "SUBSTR", "SUM", "SYS_CONTEXT", "SYSDATE", "SYSTIMESTAMP", "TAN", "TANH", "TO_CHAR", "TO_CLOB", "TO_DATE", "TO_DSINTERVAL", "TO_LOB",
"TO_MULTI_BYTE", "TO_NCLOB", "TO_NUMBER", "TO_SINGLE_BYTE", "TO_TIMESTAMP", "TO_TIMESTAMP_TZ", "TO_YMINTERVAL", "TRANSLATE", "TRIM", "TRUNC", "TZ_OFFSET", "UID", "UPPER",
"USER", "USERENV", "VAR_POP", "VAR_SAMP", "VARIANCE", "VSIZE"
};
for (auto& k : identifiers)
{
Identifier id;
id.mDeclaration = "Built-in function";
langDef.mIdentifiers.insert(std::make_pair(std::string(k), id));
}
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(L?\"(\\.|[^\"])*\")##", PaletteIndex::String));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(\'[^\']*\')##", PaletteIndex::String));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[fF]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?[0-9]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(0[0-7]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(0[xX][0-9a-fA-F]+[uU]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([a-zA-Z_][a-zA-Z0-9_]*)##", PaletteIndex::Identifier));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([\[\]\{\}\!\%\^\&\*\(\)\-\+\=\~\|\<\>\?\/\;\,\.])##", PaletteIndex::Punctuation));
langDef.mCommentStart = "/*";
langDef.mCommentEnd = "*/";
langDef.mSingleLineComment = "--";
langDef.mCaseSensitive = false;
langDef.mName = "SQL";
inited = true;
}
return langDef;
}
const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::AngelScript()
{
static bool inited = false;
static LanguageDefinition langDef;
if (!inited)
{
static const char* const keywords[] = {
"and", "abstract", "auto", "bool", "break", "case", "cast", "class", "const", "continue", "default", "do", "double", "else", "enum", "false", "final", "float", "for",
"from", "funcdef", "function", "get", "if", "import", "in", "inout", "int", "interface", "int8", "int16", "int32", "int64", "is", "mixin", "namespace", "not",
"null", "or", "out", "override", "private", "protected", "return", "set", "shared", "super", "switch", "this ", "true", "typedef", "uint", "uint8", "uint16", "uint32",
"uint64", "void", "while", "xor"
};
for (auto& k : keywords)
langDef.mKeywords.insert(k);
static const char* const identifiers[] = {
"cos", "sin", "tab", "acos", "asin", "atan", "atan2", "cosh", "sinh", "tanh", "log", "log10", "pow", "sqrt", "abs", "ceil", "floor", "fraction", "closeTo", "fpFromIEEE", "fpToIEEE",
"complex", "opEquals", "opAddAssign", "opSubAssign", "opMulAssign", "opDivAssign", "opAdd", "opSub", "opMul", "opDiv"
};
for (auto& k : identifiers)
{
Identifier id;
id.mDeclaration = "Built-in function";
langDef.mIdentifiers.insert(std::make_pair(std::string(k), id));
}
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(L?\"(\\.|[^\"])*\")##", PaletteIndex::String));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(\'\\?[^\']\')##", PaletteIndex::String));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[fF]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?[0-9]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(0[0-7]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(0[xX][0-9a-fA-F]+[uU]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([a-zA-Z_][a-zA-Z0-9_]*)##", PaletteIndex::Identifier));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([\[\]\{\}\!\%\^\&\*\(\)\-\+\=\~\|\<\>\?\/\;\,\.])##", PaletteIndex::Punctuation));
langDef.mCommentStart = "/*";
langDef.mCommentEnd = "*/";
langDef.mSingleLineComment = "//";
langDef.mCaseSensitive = true;
langDef.mName = "AngelScript";
inited = true;
}
return langDef;
}
const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::Lua()
{
static bool inited = false;
static LanguageDefinition langDef;
if (!inited)
{
static const char* const keywords[] = {
"and", "break", "do", "else", "elseif", "end", "false", "for", "function", "goto", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while"
};
for (auto& k : keywords)
langDef.mKeywords.insert(k);
static const char* const identifiers[] = {
"assert", "collectgarbage", "dofile", "error", "getmetatable", "ipairs", "loadfile", "load", "loadstring", "next", "pairs", "pcall", "print", "rawequal", "rawlen", "rawget", "rawset",
"select", "setmetatable", "tonumber", "tostring", "type", "xpcall", "_G", "_VERSION","arshift", "band", "bnot", "bor", "bxor", "btest", "extract", "lrotate", "lshift", "replace",
"rrotate", "rshift", "create", "resume", "running", "status", "wrap", "yield", "isyieldable", "debug","getuservalue", "gethook", "getinfo", "getlocal", "getregistry", "getmetatable",
"getupvalue", "upvaluejoin", "upvalueid", "setuservalue", "sethook", "setlocal", "setmetatable", "setupvalue", "traceback", "close", "flush", "input", "lines", "open", "output", "popen",
"read", "tmpfile", "type", "write", "close", "flush", "lines", "read", "seek", "setvbuf", "write", "__gc", "__tostring", "abs", "acos", "asin", "atan", "ceil", "cos", "deg", "exp", "tointeger",
"floor", "fmod", "ult", "log", "max", "min", "modf", "rad", "random", "randomseed", "sin", "sqrt", "string", "tan", "type", "atan2", "cosh", "sinh", "tanh",
"pow", "frexp", "ldexp", "log10", "pi", "huge", "maxinteger", "mininteger", "loadlib", "searchpath", "seeall", "preload", "cpath", "path", "searchers", "loaded", "module", "require", "clock",
"date", "difftime", "execute", "exit", "getenv", "remove", "rename", "setlocale", "time", "tmpname", "byte", "char", "dump", "find", "format", "gmatch", "gsub", "len", "lower", "match", "rep",
"reverse", "sub", "upper", "pack", "packsize", "unpack", "concat", "maxn", "insert", "pack", "unpack", "remove", "move", "sort", "offset", "codepoint", "char", "len", "codes", "charpattern",
"coroutine", "table", "io", "os", "string", "utf8", "bit32", "math", "debug", "package"
};
for (auto& k : identifiers)
{
Identifier id;
id.mDeclaration = "Built-in function";
langDef.mIdentifiers.insert(std::make_pair(std::string(k), id));
}
langDef.mTokenize = [](const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end, PaletteIndex& paletteIndex) -> bool
{
paletteIndex = PaletteIndex::Max;
while (in_begin < in_end && isascii(*in_begin) && isblank(*in_begin))
in_begin++;
if (in_begin == in_end)
{
out_begin = in_end;
out_end = in_end;
paletteIndex = PaletteIndex::Default;
}
else if (TokenizeLuaStyleString(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::String;
else if (TokenizeLuaStyleIdentifier(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::Identifier;
else if (TokenizeLuaStyleNumber(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::Number;
else if (TokenizeLuaStylePunctuation(in_begin, in_end, out_begin, out_end))
paletteIndex = PaletteIndex::Punctuation;
return paletteIndex != PaletteIndex::Max;
};
langDef.mCommentStart = "--[[";
langDef.mCommentEnd = "]]";
langDef.mSingleLineComment = "--";
langDef.mCaseSensitive = true;
langDef.mName = "Lua";
inited = true;
}
return langDef;
}
const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::Cs()
{
static bool inited = false;
static LanguageDefinition langDef;
if (!inited)
{
static const char* const keywords[] = {
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "in (generic modifier)", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out", "out (generic modifier)", "override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using", "using static", "void", "volatile", "while"
};
for (auto& k : keywords)
langDef.mKeywords.insert(k);
static const char* const identifiers[] = {
"add", "alias", "ascending", "async", "await", "descending", "dynamic", "from", "get", "global", "group", "into", "join", "let", "orderby", "partial", "remove", "select", "set", "value", "var", "when", "where", "yield"
};
for (auto& k : identifiers)
{
Identifier id;
id.mDeclaration = "Built-in function";
langDef.mIdentifiers.insert(std::make_pair(std::string(k), id));
}
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(($|@)?\"(\\.|[^\"])*\")##", PaletteIndex::String));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[fF]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?[0-9]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(0[0-7]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(0[xX][0-9a-fA-F]+[uU]?[lL]?[lL]?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([a-zA-Z_][a-zA-Z0-9_]*)##", PaletteIndex::Identifier));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([\[\]\{\}\!\%\^\&\*\(\)\-\+\=\~\|\<\>\?\/\;\,\.])##", PaletteIndex::Punctuation));
langDef.mCommentStart = "/*";
langDef.mCommentEnd = "*/";
langDef.mSingleLineComment = "//";
langDef.mCaseSensitive = true;
langDef.mName = "C#";
inited = true;
}
return langDef;
}
const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::Json()
{
static bool inited = false;
static LanguageDefinition langDef;
if (!inited)
{
langDef.mKeywords.clear();
langDef.mIdentifiers.clear();
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(\"(\\.|[^\"])*\")##", PaletteIndex::String));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?)##", PaletteIndex::Number));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##([\[\]\{\}\!\%\^\&\*\(\)\-\+\=\~\|\<\>\?\/\;\,\.\:])##", PaletteIndex::Punctuation));
langDef.mTokenRegexStrings.push_back(std::make_pair<std::string, PaletteIndex>(R"##(false|true)##", PaletteIndex::Keyword));
langDef.mCommentStart = "/*";
langDef.mCommentEnd = "*/";
langDef.mSingleLineComment = "//";
langDef.mCaseSensitive = true;
langDef.mName = "Json";
inited = true;
}
return langDef;
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,469 @@
#pragma once
#include <cmath>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <memory>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include "imgui.h"
class IMGUI_API TextEditor
{
public:
// ------------- Exposed API ------------- //
TextEditor();
~TextEditor();
enum class PaletteId
{
Dark, Light, Mariana, RetroBlue
};
enum class LanguageDefinitionId
{
None, Cpp, C, Cs, Python, Lua, Json, Sql, AngelScript, Glsl, Hlsl
};
enum class SetViewAtLineMode
{
FirstVisibleLine, Centered, LastVisibleLine
};
inline void SetReadOnlyEnabled(bool aValue) { mReadOnly = aValue; }
inline bool IsReadOnlyEnabled() const { return mReadOnly; }
inline void SetAutoIndentEnabled(bool aValue) { mAutoIndent = aValue; }
inline bool IsAutoIndentEnabled() const { return mAutoIndent; }
inline void SetShowWhitespacesEnabled(bool aValue) { mShowWhitespaces = aValue; }
inline bool IsShowWhitespacesEnabled() const { return mShowWhitespaces; }
inline void SetShowLineNumbersEnabled(bool aValue) { mShowLineNumbers = aValue; }
inline bool IsShowLineNumbersEnabled() const { return mShowLineNumbers; }
inline void SetShortTabsEnabled(bool aValue) { mShortTabs = aValue; }
inline bool IsShortTabsEnabled() const { return mShortTabs; }
inline int GetLineCount() const { return mLines.size(); }
inline bool IsOverwriteEnabled() const { return mOverwrite; }
void SetPalette(PaletteId aValue);
PaletteId GetPalette() const { return mPaletteId; }
void SetLanguageDefinition(LanguageDefinitionId aValue);
LanguageDefinitionId GetLanguageDefinition() const { return mLanguageDefinitionId; };
const char* GetLanguageDefinitionName() const;
void SetTabSize(int aValue);
inline int GetTabSize() const { return mTabSize; }
void SetLineSpacing(float aValue);
inline float GetLineSpacing() const { return mLineSpacing; }
inline static void SetDefaultPalette(PaletteId aValue) { defaultPalette = aValue; }
inline static PaletteId GetDefaultPalette() { return defaultPalette; }
void SelectAll();
void SelectLine(int aLine);
void SelectRegion(int aStartLine, int aStartChar, int aEndLine, int aEndChar);
void SelectNextOccurrenceOf(const char* aText, int aTextSize, bool aCaseSensitive = true);
void SelectAllOccurrencesOf(const char* aText, int aTextSize, bool aCaseSensitive = true);
bool AnyCursorHasSelection() const;
bool AllCursorsHaveSelection() const;
void ClearExtraCursors();
void ClearSelections();
void SetCursorPosition(int aLine, int aCharIndex);
inline void GetCursorPosition(int& outLine, int& outColumn) const
{
auto coords = GetActualCursorCoordinates();
outLine = coords.mLine;
outColumn = coords.mColumn;
}
int GetFirstVisibleLine();
int GetLastVisibleLine();
void SetViewAtLine(int aLine, SetViewAtLineMode aMode);
void Copy();
void Cut();
void Paste();
void Undo(int aSteps = 1);
void Redo(int aSteps = 1);
inline bool CanUndo() const { return !mReadOnly && mUndoIndex > 0; };
inline bool CanRedo() const { return !mReadOnly && mUndoIndex < (int)mUndoBuffer.size(); };
inline int GetUndoIndex() const { return mUndoIndex; };
void SetText(const std::string& aText);
std::string GetText() const;
void SetTextLines(const std::vector<std::string>& aLines);
std::vector<std::string> GetTextLines() const;
bool Render(const char* aTitle, bool aParentIsFocused = false, const ImVec2& aSize = ImVec2(), bool aBorder = false);
void ImGuiDebugPanel(const std::string& panelName = "Debug");
void UnitTests();
private:
// ------------- Generic utils ------------- //
static inline ImVec4 U32ColorToVec4(ImU32 in)
{
float s = 1.0f / 255.0f;
return ImVec4(
((in >> IM_COL32_A_SHIFT) & 0xFF) * s,
((in >> IM_COL32_B_SHIFT) & 0xFF) * s,
((in >> IM_COL32_G_SHIFT) & 0xFF) * s,
((in >> IM_COL32_R_SHIFT) & 0xFF) * s);
}
static inline bool IsUTFSequence(char c)
{
return (c & 0xC0) == 0x80;
}
static inline float Distance(const ImVec2& a, const ImVec2& b)
{
float x = a.x - b.x;
float y = a.y - b.y;
return sqrt(x * x + y * y);
}
template<typename T>
static inline T Max(T a, T b) { return a > b ? a : b; }
template<typename T>
static inline T Min(T a, T b) { return a < b ? a : b; }
// ------------- Internal ------------- //
enum class PaletteIndex
{
Default,
Keyword,
Number,
String,
CharLiteral,
Punctuation,
Preprocessor,
Identifier,
KnownIdentifier,
PreprocIdentifier,
Comment,
MultiLineComment,
Background,
Cursor,
Selection,
ErrorMarker,
ControlCharacter,
Breakpoint,
LineNumber,
CurrentLineFill,
CurrentLineFillInactive,
CurrentLineEdge,
Max
};
// Represents a character coordinate from the user's point of view,
// i. e. consider an uniform grid (assuming fixed-width font) on the
// screen as it is rendered, and each cell has its own coordinate, starting from 0.
// Tabs are counted as [1..mTabSize] count empty spaces, depending on
// how many space is necessary to reach the next tab stop.
// For example, coordinate (1, 5) represents the character 'B' in a line "\tABC", when mTabSize = 4,
// because it is rendered as " ABC" on the screen.
struct Coordinates
{
int mLine, mColumn;
Coordinates() : mLine(0), mColumn(0) {}
Coordinates(int aLine, int aColumn) : mLine(aLine), mColumn(aColumn)
{
assert(aLine >= 0);
assert(aColumn >= 0);
}
static Coordinates Invalid() { static Coordinates invalid(-1, -1); return invalid; }
bool operator ==(const Coordinates& o) const
{
return
mLine == o.mLine &&
mColumn == o.mColumn;
}
bool operator !=(const Coordinates& o) const
{
return
mLine != o.mLine ||
mColumn != o.mColumn;
}
bool operator <(const Coordinates& o) const
{
if (mLine != o.mLine)
return mLine < o.mLine;
return mColumn < o.mColumn;
}
bool operator >(const Coordinates& o) const
{
if (mLine != o.mLine)
return mLine > o.mLine;
return mColumn > o.mColumn;
}
bool operator <=(const Coordinates& o) const
{
if (mLine != o.mLine)
return mLine < o.mLine;
return mColumn <= o.mColumn;
}
bool operator >=(const Coordinates& o) const
{
if (mLine != o.mLine)
return mLine > o.mLine;
return mColumn >= o.mColumn;
}
Coordinates operator -(const Coordinates& o)
{
return Coordinates(mLine - o.mLine, mColumn - o.mColumn);
}
Coordinates operator +(const Coordinates& o)
{
return Coordinates(mLine + o.mLine, mColumn + o.mColumn);
}
};
struct Cursor
{
Coordinates mInteractiveStart = { 0, 0 };
Coordinates mInteractiveEnd = { 0, 0 };
inline Coordinates GetSelectionStart() const { return mInteractiveStart < mInteractiveEnd ? mInteractiveStart : mInteractiveEnd; }
inline Coordinates GetSelectionEnd() const { return mInteractiveStart > mInteractiveEnd ? mInteractiveStart : mInteractiveEnd; }
inline bool HasSelection() const { return mInteractiveStart != mInteractiveEnd; }
};
struct EditorState // state to be restored with undo/redo
{
int mCurrentCursor = 0;
int mLastAddedCursor = 0;
std::vector<Cursor> mCursors = { {{0,0}} };
void AddCursor();
int GetLastAddedCursorIndex();
void SortCursorsFromTopToBottom();
};
struct Identifier
{
Coordinates mLocation;
std::string mDeclaration;
};
typedef std::unordered_map<std::string, Identifier> Identifiers;
typedef std::array<ImU32, (unsigned)PaletteIndex::Max> Palette;
struct Glyph
{
char mChar;
PaletteIndex mColorIndex = PaletteIndex::Default;
bool mComment : 1;
bool mMultiLineComment : 1;
bool mPreprocessor : 1;
Glyph(char aChar, PaletteIndex aColorIndex) : mChar(aChar), mColorIndex(aColorIndex),
mComment(false), mMultiLineComment(false), mPreprocessor(false) {}
};
typedef std::vector<Glyph> Line;
struct LanguageDefinition
{
typedef std::pair<std::string, PaletteIndex> TokenRegexString;
typedef bool(*TokenizeCallback)(const char* in_begin, const char* in_end, const char*& out_begin, const char*& out_end, PaletteIndex& paletteIndex);
std::string mName;
std::unordered_set<std::string> mKeywords;
Identifiers mIdentifiers;
Identifiers mPreprocIdentifiers;
std::string mCommentStart, mCommentEnd, mSingleLineComment;
char mPreprocChar = '#';
TokenizeCallback mTokenize = nullptr;
std::vector<TokenRegexString> mTokenRegexStrings;
bool mCaseSensitive = true;
static const LanguageDefinition& Cpp();
static const LanguageDefinition& Hlsl();
static const LanguageDefinition& Glsl();
static const LanguageDefinition& Python();
static const LanguageDefinition& C();
static const LanguageDefinition& Sql();
static const LanguageDefinition& AngelScript();
static const LanguageDefinition& Lua();
static const LanguageDefinition& Cs();
static const LanguageDefinition& Json();
};
enum class UndoOperationType { Add, Delete };
struct UndoOperation
{
std::string mText;
TextEditor::Coordinates mStart;
TextEditor::Coordinates mEnd;
UndoOperationType mType;
};
class UndoRecord
{
public:
UndoRecord() {}
~UndoRecord() {}
UndoRecord(
const std::vector<UndoOperation>& aOperations,
TextEditor::EditorState& aBefore,
TextEditor::EditorState& aAfter);
void Undo(TextEditor* aEditor);
void Redo(TextEditor* aEditor);
std::vector<UndoOperation> mOperations;
EditorState mBefore;
EditorState mAfter;
};
std::string GetText(const Coordinates& aStart, const Coordinates& aEnd) const;
std::string GetClipboardText() const;
std::string GetSelectedText(int aCursor = -1) const;
void SetCursorPosition(const Coordinates& aPosition, int aCursor = -1, bool aClearSelection = true);
int InsertTextAt(Coordinates& aWhere, const char* aValue);
void InsertTextAtCursor(const char* aValue, int aCursor = -1);
enum class MoveDirection { Right = 0, Left = 1, Up = 2, Down = 3 };
bool Move(int& aLine, int& aCharIndex, bool aLeft = false, bool aLockLine = false) const;
void MoveCharIndexAndColumn(int aLine, int& aCharIndex, int& aColumn) const;
void MoveCoords(Coordinates& aCoords, MoveDirection aDirection, bool aWordMode = false, int aLineCount = 1) const;
void MoveUp(int aAmount = 1, bool aSelect = false);
void MoveDown(int aAmount = 1, bool aSelect = false);
void MoveLeft(bool aSelect = false, bool aWordMode = false);
void MoveRight(bool aSelect = false, bool aWordMode = false);
void MoveTop(bool aSelect = false);
void MoveBottom(bool aSelect = false);
void MoveHome(bool aSelect = false);
void MoveEnd(bool aSelect = false);
void EnterCharacter(ImWchar aChar, bool aShift);
void Backspace(bool aWordMode = false);
void Delete(bool aWordMode = false, const EditorState* aEditorState = nullptr);
void SetSelection(Coordinates aStart, Coordinates aEnd, int aCursor = -1);
void SetSelection(int aStartLine, int aStartChar, int aEndLine, int aEndChar, int aCursor = -1);
void SelectNextOccurrenceOf(const char* aText, int aTextSize, int aCursor = -1, bool aCaseSensitive = true);
void AddCursorForNextOccurrence(bool aCaseSensitive = true);
bool FindNextOccurrence(const char* aText, int aTextSize, const Coordinates& aFrom, Coordinates& outStart, Coordinates& outEnd, bool aCaseSensitive = true);
bool FindMatchingBracket(int aLine, int aCharIndex, Coordinates& out);
void ChangeCurrentLinesIndentation(bool aIncrease);
void MoveUpCurrentLines();
void MoveDownCurrentLines();
void ToggleLineComment();
void RemoveCurrentLines();
float TextDistanceToLineStart(const Coordinates& aFrom, bool aSanitizeCoords = true) const;
void EnsureCursorVisible(int aCursor = -1, bool aStartToo = false);
Coordinates SanitizeCoordinates(const Coordinates& aValue) const;
Coordinates GetActualCursorCoordinates(int aCursor = -1, bool aStart = false) const;
Coordinates ScreenPosToCoordinates(const ImVec2& aPosition, bool aInsertionMode = false, bool* isOverLineNumber = nullptr) const;
Coordinates FindWordStart(const Coordinates& aFrom) const;
Coordinates FindWordEnd(const Coordinates& aFrom) const;
int GetCharacterIndexL(const Coordinates& aCoordinates) const;
int GetCharacterIndexR(const Coordinates& aCoordinates) const;
int GetCharacterColumn(int aLine, int aIndex) const;
int GetFirstVisibleCharacterIndex(int aLine) const;
int GetLineMaxColumn(int aLine, int aLimit = -1) const;
Line& InsertLine(int aIndex);
void RemoveLine(int aIndex, const std::unordered_set<int>* aHandledCursors = nullptr);
void RemoveLines(int aStart, int aEnd);
void DeleteRange(const Coordinates& aStart, const Coordinates& aEnd);
void DeleteSelection(int aCursor = -1);
void RemoveGlyphsFromLine(int aLine, int aStartChar, int aEndChar = -1);
void AddGlyphsToLine(int aLine, int aTargetIndex, Line::iterator aSourceStart, Line::iterator aSourceEnd);
void AddGlyphToLine(int aLine, int aTargetIndex, Glyph aGlyph);
ImU32 GetGlyphColor(const Glyph& aGlyph) const;
void HandleKeyboardInputs(bool aParentIsFocused = false);
void HandleMouseInputs();
void UpdateViewVariables(float aScrollX, float aScrollY);
void Render(bool aParentIsFocused = false);
void OnCursorPositionChanged();
void OnLineChanged(bool aBeforeChange, int aLine, int aColumn, int aCharCount, bool aDeleted);
void MergeCursorsIfPossible();
void AddUndo(UndoRecord& aValue);
void Colorize(int aFromLine = 0, int aCount = -1);
void ColorizeRange(int aFromLine = 0, int aToLine = 0);
void ColorizeInternal();
std::vector<Line> mLines;
EditorState mState;
std::vector<UndoRecord> mUndoBuffer;
int mUndoIndex = 0;
int mTabSize = 4;
float mLineSpacing = 1.0f;
bool mOverwrite = false;
bool mReadOnly = false;
bool mAutoIndent = true;
bool mShowWhitespaces = true;
bool mShowLineNumbers = true;
bool mShortTabs = false;
int mSetViewAtLine = -1;
SetViewAtLineMode mSetViewAtLineMode;
int mEnsureCursorVisible = -1;
bool mEnsureCursorVisibleStartToo = false;
bool mScrollToTop = false;
float mTextStart = 20.0f; // position (in pixels) where a code line starts relative to the left of the TextEditor.
int mLeftMargin = 10;
ImVec2 mCharAdvance;
float mCurrentSpaceHeight = 20.0f;
float mCurrentSpaceWidth = 20.0f;
float mLastClickTime = -1.0f;
ImVec2 mLastClickPos;
int mFirstVisibleLine = 0;
int mLastVisibleLine = 0;
int mVisibleLineCount = 0;
int mFirstVisibleColumn = 0;
int mLastVisibleColumn = 0;
int mVisibleColumnCount = 0;
float mContentWidth = 0.0f;
float mContentHeight = 0.0f;
float mScrollX = 0.0f;
float mScrollY = 0.0f;
bool mPanning = false;
bool mDraggingSelection = false;
ImVec2 mLastMousePos;
bool mCursorPositionChanged = false;
bool mCursorOnBracket = false;
Coordinates mMatchingBracketCoords;
int mColorRangeMin = 0;
int mColorRangeMax = 0;
bool mCheckComments = true;
PaletteId mPaletteId;
Palette mPalette;
LanguageDefinitionId mLanguageDefinitionId;
const LanguageDefinition* mLanguageDefinition = nullptr;
inline bool IsHorizontalScrollbarVisible() const { return mCurrentSpaceWidth > mContentWidth; }
inline bool IsVerticalScrollbarVisible() const { return mCurrentSpaceHeight > mContentHeight; }
inline int TabSizeAtColumn(int aColumn) const { return mTabSize - (aColumn % mTabSize); }
static const Palette& GetDarkPalette();
static const Palette& GetMarianaPalette();
static const Palette& GetLightPalette();
static const Palette& GetRetroBluePalette();
static const std::unordered_map<char, char> OPEN_TO_CLOSE_CHAR;
static const std::unordered_map<char, char> CLOSE_TO_OPEN_CHAR;
static PaletteId defaultPalette;
};

View file

@ -26,6 +26,8 @@ If you have questions concerning this license or the applicable additional terms
===========================================================================
*/
#include "../../libs/ImGuiColorTextEdit/TextEditor.h"
#include "../util/ImGui_IdWidgets.h"
#include "ScriptEditor.h"
@ -70,7 +72,7 @@ ScriptEditor::ScriptEditor()
: isShown( false )
, windowText()
, statusBarText( "Script Editor" )
, scriptEdit()
, scriptEdit(NULL)
, okButtonEnabled( false )
, cancelButtonEnabled( true )
, findStr()
@ -81,12 +83,14 @@ ScriptEditor::ScriptEditor()
, fileName()
, firstLine( 0 )
{
scriptEdit = new TextEditor();
}
void ScriptEditor::Reset() {
windowText = "Script Editor";
statusBarText.Clear();
scriptEdit.Clear();
scriptEdit->SetText( std::string( "" ) );
scriptEdit->SetTabSize( TAB_SIZE );
okButtonEnabled = false;
cancelButtonEnabled = true;
findStr.Clear();
@ -96,6 +100,8 @@ void ScriptEditor::Reset() {
searchForward = true;
fileName.Clear();
firstLine = 0;
UpdateStatusBar();
}
void ScriptEditor::Draw()
@ -142,7 +148,8 @@ void ScriptEditor::Draw()
if (clickedSelect) {
}
ImGui::InputTextMultilineStr( "Text", &scriptEdit, ImVec2( 500, 500 ) );
scriptEdit->Render( "Text", false, ImVec2( 800, 600 ) );
UpdateStatusBar();
ImGui::BeginDisabled( !okButtonEnabled );
if ( ImGui::Button( "OK" ) ) {
@ -155,6 +162,8 @@ void ScriptEditor::Draw()
showTool = false;
}
ImGui::EndDisabled();
ImGui::TextUnformatted( statusBarText.c_str() );
}
ImGui::End();
@ -186,10 +195,10 @@ ScriptEditor::UpdateStatusBar
================
*/
void ScriptEditor::UpdateStatusBar( void ) {
//int line, column, character;
int line, column;
//scriptEdit.GetCursorPos( line, column, character );
//statusBarText = idStr::Format( "Line: %d, Column: %d, Character: %d", line, column, character );
scriptEdit->GetCursorPosition( line, column );
statusBarText = idStr::Format( "Line: %d, Column: %d", line, column );
}
/*
@ -300,12 +309,13 @@ void ScriptEditor::OpenFile( const char *fileName ) {
//scriptEdit.Init();
//scriptEdit.AllowPathNames( false );
scriptEdit.Clear();
scriptEdit->SetText(std::string(""));
idStr( fileName ).ExtractFileExtension( extension );
if ( extension.Icmp( "script" ) == 0 ) {
InitScriptEvents();
scriptEdit->SetLanguageDefinition( TextEditor::LanguageDefinitionId::Cpp );
/*
scriptEdit.SetCaseSensitive( true );
scriptEdit.LoadKeyWordsFromFile( "editors/script.def" );
@ -318,6 +328,7 @@ void ScriptEditor::OpenFile( const char *fileName ) {
scriptEdit.SetStringColor(SRE_COLOR_DARK_CYAN, SRE_COLOR_LIGHT_BROWN);
scriptEdit.LoadKeyWordsFromFile( "editors/gui.def" );
*/
scriptEdit->SetLanguageDefinition( TextEditor::LanguageDefinitionId::Json );
}
if ( fileSystem->ReadFile( fileName, &buffer ) == -1 ) {
@ -328,12 +339,7 @@ void ScriptEditor::OpenFile( const char *fileName ) {
this->fileName = fileName;
// clean up new-line crapola
scriptText.Replace( "\r", "" );
scriptText.Replace( "\n", "\r" );
scriptText.Replace( "\v", "\r" );
scriptEdit = scriptText;
scriptEdit->SetText( scriptText.c_str() );
for( const char *ptr = scriptText.c_str(); *ptr; ptr++ ) {
if ( *ptr == '\r' ) {
@ -391,6 +397,7 @@ bool ScriptEditor::OnInitDialog() {
//statusBar.CreateEx( SBARS_SIZEGRIP, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM, initialRect, this, AFX_IDW_STATUS_BAR );
//scriptEdit.LimitText( 1024 * 1024 );
scriptEdit->SetTabSize( TAB_SIZE );
//GetClientRect( initialRect );
@ -591,12 +598,7 @@ void ScriptEditor::OnBnClickedOk() {
common->Printf( "Writing \'%s\'...\n", fileName.c_str() );
scriptText = scriptEdit; // scriptEdit.GetText( scriptText );
// clean up new-line crapola
scriptText.Replace( "\n", "" );
scriptText.Replace( "\r", "\r\n" );
scriptText.Replace( "\v", "\r\n" );
scriptText = scriptEdit->GetText().c_str();
if ( fileSystem->WriteFile( fileName, scriptText, scriptText.Length(), "fs_devpath" ) == -1 ) {
//MessageBox( va( "Couldn't save: %s", fileName.c_str() ), va( "Error saving: %s", fileName.c_str() ), MB_OK | MB_ICONERROR );

View file

@ -31,6 +31,8 @@ If you have questions concerning this license or the applicable additional terms
#include "../../edit_public.h"
class TextEditor;
namespace ImGuiTools
{
@ -77,7 +79,7 @@ private:
bool isShown;
idStr windowText;
idStr statusBarText;
idStr scriptEdit;
TextEditor * scriptEdit;
bool okButtonEnabled;
bool cancelButtonEnabled;
/*