Expose Syntax highlighter for editor plugins

This commit is contained in:
Paulb23 2020-03-07 14:29:44 +00:00
parent 2f1080be9b
commit 156daddaaf
11 changed files with 71 additions and 19 deletions

View file

@ -3602,6 +3602,7 @@ void EditorNode::register_editor_types() {
ClassDB::register_class<EditorFileSystemDirectory>();
ClassDB::register_class<EditorVCSInterface>();
ClassDB::register_virtual_class<ScriptEditor>();
ClassDB::register_virtual_class<ScriptEditorBase>();
ClassDB::register_virtual_class<EditorInterface>();
ClassDB::register_class<EditorExportPlugin>();
ClassDB::register_class<EditorResourceConversionPlugin>();

View file

@ -63,6 +63,8 @@ void ScriptEditorBase::_bind_methods() {
// TODO: This signal is no use for VisualScript.
ADD_SIGNAL(MethodInfo("search_in_files_requested", PropertyInfo(Variant::STRING, "text")));
ADD_SIGNAL(MethodInfo("replace_in_files_requested", PropertyInfo(Variant::STRING, "text")));
BIND_VMETHOD(MethodInfo("add_syntax_highlighter", PropertyInfo(Variant::OBJECT, "highlighter")));
}
static bool _is_built_in_script(Script *p_script) {
@ -2019,8 +2021,11 @@ bool ScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool p_gra
if (p_resource->get_class_name() != StringName("VisualScript")) {
bool highlighter_set = false;
for (int i = 0; i < syntax_highlighters_func_count; i++) {
SyntaxHighlighter *highlighter = syntax_highlighters_funcs[i]();
for (int i = 0; i < syntax_highlighters.size(); i++) {
Ref<SyntaxHighlighter> highlighter = syntax_highlighters[i]->_create();
if (highlighter.is_null()) {
continue;
}
se->add_syntax_highlighter(highlighter);
if (script != nullptr && !highlighter_set) {
@ -2768,6 +2773,18 @@ Vector<Ref<Script>> ScriptEditor::get_open_scripts() const {
return out_scripts;
}
Array ScriptEditor::_get_open_script_editors() const {
Array script_editors;
for (int i = 0; i < tab_container->get_child_count(); i++) {
ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
if (!se) {
continue;
}
script_editors.push_back(se);
}
return script_editors;
}
void ScriptEditor::set_scene_root_script(Ref<Script> p_script) {
bool open_dominant = EditorSettings::get_singleton()->get("text_editor/files/open_dominant_script_on_scene_change");
@ -2813,12 +2830,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_syntax_highlighter(const Ref<SyntaxHighlighter> &p_syntax_highlighter) {
if (syntax_highlighters.find(p_syntax_highlighter) == -1) {
syntax_highlighters.push_back(p_syntax_highlighter);
}
}
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;
void ScriptEditor::unregister_syntax_highlighter(const Ref<SyntaxHighlighter> &p_syntax_highlighter) {
syntax_highlighters.erase(p_syntax_highlighter);
}
int ScriptEditor::script_editor_func_count = 0;
@ -2927,6 +2946,12 @@ void ScriptEditor::_bind_methods() {
ClassDB::bind_method("_update_members_overview", &ScriptEditor::_update_members_overview);
ClassDB::bind_method("_update_recent_scripts", &ScriptEditor::_update_recent_scripts);
ClassDB::bind_method("get_current_editor", &ScriptEditor::_get_current_editor);
ClassDB::bind_method("get_open_script_editors", &ScriptEditor::_get_open_script_editors);
ClassDB::bind_method(D_METHOD("register_syntax_highlighter", "syntax_highlighter"), &ScriptEditor::register_syntax_highlighter);
ClassDB::bind_method(D_METHOD("unregister_syntax_highlighter", "syntax_highlighter"), &ScriptEditor::unregister_syntax_highlighter);
ClassDB::bind_method(D_METHOD("get_drag_data_fw", "point", "from"), &ScriptEditor::get_drag_data_fw);
ClassDB::bind_method(D_METHOD("can_drop_data_fw", "point", "data", "from"), &ScriptEditor::can_drop_data_fw);
ClassDB::bind_method(D_METHOD("drop_data_fw", "point", "data", "from"), &ScriptEditor::drop_data_fw);

View file

@ -236,14 +236,12 @@ class ScriptEditor : public PanelContainer {
enum {
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];
Vector<Ref<SyntaxHighlighter> > syntax_highlighters;
struct ScriptHistory {
Control *control;
@ -322,6 +320,7 @@ class ScriptEditor : public PanelContainer {
void _script_created(Ref<Script> p_script);
ScriptEditorBase *_get_current_editor() const;
Array _get_open_script_editors() const;
void _save_layout();
void _editor_settings_changed();
@ -441,7 +440,9 @@ public:
void set_live_auto_reload_running_scripts(bool p_enabled);
static void register_create_syntax_highlighter_function(CreateSyntaxHighlighterFunc p_func);
void register_syntax_highlighter(const Ref<SyntaxHighlighter> &p_syntax_highlighter);
void unregister_syntax_highlighter(const Ref<SyntaxHighlighter> &p_syntax_highlighter);
static void register_create_script_editor_function(CreateScriptEditorFunc p_func);
ScriptEditor(EditorNode *p_editor);

View file

@ -1410,6 +1410,8 @@ void ScriptTextEditor::_bind_methods() {
ClassDB::bind_method("get_drag_data_fw", &ScriptTextEditor::get_drag_data_fw);
ClassDB::bind_method("can_drop_data_fw", &ScriptTextEditor::can_drop_data_fw);
ClassDB::bind_method("drop_data_fw", &ScriptTextEditor::drop_data_fw);
ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &ScriptTextEditor::add_syntax_highlighter);
}
Control *ScriptTextEditor::get_edit_menu() {

View file

@ -471,6 +471,7 @@ void TextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) {
}
void TextEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &TextEditor::add_syntax_highlighter);
}
static ScriptEditorBase *create_editor(const RES &p_resource) {

View file

@ -387,6 +387,8 @@ void GDScriptSyntaxHighlighter::_update_cache() {
type_color = EDITOR_GET("text_edit/highlighting/base_type_color");
}
SyntaxHighlighter *GDScriptSyntaxHighlighter::create() {
return memnew(GDScriptSyntaxHighlighter);
Ref<SyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {
Ref<GDScriptSyntaxHighlighter> syntax_highlighter;
syntax_highlighter.instance();
return syntax_highlighter;
}

View file

@ -34,6 +34,8 @@
#include "scene/gui/text_edit.h"
class GDScriptSyntaxHighlighter : public SyntaxHighlighter {
GDCLASS(GDScriptSyntaxHighlighter, SyntaxHighlighter)
private:
enum Type {
NONE,
@ -60,13 +62,13 @@ private:
Color type_color;
public:
static SyntaxHighlighter *create();
virtual void _update_cache() override;
virtual Dictionary _get_line_syntax_highlighting(int p_line) override;
virtual void _update_cache();
virtual Dictionary _get_line_syntax_highlighting(int p_line);
virtual String _get_name() const override;
virtual Array _get_supported_languages() const override;
virtual String _get_name() const;
virtual Array _get_supported_languages() const;
virtual Ref<SyntaxHighlighter> _create() const override;
};
#endif // GDSCRIPT_HIGHLIGHTER_H

View file

@ -142,6 +142,12 @@ static void _editor_init() {
gd_export.instance();
EditorExport::get_singleton()->add_export_plugin(gd_export);
#ifdef TOOLS_ENABLED
Ref<GDScriptSyntaxHighlighter> gdscript_syntax_highlighter;
gdscript_syntax_highlighter.instance();
ScriptEditor::get_singleton()->register_syntax_highlighter(gdscript_syntax_highlighter);
#endif
#ifndef GDSCRIPT_NO_LSP
register_lsp_types();
GDScriptLanguageServer *lsp_plugin = memnew(GDScriptLanguageServer);
@ -166,7 +172,6 @@ void register_gdscript_types() {
ResourceSaver::add_resource_format_saver(resource_saver_gd);
#ifdef TOOLS_ENABLED
ScriptEditor::register_create_syntax_highlighter_function(GDScriptSyntaxHighlighter::create);
EditorNode::add_init_callback(_editor_init);
gdscript_translation_parser_plugin.instance();

View file

@ -4686,6 +4686,8 @@ void VisualScriptEditor::_bind_methods() {
ClassDB::bind_method("_update_members", &VisualScriptEditor::_update_members);
ClassDB::bind_method("_generic_search", &VisualScriptEditor::_generic_search);
ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &VisualScriptEditor::add_syntax_highlighter);
}
VisualScriptEditor::VisualScriptEditor() {

View file

@ -65,6 +65,15 @@ TextEdit *SyntaxHighlighter::get_text_edit() {
return text_edit;
}
Ref<SyntaxHighlighter> SyntaxHighlighter::_create() const {
Ref<SyntaxHighlighter> syntax_highlighter;
syntax_highlighter.instance();
if (get_script_instance()) {
syntax_highlighter->set_script(get_script_instance()->get_script());
}
return syntax_highlighter;
}
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);

View file

@ -56,6 +56,8 @@ public:
void set_text_edit(TextEdit *p_text_edit);
TextEdit *get_text_edit();
virtual Ref<SyntaxHighlighter> _create() const;
SyntaxHighlighter() {}
virtual ~SyntaxHighlighter() {}
};