syntax_highlighter.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**************************************************************************/
  2. /* syntax_highlighter.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. class TextEdit;
  35. class SyntaxHighlighter : public Resource {
  36. GDCLASS(SyntaxHighlighter, Resource)
  37. private:
  38. RBMap<int, Dictionary> highlighting_cache;
  39. void _lines_edited_from(int p_from_line, int p_to_line);
  40. protected:
  41. ObjectID text_edit_instance_id; // For validity check
  42. TextEdit *text_edit = nullptr;
  43. static void _bind_methods();
  44. GDVIRTUAL1RC(Dictionary, _get_line_syntax_highlighting, int)
  45. GDVIRTUAL0(_clear_highlighting_cache)
  46. GDVIRTUAL0(_update_cache)
  47. public:
  48. Dictionary get_line_syntax_highlighting(int p_line);
  49. virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) { return Dictionary(); }
  50. void clear_highlighting_cache();
  51. virtual void _clear_highlighting_cache() {}
  52. void update_cache();
  53. virtual void _update_cache() {}
  54. void set_text_edit(TextEdit *p_text_edit);
  55. TextEdit *get_text_edit() const;
  56. SyntaxHighlighter() {}
  57. virtual ~SyntaxHighlighter() {}
  58. };
  59. ///////////////////////////////////////////////////////////////////////////////
  60. class CodeHighlighter : public SyntaxHighlighter {
  61. GDCLASS(CodeHighlighter, SyntaxHighlighter)
  62. private:
  63. struct ColorRegion {
  64. Color color;
  65. String start_key;
  66. String end_key;
  67. bool line_only = false;
  68. };
  69. Vector<ColorRegion> color_regions;
  70. HashMap<int, int> color_region_cache;
  71. Dictionary keywords;
  72. Dictionary member_keywords;
  73. Color font_color;
  74. Color member_color;
  75. Color function_color;
  76. Color symbol_color;
  77. Color number_color;
  78. bool uint_suffix_enabled = false;
  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. void set_uint_suffix_enabled(bool p_enabled);
  114. };
  115. #endif // SYNTAX_HIGHLIGHTER_H