santaclose / ImGuiColorTextEdit

Colorizing text editor for ImGui
MIT License
69 stars 19 forks source link

Add WGSL #25

Closed Zelif closed 3 months ago

Zelif commented 4 months ago

I have added a really basic implementation of WGSL it doesn't cover everything but is a little better than using the GLSL highlighting.

TextEditor.h:27 Only added to the enum. ```cpp enum class LanguageDefinitionId { None, Cpp, C, Cs, Python, Lua, Json, Sql, AngelScript, Glsl, Hlsl, Wgsl }; ```
TextEditor.cpp:92 Only added to the case statement. ```cpp case LanguageDefinitionId::Wgsl: mLanguageDefinition = &(LanguageDefinition::Wgsl()); break; } ```
LanguageDefinitions.cpp:939 I have only changed the keywords and identifiers, I was unsure about the regex's ```cpp const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::Wgsl() { static bool inited = false; static LanguageDefinition langDef; if (!inited) { static const char* const keywords[] = { // Attrubute Names "align", "binding", "builtin", "compute", "const", "diagnostic", "fragment", "group", "id", "interpolate", "invariant", "location", "blend_src", "must_use", "size", "vertex", "workgroup_size", // Predeclared Types "bool", "f16", "f32", "i32", "sampler", "sampler_comparison", "texture_depth_2d", "texture_depth_2d_array", "texture_depth_cube", "texture_depth_cube_array", "texture_depth_multisampled_2d", "texture_external", "u32", // Predeclared Alias "vec2i", "vec3i", "vec4i", "vec2u", "vec3u", "vec4u", "vec2f", "vec3f", "vec4f", "vec2h", "vec3h", "vec4h", "mat2x2f", "mat2x3f", "mat2x4f", "mat3x2f", "mat3x3f", "mat3x4f", "mat4x2f", "mat4x3f", "mat4x4f", "mat2x2h", "mat2x3h", "mat2x4h", "mat3x2h", "mat3x3h", "mat3x4h", "mat4x2h", "mat4x3h", "mat4x4h", // Keywords "alias", "break", "case", "const", "const_assert", "continue", "continuing", "default", "diagnostic", "discard", "else", "enable", "false", "fn", "for", "if", "let", "loop", "override", "requires", "return", "struct", "switch", "true", "var", "while", }; for (auto& k : keywords) langDef.mKeywords.insert(k); static const char* const identifiers[] = { // Value Constructor Built-in Functions "array", "bool", "f16", "f32", "i32", "mat2x2", "mat2x3", "mat2x4", "mat3x2", "mat3x3", "mat3x4", "mat4x2", "mat4x3", "mat4x4", "u32", "vec2", "vec3", "vec4", // Bitcast "bitcast", // Logical Built-in Functions "all", "any", "select", // Array Built-in Functions "arrayLength", // Numeric Built-in Functions "abs", "acos", "acosh", "asin", "asinh", "atan", "atanh", "atan2", "ceil", "clamp", "cos", "cosh", "countLeadingZeros", "countOneBits", "countTrailingZeros", "countTrailingZeros","cross", "dot", "dot4U8Packed", "dot4I8Packed", "exp", "exp2", "extractBits", "faceForward", "firstLeadingBit", "firstTrailingBit", "floor", "fma", "fract", "frexp", "insertBits", "inverseSqrt", "ldexp", "length", "log", "log2", "max", "min", "mix", "modf", "normalize", "pow", "quantizeToF16", "radians", "reflect", "refract", "reverseBits", "round", "saturate", "sign", "sin", "sinh", "smoothstep", "sqrt", "step", "tan", "tanh", "transpose", "trunc", // Derivative Built-in Functions "dpdx", "dpdxCoarse", "dpdxFine", "dpdy", "dpdyCoarse", "dpdyFine", "fwidth", "fwidthCoarse", "fwidthFine" // Texture Built-in Functions "textureDimensions", "textureGather", "textureGatherCompare", "textureLoad", "textureNumLayers", "textureNumLevels", "textureNumSamples", "textureSample", "textureSampleBias", "textureSampleCompare", "textureSampleCompareLevel", "textureSampleGrad", "textureSampleLevel", "textureSampleBaseClampToEdge", "textureStore", // Atomic Built-in Functions "atomicLoad", "atomicStore", "atomicAdd", "atomicSub", "atomicMax", "atomicMin", "atomicAnd", "atomicOr", "atomicXor", "atomicExchange", "atomicCompareExchangeWeak" // Data Packing Built-in Functions "pack4x8snorm", "pack4x8unorm", "pack4xI8", "pack4xU8", "pack4xI8Clamp", "pack4xU8Clamp", "pack2x16snorm", "pack2x16unorm", "pack2x16float", // Data Unpacking Built-in Functions "unpack4x8snorm", "unpack4x8unorm", "unpack4xI8", "unpack2x16snorm", "unpack2x16unorm", "unpack2x16float", // Synchronization Built-in Functions "storageBarrier", "textureBarrier", "workgroupBarrier", "workgroupUniformLoad" }; 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(R"##([ \t]*#[ \t]*[a-zA-Z_]+)##", PaletteIndex::Preprocessor)); langDef.mTokenRegexStrings.push_back(std::make_pair(R"##(L?\"(\\.|[^\"])*\")##", PaletteIndex::String)); langDef.mTokenRegexStrings.push_back(std::make_pair(R"##(\'\\?[^\']\')##", PaletteIndex::CharLiteral)); langDef.mTokenRegexStrings.push_back(std::make_pair(R"##([+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[fF]?)##", PaletteIndex::Number)); langDef.mTokenRegexStrings.push_back(std::make_pair(R"##([+-]?[0-9]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number)); langDef.mTokenRegexStrings.push_back(std::make_pair(R"##(0[0-7]+[Uu]?[lL]?[lL]?)##", PaletteIndex::Number)); langDef.mTokenRegexStrings.push_back(std::make_pair(R"##(0[xX][0-9a-fA-F]+[uU]?[lL]?[lL]?)##", PaletteIndex::Number)); langDef.mTokenRegexStrings.push_back(std::make_pair(R"##([a-zA-Z_][a-zA-Z0-9_]*)##", PaletteIndex::Identifier)); langDef.mTokenRegexStrings.push_back(std::make_pair(R"##([\[\]\{\}\!\%\^\&\*\(\)\-\+\=\~\|\<\>\?\/\;\,\.])##", PaletteIndex::Punctuation)); langDef.mCommentStart = "/*"; langDef.mCommentEnd = "*/"; langDef.mSingleLineComment = "//"; langDef.mCaseSensitive = true; langDef.mName = "WGSL"; inited = true; } return langDef; } ```

I didn't add a pullrequest yet as I haven't forked the project yet. There are specifics that I have missed most likely. mostly around the <> data types.

Example of it running

Editor_2024-07-05_15-36-46