Merge pull request #17923 from Paulb23/add_abstract_syntax_highlighter

Abstracted the syntax highlighter from text edit.
This commit is contained in:
Rémi Verschelde 2018-04-04 09:50:51 +02:00 committed by GitHub
commit 5ede505f14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 828 additions and 272 deletions

View file

@ -1778,6 +1778,20 @@ bool ScriptEditor::edit(const Ref<Script> &p_script, int p_line, int p_col, bool
}
ERR_FAIL_COND_V(!se, false);
bool highlighter_set = false;
for (int i = 0; i < syntax_highlighters_func_count; i++) {
SyntaxHighlighter *highlighter = syntax_highlighters_funcs[i]();
se->add_syntax_highlighter(highlighter);
if (!highlighter_set) {
List<String> languages = highlighter->get_supported_languages();
if (languages.find(p_script->get_language()->get_name())) {
se->set_syntax_highlighter(highlighter);
highlighter_set = true;
}
}
}
tab_container->add_child(se);
se->set_edited_script(p_script);
se->set_tooltip_request_func("_get_debug_tooltip", this);
@ -2494,6 +2508,14 @@ void ScriptEditor::_open_script_request(const String &p_path) {
}
}
int ScriptEditor::syntax_highlighters_func_count = 0;
CreateSyntaxHighlighterFunc ScriptEditor::syntax_highlighters_funcs[ScriptEditor::SYNTAX_HIGHLIGHTER_FUNC_MAX];
void ScriptEditor::register_create_syntax_highlighter_function(CreateSyntaxHighlighterFunc p_func) {
ERR_FAIL_COND(syntax_highlighters_func_count == SYNTAX_HIGHLIGHTER_FUNC_MAX);
syntax_highlighters_funcs[syntax_highlighters_func_count++] = p_func;
}
int ScriptEditor::script_editor_func_count = 0;
CreateScriptEditorFunc ScriptEditor::script_editor_funcs[ScriptEditor::SCRIPT_EDITOR_FUNC_MAX];

View file

@ -80,6 +80,9 @@ protected:
static void _bind_methods();
public:
virtual void add_syntax_highlighter(SyntaxHighlighter *p_highlighter) = 0;
virtual void set_syntax_highlighter(SyntaxHighlighter *p_highlighter) = 0;
virtual void apply_code() = 0;
virtual Ref<Script> get_edited_script() const = 0;
virtual Vector<String> get_functions() = 0;
@ -112,6 +115,7 @@ public:
ScriptEditorBase() {}
};
typedef SyntaxHighlighter *(*CreateSyntaxHighlighterFunc)();
typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const Ref<Script> &p_script);
class EditorScriptCodeCompletionCache;
@ -214,12 +218,16 @@ class ScriptEditor : public PanelContainer {
ToolButton *script_forward;
enum {
SCRIPT_EDITOR_FUNC_MAX = 32
SCRIPT_EDITOR_FUNC_MAX = 32,
SYNTAX_HIGHLIGHTER_FUNC_MAX = 32
};
static int script_editor_func_count;
static CreateScriptEditorFunc script_editor_funcs[SCRIPT_EDITOR_FUNC_MAX];
static int syntax_highlighters_func_count;
static CreateSyntaxHighlighterFunc syntax_highlighters_funcs[SYNTAX_HIGHLIGHTER_FUNC_MAX];
struct ScriptHistory {
Control *control;
@ -399,7 +407,9 @@ public:
ScriptEditorDebugger *get_debugger() { return debugger; }
void set_live_auto_reload_running_scripts(bool p_enabled);
static void register_create_syntax_highlighter_function(CreateSyntaxHighlighterFunc p_func);
static void register_create_script_editor_function(CreateScriptEditorFunc p_func);
ScriptEditor(EditorNode *p_editor);
~ScriptEditor();
};

View file

@ -573,6 +573,7 @@ void ScriptTextEditor::set_edited_script(const Ref<Script> &p_script) {
ERR_FAIL_COND(!script.is_null());
script = p_script;
_set_theme_for_script();
code_editor->get_text_edit()->set_text(script->get_source_code());
code_editor->get_text_edit()->clear_undo_history();
@ -580,8 +581,6 @@ void ScriptTextEditor::set_edited_script(const Ref<Script> &p_script) {
emit_signal("name_changed");
code_editor->update_line_and_column();
_set_theme_for_script();
}
void ScriptTextEditor::_validate_script() {
@ -1265,11 +1264,26 @@ void ScriptTextEditor::_edit_option(int p_op) {
}
}
void ScriptTextEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
highlighters[p_highlighter->get_name()] = p_highlighter;
highlighter_menu->get_popup()->add_item(p_highlighter->get_name());
}
void ScriptTextEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
TextEdit *te = code_editor->get_text_edit();
te->_set_syntax_highlighting(p_highlighter);
}
void ScriptTextEditor::_change_syntax_highlighter(int p_idx) {
set_syntax_highlighter(highlighters[highlighter_menu->get_popup()->get_item_text(p_idx)]);
}
void ScriptTextEditor::_bind_methods() {
ClassDB::bind_method("_validate_script", &ScriptTextEditor::_validate_script);
ClassDB::bind_method("_load_theme_settings", &ScriptTextEditor::_load_theme_settings);
ClassDB::bind_method("_breakpoint_toggled", &ScriptTextEditor::_breakpoint_toggled);
ClassDB::bind_method("_change_syntax_highlighter", &ScriptTextEditor::_change_syntax_highlighter);
ClassDB::bind_method("_edit_option", &ScriptTextEditor::_edit_option);
ClassDB::bind_method("_goto_line", &ScriptTextEditor::_goto_line);
ClassDB::bind_method("_lookup_symbol", &ScriptTextEditor::_lookup_symbol);
@ -1655,6 +1669,14 @@ ScriptTextEditor::ScriptTextEditor() {
edit_hb->add_child(edit_menu);
highlighters["Standard"] = NULL;
highlighter_menu = memnew(MenuButton);
highlighter_menu->set_text(TTR("Syntax Highlighter"));
highlighter_menu->get_popup()->add_item("Standard");
highlighter_menu->get_popup()->connect("id_pressed", this, "_change_syntax_highlighter");
edit_hb->add_child(highlighter_menu);
quick_open = memnew(ScriptEditorQuickOpen);
add_child(quick_open);
quick_open->connect("goto_line", this, "_goto_line");

View file

@ -49,6 +49,7 @@ class ScriptTextEditor : public ScriptEditorBase {
HBoxContainer *edit_hb;
MenuButton *edit_menu;
MenuButton *highlighter_menu;
MenuButton *search_menu;
PopupMenu *context_menu;
@ -125,6 +126,9 @@ protected:
void _notification(int p_what);
static void _bind_methods();
Map<String, SyntaxHighlighter *> highlighters;
void _change_syntax_highlighter(int p_idx);
void _edit_option(int p_op);
void _make_context_menu(bool p_selection, bool p_color, bool p_can_fold, bool p_is_folded);
void _text_edit_gui_input(const Ref<InputEvent> &ev);
@ -145,6 +149,9 @@ protected:
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
public:
virtual void add_syntax_highlighter(SyntaxHighlighter *p_highlighter);
virtual void set_syntax_highlighter(SyntaxHighlighter *p_highlighter);
virtual void apply_code();
virtual Ref<Script> get_edited_script() const;
virtual Vector<String> get_functions();

View file

@ -0,0 +1,278 @@
/*************************************************************************/
/* 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<int, TextEdit::HighlighterInfo> GDSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
Map<int, TextEdit::HighlighterInfo> 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 = -1;
int deregion = 0;
for (int i = 0; i < p_line; i++) {
int ending_color_region = text_editor->_get_line_ending_color_region(i);
if (in_region == -1) {
in_region = ending_color_region;
} else if (in_region == ending_color_region) {
in_region = -1;
} else {
const Map<int, TextEdit::Text::ColorRegionInfo> &cri_map = text_editor->_get_line_color_region_info(i);
for (const Map<int, TextEdit::Text::ColorRegionInfo>::Element *E = cri_map.front(); E; E = E->next()) {
const TextEdit::Text::ColorRegionInfo &cri = E->get();
if (cri.region == in_region) {
in_region = -1;
}
}
}
}
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;
}
// 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 GDSyntaxHighlighter::get_name() {
return "GDScript";
}
List<String> GDSyntaxHighlighter::get_supported_languages() {
List<String> languages;
languages.push_back("GDScript");
return languages;
}
void GDSyntaxHighlighter::_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 *GDSyntaxHighlighter::create() {
return memnew(GDSyntaxHighlighter);
}

View file

@ -0,0 +1,56 @@
/*************************************************************************/
/* gdscript_highlighter.h */
/*************************************************************************/
/* 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. */
/*************************************************************************/
#ifndef GDSCRIPT_HIGHLIGHTER_H
#define GDSCRIPT_HIGHLIGHTER_H
#include "scene/gui/text_edit.h"
class GDSyntaxHighlighter : public SyntaxHighlighter {
private:
// colours
Color font_color;
Color symbol_color;
Color function_color;
Color built_in_type_color;
Color number_color;
Color member_color;
public:
static SyntaxHighlighter *create();
virtual void _update_cache();
virtual Map<int, TextEdit::HighlighterInfo> _get_line_syntax_highlighting(int p_line);
virtual String get_name();
virtual List<String> get_supported_languages();
};
#endif // GDSCRIPT_HIGHLIGHTER_H

View file

@ -31,6 +31,7 @@
#include "register_types.h"
#include "gdscript.h"
#include "gdscript_highlighter.h"
#include "gdscript_tokenizer.h"
#include "io/file_access_encrypted.h"
#include "io/resource_loader.h"
@ -92,6 +93,7 @@ void register_gdscript_types() {
ResourceSaver::add_resource_format_saver(resource_saver_gd);
#ifdef TOOLS_ENABLED
ScriptEditor::register_create_syntax_highlighter_function(GDSyntaxHighlighter::create);
EditorNode::add_init_callback(_editor_init);
#endif
}

View file

@ -3236,6 +3236,12 @@ void VisualScriptEditor::_member_option(int p_option) {
}
}
void VisualScriptEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
}
void VisualScriptEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
}
void VisualScriptEditor::_bind_methods() {
ClassDB::bind_method("_member_button", &VisualScriptEditor::_member_button);

View file

@ -246,6 +246,9 @@ protected:
static void _bind_methods();
public:
virtual void add_syntax_highlighter(SyntaxHighlighter *p_highlighter);
virtual void set_syntax_highlighter(SyntaxHighlighter *p_highlighter);
virtual void apply_code();
virtual Ref<Script> get_edited_script() const;
virtual Vector<String> get_functions();

View file

@ -145,6 +145,7 @@ void TextEdit::Text::_update_line_cache(int p_line) const {
text[p_line].region_info.clear();
int ending_color_region = -1;
for (int i = 0; i < len; i++) {
if (!_is_symbol(str[i]))
@ -184,6 +185,12 @@ void TextEdit::Text::_update_line_cache(int p_line) const {
cri.region = j;
text[p_line].region_info[i] = cri;
i += lr - 1;
if (ending_color_region == -1 && !cr.line_only) {
ending_color_region = j;
} else if (ending_color_region == j) {
ending_color_region = -1;
}
break;
}
@ -211,10 +218,16 @@ void TextEdit::Text::_update_line_cache(int p_line) const {
cri.region = j;
text[p_line].region_info[i] = cri;
i += lr - 1;
if (ending_color_region == j) {
ending_color_region = -1;
}
break;
}
}
}
text[p_line].ending_color_region = ending_color_region;
}
const Map<int, TextEdit::Text::ColorRegionInfo> &TextEdit::Text::get_color_region_info(int p_line) const {
@ -619,44 +632,10 @@ void TextEdit::_notification(int p_what) {
Color color = cache.font_color;
color.a *= readonly_alpha;
int in_region = -1;
if (syntax_coloring) {
if (cache.background_color.a > 0.01) {
VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(), get_size()), cache.background_color);
}
//compute actual region to start (may be inside say, a comment).
//slow in very large documents :( but ok for source!
for (int i = 0; i < cursor.line_ofs; i++) {
const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(i);
if (in_region >= 0 && color_regions[in_region].line_only) {
in_region = -1; //reset regions that end at end of line
}
for (const Map<int, Text::ColorRegionInfo>::Element *E = cri_map.front(); E; E = E->next()) {
const Text::ColorRegionInfo &cri = E->get();
if (in_region == -1) {
if (!cri.end) {
in_region = cri.region;
}
} else if (in_region == cri.region && !color_regions[cri.region].line_only) { //ignore otherwise
if (cri.end || color_regions[cri.region].eq) {
in_region = -1;
}
}
}
}
}
int brace_open_match_line = -1;
@ -804,7 +783,6 @@ void TextEdit::_notification(int p_what) {
}
}
int deregion = 0; //force it to clear inrgion
Point2 cursor_pos;
// get the highlighted words
@ -848,19 +826,12 @@ void TextEdit::_notification(int p_what) {
if (smooth_scroll_enabled)
ofs_y -= ((v_scroll->get_value() - get_line_scroll_pos()) * get_row_height());
bool prev_is_char = false;
bool prev_is_number = false;
bool in_keyword = false;
bool underlined = false;
bool in_word = false;
bool in_function_name = false;
bool in_member_variable = false;
bool is_hex_notation = false;
Color keyword_color;
// check if line contains highlighted word
int highlighted_text_col = -1;
int search_text_col = -1;
int highlighted_word_col = -1;
if (!search_text.empty())
search_text_col = _get_column_pos_of_word(search_text, str, search_flags, 0);
@ -868,7 +839,11 @@ void TextEdit::_notification(int p_what) {
if (highlighted_text.length() != 0 && highlighted_text != search_text)
highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(line);
if (select_identifiers_enabled && highlighted_word.length() != 0) {
if (_is_char(highlighted_word[0])) {
highlighted_word_col = _get_column_pos_of_word(highlighted_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
}
}
if (text.is_marked(line)) {
@ -936,170 +911,28 @@ void TextEdit::_notification(int p_what) {
cache.font->draw(ci, Point2(cache.style_normal->get_margin(MARGIN_LEFT) + cache.breakpoint_gutter_width + ofs_x, ofs_y + cache.font->get_ascent()), fc, cache.line_number_color);
}
//loop through characters in one line
Map<int, HighlighterInfo> color_map;
if (syntax_coloring) {
color_map = _get_line_syntax_highlighting(line);
}
// ensure we at least use the font color
Color current_color = cache.font_color;
if (readonly) {
current_color.a *= readonly_alpha;
}
for (int j = 0; j < str.length(); j++) {
//look for keyword
if (deregion > 0) {
deregion--;
if (deregion == 0)
in_region = -1;
}
if (syntax_coloring && deregion == 0) {
color = cache.font_color; //reset
color.a *= readonly_alpha;
//find keyword
bool is_char = _is_text_char(str[j]);
bool is_symbol = _is_symbol(str[j]);
bool is_number = _is_number(str[j]);
if (j == 0 && in_region >= 0 && color_regions[in_region].line_only) {
in_region = -1; //reset regions that end at end of line
}
// 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 (syntax_coloring) {
if (color_map.has(j)) {
current_color = color_map[j].color;
if (readonly) {
current_color.a *= readonly_alpha;
}
}
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 Text::ColorRegionInfo &cri = cri_map[j];
if (in_region == -1) {
if (!cri.end) {
in_region = cri.region;
}
} else if (in_region == cri.region && !color_regions[cri.region].line_only) { //ignore otherwise
if (cri.end || color_regions[cri.region].eq) {
deregion = color_regions[cri.region].eq ? color_regions[cri.region].begin_key.length() : color_regions[cri.region].end_key.length();
}
}
}
if (!is_char) {
in_keyword = false;
underlined = 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++;
uint32_t hash = String::hash(&str[j], to - j);
StrRange range(&str[j], to - j);
const Color *col = keywords.custom_getptr(range, hash);
if (!col) {
col = member_keywords.custom_getptr(range, hash);
if (col) {
for (int k = j - 1; k >= 0; k--) {
if (str[k] == '.') {
col = NULL; //member indexing not allowed
break;
} else if (str[k] > 32) {
break;
}
}
}
}
if (col) {
in_keyword = true;
keyword_color = *col;
}
if (select_identifiers_enabled && highlighted_word != String()) {
if (highlighted_word == range) {
underlined = true;
}
}
}
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 = color_regions[in_region].color;
else if (in_keyword)
color = keyword_color;
else if (in_member_variable)
color = cache.member_variable_color;
else if (in_function_name)
color = cache.function_color;
else if (is_symbol)
color = cache.symbol_color;
else if (is_number)
color = cache.number_color;
prev_is_char = is_char;
prev_is_number = is_number;
color = current_color;
}
int char_w;
@ -1206,6 +1039,13 @@ void TextEdit::_notification(int p_what) {
}
}
if (highlighted_word_col != -1) {
if (j > highlighted_word_col + highlighted_word.length()) {
highlighted_word_col = _get_column_pos_of_word(highlighted_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, j);
}
underlined = (j >= highlighted_word_col && j < highlighted_word_col + highlighted_word.length());
}
if (brace_matching_enabled) {
if ((brace_open_match_line == line && brace_open_match_column == j) ||
(cursor.column == j && cursor.line == line && (brace_open_matching || brace_open_mismatch))) {
@ -1512,6 +1352,7 @@ void TextEdit::_notification(int p_what) {
OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos + Point2(0, get_row_height()));
OS::get_singleton()->set_ime_intermediate_text_callback(_ime_text_callback, this);
}
} break;
case NOTIFICATION_FOCUS_ENTER: {
@ -1528,7 +1369,6 @@ void TextEdit::_notification(int p_what) {
if (raised_from_completion) {
VisualServer::get_singleton()->canvas_item_set_z_index(get_canvas_item(), 1);
}
} break;
case NOTIFICATION_FOCUS_EXIT: {
@ -4150,6 +3990,44 @@ void TextEdit::_update_caches() {
cache.can_fold_icon = get_icon("GuiTreeArrowDown", "EditorIcons");
cache.folded_eol_icon = get_icon("GuiEllipsis", "EditorIcons");
text.set_font(cache.font);
if (syntax_highlighter) {
syntax_highlighter->_update_cache();
}
}
SyntaxHighlighter *TextEdit::_get_syntax_highlighting() {
return syntax_highlighter;
}
void TextEdit::_set_syntax_highlighting(SyntaxHighlighter *p_syntax_highlighter) {
syntax_highlighter = p_syntax_highlighter;
if (syntax_highlighter) {
syntax_highlighter->set_text_editor(this);
syntax_highlighter->_update_cache();
}
update();
}
int TextEdit::_get_line_ending_color_region(int p_line) const {
if (p_line < 0 || p_line > text.size() - 1) {
return -1;
}
return text.get_line_ending_color_region(p_line);
}
TextEdit::ColorRegion TextEdit::_get_color_region(int p_region) const {
if (p_region < 0 || p_region > color_regions.size()) {
return ColorRegion();
}
return color_regions[p_region];
}
Map<int, TextEdit::Text::ColorRegionInfo> TextEdit::_get_line_color_region_info(int p_line) const {
if (p_line < 0 || p_line > text.size() - 1) {
return Map<int, Text::ColorRegionInfo>();
}
return text.get_color_region_info(p_line);
}
void TextEdit::clear_colors() {
@ -4165,6 +4043,14 @@ void TextEdit::add_keyword_color(const String &p_keyword, const Color &p_color)
update();
}
bool TextEdit::has_keyword_color(String p_keyword) const {
return keywords.has(p_keyword);
}
Color TextEdit::get_keyword_color(String p_keyword) const {
return keywords[p_keyword];
}
void TextEdit::add_color_region(const String &p_begin_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
color_regions.push_back(ColorRegion(p_begin_key, p_end_key, p_color, p_line_only));
@ -4177,6 +4063,14 @@ void TextEdit::add_member_keyword(const String &p_keyword, const Color &p_color)
update();
}
bool TextEdit::has_member_color(String p_member) const {
return member_keywords.has(p_member);
}
Color TextEdit::get_member_color(String p_member) const {
return member_keywords[p_member];
}
void TextEdit::clear_member_keywords() {
member_keywords.clear();
update();
@ -5691,6 +5585,8 @@ void TextEdit::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_v_scroll_speed"), &TextEdit::get_v_scroll_speed);
ClassDB::bind_method(D_METHOD("add_keyword_color", "keyword", "color"), &TextEdit::add_keyword_color);
ClassDB::bind_method(D_METHOD("has_keyword_color", "keyword"), &TextEdit::has_keyword_color);
ClassDB::bind_method(D_METHOD("get_keyword_color", "keyword"), &TextEdit::get_keyword_color);
ClassDB::bind_method(D_METHOD("add_color_region", "begin_key", "end_key", "color", "line_only"), &TextEdit::add_color_region, DEFVAL(false));
ClassDB::bind_method(D_METHOD("clear_colors"), &TextEdit::clear_colors);
ClassDB::bind_method(D_METHOD("menu_option", "option"), &TextEdit::menu_option);
@ -5744,6 +5640,7 @@ TextEdit::TextEdit() {
clear();
wrap = false;
set_focus_mode(FOCUS_ALL);
syntax_highlighter = NULL;
_update_caches();
cache.size = Size2(1, 1);
cache.row_height = 1;
@ -5860,3 +5757,216 @@ TextEdit::TextEdit() {
TextEdit::~TextEdit() {
}
///////////////////////////////////////////////////////////////////////////////
Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int p_line) {
if (syntax_highlighter != NULL) {
return syntax_highlighter->_get_line_syntax_highlighting(p_line);
}
Map<int, HighlighterInfo> 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 = -1;
int deregion = 0;
for (int i = 0; i < p_line; i++) {
int ending_color_region = text.get_line_ending_color_region(i);
if (in_region == -1) {
in_region = ending_color_region;
} else if (in_region == ending_color_region) {
in_region = -1;
} else {
const Map<int, TextEdit::Text::ColorRegionInfo> &cri_map = text.get_color_region_info(i);
for (const Map<int, TextEdit::Text::ColorRegionInfo>::Element *E = cri_map.front(); E; E = E->next()) {
const TextEdit::Text::ColorRegionInfo &cri = E->get();
if (cri.region == in_region) {
in_region = -1;
}
}
}
}
const Map<int, TextEdit::Text::ColorRegionInfo> cri_map = text.get_color_region_info(p_line);
const String &str = text[p_line];
Color prev_color;
for (int j = 0; j < str.length(); j++) {
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 = cache.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 if (in_region == cri.region && !color_regions[cri.region].line_only) { //ignore otherwise
if (cri.end || color_regions[cri.region].eq) {
deregion = color_regions[cri.region].eq ? color_regions[cri.region].begin_key.length() : color_regions[cri.region].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++;
uint32_t hash = String::hash(&str[j], to - j);
StrRange range(&str[j], to - j);
const Color *col = keywords.custom_getptr(range, hash);
if (!col) {
col = member_keywords.custom_getptr(range, hash);
if (col) {
for (int k = j - 1; k >= 0; k--) {
if (str[k] == '.') {
col = NULL; //member indexing not allowed
break;
} else if (str[k] > 32) {
break;
}
}
}
}
if (col) {
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 = color_regions[in_region].color;
else if (in_keyword)
color = keyword_color;
else if (in_member_variable)
color = cache.member_variable_color;
else if (in_function_name)
color = cache.function_color;
else if (is_symbol)
color = cache.symbol_color;
else if (is_number)
color = cache.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;
}
void SyntaxHighlighter::set_text_editor(TextEdit *p_text_editor) {
text_editor = p_text_editor;
}
TextEdit *SyntaxHighlighter::get_text_editor() {
return text_editor;
}

View file

@ -36,10 +36,84 @@
#include "scene/gui/scroll_bar.h"
#include "scene/main/timer.h"
class SyntaxHighlighter;
class TextEdit : public Control {
GDCLASS(TextEdit, Control);
GDCLASS(TextEdit, Control)
public:
struct HighlighterInfo {
Color color;
};
struct ColorRegion {
Color color;
String begin_key;
String end_key;
bool line_only;
bool eq;
ColorRegion(const String &p_begin_key = "", const String &p_end_key = "", const Color &p_color = Color(), bool p_line_only = false) {
begin_key = p_begin_key;
end_key = p_end_key;
color = p_color;
line_only = p_line_only || p_end_key == "";
eq = begin_key == end_key;
}
};
class Text {
public:
struct ColorRegionInfo {
int region;
bool end;
};
struct Line {
int width_cache : 24;
bool marked : 1;
bool breakpoint : 1;
bool hidden : 1;
int ending_color_region;
Map<int, ColorRegionInfo> region_info;
String data;
};
private:
const Vector<ColorRegion> *color_regions;
mutable Vector<Line> text;
Ref<Font> font;
int indent_size;
void _update_line_cache(int p_line) const;
public:
void set_indent_size(int p_indent_size);
void set_font(const Ref<Font> &p_font);
void set_color_regions(const Vector<ColorRegion> *p_regions) { color_regions = p_regions; }
int get_line_width(int p_line) const;
int get_max_width(bool p_exclude_hidden = false) const;
const Map<int, ColorRegionInfo> &get_color_region_info(int p_line) const;
void set(int p_line, const String &p_text);
void set_marked(int p_line, bool p_marked) { text[p_line].marked = p_marked; }
bool is_marked(int p_line) const { return text[p_line].marked; }
void set_breakpoint(int p_line, bool p_breakpoint) { text[p_line].breakpoint = p_breakpoint; }
bool is_breakpoint(int p_line) const { return text[p_line].breakpoint; }
void set_hidden(int p_line, bool p_hidden) { text[p_line].hidden = p_hidden; }
bool is_hidden(int p_line) const { return text[p_line].hidden; }
int get_line_ending_color_region(int p_line) const { return text[p_line].ending_color_region; }
void insert(int p_at, const String &p_text);
void remove(int p_at);
int size() const { return text.size(); }
void clear();
void clear_caches();
_FORCE_INLINE_ const String &operator[](int p_line) const { return text[p_line].data; }
Text() { indent_size = 4; }
};
private:
struct Cursor {
int last_fit_x;
int line, column; ///< cursor
@ -115,70 +189,6 @@ class TextEdit : public Control {
Size2 size;
} cache;
struct ColorRegion {
Color color;
String begin_key;
String end_key;
bool line_only;
bool eq;
ColorRegion(const String &p_begin_key = "", const String &p_end_key = "", const Color &p_color = Color(), bool p_line_only = false) {
begin_key = p_begin_key;
end_key = p_end_key;
color = p_color;
line_only = p_line_only || p_end_key == "";
eq = begin_key == end_key;
}
};
class Text {
public:
struct ColorRegionInfo {
int region;
bool end;
};
struct Line {
int width_cache : 24;
bool marked : 1;
bool breakpoint : 1;
bool hidden : 1;
Map<int, ColorRegionInfo> region_info;
String data;
};
private:
const Vector<ColorRegion> *color_regions;
mutable Vector<Line> text;
Ref<Font> font;
int indent_size;
void _update_line_cache(int p_line) const;
public:
void set_indent_size(int p_indent_size);
void set_font(const Ref<Font> &p_font);
void set_color_regions(const Vector<ColorRegion> *p_regions) { color_regions = p_regions; }
int get_line_width(int p_line) const;
int get_max_width(bool p_exclude_hidden = false) const;
const Map<int, ColorRegionInfo> &get_color_region_info(int p_line) const;
void set(int p_line, const String &p_text);
void set_marked(int p_line, bool p_marked) { text[p_line].marked = p_marked; }
bool is_marked(int p_line) const { return text[p_line].marked; }
void set_breakpoint(int p_line, bool p_breakpoint) { text[p_line].breakpoint = p_breakpoint; }
bool is_breakpoint(int p_line) const { return text[p_line].breakpoint; }
void set_hidden(int p_line, bool p_hidden) { text[p_line].hidden = p_hidden; }
bool is_hidden(int p_line) const { return text[p_line].hidden; }
void insert(int p_at, const String &p_text);
void remove(int p_at);
int size() const { return text.size(); }
void clear();
void clear_caches();
_FORCE_INLINE_ const String &operator[](int p_line) const { return text[p_line].data; }
Text() { indent_size = 4; }
};
struct TextOperation {
enum Type {
@ -209,9 +219,12 @@ class TextEdit : public Control {
void _do_text_op(const TextOperation &p_op, bool p_reverse);
//syntax coloring
SyntaxHighlighter *syntax_highlighter;
HashMap<String, Color> keywords;
HashMap<String, Color> member_keywords;
Map<int, HighlighterInfo> _get_line_syntax_highlighting(int p_line);
Vector<ColorRegion> color_regions;
Set<String> completion_prefixes;
@ -391,6 +404,13 @@ protected:
static void _bind_methods();
public:
SyntaxHighlighter *_get_syntax_highlighting();
void _set_syntax_highlighting(SyntaxHighlighter *p_syntax_highlighter);
int _get_line_ending_color_region(int p_line) const;
ColorRegion _get_color_region(int p_region) const;
Map<int, Text::ColorRegionInfo> _get_line_color_region_info(int p_line) const;
enum MenuItems {
MENU_CUT,
MENU_COPY,
@ -545,10 +565,15 @@ public:
bool is_insert_mode() const;
void add_keyword_color(const String &p_keyword, const Color &p_color);
bool has_keyword_color(String p_keyword) const;
Color get_keyword_color(String p_keyword) const;
void add_color_region(const String &p_begin_key = String(), const String &p_end_key = String(), const Color &p_color = Color(), bool p_line_only = false);
void clear_colors();
void add_member_keyword(const String &p_keyword, const Color &p_color);
bool has_member_color(String p_member) const;
Color get_member_color(String p_member) const;
void clear_member_keywords();
int get_v_scroll() const;
@ -621,4 +646,19 @@ public:
VARIANT_ENUM_CAST(TextEdit::MenuItems);
VARIANT_ENUM_CAST(TextEdit::SearchFlags);
class SyntaxHighlighter {
protected:
TextEdit *text_editor;
public:
virtual void _update_cache() = 0;
virtual Map<int, TextEdit::HighlighterInfo> _get_line_syntax_highlighting(int p_line) = 0;
virtual String get_name() = 0;
virtual List<String> get_supported_languages() = 0;
void set_text_editor(TextEdit *p_text_editor);
TextEdit *get_text_editor();
};
#endif // TEXT_EDIT_H