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