2018-04-02 13:41:44 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gdscript_highlighter.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2019-01-01 12:53:14 +01:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2018-04-02 13:41:44 +02:00
|
|
|
/* */
|
|
|
|
/* 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"
|
2018-04-12 23:26:15 +02:00
|
|
|
#include "../gdscript_tokenizer.h"
|
|
|
|
#include "editor/editor_settings.h"
|
2018-04-13 00:49:44 +02:00
|
|
|
#include "scene/gui/text_edit.h"
|
2018-04-02 13:41:44 +02:00
|
|
|
|
|
|
|
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_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'));
|
|
|
|
}
|
|
|
|
|
2019-04-25 14:43:48 +02:00
|
|
|
static bool _is_bin_symbol(CharType c) {
|
|
|
|
return (c == '0' || c == '1');
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:02:42 +02:00
|
|
|
Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
|
2018-04-02 13:41:44 +02:00
|
|
|
Map<int, TextEdit::HighlighterInfo> color_map;
|
|
|
|
|
2018-04-12 22:46:10 +02:00
|
|
|
Type next_type = NONE;
|
|
|
|
Type current_type = NONE;
|
|
|
|
Type previous_type = NONE;
|
|
|
|
|
|
|
|
String previous_text = "";
|
|
|
|
int previous_column = 0;
|
|
|
|
|
2018-04-02 13:41:44 +02:00
|
|
|
bool prev_is_char = false;
|
|
|
|
bool prev_is_number = false;
|
|
|
|
bool in_keyword = false;
|
|
|
|
bool in_word = false;
|
|
|
|
bool in_function_name = false;
|
2018-05-30 04:16:58 +02:00
|
|
|
bool in_variable_declaration = false;
|
2018-10-13 18:49:49 +02:00
|
|
|
bool in_function_args = false;
|
2018-04-02 13:41:44 +02:00
|
|
|
bool in_member_variable = false;
|
2018-04-13 00:49:44 +02:00
|
|
|
bool in_node_path = false;
|
2018-04-02 13:41:44 +02:00
|
|
|
bool is_hex_notation = false;
|
2019-04-25 14:43:48 +02:00
|
|
|
bool is_bin_notation = false;
|
2018-05-30 04:16:58 +02:00
|
|
|
bool expect_type = false;
|
2018-04-02 13:41:44 +02:00
|
|
|
Color keyword_color;
|
|
|
|
Color color;
|
|
|
|
|
2018-04-07 15:14:19 +02:00
|
|
|
int in_region = text_editor->_is_line_in_region(p_line);
|
2018-04-02 13:41:44 +02:00
|
|
|
int deregion = 0;
|
|
|
|
|
|
|
|
const Map<int, TextEdit::Text::ColorRegionInfo> 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;
|
|
|
|
}
|
|
|
|
|
2019-04-25 14:43:48 +02:00
|
|
|
// disallow anything not a 0 or 1
|
|
|
|
if (is_bin_notation && (_is_bin_symbol(str[j]))) {
|
|
|
|
is_number = true;
|
|
|
|
} else if (is_bin_notation) {
|
|
|
|
is_bin_notation = false;
|
|
|
|
is_number = false;
|
|
|
|
} else {
|
|
|
|
is_bin_notation = false;
|
|
|
|
}
|
|
|
|
|
2018-09-08 14:41:46 +02:00
|
|
|
// check for dot or underscore or 'x' for hex notation in floating point number or 'e' for scientific notation
|
2019-04-25 14:43:48 +02:00
|
|
|
if ((str[j] == '.' || str[j] == 'x' || str[j] == 'b' || str[j] == '_' || str[j] == 'e') && !in_word && prev_is_number && !is_number) {
|
2018-04-02 13:41:44 +02:00
|
|
|
is_number = true;
|
|
|
|
is_symbol = false;
|
|
|
|
is_char = false;
|
|
|
|
|
|
|
|
if (str[j] == 'x' && str[j - 1] == '0') {
|
|
|
|
is_hex_notation = true;
|
2019-04-25 14:43:48 +02:00
|
|
|
} else if (str[j] == 'b' && str[j - 1] == '0') {
|
|
|
|
is_bin_notation = true;
|
2018-04-02 13:41:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-05-30 04:16:58 +02:00
|
|
|
} else if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::TK_PR_VAR)) {
|
|
|
|
in_variable_declaration = true;
|
2018-04-02 13:41:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-05-30 04:16:58 +02:00
|
|
|
|
2018-10-13 18:49:49 +02:00
|
|
|
if (in_function_name) {
|
|
|
|
in_function_args = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_function_args && str[j] == ')') {
|
|
|
|
in_function_args = false;
|
|
|
|
}
|
|
|
|
|
2019-09-28 12:11:06 +02:00
|
|
|
if (expect_type && (prev_is_char || str[j] == '=')) {
|
2018-05-30 04:16:58 +02:00
|
|
|
expect_type = false;
|
|
|
|
}
|
2018-10-13 18:49:49 +02:00
|
|
|
|
2018-05-30 04:16:58 +02:00
|
|
|
if (j > 0 && str[j] == '>' && str[j - 1] == '-') {
|
|
|
|
expect_type = true;
|
|
|
|
}
|
|
|
|
|
2018-10-13 18:49:49 +02:00
|
|
|
if (in_variable_declaration || in_function_args) {
|
2018-05-30 04:16:58 +02:00
|
|
|
int k = j;
|
|
|
|
// Skip space
|
|
|
|
while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str[k] == ':') {
|
|
|
|
// has type hint
|
|
|
|
expect_type = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
in_variable_declaration = false;
|
2018-10-13 18:49:49 +02:00
|
|
|
in_function_name = false;
|
|
|
|
in_member_variable = false;
|
2018-04-02 13:41:44 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 00:49:44 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-12 22:46:10 +02:00
|
|
|
if (in_region >= 0) {
|
|
|
|
next_type = REGION;
|
2018-04-02 13:41:44 +02:00
|
|
|
color = text_editor->_get_color_region(in_region).color;
|
2018-04-13 00:49:44 +02:00
|
|
|
} else if (in_node_path) {
|
|
|
|
next_type = NODE_PATH;
|
|
|
|
color = node_path_color;
|
2018-04-12 22:46:10 +02:00
|
|
|
} else if (in_keyword) {
|
|
|
|
next_type = KEYWORD;
|
2018-04-02 13:41:44 +02:00
|
|
|
color = keyword_color;
|
2018-04-12 22:46:10 +02:00
|
|
|
} else if (in_member_variable) {
|
|
|
|
next_type = MEMBER;
|
2018-04-02 13:41:44 +02:00
|
|
|
color = member_color;
|
2018-04-12 22:46:10 +02:00
|
|
|
} else if (in_function_name) {
|
|
|
|
next_type = FUNCTION;
|
2018-04-12 23:26:15 +02:00
|
|
|
|
|
|
|
if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::TK_PR_FUNCTION)) {
|
|
|
|
color = function_definition_color;
|
|
|
|
} else {
|
|
|
|
color = function_color;
|
|
|
|
}
|
2018-04-12 22:46:10 +02:00
|
|
|
} else if (is_symbol) {
|
|
|
|
next_type = SYMBOL;
|
2018-04-02 13:41:44 +02:00
|
|
|
color = symbol_color;
|
2018-04-12 22:46:10 +02:00
|
|
|
} else if (is_number) {
|
|
|
|
next_type = NUMBER;
|
2018-04-02 13:41:44 +02:00
|
|
|
color = number_color;
|
2018-05-30 04:16:58 +02:00
|
|
|
} else if (expect_type) {
|
|
|
|
next_type = TYPE;
|
|
|
|
color = type_color;
|
2018-04-12 22:46:10 +02:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 13:41:44 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-20 19:36:24 +02:00
|
|
|
String GDScriptSyntaxHighlighter::get_name() const {
|
2018-04-02 13:41:44 +02:00
|
|
|
return "GDScript";
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:02:42 +02:00
|
|
|
List<String> GDScriptSyntaxHighlighter::get_supported_languages() {
|
2018-04-02 13:41:44 +02:00
|
|
|
List<String> languages;
|
|
|
|
languages.push_back("GDScript");
|
|
|
|
return languages;
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:02:42 +02:00
|
|
|
void GDScriptSyntaxHighlighter::_update_cache() {
|
2018-04-02 13:41:44 +02:00
|
|
|
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");
|
2018-04-12 23:26:15 +02:00
|
|
|
|
2018-06-10 16:30:49 +02:00
|
|
|
EditorSettings *settings = EditorSettings::get_singleton();
|
|
|
|
String text_editor_color_theme = settings->get("text_editor/theme/color_theme");
|
|
|
|
|
|
|
|
bool default_theme = text_editor_color_theme == "Default";
|
|
|
|
bool dark_theme = settings->is_dark_theme();
|
|
|
|
|
2019-07-08 20:03:06 +02:00
|
|
|
function_definition_color = default_theme ? Color(0.0, 0.88, 1.0) : dark_theme ? Color(0.0, 0.88, 1.0) : Color(0.0, 0.65, 0.73);
|
|
|
|
node_path_color = default_theme ? Color(0.39, 0.76, 0.35) : dark_theme ? Color(0.39, 0.76, 0.35) : Color(0.32, 0.55, 0.29);
|
2018-06-10 16:30:49 +02:00
|
|
|
|
|
|
|
EDITOR_DEF("text_editor/highlighting/gdscript/function_definition_color", function_definition_color);
|
|
|
|
EDITOR_DEF("text_editor/highlighting/gdscript/node_path_color", node_path_color);
|
|
|
|
if (text_editor_color_theme == "Adaptive" || default_theme) {
|
|
|
|
settings->set_initial_value("text_editor/highlighting/gdscript/function_definition_color", function_definition_color, true);
|
|
|
|
settings->set_initial_value("text_editor/highlighting/gdscript/node_path_color", node_path_color, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function_definition_color = EDITOR_GET("text_editor/highlighting/gdscript/function_definition_color");
|
|
|
|
node_path_color = EDITOR_GET("text_editor/highlighting/gdscript/node_path_color");
|
2018-05-30 04:16:58 +02:00
|
|
|
type_color = EDITOR_GET("text_editor/highlighting/base_type_color");
|
2018-04-02 13:41:44 +02:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:02:42 +02:00
|
|
|
SyntaxHighlighter *GDScriptSyntaxHighlighter::create() {
|
|
|
|
return memnew(GDScriptSyntaxHighlighter);
|
2018-04-02 13:41:44 +02:00
|
|
|
}
|