syntax_highlighter.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*************************************************************************/
  2. /* syntax_highlighter.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef SYNTAX_HIGHLIGHTER_H
  31. #define SYNTAX_HIGHLIGHTER_H
  32. #include "core/io/resource.h"
  33. #include "core/object/gdvirtual.gen.inc"
  34. #include "core/object/script_language.h"
  35. class TextEdit;
  36. class SyntaxHighlighter : public Resource {
  37. GDCLASS(SyntaxHighlighter, Resource)
  38. private:
  39. RBMap<int, Dictionary> highlighting_cache;
  40. void _lines_edited_from(int p_from_line, int p_to_line);
  41. protected:
  42. ObjectID text_edit_instance_id; // For validity check
  43. TextEdit *text_edit = nullptr;
  44. static void _bind_methods();
  45. GDVIRTUAL1RC(Dictionary, _get_line_syntax_highlighting, int)
  46. GDVIRTUAL0(_clear_highlighting_cache)
  47. GDVIRTUAL0(_update_cache)
  48. public:
  49. Dictionary get_line_syntax_highlighting(int p_line);
  50. virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) { return Dictionary(); }
  51. void clear_highlighting_cache();
  52. virtual void _clear_highlighting_cache() {}
  53. void update_cache();
  54. virtual void _update_cache() {}
  55. void set_text_edit(TextEdit *p_text_edit);
  56. TextEdit *get_text_edit();
  57. SyntaxHighlighter() {}
  58. virtual ~SyntaxHighlighter() {}
  59. };
  60. ///////////////////////////////////////////////////////////////////////////////
  61. class CodeHighlighter : public SyntaxHighlighter {
  62. GDCLASS(CodeHighlighter, SyntaxHighlighter)
  63. private:
  64. struct ColorRegion {
  65. Color color;
  66. String start_key;
  67. String end_key;
  68. bool line_only = false;
  69. };
  70. Vector<ColorRegion> color_regions;
  71. HashMap<int, int> color_region_cache;
  72. Dictionary keywords;
  73. Dictionary member_keywords;
  74. Color font_color;
  75. Color member_color;
  76. Color function_color;
  77. Color symbol_color;
  78. Color number_color;
  79. protected:
  80. static void _bind_methods();
  81. public:
  82. virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override;
  83. virtual void _clear_highlighting_cache() override;
  84. virtual void _update_cache() override;
  85. void add_keyword_color(const String &p_keyword, const Color &p_color);
  86. void remove_keyword_color(const String &p_keyword);
  87. bool has_keyword_color(const String &p_keyword) const;
  88. Color get_keyword_color(const String &p_keyword) const;
  89. void set_keyword_colors(const Dictionary p_keywords);
  90. void clear_keyword_colors();
  91. Dictionary get_keyword_colors() const;
  92. void add_member_keyword_color(const String &p_member_keyword, const Color &p_color);
  93. void remove_member_keyword_color(const String &p_member_keyword);
  94. bool has_member_keyword_color(const String &p_member_keyword) const;
  95. Color get_member_keyword_color(const String &p_member_keyword) const;
  96. void set_member_keyword_colors(const Dictionary &p_color_regions);
  97. void clear_member_keyword_colors();
  98. Dictionary get_member_keyword_colors() const;
  99. void add_color_region(const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only = false);
  100. void remove_color_region(const String &p_start_key);
  101. bool has_color_region(const String &p_start_key) const;
  102. void set_color_regions(const Dictionary &p_member_keyword);
  103. void clear_color_regions();
  104. Dictionary get_color_regions() const;
  105. void set_number_color(Color p_color);
  106. Color get_number_color() const;
  107. void set_symbol_color(Color p_color);
  108. Color get_symbol_color() const;
  109. void set_function_color(Color p_color);
  110. Color get_function_color() const;
  111. void set_member_variable_color(Color p_color);
  112. Color get_member_variable_color() const;
  113. };
  114. #endif // SYNTAX_HIGHLIGHTER_H