Merge pull request #17923 from Paulb23/add_abstract_syntax_highlighter
Abstracted the syntax highlighter from text edit.
This commit is contained in:
commit
5ede505f14
11 changed files with 828 additions and 272 deletions
|
|
@ -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];
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue