From c3d93edf3d4203da332fc2d4feb439eb35280309 Mon Sep 17 00:00:00 2001 From: Paulb23 Date: Fri, 20 Apr 2018 20:44:07 +0100 Subject: Exclude GDScriptSyntaxHighlighter from non-tool builds --- modules/gdscript/editor/gdscript_highlighter.cpp | 262 +++++++++++++++++++++++ 1 file changed, 262 insertions(+) create mode 100644 modules/gdscript/editor/gdscript_highlighter.cpp (limited to 'modules/gdscript/editor/gdscript_highlighter.cpp') diff --git a/modules/gdscript/editor/gdscript_highlighter.cpp b/modules/gdscript/editor/gdscript_highlighter.cpp new file mode 100644 index 000000000..4e89851bf --- /dev/null +++ b/modules/gdscript/editor/gdscript_highlighter.cpp @@ -0,0 +1,262 @@ +/*************************************************************************/ +/* gdscript_highlighter.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "gdscript_highlighter.h" +#include "scene/gui/text_edit.h" + +inline bool _is_symbol(CharType c) { + + return is_symbol(c); +} + +static bool _is_text_char(CharType c) { + + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'; +} + +static bool _is_whitespace(CharType c) { + return c == '\t' || c == ' '; +} + +static bool _is_char(CharType c) { + + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; +} + +static bool _is_number(CharType c) { + return (c >= '0' && c <= '9'); +} + +static bool _is_hex_symbol(CharType c) { + return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')); +} + +Map GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) { + Map color_map; + + bool prev_is_char = false; + bool prev_is_number = false; + bool in_keyword = false; + bool in_word = false; + bool in_function_name = false; + bool in_member_variable = false; + bool is_hex_notation = false; + Color keyword_color; + Color color; + + int in_region = text_editor->_is_line_in_region(p_line); + int deregion = 0; + + const Map cri_map = text_editor->_get_line_color_region_info(p_line); + const String &str = text_editor->get_line(p_line); + Color prev_color; + for (int j = 0; j < str.length(); j++) { + TextEdit::HighlighterInfo highlighter_info; + + if (deregion > 0) { + deregion--; + if (deregion == 0) { + in_region = -1; + } + } + + if (deregion != 0) { + if (color != prev_color) { + prev_color = color; + highlighter_info.color = color; + color_map[j] = highlighter_info; + } + continue; + } + + color = font_color; + + bool is_char = _is_text_char(str[j]); + bool is_symbol = _is_symbol(str[j]); + bool is_number = _is_number(str[j]); + + // allow ABCDEF in hex notation + if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) { + is_number = true; + } else { + is_hex_notation = false; + } + + // check for dot or underscore or 'x' for hex notation in floating point number + if ((str[j] == '.' || str[j] == 'x' || str[j] == '_') && !in_word && prev_is_number && !is_number) { + is_number = true; + is_symbol = false; + is_char = false; + + if (str[j] == 'x' && str[j - 1] == '0') { + is_hex_notation = true; + } + } + + if (!in_word && _is_char(str[j]) && !is_number) { + in_word = true; + } + + if ((in_keyword || in_word) && !is_hex_notation) { + is_number = false; + } + + if (is_symbol && str[j] != '.' && in_word) { + in_word = false; + } + + if (is_symbol && cri_map.has(j)) { + const TextEdit::Text::ColorRegionInfo &cri = cri_map[j]; + + if (in_region == -1) { + if (!cri.end) { + in_region = cri.region; + } + } else { + TextEdit::ColorRegion cr = text_editor->_get_color_region(cri.region); + if (in_region == cri.region && !cr.line_only) { //ignore otherwise + if (cri.end || cr.eq) { + deregion = cr.eq ? cr.begin_key.length() : cr.end_key.length(); + } + } + } + } + + if (!is_char) { + in_keyword = false; + } + + if (in_region == -1 && !in_keyword && is_char && !prev_is_char) { + + int to = j; + while (to < str.length() && _is_text_char(str[to])) + to++; + + String word = str.substr(j, to - j); + Color col = Color(); + if (text_editor->has_keyword_color(word)) { + col = text_editor->get_keyword_color(word); + } else if (text_editor->has_member_color(word)) { + col = text_editor->get_member_color(word); + for (int k = j - 1; k >= 0; k--) { + if (str[k] == '.') { + col = Color(); //member indexing not allowed + break; + } else if (str[k] > 32) { + break; + } + } + } + + if (col != Color()) { + in_keyword = true; + keyword_color = col; + } + } + + if (!in_function_name && in_word && !in_keyword) { + + int k = j; + while (k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') { + k++; + } + + // check for space between name and bracket + while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) { + k++; + } + + if (str[k] == '(') { + in_function_name = true; + } + } + + if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) { + int k = j; + while (k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') { + k--; + } + + if (str[k] == '.') { + in_member_variable = true; + } + } + + if (is_symbol) { + in_function_name = false; + in_member_variable = false; + } + + if (in_region >= 0) + color = text_editor->_get_color_region(in_region).color; + else if (in_keyword) + color = keyword_color; + else if (in_member_variable) + color = member_color; + else if (in_function_name) + color = function_color; + else if (is_symbol) + color = symbol_color; + else if (is_number) + color = number_color; + + prev_is_char = is_char; + prev_is_number = is_number; + + if (color != prev_color) { + prev_color = color; + highlighter_info.color = color; + color_map[j] = highlighter_info; + } + } + return color_map; +} + +String GDScriptSyntaxHighlighter::get_name() { + return "GDScript"; +} + +List GDScriptSyntaxHighlighter::get_supported_languages() { + List languages; + languages.push_back("GDScript"); + return languages; +} + +void GDScriptSyntaxHighlighter::_update_cache() { + font_color = text_editor->get_color("font_color"); + symbol_color = text_editor->get_color("symbol_color"); + function_color = text_editor->get_color("function_color"); + number_color = text_editor->get_color("number_color"); + member_color = text_editor->get_color("member_variable_color"); +} + +SyntaxHighlighter *GDScriptSyntaxHighlighter::create() { + return memnew(GDScriptSyntaxHighlighter); +} -- cgit v1.2.3-70-g09d2 From 28dfc7f915fab258d38cb6081ea0f1611b87da68 Mon Sep 17 00:00:00 2001 From: Paulb23 Date: Thu, 12 Apr 2018 21:46:10 +0100 Subject: GDScript highlighter now remembers previous state --- modules/gdscript/editor/gdscript_highlighter.cpp | 51 +++++++++++++++++++++--- modules/gdscript/editor/gdscript_highlighter.h | 11 +++++ 2 files changed, 56 insertions(+), 6 deletions(-) (limited to 'modules/gdscript/editor/gdscript_highlighter.cpp') diff --git a/modules/gdscript/editor/gdscript_highlighter.cpp b/modules/gdscript/editor/gdscript_highlighter.cpp index 4e89851bf..ac65b5bcb 100644 --- a/modules/gdscript/editor/gdscript_highlighter.cpp +++ b/modules/gdscript/editor/gdscript_highlighter.cpp @@ -61,6 +61,13 @@ static bool _is_hex_symbol(CharType c) { Map GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) { Map color_map; + Type next_type = NONE; + Type current_type = NONE; + Type previous_type = NONE; + + String previous_text = ""; + int previous_column = 0; + bool prev_is_char = false; bool prev_is_number = false; bool in_keyword = false; @@ -214,18 +221,50 @@ Map GDScriptSyntaxHighlighter::_get_line_syntax_ in_member_variable = false; } - if (in_region >= 0) + if (in_region >= 0) { + next_type = REGION; color = text_editor->_get_color_region(in_region).color; - else if (in_keyword) + } else if (in_keyword) { + next_type = KEYWORD; color = keyword_color; - else if (in_member_variable) + } else if (in_member_variable) { + next_type = MEMBER; color = member_color; - else if (in_function_name) + } else if (in_function_name) { + next_type = FUNCTION; color = function_color; - else if (is_symbol) + } else if (is_symbol) { + next_type = SYMBOL; color = symbol_color; - else if (is_number) + } else if (is_number) { + next_type = NUMBER; color = number_color; + } else { + next_type = IDENTIFIER; + } + + if (next_type != current_type) { + if (current_type == NONE) { + current_type = next_type; + } else { + previous_type = current_type; + current_type = next_type; + + // no need to store regions... + if (previous_type == REGION) { + previous_text = ""; + previous_column = j; + } else { + String text = str.substr(previous_column, j - previous_column).strip_edges(); + previous_column = j; + + // ignore if just whitespace + if (text != "") { + previous_text = text; + } + } + } + } prev_is_char = is_char; prev_is_number = is_number; diff --git a/modules/gdscript/editor/gdscript_highlighter.h b/modules/gdscript/editor/gdscript_highlighter.h index ef1bdd410..91c913be8 100644 --- a/modules/gdscript/editor/gdscript_highlighter.h +++ b/modules/gdscript/editor/gdscript_highlighter.h @@ -35,6 +35,17 @@ class GDScriptSyntaxHighlighter : public SyntaxHighlighter { private: + enum Type { + NONE, + REGION, + SYMBOL, + NUMBER, + FUNCTION, + KEYWORD, + MEMBER, + IDENTIFIER + }; + // colours Color font_color; Color symbol_color; -- cgit v1.2.3-70-g09d2 From 4cd16f6ba90c0dce40672f550f403fb76a74a940 Mon Sep 17 00:00:00 2001 From: Paulb23 Date: Thu, 12 Apr 2018 22:26:15 +0100 Subject: Added GDScript function definition highlighting --- editor/editor_settings.cpp | 6 ++++++ editor/editor_themes.cpp | 6 ++++++ modules/gdscript/editor/gdscript_highlighter.cpp | 11 ++++++++++- modules/gdscript/editor/gdscript_highlighter.h | 1 + 4 files changed, 23 insertions(+), 1 deletion(-) (limited to 'modules/gdscript/editor/gdscript_highlighter.cpp') diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 85f6d99c6..15e8348b6 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -583,6 +583,9 @@ void EditorSettings::_load_default_text_editor_theme() { _initial_set("text_editor/highlighting/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15)); _initial_set("text_editor/highlighting/search_result_color", Color(0.05, 0.25, 0.05, 1)); _initial_set("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1)); + + // GDScript highlighter + _initial_set("text_editor/highlighting/gdscript/function_definition_color", Color::html("#01e1ff")); } bool EditorSettings::_save_text_editor_theme(String p_file) { @@ -619,6 +622,9 @@ bool EditorSettings::_save_text_editor_theme(String p_file) { cf->set_value(theme_section, "search_result_color", ((Color)get("text_editor/highlighting/search_result_color")).to_html()); cf->set_value(theme_section, "search_result_border_color", ((Color)get("text_editor/highlighting/search_result_border_color")).to_html()); + //GDScript highlighter + cf->set_value(theme_section, "gdscript/function_definition_color", ((Color)get("text_editor/highlighting/gdscript/function_definition_color")).to_html()); + Error err = cf->save(p_file); if (err == OK) { diff --git a/editor/editor_themes.cpp b/editor/editor_themes.cpp index 3582379e3..4bd7519ec 100644 --- a/editor/editor_themes.cpp +++ b/editor/editor_themes.cpp @@ -1039,6 +1039,8 @@ Ref create_editor_theme(const Ref p_theme) { const Color comment_color = dim_color; const Color string_color = Color::html(dark_theme ? "#ffd942" : "#ffd118").linear_interpolate(mono_color, dark_theme ? 0.5 : 0.3); + const Color function_definition_color = Color::html(dark_theme ? "#01e1ff" : "#00a5ba"); + const Color te_background_color = Color(0, 0, 0, 0); const Color completion_background_color = base_color; const Color completion_selected_color = alpha1; @@ -1097,6 +1099,8 @@ Ref create_editor_theme(const Ref p_theme) { setting->set_initial_value("text_editor/highlighting/code_folding_color", code_folding_color, true); setting->set_initial_value("text_editor/highlighting/search_result_color", search_result_color, true); setting->set_initial_value("text_editor/highlighting/search_result_border_color", search_result_border_color, true); + + setting->set_initial_value("text_editor/highlighting/gdscript/function_definition_color", function_definition_color, true); } else if (text_editor_color_theme == "Default") { setting->set_initial_value("text_editor/highlighting/symbol_color", Color::html("badfff"), true); setting->set_initial_value("text_editor/highlighting/keyword_color", Color::html("ffffb3"), true); @@ -1128,6 +1132,8 @@ Ref create_editor_theme(const Ref p_theme) { setting->set_initial_value("text_editor/highlighting/code_folding_color", Color(0.8, 0.8, 0.8, 0.8), true); setting->set_initial_value("text_editor/highlighting/search_result_color", Color(0.05, 0.25, 0.05, 1), true); setting->set_initial_value("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1), true); + + setting->set_initial_value("text_editor/highlighting/gdscript/function_definition_color", Color::html("#01e1ff"), true); } return theme; diff --git a/modules/gdscript/editor/gdscript_highlighter.cpp b/modules/gdscript/editor/gdscript_highlighter.cpp index ac65b5bcb..9e9e3df0e 100644 --- a/modules/gdscript/editor/gdscript_highlighter.cpp +++ b/modules/gdscript/editor/gdscript_highlighter.cpp @@ -29,7 +29,9 @@ /*************************************************************************/ #include "gdscript_highlighter.h" +#include "../gdscript_tokenizer.h" #include "scene/gui/text_edit.h" +#include "editor/editor_settings.h" inline bool _is_symbol(CharType c) { @@ -232,7 +234,12 @@ Map GDScriptSyntaxHighlighter::_get_line_syntax_ color = member_color; } else if (in_function_name) { next_type = FUNCTION; - color = function_color; + + if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::TK_PR_FUNCTION)) { + color = function_definition_color; + } else { + color = function_color; + } } else if (is_symbol) { next_type = SYMBOL; color = symbol_color; @@ -294,6 +301,8 @@ void GDScriptSyntaxHighlighter::_update_cache() { function_color = text_editor->get_color("function_color"); number_color = text_editor->get_color("number_color"); member_color = text_editor->get_color("member_variable_color"); + + function_definition_color = EDITOR_DEF("text_editor/highlighting/gdscript/function_definition_color", Color::html("#01e1ff")); } SyntaxHighlighter *GDScriptSyntaxHighlighter::create() { diff --git a/modules/gdscript/editor/gdscript_highlighter.h b/modules/gdscript/editor/gdscript_highlighter.h index 91c913be8..218002173 100644 --- a/modules/gdscript/editor/gdscript_highlighter.h +++ b/modules/gdscript/editor/gdscript_highlighter.h @@ -50,6 +50,7 @@ private: Color font_color; Color symbol_color; Color function_color; + Color function_definition_color; Color built_in_type_color; Color number_color; Color member_color; -- cgit v1.2.3-70-g09d2 From adeed584776a7b91d2de6dd5cbb43fe3e35114e1 Mon Sep 17 00:00:00 2001 From: Paulb23 Date: Thu, 12 Apr 2018 23:49:44 +0100 Subject: Added GDScript NodePath highlighting --- editor/editor_settings.cpp | 2 ++ editor/editor_themes.cpp | 3 +++ modules/gdscript/editor/gdscript_highlighter.cpp | 13 ++++++++++++- modules/gdscript/editor/gdscript_highlighter.h | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-) (limited to 'modules/gdscript/editor/gdscript_highlighter.cpp') diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 15e8348b6..a46353dbc 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -586,6 +586,7 @@ void EditorSettings::_load_default_text_editor_theme() { // GDScript highlighter _initial_set("text_editor/highlighting/gdscript/function_definition_color", Color::html("#01e1ff")); + _initial_set("text_editor/highlighting/gdscript/node_path_color", Color::html("#64c15a")); } bool EditorSettings::_save_text_editor_theme(String p_file) { @@ -624,6 +625,7 @@ bool EditorSettings::_save_text_editor_theme(String p_file) { //GDScript highlighter cf->set_value(theme_section, "gdscript/function_definition_color", ((Color)get("text_editor/highlighting/gdscript/function_definition_color")).to_html()); + cf->set_value(theme_section, "gdscript/node_path_color", ((Color)get("text_editor/highlighting/gdscript/node_path_color")).to_html()); Error err = cf->save(p_file); diff --git a/editor/editor_themes.cpp b/editor/editor_themes.cpp index 4bd7519ec..8d99539a0 100644 --- a/editor/editor_themes.cpp +++ b/editor/editor_themes.cpp @@ -1040,6 +1040,7 @@ Ref create_editor_theme(const Ref p_theme) { const Color string_color = Color::html(dark_theme ? "#ffd942" : "#ffd118").linear_interpolate(mono_color, dark_theme ? 0.5 : 0.3); const Color function_definition_color = Color::html(dark_theme ? "#01e1ff" : "#00a5ba"); + const Color node_path_color = Color::html(dark_theme ? "64c15a" : "#518b4b"); const Color te_background_color = Color(0, 0, 0, 0); const Color completion_background_color = base_color; @@ -1101,6 +1102,7 @@ Ref create_editor_theme(const Ref p_theme) { setting->set_initial_value("text_editor/highlighting/search_result_border_color", search_result_border_color, true); setting->set_initial_value("text_editor/highlighting/gdscript/function_definition_color", function_definition_color, true); + setting->set_initial_value("text_editor/highlighting/gdscript/node_path_color", node_path_color, true); } else if (text_editor_color_theme == "Default") { setting->set_initial_value("text_editor/highlighting/symbol_color", Color::html("badfff"), true); setting->set_initial_value("text_editor/highlighting/keyword_color", Color::html("ffffb3"), true); @@ -1134,6 +1136,7 @@ Ref create_editor_theme(const Ref p_theme) { setting->set_initial_value("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1), true); setting->set_initial_value("text_editor/highlighting/gdscript/function_definition_color", Color::html("#01e1ff"), true); + setting->set_initial_value("text_editor/highlighting/gdscript/node_path_color", Color::html("#64c15a"), true); } return theme; diff --git a/modules/gdscript/editor/gdscript_highlighter.cpp b/modules/gdscript/editor/gdscript_highlighter.cpp index 9e9e3df0e..ea3efff9c 100644 --- a/modules/gdscript/editor/gdscript_highlighter.cpp +++ b/modules/gdscript/editor/gdscript_highlighter.cpp @@ -30,8 +30,8 @@ #include "gdscript_highlighter.h" #include "../gdscript_tokenizer.h" -#include "scene/gui/text_edit.h" #include "editor/editor_settings.h" +#include "scene/gui/text_edit.h" inline bool _is_symbol(CharType c) { @@ -76,6 +76,7 @@ Map GDScriptSyntaxHighlighter::_get_line_syntax_ bool in_word = false; bool in_function_name = false; bool in_member_variable = false; + bool in_node_path = false; bool is_hex_notation = false; Color keyword_color; Color color; @@ -223,9 +224,18 @@ Map GDScriptSyntaxHighlighter::_get_line_syntax_ in_member_variable = false; } + if (!in_node_path && in_region == -1 && str[j] == '$') { + in_node_path = true; + } else if (in_region != -1 || (is_symbol && str[j] != '/')) { + in_node_path = false; + } + if (in_region >= 0) { next_type = REGION; color = text_editor->_get_color_region(in_region).color; + } else if (in_node_path) { + next_type = NODE_PATH; + color = node_path_color; } else if (in_keyword) { next_type = KEYWORD; color = keyword_color; @@ -303,6 +313,7 @@ void GDScriptSyntaxHighlighter::_update_cache() { member_color = text_editor->get_color("member_variable_color"); function_definition_color = EDITOR_DEF("text_editor/highlighting/gdscript/function_definition_color", Color::html("#01e1ff")); + node_path_color = EDITOR_DEF("text_editor/highlighting/gdscript/node_path_color", Color::html("#64c15a")); } SyntaxHighlighter *GDScriptSyntaxHighlighter::create() { diff --git a/modules/gdscript/editor/gdscript_highlighter.h b/modules/gdscript/editor/gdscript_highlighter.h index 218002173..0296ab765 100644 --- a/modules/gdscript/editor/gdscript_highlighter.h +++ b/modules/gdscript/editor/gdscript_highlighter.h @@ -38,6 +38,7 @@ private: enum Type { NONE, REGION, + NODE_PATH, SYMBOL, NUMBER, FUNCTION, @@ -54,6 +55,7 @@ private: Color built_in_type_color; Color number_color; Color member_color; + Color node_path_color; public: static SyntaxHighlighter *create(); -- cgit v1.2.3-70-g09d2