Repository: godotengine/godot
File: editor/editor_node.cpp (C++)

Code before:

	InspectorDock::get_singleton()->set_warning(editable_warning);

	if (InspectorDock::get_inspector_singleton()->is_using_folding() == disable_folding) {
		InspectorDock::get_inspector_singleton()->set_use_folding(!disable_folding);
	}

	/* Take care of PLUGIN EDITOR */

	if (!inspector_only) {
		EditorPlugin *main_plugin = editor_data.get_editor(current_obj);

		int plugin_index = 0;
		for (; plugin_index < editor_table.size(); plugin_index++) {
			if (editor_table[plugin_index] == main_plugin) {
				if (!main_editor_buttons[plugin_index]->is_visible()) {
					main_plugin = nullptr; // if button is not visible, then no plugin active
				}

				break;
			}
		}

		if (main_plugin) {
			// special case if use of external editor is true
			Resource *current_res = Object::cast_to<Resource>(current_obj);
			if (main_plugin->get_name() == "Script" && current_obj->get_class_name() != StringName("VisualScript") && current_res && !current_res->is_built_in() && (bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor")) || overrides_external_editor(current_obj))) {
				if (!changing_scene) {
					main_plugin->edit(current_obj);
				}
			}

			else if (main_plugin != editor_plugin_screen && (!ScriptEditor::get_singleton() || !ScriptEditor::get_singleton()->is_visible_in_tree() || ScriptEditor::get_singleton()->can_take_away_focus())) {
				// update screen main_plugin
				_editor_select(plugin_index);
				main_plugin->edit(current_obj);
			} else {
				editor_plugin_screen->edit(current_obj);
			}
		}

		Vector<EditorPlugin *> sub_plugins;

		if (!_is_class_editor_disabled_by_feature_profile(current_obj->get_class())) {
			sub_plugins = editor_data.get_subeditors(current_obj);
		}

		if (!sub_plugins.is_empty()) {
			_display_top_editors(false);

			_set_top_editors(sub_plugins);

Review comment (refactor):
Could use `Object::cast_to` but then I'd have to either forward declare `VisualScript` or include it, and that felt overkill for this hack.

This should be refactored so that the editor doesn't need to special case `VisualScript` (typically by adding a `ScriptLanguage` API to find if a language can be edited in an external editor or not).

Code after:
	InspectorDock::get_singleton()->set_warning(editable_warning);

	if (InspectorDock::get_inspector_singleton()->is_using_folding() == disable_folding) {
		InspectorDock::get_inspector_singleton()->set_use_folding(!disable_folding);
	}

	/* Take care of PLUGIN EDITOR */

	if (!inspector_only) {
		EditorPlugin *main_plugin = editor_data.get_editor(current_obj);

		int plugin_index = 0;
		for (; plugin_index < editor_table.size(); plugin_index++) {
			if (editor_table[plugin_index] == main_plugin) {
				if (!main_editor_buttons[plugin_index]->is_visible()) {
					main_plugin = nullptr; // if button is not visible, then no plugin active
				}

				break;
			}
		}

		if (main_plugin) {
			// special case if use of external editor is true
			Resource *current_res = Object::cast_to<Resource>(current_obj);
			if (main_plugin->get_name() == "Script" && current_obj->is_class("VisualScript") && current_res && !current_res->is_built_in() && (bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor")) || overrides_external_editor(current_obj))) {
				if (!changing_scene) {
					main_plugin->edit(current_obj);
				}
			}

			else if (main_plugin != editor_plugin_screen && (!ScriptEditor::get_singleton() || !ScriptEditor::get_singleton()->is_visible_in_tree() || ScriptEditor::get_singleton()->can_take_away_focus())) {
				// update screen main_plugin
				_editor_select(plugin_index);
				main_plugin->edit(current_obj);
			} else {
				editor_plugin_screen->edit(current_obj);
			}
		}

		Vector<EditorPlugin *> sub_plugins;

		if (!_is_class_editor_disabled_by_feature_profile(current_obj->get_class())) {
			sub_plugins = editor_data.get_subeditors(current_obj);
		}

		if (!sub_plugins.is_empty()) {
			_display_top_editors(false);

			_set_top_editors(sub_plugins);
			_set_editing_top_editors(current_obj);