Ported: Convert syntax highlighters into a resource

- Paulb23
2f1080be9b
This commit is contained in:
Relintai 2023-02-20 14:51:10 +01:00
parent ddcf36addd
commit 0edf8fe956
16 changed files with 265 additions and 160 deletions

View File

@ -798,9 +798,9 @@ Variant EditorCodeTextEditor::get_edit_state() {
state["bookmarks"] = text_editor->get_bookmarks_array();
state["syntax_highlighter"] = TTR("Standard");
SyntaxHighlighter *syntax_highlighter = text_editor->_get_syntax_highlighting();
if (syntax_highlighter) {
state["syntax_highlighter"] = syntax_highlighter->get_name();
Ref<SyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighting();
if (syntax_highlighter.is_valid()) {
state["syntax_highlighter"] = syntax_highlighter->_get_name();
}
return state;

View File

@ -2095,8 +2095,8 @@ bool EditorScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool
se->add_syntax_highlighter(highlighter);
if (script != nullptr && !highlighter_set) {
List<String> languages = highlighter->get_supported_languages();
if (languages.find(script->get_language()->get_name())) {
Array languages = highlighter->_get_supported_languages();
if (languages.find(script->get_language()->get_name()) > -1) {
se->set_syntax_highlighter(highlighter);
highlighter_set = true;
}

View File

@ -43,8 +43,8 @@ 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 add_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) = 0;
virtual void set_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) = 0;
virtual void apply_code() = 0;
virtual RES get_edited_resource() const = 0;

View File

@ -72,9 +72,9 @@
#include "editor_script_editor_quick_open.h"
#include "editor_connection_info_dialog.h"
#include "editor_script_editor.h"
#include "editor_goto_line_dialog.h"
#include "editor_find_replace_bar.h"
#include "editor_goto_line_dialog.h"
#include "editor_script_editor.h"
Vector<String> EditorScriptTextEditor::get_functions() {
String errortxt;
@ -1346,28 +1346,27 @@ void EditorScriptTextEditor::_edit_option_toggle_inline_comment() {
code_editor->toggle_inline_comment(delimiter);
}
void EditorScriptTextEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
highlighters[p_highlighter->get_name()] = p_highlighter;
highlighter_menu->add_radio_check_item(p_highlighter->get_name());
void EditorScriptTextEditor::add_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) {
highlighters[p_highlighter->_get_name()] = p_highlighter;
highlighter_menu->add_radio_check_item(p_highlighter->_get_name());
}
void EditorScriptTextEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
void EditorScriptTextEditor::set_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) {
TextEdit *te = code_editor->get_text_edit();
te->_set_syntax_highlighting(p_highlighter);
if (p_highlighter != nullptr) {
highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(p_highlighter->get_name()), true);
te->set_syntax_highlighting(p_highlighter);
if (p_highlighter.is_valid()) {
highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(p_highlighter->_get_name()), true);
} else {
highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(TTR("Standard")), true);
}
}
void EditorScriptTextEditor::_change_syntax_highlighter(int p_idx) {
RBMap<String, SyntaxHighlighter *>::Element *el = highlighters.front();
RBMap<String, Ref<SyntaxHighlighter> >::Element *el = highlighters.front();
while (el != nullptr) {
highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(el->key()), false);
el = el->next();
}
// highlighter_menu->set_item_checked(p_idx, true);
set_syntax_highlighter(highlighters[highlighter_menu->get_item_text(p_idx)]);
}
@ -1904,7 +1903,7 @@ EditorScriptTextEditor::EditorScriptTextEditor() {
convert_case = memnew(PopupMenu);
convert_case->set_name("convert_case");
highlighters[TTR("Standard")] = NULL;
highlighters[TTR("Standard")] = Ref<SyntaxHighlighter>();
highlighter_menu = memnew(PopupMenu);
highlighter_menu->set_name("highlighter_menu");
highlighter_menu->add_radio_check_item(TTR("Standard"));
@ -1933,11 +1932,6 @@ EditorScriptTextEditor::EditorScriptTextEditor() {
}
EditorScriptTextEditor::~EditorScriptTextEditor() {
for (const RBMap<String, SyntaxHighlighter *>::Element *E = highlighters.front(); E; E = E->next()) {
if (E->get() != NULL) {
memdelete(E->get());
}
}
highlighters.clear();
if (!editor_enabled) {

View File

@ -186,7 +186,7 @@ protected:
static void _bind_methods();
RBMap<String, SyntaxHighlighter *> highlighters;
RBMap<String, Ref<SyntaxHighlighter>> highlighters;
void _change_syntax_highlighter(int p_idx);
void _edit_option(int p_op);
@ -212,8 +212,8 @@ protected:
public:
void _update_connected_methods();
virtual void add_syntax_highlighter(SyntaxHighlighter *p_highlighter);
virtual void set_syntax_highlighter(SyntaxHighlighter *p_highlighter);
virtual void add_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter);
virtual void set_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter);
void update_toggle_scripts_button();
virtual void apply_code();

View File

@ -54,16 +54,16 @@
#include "editor_goto_line_dialog.h"
#include "editor_find_replace_bar.h"
void EditorTextEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
highlighters[p_highlighter->get_name()] = p_highlighter;
highlighter_menu->add_radio_check_item(p_highlighter->get_name());
void EditorTextEditor::add_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) {
highlighters[p_highlighter->_get_name()] = p_highlighter;
highlighter_menu->add_radio_check_item(p_highlighter->_get_name());
}
void EditorTextEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
void EditorTextEditor::set_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) {
TextEdit *te = code_editor->get_text_edit();
te->_set_syntax_highlighting(p_highlighter);
if (p_highlighter != nullptr) {
highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(p_highlighter->get_name()), true);
te->set_syntax_highlighting(p_highlighter);
if (p_highlighter.is_valid()) {
highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(p_highlighter->_get_name()), true);
} else {
highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(TTR("Standard")), true);
}
@ -82,7 +82,7 @@ void EditorTextEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter)
}
void EditorTextEditor::_change_syntax_highlighter(int p_idx) {
RBMap<String, SyntaxHighlighter *>::Element *el = highlighters.front();
RBMap<String, Ref<SyntaxHighlighter> >::Element *el = highlighters.front();
while (el != nullptr) {
highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(el->key()), false);
el = el->next();
@ -690,7 +690,7 @@ EditorTextEditor::EditorTextEditor() {
convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize")), EDIT_CAPITALIZE);
convert_case->connect("id_pressed", this, "_edit_option");
highlighters[TTR("Standard")] = NULL;
highlighters[TTR("Standard")] = Ref<SyntaxHighlighter>();
highlighter_menu = memnew(PopupMenu);
highlighter_menu->set_name("highlighter_menu");
edit_menu->get_popup()->add_child(highlighter_menu);
@ -722,11 +722,6 @@ EditorTextEditor::EditorTextEditor() {
}
EditorTextEditor::~EditorTextEditor() {
for (const RBMap<String, SyntaxHighlighter *>::Element *E = highlighters.front(); E; E = E->next()) {
if (E->get() != NULL) {
memdelete(E->get());
}
}
highlighters.clear();
}

View File

@ -127,7 +127,7 @@ protected:
void _text_edit_gui_input(const Ref<InputEvent> &ev);
void _prepare_edit_menu();
RBMap<String, SyntaxHighlighter *> highlighters;
RBMap<String, Ref<SyntaxHighlighter> > highlighters;
void _change_syntax_highlighter(int p_idx);
void _load_theme_settings();
@ -139,8 +139,8 @@ protected:
void _bookmark_item_pressed(int p_idx);
public:
virtual void add_syntax_highlighter(SyntaxHighlighter *p_highlighter);
virtual void set_syntax_highlighter(SyntaxHighlighter *p_highlighter);
virtual void add_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter);
virtual void set_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter);
virtual String get_name();
virtual Ref<Texture> get_icon();

View File

@ -31,7 +31,6 @@
#include "cscript_highlighter.h"
#include "../cscript_tokenizer.h"
#include "editor/editor_settings.h"
#include "scene/gui/text_edit.h"
inline bool _is_symbol(CharType c) {
return is_symbol(c);
@ -57,8 +56,8 @@ static bool _is_bin_symbol(CharType c) {
return (c == '0' || c == '1');
}
RBMap<int, TextEdit::HighlighterInfo> CScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
RBMap<int, TextEdit::HighlighterInfo> color_map;
Dictionary CScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
Dictionary color_map;
Type next_type = NONE;
Type current_type = NONE;
@ -82,14 +81,14 @@ RBMap<int, TextEdit::HighlighterInfo> CScriptSyntaxHighlighter::_get_line_syntax
Color keyword_color;
Color color;
int in_region = text_editor->_is_line_in_region(p_line);
int in_region = text_edit->_is_line_in_region(p_line);
int deregion = 0;
const RBMap<int, TextEdit::Text::ColorRegionInfo> cri_map = text_editor->_get_line_color_region_info(p_line);
const String &str = text_editor->get_line(p_line);
const RBMap<int, TextEdit::Text::ColorRegionInfo> cri_map = text_edit->_get_line_color_region_info(p_line);
const String &str = text_edit->get_line(p_line);
Color prev_color;
for (int j = 0; j < str.length(); j++) {
TextEdit::HighlighterInfo highlighter_info;
Dictionary highlighter_info;
if (deregion > 0) {
deregion--;
@ -101,7 +100,7 @@ RBMap<int, TextEdit::HighlighterInfo> CScriptSyntaxHighlighter::_get_line_syntax
if (deregion != 0) {
if (color != prev_color) {
prev_color = color;
highlighter_info.color = color;
highlighter_info["color"] = color;
color_map[j] = highlighter_info;
}
continue;
@ -163,7 +162,7 @@ RBMap<int, TextEdit::HighlighterInfo> CScriptSyntaxHighlighter::_get_line_syntax
in_region = cri.region;
}
} else {
TextEdit::ColorRegion cr = text_editor->_get_color_region(cri.region);
TextEdit::ColorRegion cr = text_edit->_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();
@ -184,10 +183,10 @@ RBMap<int, TextEdit::HighlighterInfo> CScriptSyntaxHighlighter::_get_line_syntax
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);
if (text_edit->has_keyword_color(word)) {
col = text_edit->get_keyword_color(word);
} else if (text_edit->has_member_color(word)) {
col = text_edit->get_member_color(word);
}
if (col != Color()) {
@ -278,7 +277,7 @@ RBMap<int, TextEdit::HighlighterInfo> CScriptSyntaxHighlighter::_get_line_syntax
if (in_region >= 0) {
next_type = REGION;
color = text_editor->_get_color_region(in_region).color;
color = text_edit->_get_color_region(in_region).color;
} else if (in_node_path) {
next_type = NODE_PATH;
color = node_path_color;
@ -337,32 +336,32 @@ RBMap<int, TextEdit::HighlighterInfo> CScriptSyntaxHighlighter::_get_line_syntax
if (color != prev_color) {
prev_color = color;
highlighter_info.color = color;
highlighter_info["color"] = color;
color_map[j] = highlighter_info;
}
}
return color_map;
}
String CScriptSyntaxHighlighter::get_name() const {
String CScriptSyntaxHighlighter::_get_name() const {
return "CScript";
}
List<String> CScriptSyntaxHighlighter::get_supported_languages() {
List<String> languages;
Array CScriptSyntaxHighlighter::_get_supported_languages() const {
Array languages;
languages.push_back("CScript");
return languages;
}
void CScriptSyntaxHighlighter::_update_cache() {
font_color = text_editor->get_theme_color("font_color");
symbol_color = text_editor->get_theme_color("symbol_color");
function_color = text_editor->get_theme_color("function_color");
number_color = text_editor->get_theme_color("number_color");
member_color = text_editor->get_theme_color("member_variable_color");
font_color = text_edit->get_theme_color("font_color");
symbol_color = text_edit->get_theme_color("symbol_color");
function_color = text_edit->get_theme_color("function_color");
number_color = text_edit->get_theme_color("number_color");
member_color = text_edit->get_theme_color("member_variable_color");
const String text_editor_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
const bool default_theme = text_editor_color_theme == "Default";
const String text_edit_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
const bool default_theme = text_edit_color_theme == "Default";
if (default_theme || EditorSettings::get_singleton()->is_dark_theme()) {
function_definition_color = Color(0.4, 0.9, 1.0);
@ -374,7 +373,7 @@ void CScriptSyntaxHighlighter::_update_cache() {
EDITOR_DEF("text_editor/highlighting/cscript/function_definition_color", function_definition_color);
EDITOR_DEF("text_editor/highlighting/cscript/node_path_color", node_path_color);
if (text_editor_color_theme == "Adaptive" || default_theme) {
if (text_edit_color_theme == "Adaptive" || default_theme) {
EditorSettings::get_singleton()->set_initial_value(
"text_editor/highlighting/cscript/function_definition_color",
function_definition_color,

View File

@ -62,10 +62,10 @@ public:
static SyntaxHighlighter *create();
virtual void _update_cache();
virtual RBMap<int, TextEdit::HighlighterInfo> _get_line_syntax_highlighting(int p_line);
virtual Dictionary _get_line_syntax_highlighting(int p_line);
virtual String get_name() const;
virtual List<String> get_supported_languages();
virtual String _get_name() const;
virtual Array _get_supported_languages() const;
};
#endif // CSCRIPT_HIGHLIGHTER_H

View File

@ -31,7 +31,6 @@
#include "gdscript_highlighter.h"
#include "../gdscript_tokenizer.h"
#include "editor/editor_settings.h"
#include "scene/gui/text_edit.h"
inline bool _is_symbol(CharType c) {
return is_symbol(c);
@ -57,8 +56,8 @@ static bool _is_bin_symbol(CharType c) {
return (c == '0' || c == '1');
}
RBMap<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
RBMap<int, TextEdit::HighlighterInfo> color_map;
Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
Dictionary color_map;
Type next_type = NONE;
Type current_type = NONE;
@ -82,14 +81,14 @@ RBMap<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_synta
Color keyword_color;
Color color;
int in_region = text_editor->_is_line_in_region(p_line);
int in_region = text_edit->_is_line_in_region(p_line);
int deregion = 0;
const RBMap<int, TextEdit::Text::ColorRegionInfo> cri_map = text_editor->_get_line_color_region_info(p_line);
const String &str = text_editor->get_line(p_line);
const RBMap<int, TextEdit::Text::ColorRegionInfo> cri_map = text_edit->_get_line_color_region_info(p_line);
const String &str = text_edit->get_line(p_line);
Color prev_color;
for (int j = 0; j < str.length(); j++) {
TextEdit::HighlighterInfo highlighter_info;
Dictionary highlighter_info;
if (deregion > 0) {
deregion--;
@ -101,7 +100,7 @@ RBMap<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_synta
if (deregion != 0) {
if (color != prev_color) {
prev_color = color;
highlighter_info.color = color;
highlighter_info["color"] = color;
color_map[j] = highlighter_info;
}
continue;
@ -163,7 +162,7 @@ RBMap<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_synta
in_region = cri.region;
}
} else {
TextEdit::ColorRegion cr = text_editor->_get_color_region(cri.region);
TextEdit::ColorRegion cr = text_edit->_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();
@ -184,10 +183,10 @@ RBMap<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_synta
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);
if (text_edit->has_keyword_color(word)) {
col = text_edit->get_keyword_color(word);
} else if (text_edit->has_member_color(word)) {
col = text_edit->get_member_color(word);
}
if (col != Color()) {
@ -278,7 +277,7 @@ RBMap<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_synta
if (in_region >= 0) {
next_type = REGION;
color = text_editor->_get_color_region(in_region).color;
color = text_edit->_get_color_region(in_region).color;
} else if (in_node_path) {
next_type = NODE_PATH;
color = node_path_color;
@ -337,32 +336,32 @@ RBMap<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_synta
if (color != prev_color) {
prev_color = color;
highlighter_info.color = color;
highlighter_info["color"] = color;
color_map[j] = highlighter_info;
}
}
return color_map;
}
String GDScriptSyntaxHighlighter::get_name() const {
String GDScriptSyntaxHighlighter::_get_name() const {
return "GDScript";
}
List<String> GDScriptSyntaxHighlighter::get_supported_languages() {
List<String> languages;
Array GDScriptSyntaxHighlighter::_get_supported_languages() const {
Array languages;
languages.push_back("GDScript");
return languages;
}
void GDScriptSyntaxHighlighter::_update_cache() {
font_color = text_editor->get_theme_color("font_color");
symbol_color = text_editor->get_theme_color("symbol_color");
function_color = text_editor->get_theme_color("function_color");
number_color = text_editor->get_theme_color("number_color");
member_color = text_editor->get_theme_color("member_variable_color");
font_color = text_edit->get_theme_color("font_color");
symbol_color = text_edit->get_theme_color("symbol_color");
function_color = text_edit->get_theme_color("function_color");
number_color = text_edit->get_theme_color("number_color");
member_color = text_edit->get_theme_color("member_variable_color");
const String text_editor_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
const bool default_theme = text_editor_color_theme == "Default";
const String text_edit_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
const bool default_theme = text_edit_color_theme == "Default";
if (default_theme || EditorSettings::get_singleton()->is_dark_theme()) {
function_definition_color = Color(0.4, 0.9, 1.0);
@ -374,7 +373,7 @@ void GDScriptSyntaxHighlighter::_update_cache() {
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) {
if (text_edit_color_theme == "Adaptive" || default_theme) {
EditorSettings::get_singleton()->set_initial_value(
"text_editor/highlighting/gdscript/function_definition_color",
function_definition_color,

View File

@ -62,10 +62,10 @@ public:
static SyntaxHighlighter *create();
virtual void _update_cache();
virtual RBMap<int, TextEdit::HighlighterInfo> _get_line_syntax_highlighting(int p_line);
virtual Dictionary _get_line_syntax_highlighting(int p_line);
virtual String get_name() const;
virtual List<String> get_supported_languages();
virtual String _get_name() const;
virtual Array _get_supported_languages() const;
};
#endif // GDSCRIPT_HIGHLIGHTER_H

View File

@ -30,16 +30,16 @@
#include "text_edit.h"
#include "core/object/message_queue.h"
#include "core/config/project_settings.h"
#include "core/input/input.h"
#include "core/input/shortcut.h"
#include "core/object/message_queue.h"
#include "core/object/script_language.h"
#include "core/os/keyboard.h"
#include "core/os/os.h"
#include "core/config/project_settings.h"
#include "core/object/script_language.h"
#include "label.h"
#include "scene/gui/popup_menu.h"
#include "scene/gui/scroll_bar.h"
#include "core/input/shortcut.h"
#include "scene/main/timer.h"
#include "scene/main/viewport.h"
@ -985,7 +985,7 @@ void TextEdit::_notification(int p_what) {
break;
}
RBMap<int, HighlighterInfo> color_map;
Dictionary color_map;
if (syntax_coloring) {
color_map = _get_line_syntax_highlighting(minimap_line);
}
@ -1028,7 +1028,7 @@ void TextEdit::_notification(int p_what) {
for (int j = 0; j < str.length(); j++) {
if (syntax_coloring) {
if (color_map.has(last_wrap_column + j)) {
current_color = color_map[last_wrap_column + j].color;
current_color = color_map[last_wrap_column + j].get("color");
if (readonly) {
current_color.a = cache.font_color_readonly.a;
}
@ -1125,7 +1125,7 @@ void TextEdit::_notification(int p_what) {
const String &fullstr = text[line];
LineDrawingCache cache_entry;
RBMap<int, HighlighterInfo> color_map;
Dictionary color_map;
if (syntax_coloring) {
color_map = _get_line_syntax_highlighting(line);
}
@ -1342,7 +1342,7 @@ void TextEdit::_notification(int p_what) {
for (; j < str.length(); j++) {
if (syntax_coloring) {
if (color_map.has(last_wrap_column + j)) {
current_color = color_map[last_wrap_column + j].color;
current_color = color_map[last_wrap_column + j].get("color");
if (readonly && current_color.a > cache.font_color_readonly.a) {
current_color.a = cache.font_color_readonly.a;
}
@ -4590,7 +4590,7 @@ void TextEdit::update_cursor_wrap_offset() {
}
bool TextEdit::line_wraps(int line) const {
ERR_FAIL_INDEX_V(line, text.size(), 0);
ERR_FAIL_INDEX_V(line, text.size(), false);
if (!is_wrap_enabled()) {
return false;
}
@ -5428,20 +5428,20 @@ void TextEdit::_update_caches() {
cache.executing_icon = get_theme_icon("TextEditorPlay", "EditorIcons");
text.set_font(cache.font);
if (syntax_highlighter) {
syntax_highlighter->_update_cache();
if (syntax_highlighter.is_valid()) {
syntax_highlighter->update_cache();
}
}
SyntaxHighlighter *TextEdit::_get_syntax_highlighting() {
Ref<SyntaxHighlighter> TextEdit::get_syntax_highlighting() {
return syntax_highlighter;
}
void TextEdit::_set_syntax_highlighting(SyntaxHighlighter *p_syntax_highlighter) {
void TextEdit::set_syntax_highlighting(Ref<SyntaxHighlighter> p_syntax_highlighter) {
syntax_highlighter = p_syntax_highlighter;
if (syntax_highlighter) {
syntax_highlighter->set_text_editor(this);
syntax_highlighter->_update_cache();
if (syntax_highlighter.is_valid()) {
syntax_highlighter->set_text_edit(this);
syntax_highlighter->update_cache();
}
syntax_highlighting_cache.clear();
update();
@ -7527,6 +7527,9 @@ void TextEdit::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_syntax_coloring", "enable"), &TextEdit::set_syntax_coloring);
ClassDB::bind_method(D_METHOD("is_syntax_coloring_enabled"), &TextEdit::is_syntax_coloring_enabled);
ClassDB::bind_method(D_METHOD("set_syntax_highlighting", "syntax_highlighter"), &TextEdit::set_syntax_highlighting);
ClassDB::bind_method(D_METHOD("get_syntax_highlighting"), &TextEdit::get_syntax_highlighting);
ClassDB::bind_method(D_METHOD("set_highlight_current_line", "enabled"), &TextEdit::set_highlight_current_line);
ClassDB::bind_method(D_METHOD("is_highlight_current_line_enabled"), &TextEdit::is_highlight_current_line_enabled);
@ -7559,6 +7562,7 @@ void TextEdit::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "readonly"), "set_readonly", "is_readonly");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_current_line"), "set_highlight_current_line", "is_highlight_current_line_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "syntax_highlighting"), "set_syntax_coloring", "is_syntax_coloring_enabled");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "syntax_highlighter", PROPERTY_HINT_RESOURCE_TYPE, "SyntaxHighlighter"), "set_syntax_highlighting", "get_syntax_highlighting");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_line_numbers"), "set_show_line_numbers", "is_show_line_numbers_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_tabs"), "set_draw_tabs", "is_drawing_tabs");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_spaces"), "set_draw_spaces", "is_drawing_spaces");
@ -7623,7 +7627,6 @@ TextEdit::TextEdit() {
wrap_at = 0;
wrap_right_offset = 10;
set_focus_mode(FOCUS_ALL);
syntax_highlighter = nullptr;
_update_caches();
cache.row_height = 1;
cache.line_spacing = 1;
@ -7761,18 +7764,18 @@ TextEdit::~TextEdit() {
///////////////////////////////////////////////////////////////////////////////
RBMap<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int p_line) {
Dictionary TextEdit::_get_line_syntax_highlighting(int p_line) {
if (syntax_highlighting_cache.has(p_line)) {
return syntax_highlighting_cache[p_line];
}
if (syntax_highlighter != nullptr) {
RBMap<int, HighlighterInfo> color_map = syntax_highlighter->_get_line_syntax_highlighting(p_line);
if (syntax_highlighter.is_valid()) {
Dictionary color_map = syntax_highlighter->get_line_syntax_highlighting(p_line);
syntax_highlighting_cache[p_line] = color_map;
return color_map;
}
RBMap<int, HighlighterInfo> color_map;
Dictionary color_map;
bool prev_is_char = false;
bool prev_is_number = false;
@ -7791,7 +7794,7 @@ RBMap<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(in
const String &str = text[p_line];
Color prev_color;
for (int j = 0; j < str.length(); j++) {
HighlighterInfo highlighter_info;
Dictionary highlighter_info;
if (deregion > 0) {
deregion--;
@ -7803,7 +7806,7 @@ RBMap<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(in
if (deregion != 0) {
if (color != prev_color) {
prev_color = color;
highlighter_info.color = color;
highlighter_info["color"] = color;
color_map[j] = highlighter_info;
}
continue;
@ -7946,7 +7949,7 @@ RBMap<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(in
if (color != prev_color) {
prev_color = color;
highlighter_info.color = color;
highlighter_info["color"] = color;
color_map[j] = highlighter_info;
}
}
@ -7954,11 +7957,3 @@ RBMap<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(in
syntax_highlighting_cache[p_line] = color_map;
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

@ -32,7 +32,8 @@
#include "scene/gui/control.h"
class SyntaxHighlighter;
#include "scene/resources/syntax_highlighter.h"
class PopupMenu;
class Timer;
class HScrollBar;
@ -42,10 +43,6 @@ class TextEdit : public Control {
GDCLASS(TextEdit, Control);
public:
struct HighlighterInfo {
Color color;
};
struct ColorRegion {
Color color;
String begin_key;
@ -273,7 +270,7 @@ private:
} cache;
RBMap<int, int> color_region_cache;
RBMap<int, RBMap<int, HighlighterInfo>> syntax_highlighting_cache;
RBMap<int, Dictionary> syntax_highlighting_cache;
struct TextOperation {
enum Type {
@ -316,11 +313,11 @@ private:
void _do_text_op(const TextOperation &p_op, bool p_reverse);
//syntax coloring
SyntaxHighlighter *syntax_highlighter;
Ref<SyntaxHighlighter> syntax_highlighter;
HashMap<String, Color> keywords;
HashMap<String, Color> member_keywords;
RBMap<int, HighlighterInfo> _get_line_syntax_highlighting(int p_line);
Dictionary _get_line_syntax_highlighting(int p_line);
Vector<ColorRegion> color_regions;
@ -565,8 +562,8 @@ protected:
static void _bind_methods();
public:
SyntaxHighlighter *_get_syntax_highlighting();
void _set_syntax_highlighting(SyntaxHighlighter *p_syntax_highlighter);
Ref<SyntaxHighlighter> get_syntax_highlighting();
void set_syntax_highlighting(Ref<SyntaxHighlighter> p_syntax_highlighter);
int _is_line_in_region(int p_line);
ColorRegion _get_color_region(int p_region) const;
@ -884,20 +881,4 @@ VARIANT_ENUM_CAST(TextEdit::MenuItems);
VARIANT_ENUM_CAST(TextEdit::SearchFlags);
VARIANT_ENUM_CAST(TextEdit::SearchResult);
class SyntaxHighlighter {
protected:
TextEdit *text_editor;
public:
virtual ~SyntaxHighlighter() {}
virtual void _update_cache() = 0;
virtual RBMap<int, TextEdit::HighlighterInfo> _get_line_syntax_highlighting(int p_line) = 0;
virtual String get_name() const = 0;
virtual List<String> get_supported_languages() = 0;
void set_text_editor(TextEdit *p_text_editor);
TextEdit *get_text_editor();
};
#endif // TEXT_EDIT_H

View File

@ -171,6 +171,7 @@
#include "scene/resources/sky.h"
#include "scene/resources/sphere_shape.h"
#include "scene/resources/surface_tool.h"
#include "scene/resources/syntax_highlighter.h"
#include "scene/resources/text_file.h"
#include "scene/resources/texture.h"
#include "scene/resources/video_stream.h"
@ -355,6 +356,7 @@ void register_scene_types() {
ClassDB::register_class<Tree>();
ClassDB::register_class<TextEdit>();
ClassDB::register_class<SyntaxHighlighter>();
ClassDB::register_virtual_class<TreeItem>();
ClassDB::register_class<OptionButton>();

View File

@ -0,0 +1,77 @@
/*************************************************************************/
/* syntax_highlighter.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 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 "syntax_highlighter.h"
#include "core/object/script_language.h"
#include "scene/gui/text_edit.h"
Dictionary SyntaxHighlighter::get_line_syntax_highlighting(int p_line) {
return call("_get_line_syntax_highlighting", p_line);
}
void SyntaxHighlighter::update_cache() {
call("_update_cache");
}
String SyntaxHighlighter::_get_name() const {
ScriptInstance *si = get_script_instance();
if (si && si->has_method("_get_name")) {
return si->call("_get_name");
}
return "Unamed";
}
Array SyntaxHighlighter::_get_supported_languages() const {
ScriptInstance *si = get_script_instance();
if (si && si->has_method("_get_supported_languages")) {
return si->call("_get_supported_languages");
}
return Array();
}
void SyntaxHighlighter::set_text_edit(TextEdit *p_text_edit) {
text_edit = p_text_edit;
}
TextEdit *SyntaxHighlighter::get_text_edit() {
return text_edit;
}
void SyntaxHighlighter::_bind_methods() {
ClassDB::bind_method(D_METHOD("_get_line_syntax_highlighting", "p_line"), &SyntaxHighlighter::_get_line_syntax_highlighting);
ClassDB::bind_method(D_METHOD("_update_cache"), &SyntaxHighlighter::_update_cache);
ClassDB::bind_method(D_METHOD("get_text_edit"), &SyntaxHighlighter::get_text_edit);
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_name"));
BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_get_supported_languages"));
BIND_VMETHOD(MethodInfo(Variant::DICTIONARY, "_get_line_syntax_highlighting", PropertyInfo(Variant::INT, "p_line")));
BIND_VMETHOD(MethodInfo("_update_cache"));
}

View File

@ -0,0 +1,63 @@
/*************************************************************************/
/* syntax_highlighter.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 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 SYNTAX_HIGHLIGHTER_H
#define SYNTAX_HIGHLIGHTER_H
#include "core/object/resource.h"
class TextEdit;
class SyntaxHighlighter : public Resource {
GDCLASS(SyntaxHighlighter, Resource)
protected:
TextEdit *text_edit;
static void _bind_methods();
public:
Dictionary get_line_syntax_highlighting(int p_line);
virtual Dictionary _get_line_syntax_highlighting(int p_line) { return Dictionary(); }
void update_cache();
virtual void _update_cache() {}
virtual String _get_name() const;
virtual Array _get_supported_languages() const;
void set_text_edit(TextEdit *p_text_edit);
TextEdit *get_text_edit();
SyntaxHighlighter() {}
virtual ~SyntaxHighlighter() {}
};
#endif