merge parts of script and text editor

This commit is contained in:
Simon Döhl 2026-02-01 13:28:34 +01:00
parent 98782b6c8c
commit 2363720b53
16 changed files with 2031 additions and 2637 deletions

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="ScriptEditorBase" inherits="VBoxContainer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<class name="ScriptEditorBase" inherits="Control" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Base editor for editing scripts in the [ScriptEditor].
</brief_description>

View file

@ -35,7 +35,7 @@
#include "core/string/string_builder.h"
#include "editor/editor_node.h"
#include "editor/editor_string_names.h"
#include "editor/script/script_editor_plugin.h"
#include "editor/script/syntax_highlighters.h"
#include "editor/settings/editor_settings.h"
#include "editor/themes/editor_scale.h"
#include "editor/themes/editor_theme_manager.h"

View file

@ -46,7 +46,7 @@
#include "editor/inspector/editor_resource_picker.h"
#include "editor/inspector/property_selector.h"
#include "editor/scene/scene_tree_editor.h"
#include "editor/script/script_editor_plugin.h"
#include "editor/script/syntax_highlighters.h"
#include "editor/settings/editor_settings.h"
#include "editor/settings/project_settings_editor.h"
#include "editor/themes/editor_scale.h"

View file

@ -123,6 +123,7 @@
#include "editor/script/editor_script.h"
#include "editor/script/editor_script_plugin.h"
#include "editor/script/script_editor_plugin.h"
#include "editor/script/syntax_highlighters.h"
#include "editor/settings/editor_command_palette.h"
#include "editor/settings/editor_feature_profile.h"
#include "editor/settings/editor_settings.h"

View file

@ -0,0 +1,697 @@
/**************************************************************************/
/* script_editor_base.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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 "script_editor_base.h"
#include "core/io/json.h"
#include "editor/editor_node.h"
#include "editor/script/script_editor_plugin.h"
#include "editor/script/syntax_highlighters.h"
#include "editor/settings/editor_settings.h"
#include "scene/gui/menu_button.h"
#include "scene/gui/rich_text_label.h"
#include "scene/gui/split_container.h"
void ScriptEditorBase::_bind_methods() {
ADD_SIGNAL(MethodInfo("name_changed"));
// First use in TextEditorBase.
ADD_SIGNAL(MethodInfo("edited_script_changed"));
ADD_SIGNAL(MethodInfo("search_in_files_requested", PropertyInfo(Variant::STRING, "text")));
ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &ScriptEditorBase::add_syntax_highlighter);
ClassDB::bind_method(D_METHOD("get_base_editor"), &ScriptEditorBase::get_base_editor);
// First use in ScriptTextEditor.
ADD_SIGNAL(MethodInfo("request_save_history"));
ADD_SIGNAL(MethodInfo("request_help", PropertyInfo(Variant::STRING, "topic")));
ADD_SIGNAL(MethodInfo("request_open_script_at_line", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::INT, "line")));
ADD_SIGNAL(MethodInfo("go_to_help", PropertyInfo(Variant::STRING, "what")));
ADD_SIGNAL(MethodInfo("request_save_previous_state", PropertyInfo(Variant::DICTIONARY, "state")));
ADD_SIGNAL(MethodInfo("replace_in_files_requested", PropertyInfo(Variant::STRING, "text")));
ADD_SIGNAL(MethodInfo("go_to_method", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::STRING, "method")));
}
String ScriptEditorBase::get_name() {
String name;
name = edited_res->get_path().get_file();
if (name.is_empty()) {
// This appears for newly created built-in text_files before saving the scene.
name = TTR("[unsaved]");
} else if (edited_res->is_built_in()) {
const String &text_file_name = edited_res->get_name();
if (!text_file_name.is_empty()) {
// If the built-in text_file has a custom resource name defined,
// display the built-in text_file name as follows: `ResourceName (scene_file.tscn)`
name = vformat("%s (%s)", text_file_name, name.get_slice("::", 0));
}
}
if (is_unsaved()) {
name += "(*)";
}
return name;
}
Ref<Texture2D> ScriptEditorBase::get_theme_icon() {
return EditorNode::get_singleton()->get_object_icon(edited_res.ptr());
}
void ScriptEditorBase::tag_saved_version() {
edited_file_data.last_modified_time = FileAccess::get_modified_time(edited_file_data.path);
}
//// TextEditorBase
TextEditorBase *TextEditorBase::EditMenus::_get_active_editor() {
return Object::cast_to<TextEditorBase>(ScriptEditor::get_singleton()->get_current_editor());
}
void TextEditorBase::EditMenus::_edit_option(int p_op) {
TextEditorBase *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
script_text_editor->_edit_option(p_op);
}
void TextEditorBase::EditMenus::_prepare_edit_menu() {
TextEditorBase *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
const CodeEdit *tx = script_text_editor->code_editor->get_text_editor();
PopupMenu *popup = edit_menu->get_popup();
popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());
popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());
}
void TextEditorBase::EditMenus::_update_highlighter_menu() {
TextEditorBase *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
Ref<EditorSyntaxHighlighter> current_highlighter = script_text_editor->get_code_editor()->get_text_editor()->get_syntax_highlighter();
highlighter_menu->clear();
for (const Ref<EditorSyntaxHighlighter> &highlighter : script_text_editor->highlighters) {
highlighter_menu->add_radio_check_item(highlighter->_get_name());
highlighter_menu->set_item_checked(-1, highlighter == current_highlighter);
}
}
void TextEditorBase::EditMenus::_change_syntax_highlighter(int p_idx) {
TextEditorBase *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
ERR_FAIL_INDEX(p_idx, (int)script_text_editor->highlighters.size());
script_text_editor->set_syntax_highlighter(script_text_editor->highlighters[p_idx]);
}
void TextEditorBase::EditMenus::_update_bookmark_list() {
TextEditorBase *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
bookmarks_menu->clear();
bookmarks_menu->reset_size();
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
PackedInt32Array bookmark_list = script_text_editor->code_editor->get_text_editor()->get_bookmarked_lines();
if (bookmark_list.is_empty()) {
return;
}
bookmarks_menu->add_separator();
for (int32_t bookmark : bookmark_list) {
// Strip edges to remove spaces or tabs.
// Also replace any tabs by spaces, since we can't print tabs in the menu.
String line = script_text_editor->code_editor->get_text_editor()->get_line(bookmark).replace("\t", " ").strip_edges();
// Limit the size of the line if too big.
if (line.length() > 50) {
line = line.substr(0, 50);
}
bookmarks_menu->add_item(String::num_int64(bookmark + 1) + " - `" + line + "`");
bookmarks_menu->set_item_metadata(-1, bookmark);
}
}
void TextEditorBase::EditMenus::_bookmark_item_pressed(int p_idx) {
TextEditorBase *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
if (p_idx < 4) { // Any item before the separator.
script_text_editor->_edit_option(bookmarks_menu->get_item_id(p_idx));
} else {
script_text_editor->code_editor->goto_line_centered(bookmarks_menu->get_item_metadata(p_idx));
}
}
TextEditorBase::EditMenus::EditMenus() {
edit_menu = memnew(MenuButton);
edit_menu->set_flat(false);
edit_menu->set_theme_type_variation("FlatMenuButton");
edit_menu->set_text(TTRC("Edit"));
edit_menu->set_switch_on_hover(true);
add_child(edit_menu);
edit_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_prepare_edit_menu));
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP);
edit_menu->get_popup()->add_separator();
{
edit_menu_line = memnew(PopupMenu);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
edit_menu_line->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_submenu_node_item(TTRC("Line"), edit_menu_line);
}
{
edit_menu_fold = memnew(PopupMenu);
edit_menu_fold->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
edit_menu_fold->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
edit_menu_fold->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
edit_menu_fold->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_submenu_node_item(TTRC("Folding"), edit_menu_fold);
}
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_final_newlines"), EDIT_TRIM_FINAL_NEWLINES);
{
edit_menu_convert_indent = memnew(PopupMenu);
edit_menu_convert_indent->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
edit_menu_convert_indent->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
edit_menu_convert_indent->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_submenu_node_item(TTRC("Indentation"), edit_menu_convert_indent);
}
edit_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_separator();
{
edit_menu_convert = memnew(PopupMenu);
edit_menu_convert->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
edit_menu_convert->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
edit_menu_convert->add_shortcut(ED_GET_SHORTCUT("script_text_editor/capitalize"), EDIT_CAPITALIZE);
edit_menu_convert->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_submenu_node_item(TTRC("Convert Case"), edit_menu_convert);
}
highlighter_menu = memnew(PopupMenu);
edit_menu->get_popup()->add_submenu_node_item(TTRC("Syntax Highlighter"), highlighter_menu);
highlighter_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_update_highlighter_menu));
highlighter_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_change_syntax_highlighter));
search_menu = memnew(MenuButton);
search_menu->set_flat(false);
search_menu->set_theme_type_variation("FlatMenuButton");
search_menu->set_text(TTRC("Search"));
search_menu->set_switch_on_hover(true);
add_child(search_menu);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
search_menu->get_popup()->add_separator();
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("editor/find_in_files"), SEARCH_IN_FILES);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES);
search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
goto_menu = memnew(MenuButton);
goto_menu->set_flat(false);
goto_menu->set_theme_type_variation("FlatMenuButton");
goto_menu->set_text(TTRC("Go To"));
goto_menu->set_switch_on_hover(true);
add_child(goto_menu);
goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
goto_menu->get_popup()->add_separator();
bookmarks_menu = memnew(PopupMenu);
goto_menu->get_popup()->add_submenu_node_item(TTRC("Bookmarks"), bookmarks_menu);
bookmarks_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_update_bookmark_list));
bookmarks_menu->connect("index_pressed", callable_mp(this, &EditMenus::_bookmark_item_pressed));
goto_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
}
void TextEditorBase::_make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position, bool p_show) {
context_menu->clear();
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
context_menu->add_item(TTRC("Emoji & Symbols"), EDIT_EMOJI_AND_SYMBOL);
context_menu->add_separator();
}
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
if (p_selection) {
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
}
if (p_foldable) {
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
}
if (p_show) {
_show_context_menu(p_position);
}
}
void TextEditorBase::_show_context_menu(const Vector2 &p_position) {
const CodeEdit *tx = code_editor->get_text_editor();
context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo());
context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo());
context_menu->set_position(get_screen_position() + p_position);
context_menu->reset_size();
context_menu->popup();
}
void TextEditorBase::_text_edit_gui_input(const Ref<InputEvent> &p_ev) {
Ref<InputEventMouseButton> mb = p_ev;
if (mb.is_valid()) {
if (mb->get_button_index() == MouseButton::RIGHT) {
CodeEdit *tx = code_editor->get_text_editor();
tx->apply_ime();
Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position());
int row = pos.y;
int col = pos.x;
tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click"));
if (tx->is_move_caret_on_right_click_enabled()) {
tx->remove_secondary_carets();
if (tx->has_selection()) {
int from_line = tx->get_selection_from_line();
int to_line = tx->get_selection_to_line();
int from_column = tx->get_selection_from_column();
int to_column = tx->get_selection_to_column();
if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
// Right click is outside the selected text.
tx->deselect();
}
}
if (!tx->has_selection()) {
tx->set_caret_line(row, true, false, -1);
tx->set_caret_column(col);
}
}
if (!mb->is_pressed()) {
bool can_fold = tx->can_fold_line(row);
bool is_folded = tx->is_line_folded(row);
_make_context_menu(tx->has_selection(), can_fold || is_folded, get_local_mouse_position());
}
}
}
Ref<InputEventKey> k = p_ev;
if (k.is_valid() && k->is_pressed() && k->is_action("ui_menu", true)) {
CodeEdit *tx = code_editor->get_text_editor();
int line = tx->get_caret_line();
tx->adjust_viewport_to_caret();
bool can_fold = tx->can_fold_line(line);
bool is_folded = tx->is_line_folded(line);
_make_context_menu(tx->has_selection(0), can_fold || is_folded, (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->get_caret_draw_pos(0)));
context_menu->grab_focus();
}
}
bool TextEditorBase::_edit_option(int p_op) {
CodeEdit *tx = code_editor->get_text_editor();
tx->apply_ime();
switch (p_op) {
case EDIT_UNDO: {
tx->undo();
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_REDO: {
tx->redo();
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_CUT: {
tx->cut();
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_COPY: {
tx->copy();
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_PASTE: {
tx->paste();
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_SELECT_ALL: {
tx->select_all();
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_MOVE_LINE_UP: {
tx->move_lines_up();
} break;
case EDIT_MOVE_LINE_DOWN: {
tx->move_lines_down();
} break;
case EDIT_INDENT: {
tx->indent_lines();
} break;
case EDIT_UNINDENT: {
tx->unindent_lines();
} break;
case EDIT_DELETE_LINE: {
tx->delete_lines();
} break;
case EDIT_DUPLICATE_SELECTION: {
tx->duplicate_selection();
} break;
case EDIT_DUPLICATE_LINES: {
tx->duplicate_lines();
} break;
case EDIT_TRIM_TRAILING_WHITESAPCE: {
trim_trailing_whitespace();
} break;
case EDIT_TRIM_FINAL_NEWLINES: {
trim_final_newlines();
} break;
case EDIT_CONVERT_INDENT_TO_SPACES: {
code_editor->set_indent_using_spaces(true);
convert_indent();
} break;
case EDIT_CONVERT_INDENT_TO_TABS: {
code_editor->set_indent_using_spaces(false);
convert_indent();
} break;
case EDIT_TO_UPPERCASE: {
_convert_case(CodeTextEditor::UPPER);
} break;
case EDIT_TO_LOWERCASE: {
_convert_case(CodeTextEditor::LOWER);
} break;
case EDIT_CAPITALIZE: {
_convert_case(CodeTextEditor::CAPITALIZE);
} break;
case EDIT_TOGGLE_WORD_WRAP: {
TextEdit::LineWrappingMode wrap = tx->get_line_wrapping_mode();
tx->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY);
} break;
case SEARCH_FIND: {
code_editor->get_find_replace_bar()->popup_search();
} break;
case SEARCH_FIND_NEXT: {
code_editor->get_find_replace_bar()->search_next();
} break;
case SEARCH_FIND_PREV: {
code_editor->get_find_replace_bar()->search_prev();
} break;
case SEARCH_REPLACE: {
code_editor->get_find_replace_bar()->popup_replace();
} break;
case SEARCH_IN_FILES: {
String selected_text = tx->get_selected_text();
// Yep, because it doesn't make sense to instance this dialog for every single script open...
// So this will be delegated to the ScriptEditor.
emit_signal(SNAME("search_in_files_requested"), selected_text);
} break;
case REPLACE_IN_FILES: {
String selected_text = tx->get_selected_text();
emit_signal(SNAME("replace_in_files_requested"), selected_text);
} break;
case SEARCH_GOTO_LINE: {
goto_line_popup->popup_find_line(code_editor);
} break;
case BOOKMARK_TOGGLE: {
code_editor->toggle_bookmark();
} break;
case BOOKMARK_GOTO_NEXT: {
code_editor->goto_next_bookmark();
} break;
case BOOKMARK_GOTO_PREV: {
code_editor->goto_prev_bookmark();
} break;
case BOOKMARK_REMOVE_ALL: {
code_editor->remove_all_bookmarks();
} break;
case EDIT_EMOJI_AND_SYMBOL: {
tx->show_emoji_and_symbol_picker();
} break;
case EDIT_TOGGLE_FOLD_LINE: {
tx->toggle_foldable_lines_at_carets();
} break;
case EDIT_FOLD_ALL_LINES: {
tx->fold_all_lines();
} break;
case EDIT_UNFOLD_ALL_LINES: {
tx->unfold_all_lines();
} break;
default: {
return false;
}
}
return true;
}
void TextEditorBase::_load_theme_settings() {
code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
}
void TextEditorBase::_validate_script() {
emit_signal(SNAME("name_changed"));
emit_signal(SNAME("edited_script_changed"));
}
void TextEditorBase::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
ERR_FAIL_COND(p_highlighter.is_null());
highlighters.push_back(p_highlighter);
}
void TextEditorBase::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
ERR_FAIL_COND(p_highlighter.is_null());
CodeEdit *te = code_editor->get_text_editor();
te->set_syntax_highlighter(p_highlighter);
}
void TextEditorBase::set_edited_resource(const Ref<Resource> &p_res) {
ERR_FAIL_COND(edited_res.is_valid());
ERR_FAIL_COND(p_res.is_null());
edited_res = p_res;
Ref<TextFile> text_file = edited_res;
String text;
if (text_file.is_valid()) {
text = text_file->get_text();
}
Ref<JSON> json_file = edited_res;
if (json_file.is_valid()) {
text = json_file->get_parsed_text();
}
code_editor->get_text_editor()->set_text(text);
code_editor->get_text_editor()->clear_undo_history();
code_editor->get_text_editor()->tag_saved_version();
emit_signal(SNAME("name_changed"));
code_editor->update_line_and_column();
}
bool TextEditorBase::is_unsaved() {
return code_editor->get_text_editor()->get_version() != code_editor->get_text_editor()->get_saved_version() || edited_res->get_path().is_empty(); // In memory.
}
void TextEditorBase::tag_saved_version() {
code_editor->get_text_editor()->tag_saved_version();
ScriptEditorBase::tag_saved_version();
}
void TextEditorBase::reload_text() {
ERR_FAIL_COND(edited_res.is_null());
CodeEdit *te = code_editor->get_text_editor();
int column = te->get_caret_column();
int row = te->get_caret_line();
int h = te->get_h_scroll();
int v = te->get_v_scroll();
Ref<TextFile> text_file = edited_res;
if (text_file.is_valid()) {
te->set_text(text_file->get_text());
}
Ref<JSON> json_file = edited_res;
if (json_file.is_valid()) {
te->set_text(json_file->get_parsed_text());
}
Ref<Script> script = edited_res;
if (script.is_valid()) {
te->set_text(script->get_source_code());
}
te->set_caret_line(row);
te->set_caret_column(column);
te->set_h_scroll(h);
te->set_v_scroll(v);
te->tag_saved_version();
code_editor->update_line_and_column();
if (editor_enabled) {
_validate_script();
}
}
void TextEditorBase::enable_editor() {
if (editor_enabled) {
return;
}
editor_enabled = true;
_load_theme_settings();
_validate_script();
}
void TextEditorBase::set_tooltip_request_func(const Callable &p_toolip_callback) {
Variant args[1] = { this };
const Variant *argp[] = { &args[0] };
code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1));
}
void TextEditorBase::set_edit_state(const Variant &p_state) {
code_editor->set_edit_state(p_state);
Dictionary state = p_state;
if (state.has("syntax_highlighter")) {
for (const Ref<EditorSyntaxHighlighter> &highlighter : highlighters) {
if (highlighter->_get_name() == String(state["syntax_highlighter"])) {
set_syntax_highlighter(highlighter);
break;
}
}
}
ensure_focus();
}
TextEditorBase::TextEditorBase() {
code_editor = memnew(CodeTextEditor);
code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
code_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
code_editor->show_toggle_files_button();
code_editor->get_text_editor()->set_context_menu_enabled(false);
code_editor->get_text_editor()->connect(SceneStringName(gui_input), callable_mp(this, &TextEditorBase::_text_edit_gui_input));
code_editor->connect("validate_script", callable_mp(this, &TextEditorBase::_validate_script));
code_editor->connect("load_theme_settings", callable_mp(this, &TextEditorBase::_load_theme_settings));
context_menu = memnew(PopupMenu);
context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &TextEditorBase::_edit_option));
add_child(context_menu);
edit_hb = memnew(HBoxContainer);
Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;
plain_highlighter.instantiate();
add_syntax_highlighter(plain_highlighter);
Ref<EditorStandardSyntaxHighlighter> highlighter;
highlighter.instantiate();
add_syntax_highlighter(highlighter);
set_syntax_highlighter(plain_highlighter);
update_settings();
}
TextEditorBase::~TextEditorBase() {
highlighters.clear();
}
//// CodeEditorBase
bool CodeEditorBase::_warning_clicked(const Variant &p_line) {
if (p_line.get_type() == Variant::INT) {
goto_line_centered(p_line.operator int64_t());
return true;
}
return false;
}
CodeEditorBase::EditMenusCEB::EditMenusCEB() {
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);
_popup_move_item(EDIT_TRIM_TRAILING_WHITESAPCE, edit_menu->get_popup(), false);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
}
CodeEditorBase::CodeEditorBase() {
warnings_panel = memnew(RichTextLabel);
warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);
warnings_panel->set_meta_underline(true);
warnings_panel->set_selection_enabled(true);
warnings_panel->set_context_menu_enabled(true);
warnings_panel->set_focus_mode(FOCUS_CLICK);
warnings_panel->hide();
warnings_panel->connect("meta_clicked", callable_mp(this, &CodeEditorBase::_warning_clicked));
editor_box = memnew(VSplitContainer);
add_child(editor_box);
editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
editor_box->set_v_size_flags(SIZE_EXPAND_FILL);
editor_box->add_child(code_editor);
editor_box->add_child(warnings_panel);
}

View file

@ -0,0 +1,267 @@
/**************************************************************************/
/* script_editor_base.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#pragma once
#include "editor/gui/code_editor.h"
class EditorSyntaxHighlighter;
class MenuButton;
class VSplitContainer;
class ScriptEditorBase : public Control {
GDCLASS(ScriptEditorBase, Control);
protected:
Ref<Resource> edited_res;
static void _bind_methods();
public:
struct EditedFileData {
String path;
uint64_t last_modified_time = -1;
} edited_file_data;
virtual String get_name();
virtual Ref<Texture2D> get_theme_icon();
virtual void set_toggle_list_control(Control *p_toggle_list_control) = 0;
virtual void update_toggle_files_button() = 0;
virtual bool show_members_overview() { return false; }
virtual void set_edited_resource(const Ref<Resource> &p_res) = 0;
virtual Ref<Resource> get_edited_resource() const { return edited_res; }
virtual void apply_code() = 0;
virtual void validate_script() = 0;
virtual bool is_unsaved() = 0;
virtual void tag_saved_version();
virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {}
virtual Control *get_base_editor() const { return nullptr; }
};
typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const Ref<Resource> &p_resource);
class TextEditorBase : public ScriptEditorBase {
GDCLASS(TextEditorBase, ScriptEditorBase);
void _post_init();
protected:
enum {
EDIT_UNDO,
EDIT_REDO,
EDIT_CUT,
EDIT_COPY,
EDIT_PASTE,
EDIT_SELECT_ALL,
EDIT_TRIM_TRAILING_WHITESAPCE,
EDIT_TRIM_FINAL_NEWLINES,
EDIT_CONVERT_INDENT_TO_SPACES,
EDIT_CONVERT_INDENT_TO_TABS,
EDIT_MOVE_LINE_UP,
EDIT_MOVE_LINE_DOWN,
EDIT_INDENT,
EDIT_UNINDENT,
EDIT_DELETE_LINE,
EDIT_DUPLICATE_SELECTION,
EDIT_DUPLICATE_LINES,
EDIT_TO_UPPERCASE,
EDIT_TO_LOWERCASE,
EDIT_CAPITALIZE,
EDIT_TOGGLE_FOLD_LINE,
EDIT_FOLD_ALL_LINES,
EDIT_TOGGLE_WORD_WRAP,
EDIT_UNFOLD_ALL_LINES,
EDIT_EMOJI_AND_SYMBOL,
SEARCH_FIND,
SEARCH_FIND_NEXT,
SEARCH_FIND_PREV,
SEARCH_REPLACE,
SEARCH_IN_FILES,
SEARCH_GOTO_LINE,
REPLACE_IN_FILES,
BOOKMARK_TOGGLE,
BOOKMARK_GOTO_NEXT,
BOOKMARK_GOTO_PREV,
BOOKMARK_REMOVE_ALL,
BASE_ENUM_COUNT,
};
class EditMenus : public HBoxContainer {
GDCLASS(EditMenus, HBoxContainer);
protected:
MenuButton *edit_menu = nullptr;
MenuButton *search_menu = nullptr;
MenuButton *goto_menu = nullptr;
PopupMenu *bookmarks_menu = nullptr;
PopupMenu *highlighter_menu = nullptr;
PopupMenu *edit_menu_line = nullptr;
PopupMenu *edit_menu_fold = nullptr;
PopupMenu *edit_menu_convert_indent = nullptr;
PopupMenu *edit_menu_convert = nullptr;
TextEditorBase *_get_active_editor();
void _edit_option(int p_op);
void _prepare_edit_menu();
void _update_highlighter_menu();
void _change_syntax_highlighter(int p_idx);
void _update_bookmark_list();
void _bookmark_item_pressed(int p_idx);
public:
EditMenus();
};
static void _popup_move_item(int p_target_id, PopupMenu *r_popup, bool p_move_after = true, int p_idx = -1) {
int target_idx = r_popup->get_item_index(p_target_id) + p_move_after;
if (target_idx >= 0 && target_idx < r_popup->get_item_count()) {
r_popup->set_item_index(p_idx, target_idx);
}
}
static inline EditMenus *edit_menus = nullptr;
bool editor_enabled = false;
CodeTextEditor *code_editor = nullptr;
HBoxContainer *edit_hb = nullptr;
GotoLinePopup *goto_line_popup = nullptr;
LocalVector<Ref<EditorSyntaxHighlighter>> highlighters;
PopupMenu *context_menu = nullptr;
MenuButton *search_menu = nullptr;
void _make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position = Vector2(0, 0), bool p_show = true);
void _show_context_menu(const Vector2 &p_position);
virtual void _text_edit_gui_input(const Ref<InputEvent> &p_ev);
virtual bool _edit_option(int p_op);
virtual void _load_theme_settings();
virtual void _validate_script();
void _convert_case(CodeTextEditor::CaseStyle p_case) { code_editor->convert_case(p_case); }
public:
virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter);
virtual void set_edited_resource(const Ref<Resource> &p_res) override;
virtual bool is_unsaved() override;
virtual void tag_saved_version() override;
virtual void reload_text();
virtual void enable_editor();
virtual Control *get_edit_menu() = 0;
virtual Control *get_base_editor() const override { return code_editor->get_text_editor(); }
virtual CodeTextEditor *get_code_editor() const { return code_editor; }
virtual void set_tooltip_request_func(const Callable &p_toolip_callback);
virtual void ensure_focus() { code_editor->get_text_editor()->grab_focus(); }
virtual void convert_indent() { code_editor->get_text_editor()->convert_indent(); }
virtual void trim_trailing_whitespace() { code_editor->trim_trailing_whitespace(); }
virtual void trim_final_newlines() { code_editor->trim_final_newlines(); }
virtual void insert_final_newline() { code_editor->insert_final_newline(); }
virtual void goto_line(int p_line, int p_column = 0) { code_editor->goto_line(p_line, p_column); }
virtual void goto_line_selection(int p_line, int p_begin, int p_end) { code_editor->goto_line_selection(p_line, p_begin, p_end); }
virtual void goto_line_centered(int p_line, int p_column = 0) { code_editor->goto_line_centered(p_line, p_column); }
virtual void set_executing_line(int p_line) { code_editor->set_executing_line(p_line); }
virtual void clear_executing_line() { code_editor->clear_executing_line(); }
virtual Variant get_edit_state() { return code_editor->get_edit_state(); }
virtual void set_edit_state(const Variant &p_state);
virtual Variant get_navigation_state() { return code_editor->get_navigation_state(); }
virtual void update_settings() { code_editor->update_editor_settings(); }
virtual void set_find_replace_bar(FindReplaceBar *p_bar) { code_editor->set_find_replace_bar(p_bar); }
virtual void validate_script() override { code_editor->validate_script(); }
virtual void set_toggle_list_control(Control *p_toggle_list_control) override {
code_editor->set_toggle_list_control(p_toggle_list_control);
}
virtual void update_toggle_files_button() override { code_editor->update_toggle_files_button(); }
TextEditorBase();
~TextEditorBase();
};
class CodeEditorBase : public TextEditorBase {
GDCLASS(CodeEditorBase, TextEditorBase);
protected:
enum {
EDIT_COMPLETE = BASE_ENUM_COUNT,
EDIT_TOGGLE_COMMENT,
CODE_ENUM_COUNT,
};
class EditMenusCEB : public EditMenus {
GDCLASS(EditMenusCEB, EditMenus);
public:
EditMenusCEB();
};
VSplitContainer *editor_box = nullptr;
RichTextLabel *warnings_panel = nullptr;
virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) = 0;
virtual bool _warning_clicked(const Variant &p_line);
public:
virtual bool show_members_overview() override { return true; }
virtual Vector<String> get_functions() { return Vector<String>(); }
virtual PackedInt32Array get_breakpoints() { return PackedInt32Array(); }
virtual void set_breakpoint(int p_line, bool p_enabled) {}
virtual void clear_breakpoints() {}
CodeEditorBase();
};

File diff suppressed because it is too large Load diff

View file

@ -32,9 +32,9 @@
#include "core/object/script_language.h"
#include "editor/plugins/editor_plugin.h"
#include "editor/script/script_editor_base.h"
#include "scene/gui/dialogs.h"
#include "scene/gui/panel_container.h"
#include "scene/resources/syntax_highlighter.h"
#include "scene/resources/text_file.h"
class CodeTextEditor;
@ -49,118 +49,8 @@ class TextureRect;
class Tree;
class VSplitContainer;
class WindowWrapper;
class EditorSyntaxHighlighter : public SyntaxHighlighter {
GDCLASS(EditorSyntaxHighlighter, SyntaxHighlighter)
private:
Ref<RefCounted> edited_resource;
protected:
static void _bind_methods();
GDVIRTUAL0RC(String, _get_name)
GDVIRTUAL0RC(PackedStringArray, _get_supported_languages)
GDVIRTUAL0RC(Ref<EditorSyntaxHighlighter>, _create)
public:
virtual String _get_name() const;
virtual PackedStringArray _get_supported_languages() const;
void _set_edited_resource(const Ref<Resource> &p_res) { edited_resource = p_res; }
Ref<RefCounted> _get_edited_resource() { return edited_resource; }
virtual Ref<EditorSyntaxHighlighter> _create() const;
};
class EditorStandardSyntaxHighlighter : public EditorSyntaxHighlighter {
GDCLASS(EditorStandardSyntaxHighlighter, EditorSyntaxHighlighter)
private:
Ref<CodeHighlighter> highlighter;
ScriptLanguage *script_language = nullptr; // See GH-89610.
public:
virtual void _update_cache() override;
virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }
virtual String _get_name() const override { return TTR("Standard"); }
virtual Ref<EditorSyntaxHighlighter> _create() const override;
void _set_script_language(ScriptLanguage *p_script_language) { script_language = p_script_language; }
EditorStandardSyntaxHighlighter() { highlighter.instantiate(); }
};
class EditorPlainTextSyntaxHighlighter : public EditorSyntaxHighlighter {
GDCLASS(EditorPlainTextSyntaxHighlighter, EditorSyntaxHighlighter)
public:
virtual String _get_name() const override { return TTR("Plain Text"); }
virtual Ref<EditorSyntaxHighlighter> _create() const override;
};
class EditorJSONSyntaxHighlighter : public EditorSyntaxHighlighter {
GDCLASS(EditorJSONSyntaxHighlighter, EditorSyntaxHighlighter)
private:
Ref<CodeHighlighter> highlighter;
public:
virtual void _update_cache() override;
virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }
virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "json" }; }
virtual String _get_name() const override { return TTR("JSON"); }
virtual Ref<EditorSyntaxHighlighter> _create() const override;
EditorJSONSyntaxHighlighter() { highlighter.instantiate(); }
};
class EditorMarkdownSyntaxHighlighter : public EditorSyntaxHighlighter {
GDCLASS(EditorMarkdownSyntaxHighlighter, EditorSyntaxHighlighter)
private:
Ref<CodeHighlighter> highlighter;
public:
virtual void _update_cache() override;
virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }
virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "md", "markdown" }; }
virtual String _get_name() const override { return TTR("Markdown"); }
virtual Ref<EditorSyntaxHighlighter> _create() const override;
EditorMarkdownSyntaxHighlighter() { highlighter.instantiate(); }
};
class EditorConfigFileSyntaxHighlighter : public EditorSyntaxHighlighter {
GDCLASS(EditorConfigFileSyntaxHighlighter, EditorSyntaxHighlighter)
private:
Ref<CodeHighlighter> highlighter;
public:
virtual void _update_cache() override;
virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }
// While not explicitly designed for those formats, this highlighter happens
// to handle TSCN, TRES, `project.godot` well. We can expose it in case the
// user opens one of these using the script editor (which can be done using
// the All Files filter).
virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "ini", "cfg", "tscn", "tres", "godot" }; }
virtual String _get_name() const override { return TTR("ConfigFile"); }
virtual Ref<EditorSyntaxHighlighter> _create() const override;
EditorConfigFileSyntaxHighlighter() { highlighter.instantiate(); }
};
///////////////////////////////////////////////////////////////////////////////
class EditorSyntaxHighlighter;
class ScriptEditorBase;
class ScriptEditorQuickOpen : public ConfirmationDialog {
GDCLASS(ScriptEditorQuickOpen, ConfirmationDialog);
@ -186,67 +76,6 @@ public:
ScriptEditorQuickOpen();
};
class EditorDebuggerNode;
class ScriptEditorBase : public VBoxContainer {
GDCLASS(ScriptEditorBase, VBoxContainer);
protected:
static void _bind_methods();
public:
struct EditedFileData {
String path;
uint64_t last_modified_time = -1;
} edited_file_data;
virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) = 0;
virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) = 0;
virtual void apply_code() = 0;
virtual Ref<Resource> get_edited_resource() const = 0;
virtual Vector<String> get_functions() = 0;
virtual void set_edited_resource(const Ref<Resource> &p_res) = 0;
virtual void enable_editor() = 0;
virtual void reload_text() = 0;
virtual String get_name() = 0;
virtual Ref<Texture2D> get_theme_icon() = 0;
virtual bool is_unsaved() = 0;
virtual Variant get_edit_state() = 0;
virtual void set_edit_state(const Variant &p_state) = 0;
virtual Variant get_navigation_state() = 0;
virtual void goto_line(int p_line, int p_column = 0) = 0;
virtual void set_executing_line(int p_line) = 0;
virtual void clear_executing_line() = 0;
virtual void trim_trailing_whitespace() = 0;
virtual void trim_final_newlines() = 0;
virtual void insert_final_newline() = 0;
virtual void convert_indent() = 0;
virtual void ensure_focus() = 0;
virtual void tag_saved_version() = 0;
virtual void reload(bool p_soft) {}
virtual PackedInt32Array get_breakpoints() = 0;
virtual void set_breakpoint(int p_line, bool p_enabled) = 0;
virtual void clear_breakpoints() = 0;
virtual void add_callback(const String &p_function, const PackedStringArray &p_args) = 0;
virtual void update_settings() = 0;
virtual void set_debugger_active(bool p_active) = 0;
virtual void update_toggle_files_button() {}
virtual bool show_members_overview() = 0;
virtual void set_tooltip_request_func(const Callable &p_toolip_callback) = 0;
virtual Control *get_edit_menu() = 0;
virtual void set_find_replace_bar(FindReplaceBar *p_bar) = 0;
virtual Control *get_base_editor() const = 0;
virtual CodeTextEditor *get_code_editor() const = 0;
virtual void validate() = 0;
};
typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const Ref<Resource> &p_resource);
class EditorScriptCodeCompletionCache;
class FindInFilesContainer;
class FindInFilesDialog;
@ -256,7 +85,7 @@ class ScriptEditor : public PanelContainer {
enum MenuOptions {
// File.
FILE_MENU_NEW,
FILE_MENU_NEW_SCRIPT,
FILE_MENU_NEW_TEXTFILE,
FILE_MENU_OPEN,
FILE_MENU_REOPEN_CLOSED,
@ -397,6 +226,8 @@ class ScriptEditor : public PanelContainer {
List<String> previous_scripts;
List<int> script_close_queue;
List<String> _get_recognized_extensions();
void _tab_changed(int p_which);
void _menu_option(int p_option);
void _theme_option(int p_option);
@ -447,8 +278,6 @@ class ScriptEditor : public PanelContainer {
void _update_selected_editor_menu();
void _editor_stop();
int edit_pass;
void _add_callback(Object *p_obj, const String &p_function, const PackedStringArray &p_args);
@ -463,10 +292,10 @@ class ScriptEditor : public PanelContainer {
void _goto_script_line2(int p_line);
void _goto_script_line(Ref<RefCounted> p_script, int p_line);
void _set_execution(Ref<RefCounted> p_script, int p_line);
void _clear_execution(Ref<RefCounted> p_script);
void _change_execution(Ref<RefCounted> p_script, int p_line = -1, bool p_set = false);
void _set_execution(Ref<RefCounted> p_script, int p_line) { _change_execution(p_script, p_line, true); }
void _clear_execution(Ref<RefCounted> p_script) { _change_execution(p_script); }
String _get_debug_tooltip(const String &p_text, Node *p_se);
void _breaked(bool p_breaked, bool p_can_debug);
void _script_created(Ref<Script> p_script);
void _set_breakpoint(Ref<RefCounted> p_script, int p_line, bool p_enabled);
void _clear_breakpoints();
@ -485,6 +314,7 @@ class ScriptEditor : public PanelContainer {
void _autosave_scripts();
void _update_autosave_timer();
void _reload_scripts(bool p_refresh_only = false);
void _auto_format_text(ScriptEditorBase *p_seb);
void _update_members_overview_visibility();
void _update_members_overview();

File diff suppressed because it is too large Load diff

View file

@ -30,6 +30,7 @@
#pragma once
#include "editor/script/script_editor_base.h"
#include "script_editor_plugin.h"
#include "editor/gui/code_editor.h"
@ -54,40 +55,13 @@ public:
ConnectionInfoDialog();
};
class ScriptTextEditor : public ScriptEditorBase {
GDCLASS(ScriptTextEditor, ScriptEditorBase);
class ScriptTextEditor : public CodeEditorBase {
GDCLASS(ScriptTextEditor, CodeEditorBase);
class EditMenusSTE : public HBoxContainer {
GDCLASS(EditMenusSTE, HBoxContainer);
MenuButton *edit_menu = nullptr;
MenuButton *search_menu = nullptr;
MenuButton *goto_menu = nullptr;
PopupMenu *bookmarks_menu = nullptr;
PopupMenu *breakpoints_menu = nullptr;
PopupMenu *highlighter_menu = nullptr;
ScriptTextEditor *_get_active_editor();
void _edit_option(int p_op);
void _prepare_edit_menu();
void _update_highlighter_menu();
void _change_syntax_highlighter(int p_idx);
void _update_bookmark_list();
void _bookmark_item_pressed(int p_idx);
void _update_breakpoint_list();
void _breakpoint_item_pressed(int p_idx);
public:
EditMenusSTE();
};
CodeTextEditor *code_editor = nullptr;
RichTextLabel *warnings_panel = nullptr;
RichTextLabel *errors_panel = nullptr;
Ref<Script> script;
Variant pending_state;
bool script_is_valid = false;
bool editor_enabled = false;
RichTextLabel *errors_panel = nullptr;
Vector<String> functions;
List<ScriptLanguage::Warning> warnings;
@ -97,9 +71,6 @@ class ScriptTextEditor : public ScriptEditorBase {
List<Connection> missing_connections;
static inline EditMenusSTE *edit_menus = nullptr;
PopupMenu *context_menu = nullptr;
int inline_color_line = -1;
int inline_color_start = -1;
int inline_color_end = -1;
@ -108,7 +79,6 @@ class ScriptTextEditor : public ScriptEditorBase {
OptionButton *inline_color_options = nullptr;
Ref<Texture2D> color_alpha_texture;
GotoLinePopup *goto_line_popup = nullptr;
ScriptEditorQuickOpen *quick_open = nullptr;
ConnectionInfoDialog *connection_info_dialog = nullptr;
@ -132,58 +102,21 @@ class ScriptTextEditor : public ScriptEditorBase {
bool theme_loaded = false;
LocalVector<Ref<EditorSyntaxHighlighter>> highlighters;
enum {
EDIT_UNDO,
EDIT_REDO,
EDIT_CUT,
EDIT_COPY,
EDIT_PASTE,
EDIT_SELECT_ALL,
EDIT_COMPLETE,
EDIT_AUTO_INDENT,
EDIT_TRIM_TRAILING_WHITESAPCE,
EDIT_TRIM_FINAL_NEWLINES,
EDIT_CONVERT_INDENT_TO_SPACES,
EDIT_CONVERT_INDENT_TO_TABS,
EDIT_TOGGLE_COMMENT,
EDIT_MOVE_LINE_UP,
EDIT_MOVE_LINE_DOWN,
EDIT_INDENT,
EDIT_UNINDENT,
EDIT_DELETE_LINE,
EDIT_DUPLICATE_SELECTION,
EDIT_DUPLICATE_LINES,
EDIT_AUTO_INDENT = CODE_ENUM_COUNT,
EDIT_PICK_COLOR,
EDIT_TO_UPPERCASE,
EDIT_TO_LOWERCASE,
EDIT_CAPITALIZE,
EDIT_EVALUATE,
EDIT_TOGGLE_WORD_WRAP,
EDIT_TOGGLE_FOLD_LINE,
EDIT_FOLD_ALL_LINES,
EDIT_CREATE_CODE_REGION,
EDIT_UNFOLD_ALL_LINES,
SEARCH_FIND,
SEARCH_FIND_NEXT,
SEARCH_FIND_PREV,
SEARCH_REPLACE,
SEARCH_LOCATE_FUNCTION,
SEARCH_GOTO_LINE,
SEARCH_IN_FILES,
REPLACE_IN_FILES,
BOOKMARK_TOGGLE,
BOOKMARK_GOTO_NEXT,
BOOKMARK_GOTO_PREV,
BOOKMARK_REMOVE_ALL,
DEBUG_TOGGLE_BREAKPOINT,
DEBUG_REMOVE_ALL_BREAKPOINTS,
DEBUG_GOTO_NEXT_BREAKPOINT,
DEBUG_GOTO_PREV_BREAKPOINT,
HELP_CONTEXTUAL,
LOOKUP_SYMBOL,
EDIT_EMOJI_AND_SYMBOL,
};
enum COLOR_MODE {
@ -196,6 +129,17 @@ class ScriptTextEditor : public ScriptEditorBase {
MODE_MAX
};
class EditMenusSTE : public EditMenusCEB {
GDCLASS(EditMenusSTE, EditMenusCEB);
PopupMenu *breakpoints_menu = nullptr;
void _update_breakpoint_list();
void _breakpoint_item_pressed(int p_idx);
public:
EditMenusSTE();
};
void _enable_code_editor();
struct DraggedExport {
@ -210,24 +154,24 @@ class ScriptTextEditor : public ScriptEditorBase {
String _get_dropped_resource_as_exported_member(const Ref<Resource> &p_resource, const Vector<ObjectID> &p_script_instance_obj_ids);
void _assign_dragged_export_variables();
static ScriptEditorBase *create_editor(const Ref<Resource> &p_resource);
protected:
void _breakpoint_toggled(int p_row);
void _on_caret_moved();
void _validate_script(); // No longer virtual.
void _update_warnings();
void _update_errors();
static void _code_complete_scripts(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force);
void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force);
virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) override;
void _load_theme_settings();
void _set_theme_for_script();
void _show_errors_panel(bool p_show);
void _show_warnings_panel(bool p_show);
void _error_clicked(const Variant &p_line);
void _warning_clicked(const Variant &p_line);
virtual bool _warning_clicked(const Variant &p_line) override;
bool _is_valid_color_info(const Dictionary &p_info);
Array _inline_object_parse(const String &p_text);
@ -241,82 +185,54 @@ protected:
void _notification(int p_what);
void _edit_option(int p_op);
void _edit_option_toggle_inline_comment();
void _make_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_goto_definition, Vector2 p_pos);
void _text_edit_gui_input(const Ref<InputEvent> &ev);
void _color_changed(const Color &p_color);
void _goto_line(int p_line) { goto_line(p_line); }
void _lookup_symbol(const String &p_symbol, int p_row, int p_column);
void _validate_symbol(const String &p_symbol);
void _show_symbol_tooltip(const String &p_symbol, int p_row, int p_column);
void _convert_case(CodeTextEditor::CaseStyle p_case);
Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
String _get_absolute_path(const String &rel_path);
void _goto_line(int p_line) { goto_line(p_line); }
void _make_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_goto_definition, const Vector2 &p_pos);
virtual void _text_edit_gui_input(const Ref<InputEvent> &p_ev) override;
virtual bool _edit_option(int p_op) override;
virtual void _load_theme_settings() override;
virtual void _validate_script() override;
public:
void _update_connected_methods();
virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
void update_toggle_files_button() override;
virtual void apply_code() override;
virtual Ref<Resource> get_edited_resource() const override;
virtual void set_edited_resource(const Ref<Resource> &p_res) override;
virtual void enable_editor() override;
virtual Vector<String> get_functions() override;
virtual void reload_text() override;
virtual String get_name() override;
virtual Control *get_edit_menu() override;
virtual Ref<Texture2D> get_theme_icon() override;
virtual bool is_unsaved() override;
virtual Variant get_edit_state() override;
virtual void set_edit_state(const Variant &p_state) override;
virtual Variant get_navigation_state() override;
virtual void ensure_focus() override;
virtual void trim_trailing_whitespace() override;
virtual void trim_final_newlines() override;
virtual void insert_final_newline() override;
virtual void convert_indent() override;
virtual void tag_saved_version() override;
virtual void goto_line(int p_line, int p_column = 0) override;
void goto_line_selection(int p_line, int p_begin, int p_end);
void goto_line_centered(int p_line, int p_column = 0);
virtual void set_executing_line(int p_line) override;
virtual void clear_executing_line() override;
virtual void reload(bool p_soft) override;
virtual PackedInt32Array get_breakpoints() override;
virtual void set_breakpoint(int p_line, bool p_enabled) override;
virtual void clear_breakpoints() override;
virtual void add_callback(const String &p_function, const PackedStringArray &p_args) override;
virtual void add_callback(const String &p_function, const PackedStringArray &p_args);
virtual void update_settings() override;
virtual bool show_members_overview() override;
virtual void set_tooltip_request_func(const Callable &p_toolip_callback) override;
virtual void set_debugger_active(bool p_active) override;
virtual Control *get_edit_menu() override;
virtual void set_find_replace_bar(FindReplaceBar *p_bar) override;
static void register_editor();
virtual Control *get_base_editor() const override;
virtual CodeTextEditor *get_code_editor() const override;
virtual void validate() override;
Variant get_previous_state();
void store_previous_state();

View file

@ -0,0 +1,307 @@
/**************************************************************************/
/* syntax_highlighters.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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_highlighters.h"
#include "core/config/project_settings.h"
#include "core/object/script_language.h"
#include "editor/settings/editor_settings.h"
String EditorSyntaxHighlighter::_get_name() const {
String ret = "Unnamed";
GDVIRTUAL_CALL(_get_name, ret);
return ret;
}
PackedStringArray EditorSyntaxHighlighter::_get_supported_languages() const {
PackedStringArray ret;
GDVIRTUAL_CALL(_get_supported_languages, ret);
return ret;
}
Ref<EditorSyntaxHighlighter> EditorSyntaxHighlighter::_create() const {
Ref<EditorSyntaxHighlighter> syntax_highlighter;
if (GDVIRTUAL_IS_OVERRIDDEN(_create)) {
GDVIRTUAL_CALL(_create, syntax_highlighter);
} else {
syntax_highlighter.instantiate();
if (get_script_instance()) {
syntax_highlighter->set_script(get_script_instance()->get_script());
}
}
return syntax_highlighter;
}
void EditorSyntaxHighlighter::_bind_methods() {
ClassDB::bind_method(D_METHOD("_get_edited_resource"), &EditorSyntaxHighlighter::_get_edited_resource);
GDVIRTUAL_BIND(_get_name)
GDVIRTUAL_BIND(_get_supported_languages)
GDVIRTUAL_BIND(_create)
}
////
void EditorStandardSyntaxHighlighter::_update_cache() {
highlighter->set_text_edit(text_edit);
highlighter->clear_keyword_colors();
highlighter->clear_member_keyword_colors();
highlighter->clear_color_regions();
highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color"));
highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/function_color"));
highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color"));
highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/member_variable_color"));
/* Engine types. */
const Color type_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
LocalVector<StringName> types;
ClassDB::get_class_list(types);
for (const StringName &type : types) {
highlighter->add_keyword_color(type, type_color);
}
/* User types. */
const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
LocalVector<StringName> global_classes;
ScriptServer::get_global_class_list(global_classes);
for (const StringName &class_name : global_classes) {
highlighter->add_keyword_color(class_name, usertype_color);
}
/* Autoloads. */
HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads(ProjectSettings::get_singleton()->get_autoload_list());
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
const ProjectSettings::AutoloadInfo &info = E.value;
if (info.is_singleton) {
highlighter->add_keyword_color(info.name, usertype_color);
}
}
const ScriptLanguage *scr_lang = script_language;
StringName instance_base;
if (scr_lang == nullptr) {
const Ref<Script> scr = _get_edited_resource();
if (scr.is_valid()) {
scr_lang = scr->get_language();
instance_base = scr->get_instance_base_type();
}
}
if (scr_lang != nullptr) {
/* Core types. */
const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
List<String> core_types;
scr_lang->get_core_type_words(&core_types);
for (const String &E : core_types) {
highlighter->add_keyword_color(E, basetype_color);
}
/* Reserved words. */
const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
for (const String &keyword : scr_lang->get_reserved_words()) {
if (scr_lang->is_control_flow_keyword(keyword)) {
highlighter->add_keyword_color(keyword, control_flow_keyword_color);
} else {
highlighter->add_keyword_color(keyword, keyword_color);
}
}
/* Member types. */
const Color member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
if (instance_base != StringName()) {
List<PropertyInfo> plist;
ClassDB::get_property_list(instance_base, &plist);
for (const PropertyInfo &E : plist) {
String prop_name = E.name;
if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
continue;
}
if (prop_name.contains_char('/')) {
continue;
}
highlighter->add_member_keyword_color(prop_name, member_variable_color);
}
List<String> clist;
ClassDB::get_integer_constant_list(instance_base, &clist);
for (const String &E : clist) {
highlighter->add_member_keyword_color(E, member_variable_color);
}
}
/* Comments */
const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
for (const String &comment : scr_lang->get_comment_delimiters()) {
String beg = comment.get_slicec(' ', 0);
String end = comment.get_slice_count(" ") > 1 ? comment.get_slicec(' ', 1) : String();
highlighter->add_color_region(beg, end, comment_color, end.is_empty());
}
/* Doc comments */
const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
for (const String &doc_comment : scr_lang->get_doc_comment_delimiters()) {
String beg = doc_comment.get_slicec(' ', 0);
String end = doc_comment.get_slice_count(" ") > 1 ? doc_comment.get_slicec(' ', 1) : String();
highlighter->add_color_region(beg, end, doc_comment_color, end.is_empty());
}
/* Strings */
const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
for (const String &string : scr_lang->get_string_delimiters()) {
String beg = string.get_slicec(' ', 0);
String end = string.get_slice_count(" ") > 1 ? string.get_slicec(' ', 1) : String();
highlighter->add_color_region(beg, end, string_color, end.is_empty());
}
}
}
Ref<EditorSyntaxHighlighter> EditorStandardSyntaxHighlighter::_create() const {
Ref<EditorStandardSyntaxHighlighter> syntax_highlighter;
syntax_highlighter.instantiate();
return syntax_highlighter;
}
////
Ref<EditorSyntaxHighlighter> EditorPlainTextSyntaxHighlighter::_create() const {
Ref<EditorPlainTextSyntaxHighlighter> syntax_highlighter;
syntax_highlighter.instantiate();
return syntax_highlighter;
}
////
void EditorJSONSyntaxHighlighter::_update_cache() {
highlighter->set_text_edit(text_edit);
highlighter->clear_keyword_colors();
highlighter->clear_member_keyword_colors();
highlighter->clear_color_regions();
highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color"));
highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color"));
const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
highlighter->add_color_region("\"", "\"", string_color);
}
Ref<EditorSyntaxHighlighter> EditorJSONSyntaxHighlighter::_create() const {
Ref<EditorJSONSyntaxHighlighter> syntax_highlighter;
syntax_highlighter.instantiate();
return syntax_highlighter;
}
////
void EditorMarkdownSyntaxHighlighter::_update_cache() {
highlighter->set_text_edit(text_edit);
highlighter->clear_keyword_colors();
highlighter->clear_member_keyword_colors();
highlighter->clear_color_regions();
// Disable automatic symbolic highlights, as these don't make sense for prose.
highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
// Headings (any level).
const Color function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
highlighter->add_color_region("#", "", function_color);
// Bold.
highlighter->add_color_region("**", "**", function_color);
// `__bold__` syntax is not supported as color regions must begin with a symbol,
// not a character that is valid in an identifier.
// Code (both inline code and triple-backticks code blocks).
const Color code_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
highlighter->add_color_region("`", "`", code_color);
// Link (both references and inline links with URLs). The URL is not highlighted.
const Color link_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
highlighter->add_color_region("[", "]", link_color);
// Quote.
const Color quote_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
highlighter->add_color_region(">", "", quote_color, true);
// HTML comment, which is also supported in Markdown.
const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
highlighter->add_color_region("<!--", "-->", comment_color);
}
Ref<EditorSyntaxHighlighter> EditorMarkdownSyntaxHighlighter::_create() const {
Ref<EditorMarkdownSyntaxHighlighter> syntax_highlighter;
syntax_highlighter.instantiate();
return syntax_highlighter;
}
///
void EditorConfigFileSyntaxHighlighter::_update_cache() {
highlighter->set_text_edit(text_edit);
highlighter->clear_keyword_colors();
highlighter->clear_member_keyword_colors();
highlighter->clear_color_regions();
highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color"));
highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color"));
// Assume that all function-style syntax is for types such as `Vector2()` and `PackedStringArray()`.
highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/base_type_color"));
// Disable member variable highlighting as it's not relevant for ConfigFile.
highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
highlighter->add_color_region("\"", "\"", string_color);
// FIXME: Sections in ConfigFile must be at the beginning of a line. Otherwise, it can be an array within a line.
const Color function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
highlighter->add_color_region("[", "]", function_color);
const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
highlighter->add_keyword_color("true", keyword_color);
highlighter->add_keyword_color("false", keyword_color);
highlighter->add_keyword_color("null", keyword_color);
highlighter->add_keyword_color("ExtResource", keyword_color);
highlighter->add_keyword_color("SubResource", keyword_color);
const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
highlighter->add_color_region(";", "", comment_color);
}
Ref<EditorSyntaxHighlighter> EditorConfigFileSyntaxHighlighter::_create() const {
Ref<EditorConfigFileSyntaxHighlighter> syntax_highlighter;
syntax_highlighter.instantiate();
return syntax_highlighter;
}

View file

@ -0,0 +1,143 @@
/**************************************************************************/
/* syntax_highlighters.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#pragma once
#include "scene/resources/syntax_highlighter.h"
class EditorSyntaxHighlighter : public SyntaxHighlighter {
GDCLASS(EditorSyntaxHighlighter, SyntaxHighlighter)
private:
Ref<RefCounted> edited_resource;
protected:
static void _bind_methods();
GDVIRTUAL0RC(String, _get_name)
GDVIRTUAL0RC(PackedStringArray, _get_supported_languages)
GDVIRTUAL0RC(Ref<EditorSyntaxHighlighter>, _create)
public:
virtual String _get_name() const;
virtual PackedStringArray _get_supported_languages() const;
void _set_edited_resource(const Ref<Resource> &p_res) { edited_resource = p_res; }
Ref<RefCounted> _get_edited_resource() { return edited_resource; }
virtual Ref<EditorSyntaxHighlighter> _create() const;
};
class EditorStandardSyntaxHighlighter : public EditorSyntaxHighlighter {
GDCLASS(EditorStandardSyntaxHighlighter, EditorSyntaxHighlighter)
private:
Ref<CodeHighlighter> highlighter;
ScriptLanguage *script_language = nullptr; // See GH-89610.
public:
virtual void _update_cache() override;
virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }
virtual String _get_name() const override { return TTR("Standard"); }
virtual Ref<EditorSyntaxHighlighter> _create() const override;
void _set_script_language(ScriptLanguage *p_script_language) { script_language = p_script_language; }
EditorStandardSyntaxHighlighter() { highlighter.instantiate(); }
};
class EditorPlainTextSyntaxHighlighter : public EditorSyntaxHighlighter {
GDCLASS(EditorPlainTextSyntaxHighlighter, EditorSyntaxHighlighter)
public:
virtual String _get_name() const override { return TTR("Plain Text"); }
virtual Ref<EditorSyntaxHighlighter> _create() const override;
};
class EditorJSONSyntaxHighlighter : public EditorSyntaxHighlighter {
GDCLASS(EditorJSONSyntaxHighlighter, EditorSyntaxHighlighter)
private:
Ref<CodeHighlighter> highlighter;
public:
virtual void _update_cache() override;
virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }
virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "json" }; }
virtual String _get_name() const override { return TTR("JSON"); }
virtual Ref<EditorSyntaxHighlighter> _create() const override;
EditorJSONSyntaxHighlighter() { highlighter.instantiate(); }
};
class EditorMarkdownSyntaxHighlighter : public EditorSyntaxHighlighter {
GDCLASS(EditorMarkdownSyntaxHighlighter, EditorSyntaxHighlighter)
private:
Ref<CodeHighlighter> highlighter;
public:
virtual void _update_cache() override;
virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }
virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "md", "markdown" }; }
virtual String _get_name() const override { return TTR("Markdown"); }
virtual Ref<EditorSyntaxHighlighter> _create() const override;
EditorMarkdownSyntaxHighlighter() { highlighter.instantiate(); }
};
class EditorConfigFileSyntaxHighlighter : public EditorSyntaxHighlighter {
GDCLASS(EditorConfigFileSyntaxHighlighter, EditorSyntaxHighlighter)
private:
Ref<CodeHighlighter> highlighter;
public:
virtual void _update_cache() override;
virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }
// While not explicitly designed for those formats, this highlighter happens
// to handle TSCN, TRES, `project.godot` well. We can expose it in case the
// user opens one of these using the script editor (which can be done using
// the All Files filter).
virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "ini", "cfg", "tscn", "tres", "godot" }; }
virtual String _get_name() const override { return TTR("ConfigFile"); }
virtual Ref<EditorSyntaxHighlighter> _create() const override;
EditorConfigFileSyntaxHighlighter() { highlighter.instantiate(); }
};

View file

@ -31,327 +31,11 @@
#include "text_editor.h"
#include "core/io/json.h"
#include "editor/editor_node.h"
#include "editor/script/script_editor_plugin.h"
#include "editor/settings/editor_settings.h"
#include "scene/gui/menu_button.h"
#include "scene/gui/split_container.h"
TextEditor *TextEditor::EditMenus::_get_active_editor() {
return Object::cast_to<TextEditor>(ScriptEditor::get_singleton()->get_current_editor());
}
void TextEditor::EditMenus::_edit_option(int p_op) {
TextEditor *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
script_text_editor->_edit_option(p_op);
}
void TextEditor::EditMenus::_prepare_edit_menu() {
TextEditor *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
const CodeEdit *tx = script_text_editor->code_editor->get_text_editor();
PopupMenu *popup = edit_menu->get_popup();
popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());
popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());
}
void TextEditor::EditMenus::_update_highlighter_menu() {
TextEditor *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
Ref<EditorSyntaxHighlighter> current_highlighter = script_text_editor->get_code_editor()->get_text_editor()->get_syntax_highlighter();
highlighter_menu->clear();
for (const Ref<EditorSyntaxHighlighter> &highlighter : script_text_editor->highlighters) {
highlighter_menu->add_radio_check_item(highlighter->_get_name());
highlighter_menu->set_item_checked(-1, highlighter == current_highlighter);
}
}
void TextEditor::EditMenus::_change_syntax_highlighter(int p_idx) {
TextEditor *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
ERR_FAIL_INDEX(p_idx, (int)script_text_editor->highlighters.size());
script_text_editor->set_syntax_highlighter(script_text_editor->highlighters[p_idx]);
}
void TextEditor::EditMenus::_update_bookmark_list() {
TextEditor *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
bookmarks_menu->clear();
bookmarks_menu->reset_size();
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
PackedInt32Array bookmark_list = script_text_editor->code_editor->get_text_editor()->get_bookmarked_lines();
if (bookmark_list.is_empty()) {
return;
}
bookmarks_menu->add_separator();
for (int i = 0; i < bookmark_list.size(); i++) {
// Strip edges to remove spaces or tabs.
// Also replace any tabs by spaces, since we can't print tabs in the menu.
String line = script_text_editor->code_editor->get_text_editor()->get_line(bookmark_list[i]).replace("\t", " ").strip_edges();
// Limit the size of the line if too big.
if (line.length() > 50) {
line = line.substr(0, 50);
}
bookmarks_menu->add_item(String::num_int64(bookmark_list[i] + 1) + " - `" + line + "`");
bookmarks_menu->set_item_metadata(-1, bookmark_list[i]);
}
}
void TextEditor::EditMenus::_bookmark_item_pressed(int p_idx) {
TextEditor *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
if (p_idx < 4) { // Any item before the separator.
script_text_editor->_edit_option(bookmarks_menu->get_item_id(p_idx));
} else {
script_text_editor->code_editor->goto_line_centered(bookmarks_menu->get_item_metadata(p_idx));
}
}
TextEditor::EditMenus::EditMenus() {
edit_menu = memnew(MenuButton);
edit_menu->set_flat(false);
edit_menu->set_theme_type_variation("FlatMenuButton");
edit_menu->set_text(TTRC("Edit"));
edit_menu->set_switch_on_hover(true);
add_child(edit_menu);
edit_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_prepare_edit_menu));
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP);
edit_menu->get_popup()->add_separator();
{
PopupMenu *sub_menu = memnew(PopupMenu);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_submenu_node_item(TTRC("Line"), sub_menu);
}
{
PopupMenu *sub_menu = memnew(PopupMenu);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_submenu_node_item(TTRC("Folding"), sub_menu);
}
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_final_newlines"), EDIT_TRIM_FINAL_NEWLINES);
{
PopupMenu *sub_menu = memnew(PopupMenu);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_submenu_node_item(TTRC("Indentation"), sub_menu);
}
edit_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_separator();
{
PopupMenu *sub_menu = memnew(PopupMenu);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/capitalize"), EDIT_CAPITALIZE);
sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_submenu_node_item(TTRC("Convert Case"), sub_menu);
}
highlighter_menu = memnew(PopupMenu);
edit_menu->get_popup()->add_submenu_node_item(TTRC("Syntax Highlighter"), highlighter_menu);
highlighter_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_update_highlighter_menu));
highlighter_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_change_syntax_highlighter));
search_menu = memnew(MenuButton);
search_menu->set_flat(false);
search_menu->set_theme_type_variation("FlatMenuButton");
search_menu->set_text(TTRC("Search"));
search_menu->set_switch_on_hover(true);
add_child(search_menu);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
search_menu->get_popup()->add_separator();
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("editor/find_in_files"), SEARCH_IN_FILES);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES);
search_menu->get_popup()->add_separator();
search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
goto_menu = memnew(MenuButton);
goto_menu->set_flat(false);
goto_menu->set_theme_type_variation("FlatMenuButton");
goto_menu->set_text(TTRC("Go To"));
goto_menu->set_switch_on_hover(true);
add_child(goto_menu);
goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
goto_menu->get_popup()->add_separator();
bookmarks_menu = memnew(PopupMenu);
goto_menu->get_popup()->add_submenu_node_item(TTRC("Bookmarks"), bookmarks_menu);
bookmarks_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_update_bookmark_list));
bookmarks_menu->connect("index_pressed", callable_mp(this, &EditMenus::_bookmark_item_pressed));
goto_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
}
////////////////////////////////////////////////////////////////////////////////
void TextEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
ERR_FAIL_COND(p_highlighter.is_null());
highlighters.push_back(p_highlighter);
}
void TextEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
ERR_FAIL_COND(p_highlighter.is_null());
CodeEdit *te = code_editor->get_text_editor();
te->set_syntax_highlighter(p_highlighter);
}
void TextEditor::_load_theme_settings() {
code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
}
String TextEditor::get_name() {
String name;
name = edited_res->get_path().get_file();
if (name.is_empty()) {
// This appears for newly created built-in text_files before saving the scene.
name = TTR("[unsaved]");
} else if (edited_res->is_built_in()) {
const String &text_file_name = edited_res->get_name();
if (!text_file_name.is_empty()) {
// If the built-in text_file has a custom resource name defined,
// display the built-in text_file name as follows: `ResourceName (scene_file.tscn)`
name = vformat("%s (%s)", text_file_name, name.get_slice("::", 0));
}
}
if (is_unsaved()) {
name += "(*)";
}
return name;
}
Ref<Texture2D> TextEditor::get_theme_icon() {
return EditorNode::get_singleton()->get_object_icon(edited_res.ptr());
}
Ref<Resource> TextEditor::get_edited_resource() const {
return edited_res;
}
void TextEditor::set_edited_resource(const Ref<Resource> &p_res) {
ERR_FAIL_COND(edited_res.is_valid());
ERR_FAIL_COND(p_res.is_null());
edited_res = p_res;
Ref<TextFile> text_file = edited_res;
if (text_file.is_valid()) {
code_editor->get_text_editor()->set_text(text_file->get_text());
}
Ref<JSON> json_file = edited_res;
if (json_file.is_valid()) {
code_editor->get_text_editor()->set_text(json_file->get_parsed_text());
}
code_editor->get_text_editor()->clear_undo_history();
code_editor->get_text_editor()->tag_saved_version();
emit_signal(SNAME("name_changed"));
code_editor->update_line_and_column();
}
void TextEditor::enable_editor() {
if (editor_enabled) {
return;
}
editor_enabled = true;
_load_theme_settings();
_validate_script();
}
void TextEditor::add_callback(const String &p_function, const PackedStringArray &p_args) {
}
void TextEditor::set_debugger_active(bool p_active) {
}
Control *TextEditor::get_base_editor() const {
return code_editor->get_text_editor();
}
CodeTextEditor *TextEditor::get_code_editor() const {
return code_editor;
}
PackedInt32Array TextEditor::get_breakpoints() {
return PackedInt32Array();
}
void TextEditor::reload_text() {
ERR_FAIL_COND(edited_res.is_null());
CodeEdit *te = code_editor->get_text_editor();
int column = te->get_caret_column();
int row = te->get_caret_line();
int h = te->get_h_scroll();
int v = te->get_v_scroll();
Ref<TextFile> text_file = edited_res;
if (text_file.is_valid()) {
te->set_text(text_file->get_text());
}
Ref<JSON> json_file = edited_res;
if (json_file.is_valid()) {
te->set_text(json_file->get_parsed_text());
}
te->set_caret_line(row);
te->set_caret_column(column);
te->set_h_scroll(h);
te->set_v_scroll(v);
te->tag_saved_version();
code_editor->update_line_and_column();
_validate_script();
}
void TextEditor::_validate_script() {
emit_signal(SNAME("name_changed"));
emit_signal(SNAME("edited_script_changed"));
TextEditorBase::_validate_script();
Ref<JSON> json_file = edited_res;
if (json_file.is_valid()) {
@ -381,94 +65,11 @@ void TextEditor::apply_code() {
code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
}
bool TextEditor::is_unsaved() {
const bool unsaved =
code_editor->get_text_editor()->get_version() != code_editor->get_text_editor()->get_saved_version() ||
edited_res->get_path().is_empty(); // In memory.
return unsaved;
}
Variant TextEditor::get_edit_state() {
return code_editor->get_edit_state();
}
void TextEditor::set_edit_state(const Variant &p_state) {
code_editor->set_edit_state(p_state);
Dictionary state = p_state;
if (state.has("syntax_highlighter")) {
for (const Ref<EditorSyntaxHighlighter> &highlighter : highlighters) {
if (highlighter->_get_name() == String(state["syntax_highlighter"])) {
set_syntax_highlighter(highlighter);
break;
}
}
ScriptEditorBase *TextEditor::create_editor(const Ref<Resource> &p_resource) {
if (Object::cast_to<TextFile>(*p_resource) || Object::cast_to<JSON>(*p_resource)) {
return memnew(TextEditor);
}
ensure_focus();
}
Variant TextEditor::get_navigation_state() {
return code_editor->get_navigation_state();
}
void TextEditor::trim_trailing_whitespace() {
code_editor->trim_trailing_whitespace();
}
void TextEditor::trim_final_newlines() {
code_editor->trim_final_newlines();
}
void TextEditor::insert_final_newline() {
code_editor->insert_final_newline();
}
void TextEditor::convert_indent() {
code_editor->get_text_editor()->convert_indent();
}
void TextEditor::tag_saved_version() {
code_editor->get_text_editor()->tag_saved_version();
edited_file_data.last_modified_time = FileAccess::get_modified_time(edited_file_data.path);
}
void TextEditor::goto_line(int p_line, int p_column) {
code_editor->goto_line(p_line, p_column);
}
void TextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
code_editor->goto_line_selection(p_line, p_begin, p_end);
}
void TextEditor::set_executing_line(int p_line) {
code_editor->set_executing_line(p_line);
}
void TextEditor::clear_executing_line() {
code_editor->clear_executing_line();
}
void TextEditor::ensure_focus() {
code_editor->get_text_editor()->grab_focus();
}
Vector<String> TextEditor::get_functions() {
return Vector<String>();
}
bool TextEditor::show_members_overview() {
return true;
}
void TextEditor::update_settings() {
code_editor->update_editor_settings();
}
void TextEditor::set_tooltip_request_func(const Callable &p_toolip_callback) {
Variant args[1] = { this };
const Variant *argp[] = { &args[0] };
code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1));
return nullptr;
}
Control *TextEditor::get_edit_menu() {
@ -478,287 +79,10 @@ Control *TextEditor::get_edit_menu() {
return edit_menus;
}
void TextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {
code_editor->set_find_replace_bar(p_bar);
}
void TextEditor::_edit_option(int p_op) {
CodeEdit *tx = code_editor->get_text_editor();
tx->apply_ime();
switch (p_op) {
case EDIT_UNDO: {
tx->undo();
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_REDO: {
tx->redo();
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_CUT: {
tx->cut();
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_COPY: {
tx->copy();
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_PASTE: {
tx->paste();
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_SELECT_ALL: {
tx->select_all();
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_MOVE_LINE_UP: {
code_editor->get_text_editor()->move_lines_up();
} break;
case EDIT_MOVE_LINE_DOWN: {
code_editor->get_text_editor()->move_lines_down();
} break;
case EDIT_INDENT: {
tx->indent_lines();
} break;
case EDIT_UNINDENT: {
tx->unindent_lines();
} break;
case EDIT_DELETE_LINE: {
code_editor->get_text_editor()->delete_lines();
} break;
case EDIT_DUPLICATE_SELECTION: {
code_editor->get_text_editor()->duplicate_selection();
} break;
case EDIT_DUPLICATE_LINES: {
code_editor->get_text_editor()->duplicate_lines();
} break;
case EDIT_TOGGLE_FOLD_LINE: {
tx->toggle_foldable_lines_at_carets();
} break;
case EDIT_FOLD_ALL_LINES: {
tx->fold_all_lines();
} break;
case EDIT_UNFOLD_ALL_LINES: {
tx->unfold_all_lines();
} break;
case EDIT_TRIM_TRAILING_WHITESAPCE: {
trim_trailing_whitespace();
} break;
case EDIT_TRIM_FINAL_NEWLINES: {
trim_final_newlines();
} break;
case EDIT_CONVERT_INDENT_TO_SPACES: {
code_editor->set_indent_using_spaces(true);
convert_indent();
} break;
case EDIT_CONVERT_INDENT_TO_TABS: {
code_editor->set_indent_using_spaces(false);
convert_indent();
} break;
case EDIT_TO_UPPERCASE: {
_convert_case(CodeTextEditor::UPPER);
} break;
case EDIT_TO_LOWERCASE: {
_convert_case(CodeTextEditor::LOWER);
} break;
case EDIT_CAPITALIZE: {
_convert_case(CodeTextEditor::CAPITALIZE);
} break;
case EDIT_TOGGLE_WORD_WRAP: {
TextEdit::LineWrappingMode wrap = code_editor->get_text_editor()->get_line_wrapping_mode();
code_editor->get_text_editor()->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY);
} break;
case SEARCH_FIND: {
code_editor->get_find_replace_bar()->popup_search();
} break;
case SEARCH_FIND_NEXT: {
code_editor->get_find_replace_bar()->search_next();
} break;
case SEARCH_FIND_PREV: {
code_editor->get_find_replace_bar()->search_prev();
} break;
case SEARCH_REPLACE: {
code_editor->get_find_replace_bar()->popup_replace();
} break;
case SEARCH_IN_FILES: {
String selected_text = code_editor->get_text_editor()->get_selected_text();
// Yep, because it doesn't make sense to instance this dialog for every single script open...
// So this will be delegated to the ScriptEditor.
emit_signal(SNAME("search_in_files_requested"), selected_text);
} break;
case REPLACE_IN_FILES: {
String selected_text = code_editor->get_text_editor()->get_selected_text();
emit_signal(SNAME("replace_in_files_requested"), selected_text);
} break;
case SEARCH_GOTO_LINE: {
goto_line_popup->popup_find_line(code_editor);
} break;
case BOOKMARK_TOGGLE: {
code_editor->toggle_bookmark();
} break;
case BOOKMARK_GOTO_NEXT: {
code_editor->goto_next_bookmark();
} break;
case BOOKMARK_GOTO_PREV: {
code_editor->goto_prev_bookmark();
} break;
case BOOKMARK_REMOVE_ALL: {
code_editor->remove_all_bookmarks();
} break;
case EDIT_EMOJI_AND_SYMBOL: {
code_editor->get_text_editor()->show_emoji_and_symbol_picker();
} break;
}
}
void TextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) {
code_editor->convert_case(p_case);
}
ScriptEditorBase *TextEditor::create_editor(const Ref<Resource> &p_resource) {
if (Object::cast_to<TextFile>(*p_resource) || Object::cast_to<JSON>(*p_resource)) {
return memnew(TextEditor);
}
return nullptr;
}
void TextEditor::register_editor() {
ScriptEditor::register_create_script_editor_function(create_editor);
}
void TextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
Ref<InputEventMouseButton> mb = ev;
if (mb.is_valid()) {
if (mb->get_button_index() == MouseButton::RIGHT) {
CodeEdit *tx = code_editor->get_text_editor();
tx->apply_ime();
Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position());
int row = pos.y;
int col = pos.x;
tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click"));
bool can_fold = tx->can_fold_line(row);
bool is_folded = tx->is_line_folded(row);
if (tx->is_move_caret_on_right_click_enabled()) {
tx->remove_secondary_carets();
if (tx->has_selection()) {
int from_line = tx->get_selection_from_line();
int to_line = tx->get_selection_to_line();
int from_column = tx->get_selection_from_column();
int to_column = tx->get_selection_to_column();
if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
// Right click is outside the selected text.
tx->deselect();
}
}
if (!tx->has_selection()) {
tx->set_caret_line(row, true, false, -1);
tx->set_caret_column(col);
}
}
if (!mb->is_pressed()) {
_make_context_menu(tx->has_selection(), can_fold, is_folded, get_local_mouse_position());
}
}
}
Ref<InputEventKey> k = ev;
if (k.is_valid() && k->is_pressed() && k->is_action("ui_menu", true)) {
CodeEdit *tx = code_editor->get_text_editor();
int line = tx->get_caret_line(0);
tx->adjust_viewport_to_caret(0);
_make_context_menu(tx->has_selection(0), tx->can_fold_line(line), tx->is_line_folded(line), (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->get_caret_draw_pos(0)));
context_menu->grab_focus();
}
}
void TextEditor::_make_context_menu(bool p_selection, bool p_can_fold, bool p_is_folded, Vector2 p_position) {
context_menu->clear();
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
context_menu->add_item(TTRC("Emoji & Symbols"), EDIT_EMOJI_AND_SYMBOL);
context_menu->add_separator();
}
if (p_selection) {
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
}
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
if (p_selection) {
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
}
if (p_can_fold || p_is_folded) {
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
}
const CodeEdit *tx = code_editor->get_text_editor();
context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo());
context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo());
context_menu->set_position(get_screen_position() + p_position);
context_menu->reset_size();
context_menu->popup();
}
void TextEditor::update_toggle_files_button() {
code_editor->update_toggle_files_button();
}
TextEditor::TextEditor() {
code_editor = memnew(CodeTextEditor);
add_child(code_editor);
code_editor->connect("load_theme_settings", callable_mp(this, &TextEditor::_load_theme_settings));
code_editor->connect("validate_script", callable_mp(this, &TextEditor::_validate_script));
code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
code_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
code_editor->show_toggle_files_button();
code_editor->set_toggle_list_control(ScriptEditor::get_singleton()->get_left_list_split());
update_settings();
code_editor->get_text_editor()->set_context_menu_enabled(false);
code_editor->get_text_editor()->connect(SceneStringName(gui_input), callable_mp(this, &TextEditor::_text_edit_gui_input));
context_menu = memnew(PopupMenu);
add_child(context_menu);
context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &TextEditor::_edit_option));
Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;
plain_highlighter.instantiate();
add_syntax_highlighter(plain_highlighter);
Ref<EditorStandardSyntaxHighlighter> highlighter;
highlighter.instantiate();
add_syntax_highlighter(highlighter);
set_syntax_highlighter(plain_highlighter);
goto_line_popup = memnew(GotoLinePopup);
add_child(goto_line_popup);
}
TextEditor::~TextEditor() {
highlighters.clear();
}
void TextEditor::validate() {
code_editor->validate_script();
}

View file

@ -30,142 +30,24 @@
#pragma once
#include "editor/script/script_editor_plugin.h"
#include "editor/script/script_editor_base.h"
#include "editor/gui/code_editor.h"
class TextEditor : public ScriptEditorBase {
GDCLASS(TextEditor, ScriptEditorBase);
class EditMenus : public HBoxContainer {
GDCLASS(EditMenus, HBoxContainer);
MenuButton *edit_menu = nullptr;
MenuButton *search_menu = nullptr;
MenuButton *goto_menu = nullptr;
PopupMenu *bookmarks_menu = nullptr;
PopupMenu *highlighter_menu = nullptr;
TextEditor *_get_active_editor();
void _edit_option(int p_op);
void _prepare_edit_menu();
void _update_highlighter_menu();
void _change_syntax_highlighter(int p_idx);
void _update_bookmark_list();
void _bookmark_item_pressed(int p_idx);
public:
EditMenus();
};
class TextEditor : public TextEditorBase {
GDCLASS(TextEditor, TextEditorBase);
static ScriptEditorBase *create_editor(const Ref<Resource> &p_resource);
CodeTextEditor *code_editor = nullptr;
Ref<Resource> edited_res;
bool editor_enabled = false;
static inline EditMenus *edit_menus = nullptr;
PopupMenu *context_menu = nullptr;
GotoLinePopup *goto_line_popup = nullptr;
LocalVector<Ref<EditorSyntaxHighlighter>> highlighters;
enum {
EDIT_UNDO,
EDIT_REDO,
EDIT_CUT,
EDIT_COPY,
EDIT_PASTE,
EDIT_SELECT_ALL,
EDIT_TRIM_TRAILING_WHITESAPCE,
EDIT_TRIM_FINAL_NEWLINES,
EDIT_CONVERT_INDENT_TO_SPACES,
EDIT_CONVERT_INDENT_TO_TABS,
EDIT_MOVE_LINE_UP,
EDIT_MOVE_LINE_DOWN,
EDIT_INDENT,
EDIT_UNINDENT,
EDIT_DELETE_LINE,
EDIT_DUPLICATE_SELECTION,
EDIT_DUPLICATE_LINES,
EDIT_TO_UPPERCASE,
EDIT_TO_LOWERCASE,
EDIT_CAPITALIZE,
EDIT_TOGGLE_WORD_WRAP,
EDIT_TOGGLE_FOLD_LINE,
EDIT_FOLD_ALL_LINES,
EDIT_UNFOLD_ALL_LINES,
SEARCH_FIND,
SEARCH_FIND_NEXT,
SEARCH_FIND_PREV,
SEARCH_REPLACE,
SEARCH_IN_FILES,
REPLACE_IN_FILES,
SEARCH_GOTO_LINE,
BOOKMARK_TOGGLE,
BOOKMARK_GOTO_NEXT,
BOOKMARK_GOTO_PREV,
BOOKMARK_REMOVE_ALL,
EDIT_EMOJI_AND_SYMBOL,
};
protected:
void _edit_option(int p_op);
void _make_context_menu(bool p_selection, bool p_can_fold, bool p_is_folded, Vector2 p_position);
void _text_edit_gui_input(const Ref<InputEvent> &ev);
void _load_theme_settings();
void _convert_case(CodeTextEditor::CaseStyle p_case);
void _validate_script();
virtual void _validate_script() override;
public:
virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
virtual String get_name() override;
virtual Ref<Texture2D> get_theme_icon() override;
virtual Ref<Resource> get_edited_resource() const override;
virtual void set_edited_resource(const Ref<Resource> &p_res) override;
virtual void enable_editor() override;
virtual void reload_text() override;
virtual void apply_code() override;
virtual bool is_unsaved() override;
virtual Variant get_edit_state() override;
virtual void set_edit_state(const Variant &p_state) override;
virtual Variant get_navigation_state() override;
virtual Vector<String> get_functions() override;
virtual PackedInt32Array get_breakpoints() override;
virtual void set_breakpoint(int p_line, bool p_enabled) override {}
virtual void clear_breakpoints() override {}
virtual void goto_line(int p_line, int p_column = 0) override;
void goto_line_selection(int p_line, int p_begin, int p_end);
virtual void set_executing_line(int p_line) override;
virtual void clear_executing_line() override;
virtual void trim_trailing_whitespace() override;
virtual void trim_final_newlines() override;
virtual void insert_final_newline() override;
virtual void convert_indent() override;
virtual void ensure_focus() override;
virtual void tag_saved_version() override;
virtual void update_settings() override;
virtual bool show_members_overview() override;
virtual void set_debugger_active(bool p_active) override;
virtual void set_tooltip_request_func(const Callable &p_toolip_callback) override;
virtual void add_callback(const String &p_function, const PackedStringArray &p_args) override;
void update_toggle_files_button() override;
virtual Control *get_edit_menu() override;
virtual void set_find_replace_bar(FindReplaceBar *p_bar) override;
virtual void validate() override;
virtual Control *get_base_editor() const override;
virtual CodeTextEditor *get_code_editor() const override;
static void register_editor();
TextEditor();
~TextEditor();
};

View file

@ -30,7 +30,7 @@
#pragma once
#include "editor/script/script_editor_plugin.h"
#include "editor/script/syntax_highlighters.h"
class GDScriptSyntaxHighlighter : public EditorSyntaxHighlighter {
GDCLASS(GDScriptSyntaxHighlighter, EditorSyntaxHighlighter)

View file

@ -39,6 +39,7 @@
#ifdef TOOLS_ENABLED
#include "editor/gdscript_highlighter.h"
#include "editor/gdscript_translation_parser_plugin.h"
#include "editor/script/script_editor_plugin.h"
#ifndef GDSCRIPT_NO_LSP
#include "language_server/gdscript_language_server.h"