2018-04-02 13:41:44 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gdscript_highlighter.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 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"
|
2020-05-03 18:08:15 +02:00
|
|
|
#include "../gdscript.h"
|
2018-04-12 23:26:15 +02:00
|
|
|
#include "../gdscript_tokenizer.h"
|
|
|
|
#include "editor/editor_settings.h"
|
2018-04-02 13:41:44 +02:00
|
|
|
|
2020-07-27 12:43:20 +02:00
|
|
|
static bool _is_char(char32_t c) {
|
2018-04-02 13:41:44 +02:00
|
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
|
|
|
|
}
|
|
|
|
|
2020-07-27 12:43:20 +02:00
|
|
|
static bool _is_hex_symbol(char32_t c) {
|
2018-04-02 13:41:44 +02:00
|
|
|
return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
|
|
|
|
}
|
|
|
|
|
2020-07-27 12:43:20 +02:00
|
|
|
static bool _is_bin_symbol(char32_t c) {
|
2019-04-25 14:43:48 +02:00
|
|
|
return (c == '0' || c == '1');
|
|
|
|
}
|
|
|
|
|
2020-03-07 12:17:18 +01:00
|
|
|
Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
|
|
|
|
Dictionary color_map;
|
2018-04-02 13:41:44 +02:00
|
|
|
|
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;
|
2021-06-06 21:19:59 +02:00
|
|
|
bool in_annotation = 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;
|
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
color_region_cache[p_line] = -1;
|
|
|
|
int in_region = -1;
|
|
|
|
if (p_line != 0) {
|
2020-10-03 15:56:13 +02:00
|
|
|
int prev_region_line = p_line - 1;
|
|
|
|
while (prev_region_line > 0 && !color_region_cache.has(prev_region_line)) {
|
|
|
|
prev_region_line--;
|
|
|
|
}
|
|
|
|
for (int i = prev_region_line; i < p_line - 1; i++) {
|
|
|
|
get_line_syntax_highlighting(i);
|
|
|
|
}
|
2020-05-03 18:08:15 +02:00
|
|
|
if (!color_region_cache.has(p_line - 1)) {
|
|
|
|
get_line_syntax_highlighting(p_line - 1);
|
|
|
|
}
|
|
|
|
in_region = color_region_cache[p_line - 1];
|
|
|
|
}
|
2018-04-02 13:41:44 +02:00
|
|
|
|
2020-03-07 12:17:18 +01:00
|
|
|
const String &str = text_edit->get_line(p_line);
|
2020-05-03 18:08:15 +02:00
|
|
|
const int line_length = str.length();
|
2018-04-02 13:41:44 +02:00
|
|
|
Color prev_color;
|
2020-08-08 16:36:10 +02:00
|
|
|
|
|
|
|
if (in_region != -1 && str.length() == 0) {
|
|
|
|
color_region_cache[p_line] = in_region;
|
|
|
|
}
|
2018-04-02 13:41:44 +02:00
|
|
|
for (int j = 0; j < str.length(); j++) {
|
2020-03-07 12:17:18 +01:00
|
|
|
Dictionary highlighter_info;
|
2018-04-02 13:41:44 +02:00
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
color = font_color;
|
|
|
|
bool is_char = !is_symbol(str[j]);
|
|
|
|
bool is_a_symbol = is_symbol(str[j]);
|
|
|
|
bool is_number = (str[j] >= '0' && str[j] <= '9');
|
|
|
|
|
|
|
|
/* color regions */
|
|
|
|
if (is_a_symbol || in_region != -1) {
|
|
|
|
int from = j;
|
|
|
|
for (; from < line_length; from++) {
|
|
|
|
if (str[from] == '\\') {
|
|
|
|
from++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2018-04-02 13:41:44 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
if (from != line_length) {
|
|
|
|
/* check if we are in entering a region */
|
|
|
|
if (in_region == -1) {
|
|
|
|
for (int c = 0; c < color_regions.size(); c++) {
|
|
|
|
/* check there is enough room */
|
|
|
|
int chars_left = line_length - from;
|
|
|
|
int start_key_length = color_regions[c].start_key.length();
|
|
|
|
int end_key_length = color_regions[c].end_key.length();
|
|
|
|
if (chars_left < start_key_length) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* search the line */
|
|
|
|
bool match = true;
|
2020-07-27 12:43:20 +02:00
|
|
|
const char32_t *start_key = color_regions[c].start_key.get_data();
|
2020-05-03 18:08:15 +02:00
|
|
|
for (int k = 0; k < start_key_length; k++) {
|
|
|
|
if (start_key[k] != str[from + k]) {
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!match) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
in_region = c;
|
|
|
|
from += start_key_length;
|
|
|
|
|
|
|
|
/* check if it's the whole line */
|
|
|
|
if (end_key_length == 0 || color_regions[c].line_only || from + end_key_length > line_length) {
|
|
|
|
prev_color = color_regions[in_region].color;
|
|
|
|
highlighter_info["color"] = color_regions[c].color;
|
|
|
|
color_map[j] = highlighter_info;
|
|
|
|
|
|
|
|
j = line_length;
|
|
|
|
if (!color_regions[c].line_only) {
|
|
|
|
color_region_cache[p_line] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-04-02 13:41:44 +02:00
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
if (j == line_length) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 13:41:44 +02:00
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
/* if we are in one find the end key */
|
|
|
|
if (in_region != -1) {
|
|
|
|
/* search the line */
|
|
|
|
int region_end_index = -1;
|
2020-08-22 20:55:44 +02:00
|
|
|
int end_key_length = color_regions[in_region].end_key.length();
|
2020-07-27 12:43:20 +02:00
|
|
|
const char32_t *end_key = color_regions[in_region].end_key.get_data();
|
2020-05-03 18:08:15 +02:00
|
|
|
for (; from < line_length; from++) {
|
2020-08-22 20:55:44 +02:00
|
|
|
if (line_length - from < end_key_length) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_symbol(str[from])) {
|
2020-05-03 18:08:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str[from] == '\\') {
|
|
|
|
from++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-22 20:55:44 +02:00
|
|
|
region_end_index = from;
|
2020-05-03 18:08:15 +02:00
|
|
|
for (int k = 0; k < end_key_length; k++) {
|
2020-08-22 20:55:44 +02:00
|
|
|
if (end_key[k] != str[from + k]) {
|
|
|
|
region_end_index = -1;
|
2020-05-03 18:08:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (region_end_index != -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_color = color_regions[in_region].color;
|
|
|
|
highlighter_info["color"] = color_regions[in_region].color;
|
|
|
|
color_map[j] = highlighter_info;
|
|
|
|
|
|
|
|
previous_type = REGION;
|
|
|
|
previous_text = "";
|
|
|
|
previous_column = j;
|
2020-08-22 20:55:44 +02:00
|
|
|
j = from + (end_key_length - 1);
|
2020-05-03 18:08:15 +02:00
|
|
|
if (region_end_index == -1) {
|
|
|
|
color_region_cache[p_line] = in_region;
|
|
|
|
}
|
|
|
|
|
|
|
|
in_region = -1;
|
|
|
|
prev_is_char = false;
|
|
|
|
prev_is_number = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 13:41:44 +02:00
|
|
|
|
|
|
|
// 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;
|
2020-05-03 18:08:15 +02:00
|
|
|
is_a_symbol = false;
|
2018-04-02 13:41:44 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
if (is_a_symbol && str[j] != '.' && in_word) {
|
2018-04-02 13:41:44 +02:00
|
|
|
in_word = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_char) {
|
|
|
|
in_keyword = false;
|
|
|
|
}
|
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
if (!in_keyword && is_char && !prev_is_char) {
|
2018-04-02 13:41:44 +02:00
|
|
|
int to = j;
|
2020-05-03 18:08:15 +02:00
|
|
|
while (to < str.length() && !is_symbol(str[to])) {
|
2018-04-02 13:41:44 +02:00
|
|
|
to++;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-04-02 13:41:44 +02:00
|
|
|
|
|
|
|
String word = str.substr(j, to - j);
|
|
|
|
Color col = Color();
|
2020-05-03 18:08:15 +02:00
|
|
|
if (keywords.has(word)) {
|
|
|
|
col = keywords[word];
|
|
|
|
} else if (member_keywords.has(word)) {
|
|
|
|
col = member_keywords[word];
|
2021-01-26 14:58:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (col != Color()) {
|
2018-04-02 13:41:44 +02:00
|
|
|
for (int k = j - 1; k >= 0; k--) {
|
|
|
|
if (str[k] == '.') {
|
2021-01-26 14:58:28 +01:00
|
|
|
col = Color(); // keyword & member indexing not allowed
|
2018-04-02 13:41:44 +02:00
|
|
|
break;
|
|
|
|
} else if (str[k] > 32) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 14:58:28 +01:00
|
|
|
if (col != Color()) {
|
|
|
|
in_keyword = true;
|
|
|
|
keyword_color = col;
|
|
|
|
}
|
2018-04-02 13:41:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_function_name && in_word && !in_keyword) {
|
|
|
|
int k = j;
|
2020-05-03 18:08:15 +02:00
|
|
|
while (k < str.length() && !is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
|
2018-04-02 13:41:44 +02:00
|
|
|
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;
|
2020-05-02 00:14:56 +02:00
|
|
|
} else if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::VAR)) {
|
2018-05-30 04:16:58 +02:00
|
|
|
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;
|
2020-05-03 18:08:15 +02:00
|
|
|
while (k > 0 && !is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
|
2018-04-02 13:41:44 +02:00
|
|
|
k--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str[k] == '.') {
|
|
|
|
in_member_variable = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
if (is_a_symbol) {
|
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;
|
2020-05-03 18:08:15 +02:00
|
|
|
} else if (in_region != -1 || (is_a_symbol && str[j] != '/')) {
|
2018-04-13 00:49:44 +02:00
|
|
|
in_node_path = false;
|
|
|
|
}
|
|
|
|
|
2021-06-06 21:19:59 +02:00
|
|
|
if (!in_annotation && in_region == -1 && str[j] == '@') {
|
|
|
|
in_annotation = true;
|
|
|
|
} else if (in_region != -1 || is_a_symbol) {
|
|
|
|
in_annotation = false;
|
|
|
|
}
|
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
if (in_node_path) {
|
2018-04-13 00:49:44 +02:00
|
|
|
next_type = NODE_PATH;
|
|
|
|
color = node_path_color;
|
2021-06-06 21:19:59 +02:00
|
|
|
} else if (in_annotation) {
|
|
|
|
next_type = ANNOTATION;
|
|
|
|
color = annotation_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
|
|
|
|
2020-05-02 00:14:56 +02:00
|
|
|
if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
|
2018-04-12 23:26:15 +02:00
|
|
|
color = function_definition_color;
|
|
|
|
} else {
|
|
|
|
color = function_color;
|
|
|
|
}
|
2020-05-03 18:08:15 +02:00
|
|
|
} else if (is_a_symbol) {
|
2018-04-12 22:46:10 +02:00
|
|
|
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;
|
2020-03-07 12:17:18 +01:00
|
|
|
highlighter_info["color"] = color;
|
2018-04-02 13:41:44 +02:00
|
|
|
color_map[j] = highlighter_info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return color_map;
|
|
|
|
}
|
|
|
|
|
2020-03-07 12:17:18 +01:00
|
|
|
String GDScriptSyntaxHighlighter::_get_name() const {
|
2018-04-02 13:41:44 +02:00
|
|
|
return "GDScript";
|
|
|
|
}
|
|
|
|
|
2020-03-07 12:17:18 +01:00
|
|
|
Array GDScriptSyntaxHighlighter::_get_supported_languages() const {
|
|
|
|
Array languages;
|
2018-04-02 13:41:44 +02:00
|
|
|
languages.push_back("GDScript");
|
|
|
|
return languages;
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:02:42 +02:00
|
|
|
void GDScriptSyntaxHighlighter::_update_cache() {
|
2020-05-03 18:08:15 +02:00
|
|
|
keywords.clear();
|
|
|
|
member_keywords.clear();
|
|
|
|
color_regions.clear();
|
|
|
|
color_region_cache.clear();
|
|
|
|
|
2020-03-07 12:17:18 +01:00
|
|
|
font_color = text_edit->get_theme_color("font_color");
|
2020-05-03 18:08:15 +02:00
|
|
|
symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
|
|
|
|
function_color = EDITOR_GET("text_editor/highlighting/function_color");
|
|
|
|
number_color = EDITOR_GET("text_editor/highlighting/number_color");
|
|
|
|
member_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
|
|
|
|
|
|
|
|
/* Engine types. */
|
|
|
|
const Color types_color = EDITOR_GET("text_editor/highlighting/engine_type_color");
|
|
|
|
List<StringName> types;
|
|
|
|
ClassDB::get_class_list(&types);
|
|
|
|
for (List<StringName>::Element *E = types.front(); E; E = E->next()) {
|
|
|
|
String n = E->get();
|
|
|
|
if (n.begins_with("_")) {
|
|
|
|
n = n.substr(1, n.length());
|
|
|
|
}
|
|
|
|
keywords[n] = types_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* User types. */
|
|
|
|
const Color usertype_color = EDITOR_GET("text_editor/highlighting/user_type_color");
|
|
|
|
List<StringName> global_classes;
|
|
|
|
ScriptServer::get_global_class_list(&global_classes);
|
|
|
|
for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) {
|
|
|
|
keywords[String(E->get())] = usertype_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Autoloads. */
|
|
|
|
Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
|
|
|
|
for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) {
|
|
|
|
const ProjectSettings::AutoloadInfo &info = E->value();
|
|
|
|
if (info.is_singleton) {
|
|
|
|
keywords[info.name] = usertype_color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();
|
|
|
|
|
|
|
|
/* Core types. */
|
|
|
|
const Color basetype_color = EDITOR_GET("text_editor/highlighting/base_type_color");
|
|
|
|
List<String> core_types;
|
|
|
|
gdscript->get_core_type_words(&core_types);
|
|
|
|
for (List<String>::Element *E = core_types.front(); E; E = E->next()) {
|
|
|
|
keywords[E->get()] = basetype_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reserved words. */
|
|
|
|
const Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
|
2021-04-08 16:12:22 +02:00
|
|
|
const Color control_flow_keyword_color = EDITOR_GET("text_editor/highlighting/control_flow_keyword_color");
|
2020-05-03 18:08:15 +02:00
|
|
|
List<String> keyword_list;
|
|
|
|
gdscript->get_reserved_words(&keyword_list);
|
|
|
|
for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
|
2021-04-08 16:12:22 +02:00
|
|
|
if (gdscript->is_control_flow_keyword(E->get())) {
|
|
|
|
keywords[E->get()] = control_flow_keyword_color;
|
|
|
|
} else {
|
|
|
|
keywords[E->get()] = keyword_color;
|
|
|
|
}
|
2020-05-03 18:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Comments */
|
|
|
|
const Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
|
|
|
|
List<String> comments;
|
|
|
|
gdscript->get_comment_delimiters(&comments);
|
|
|
|
for (List<String>::Element *E = comments.front(); E; E = E->next()) {
|
|
|
|
String comment = E->get();
|
|
|
|
String beg = comment.get_slice(" ", 0);
|
|
|
|
String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
|
|
|
|
add_color_region(beg, end, comment_color, end == "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Strings */
|
|
|
|
const Color string_color = EDITOR_GET("text_editor/highlighting/string_color");
|
|
|
|
List<String> strings;
|
|
|
|
gdscript->get_string_delimiters(&strings);
|
|
|
|
for (List<String>::Element *E = strings.front(); E; E = E->next()) {
|
|
|
|
String string = E->get();
|
|
|
|
String beg = string.get_slice(" ", 0);
|
|
|
|
String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
|
|
|
|
add_color_region(beg, end, string_color, end == "");
|
|
|
|
}
|
|
|
|
|
|
|
|
const Ref<Script> script = _get_edited_resource();
|
|
|
|
if (script.is_valid()) {
|
|
|
|
/* Member types. */
|
|
|
|
const Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
|
|
|
|
StringName instance_base = script->get_instance_base_type();
|
|
|
|
if (instance_base != StringName()) {
|
|
|
|
List<PropertyInfo> plist;
|
|
|
|
ClassDB::get_property_list(instance_base, &plist);
|
|
|
|
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
|
|
|
|
String name = E->get().name;
|
|
|
|
if (E->get().usage & PROPERTY_USAGE_CATEGORY || E->get().usage & PROPERTY_USAGE_GROUP || E->get().usage & PROPERTY_USAGE_SUBGROUP) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (name.find("/") != -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
member_keywords[name] = member_variable_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> clist;
|
|
|
|
ClassDB::get_integer_constant_list(instance_base, &clist);
|
|
|
|
for (List<String>::Element *E = clist.front(); E; E = E->next()) {
|
|
|
|
member_keywords[E->get()] = member_variable_color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-12 23:26:15 +02:00
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
const String text_edit_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
|
2021-06-06 21:19:59 +02:00
|
|
|
const bool godot_2_theme = text_edit_color_theme == "Godot 2";
|
2019-10-02 09:13:14 +02:00
|
|
|
|
2021-06-06 21:19:59 +02:00
|
|
|
if (godot_2_theme || EditorSettings::get_singleton()->is_dark_theme()) {
|
2019-10-02 09:13:14 +02:00
|
|
|
function_definition_color = Color(0.4, 0.9, 1.0);
|
|
|
|
node_path_color = Color(0.39, 0.76, 0.35);
|
2021-06-06 21:19:59 +02:00
|
|
|
annotation_color = Color(1.0, 0.7, 0.45);
|
2019-10-02 09:13:14 +02:00
|
|
|
} else {
|
|
|
|
function_definition_color = Color(0.0, 0.65, 0.73);
|
|
|
|
node_path_color = Color(0.32, 0.55, 0.29);
|
2021-06-06 21:19:59 +02:00
|
|
|
annotation_color = Color(0.8, 0.5, 0.25);
|
2019-10-02 09:13:14 +02:00
|
|
|
}
|
2018-06-10 16:30:49 +02:00
|
|
|
|
2020-05-03 18:08:15 +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);
|
2021-06-06 21:19:59 +02:00
|
|
|
EDITOR_DEF("text_editor/highlighting/gdscript/annotation_color", annotation_color);
|
|
|
|
if (text_edit_color_theme == "Default" || godot_2_theme) {
|
2019-10-02 09:13:14 +02:00
|
|
|
EditorSettings::get_singleton()->set_initial_value(
|
2020-05-03 18:08:15 +02:00
|
|
|
"text_editor/highlighting/gdscript/function_definition_color",
|
2019-10-02 09:13:14 +02:00
|
|
|
function_definition_color,
|
|
|
|
true);
|
|
|
|
EditorSettings::get_singleton()->set_initial_value(
|
2020-05-03 18:08:15 +02:00
|
|
|
"text_editor/highlighting/gdscript/node_path_color",
|
2019-10-02 09:13:14 +02:00
|
|
|
node_path_color,
|
|
|
|
true);
|
2021-06-06 21:19:59 +02:00
|
|
|
EditorSettings::get_singleton()->set_initial_value(
|
|
|
|
"text_editor/highlighting/gdscript/annotation_color",
|
|
|
|
annotation_color,
|
|
|
|
true);
|
2018-06-10 16:30:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
function_definition_color = EDITOR_GET("text_editor/highlighting/gdscript/function_definition_color");
|
|
|
|
node_path_color = EDITOR_GET("text_editor/highlighting/gdscript/node_path_color");
|
2021-06-06 21:19:59 +02:00
|
|
|
annotation_color = EDITOR_GET("text_editor/highlighting/gdscript/annotation_color");
|
2020-05-03 18:08:15 +02:00
|
|
|
type_color = EDITOR_GET("text_editor/highlighting/base_type_color");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptSyntaxHighlighter::add_color_region(const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
|
|
|
|
for (int i = 0; i < p_start_key.length(); i++) {
|
|
|
|
ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "color regions must start with a symbol");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_end_key.length() > 0) {
|
|
|
|
for (int i = 0; i < p_end_key.length(); i++) {
|
|
|
|
ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "color regions must end with a symbol");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 20:55:44 +02:00
|
|
|
int at = 0;
|
2020-05-03 18:08:15 +02:00
|
|
|
for (int i = 0; i < color_regions.size(); i++) {
|
|
|
|
ERR_FAIL_COND_MSG(color_regions[i].start_key == p_start_key, "color region with start key '" + p_start_key + "' already exists.");
|
2020-08-22 20:55:44 +02:00
|
|
|
if (p_start_key.length() < color_regions[i].start_key.length()) {
|
|
|
|
at++;
|
|
|
|
}
|
2020-05-03 18:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ColorRegion color_region;
|
|
|
|
color_region.color = p_color;
|
|
|
|
color_region.start_key = p_start_key;
|
|
|
|
color_region.end_key = p_end_key;
|
|
|
|
color_region.line_only = p_line_only;
|
2020-08-22 20:55:44 +02:00
|
|
|
color_regions.insert(at, color_region);
|
|
|
|
clear_highlighting_cache();
|
2018-04-02 13:41:44 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {
|
2020-03-07 15:29:44 +01:00
|
|
|
Ref<GDScriptSyntaxHighlighter> syntax_highlighter;
|
2021-06-18 00:03:09 +02:00
|
|
|
syntax_highlighter.instantiate();
|
2020-03-07 15:29:44 +01:00
|
|
|
return syntax_highlighter;
|
2018-04-02 13:41:44 +02:00
|
|
|
}
|