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) 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. #pragma once
  31. #include "core/io/resource.h"
  32. #include "core/object/gdvirtual.gen.inc"
  33. class TextEdit;
  34. class SyntaxHighlighter : public Resource {
  35. GDCLASS(SyntaxHighlighter, Resource)
  36. private:
  37. RBMap<int, Dictionary> highlighting_cache;
  38. void _lines_edited_from(int p_from_line, int p_to_line);
  39. protected:
  40. ObjectID text_edit_instance_id; // For validity check
  41. TextEdit *text_edit = nullptr;
  42. static void _bind_methods();
  43. GDVIRTUAL1RC(Dictionary, _get_line_syntax_highlighting, int)
  44. GDVIRTUAL0(_clear_highlighting_cache)
  45. GDVIRTUAL0(_update_cache)
  46. public:
  47. Dictionary get_line_syntax_highlighting(int p_line);
  48. virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) { return Dictionary(); }
  49. void clear_highlighting_cache();
  50. virtual void _clear_highlighting_cache() {}
  51. void update_cache();
  52. virtual void _update_cache() {}
  53. void set_text_edit(TextEdit *p_text_edit);
  54. TextEdit *get_text_edit() const;
  55. virtual ~SyntaxHighlighter() {}
  56. };
  57. ///////////////////////////////////////////////////////////////////////////////
  58. class CodeHighlighter : public SyntaxHighlighter {
  59. GDCLASS(CodeHighlighter, SyntaxHighlighter)
  60. private:
  61. struct ColorRegion {
  62. Color color;
  63. String start_key;
  64. String end_key;
  65. bool line_only = false;
  66. };
  67. Vector<ColorRegion> color_regions;
  68. HashMap<int, int> color_region_cache;
  69. Dictionary keywords;
  70. Dictionary member_keywords;
  71. Color font_color;
  72. Color member_color;
  73. Color function_color;
  74. Color symbol_color;
  75. Color number_color;
  76. bool uint_suffix_enabled = false;
  77. protected:
  78. static void _bind_methods();
  79. public:
  80. virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override;
  81. virtual void _clear_highlighting_cache() override;
  82. virtual void _update_cache() override;
  83. void add_keyword_color(const String &p_keyword, const Color &p_color);
  84. void remove_keyword_color(const String &p_keyword);
  85. bool has_keyword_color(const String &p_keyword) const;
  86. Color get_keyword_color(const String &p_keyword) const;
  87. void set_keyword_colors(const Dictionary p_keywords);
  88. void clear_keyword_colors();
  89. Dictionary get_keyword_colors() const;
  90. void add_member_keyword_color(const String &p_member_keyword, const Color &p_color);
  91. void remove_member_keyword_color(const String &p_member_keyword);
  92. bool has_member_keyword_color(const String &p_member_keyword) const;
  93. Color get_member_keyword_color(const String &p_member_keyword) const;
  94. void set_member_keyword_colors(const Dictionary &p_color_regions);
  95. void clear_member_keyword_colors();
  96. Dictionary get_member_keyword_colors() const;
  97. void add_color_region(const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only = false);
  98. void remove_color_region(const String &p_start_key);
  99. bool has_color_region(const String &p_start_key) const;
  100. void set_color_regions(const Dictionary &p_member_keyword);
  101. void clear_color_regions();
  102. Dictionary get_color_regions() const;
  103. void set_number_color(Color p_color);
  104. Color get_number_color() const;
  105. void set_symbol_color(Color p_color);
  106. Color get_symbol_color() const;
  107. void set_function_color(Color p_color);
  108. Color get_function_color() const;
  109. void set_member_variable_color(Color p_color);
  110. Color get_member_variable_color() const;
  111. void set_uint_suffix_enabled(bool p_enabled);
  112. };