Skip to content

Commit

Permalink
GDScript: Highlight script members like native ones
Browse files Browse the repository at this point in the history
  • Loading branch information
dalexeev authored and RadiantUwU committed Jul 15, 2024
1 parent f353f6e commit d299ba5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
77 changes: 63 additions & 14 deletions modules/gdscript/editor/gdscript_highlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
expect_type = false;
} else if (member_keywords.has(word)) {
col = member_keywords[word];
in_member_variable = true;
}

if (col != Color()) {
Expand All @@ -432,7 +433,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
}
}

if (col != Color()) {
if (!in_member_variable && col != Color()) {
in_keyword = true;
keyword_color = col;
}
Expand Down Expand Up @@ -612,7 +613,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
color = keyword_color;
} else if (in_signal_declaration) {
next_type = SIGNAL;
color = member_color;
color = member_variable_color;
} else if (in_function_name) {
next_type = FUNCTION;
if (!in_lambda && in_function_declaration) {
Expand All @@ -631,7 +632,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
color = type_color;
} else if (in_member_variable) {
next_type = MEMBER;
color = member_color;
color = member_variable_color;
} else {
next_type = IDENTIFIER;
}
Expand Down Expand Up @@ -694,7 +695,7 @@ void GDScriptSyntaxHighlighter::_update_cache() {
symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");
function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
number_color = EDITOR_GET("text_editor/theme/highlighting/number_color");
member_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");

/* Engine types. */
const Color types_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
Expand Down Expand Up @@ -764,7 +765,7 @@ void GDScriptSyntaxHighlighter::_update_cache() {
global_functions.insert(E);
}

/* Comments */
/* Comments. */
const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
List<String> comments;
gdscript->get_comment_delimiters(&comments);
Expand Down Expand Up @@ -800,15 +801,14 @@ void GDScriptSyntaxHighlighter::_update_cache() {
add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color, false, true);
add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color, false, true);

const Ref<Script> scr = _get_edited_resource();
/* Members. */
Ref<Script> scr = _get_edited_resource();
if (scr.is_valid()) {
/* Member types. */
const Color member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
StringName instance_base = scr->get_instance_base_type();
if (instance_base != StringName()) {
List<PropertyInfo> plist;
ClassDB::get_property_list(instance_base, &plist);
for (const PropertyInfo &E : plist) {
List<PropertyInfo> property_list;
ClassDB::get_property_list(instance_base, &property_list);
for (const PropertyInfo &E : property_list) {
String prop_name = E.name;
if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
continue;
Expand All @@ -819,12 +819,61 @@ void GDScriptSyntaxHighlighter::_update_cache() {
member_keywords[prop_name] = member_variable_color;
}

List<String> clist;
ClassDB::get_integer_constant_list(instance_base, &clist);
for (const String &E : clist) {
List<MethodInfo> signal_list;
ClassDB::get_signal_list(instance_base, &signal_list);
for (const MethodInfo &E : signal_list) {
member_keywords[E.name] = member_variable_color;
}

// For callables.
List<MethodInfo> method_list;
ClassDB::get_method_list(instance_base, &method_list);
for (const MethodInfo &E : method_list) {
member_keywords[E.name] = member_variable_color;
}

List<String> constant_list;
ClassDB::get_integer_constant_list(instance_base, &constant_list);
for (const String &E : constant_list) {
member_keywords[E] = member_variable_color;
}
}

List<PropertyInfo> scr_property_list;
scr->get_script_property_list(&scr_property_list);
for (const PropertyInfo &E : scr_property_list) {
String prop_name = E.name;
if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
continue;
}
if (prop_name.contains("/")) {
continue;
}
member_keywords[prop_name] = member_variable_color;
}

List<MethodInfo> scr_signal_list;
scr->get_script_signal_list(&scr_signal_list);
for (const MethodInfo &E : scr_signal_list) {
member_keywords[E.name] = member_variable_color;
}

// For callables.
List<MethodInfo> scr_method_list;
scr->get_script_method_list(&scr_method_list);
for (const MethodInfo &E : scr_method_list) {
member_keywords[E.name] = member_variable_color;
}

Ref<Script> scr_class = scr;
while (scr_class.is_valid()) {
HashMap<StringName, Variant> scr_constant_list;
scr_class->get_constants(&scr_constant_list);
for (const KeyValue<StringName, Variant> &E : scr_constant_list) {
member_keywords[E.key.operator String()] = member_variable_color;
}
scr_class = scr_class->get_base_script();
}
}

const String text_edit_color_theme = EDITOR_GET("text_editor/theme/color_theme");
Expand Down
2 changes: 1 addition & 1 deletion modules/gdscript/editor/gdscript_highlighter.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class GDScriptSyntaxHighlighter : public EditorSyntaxHighlighter {
Color function_definition_color;
Color built_in_type_color;
Color number_color;
Color member_color;
Color member_variable_color;
Color string_color;
Color node_path_color;
Color node_ref_color;
Expand Down

0 comments on commit d299ba5

Please sign in to comment.