Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Add simple symbol renaming

kobewif1f7f62d6810
diff --git a/core/object/editor_language.h b/core/object/editor_language.h
index 8fa8b5bbb8d..3422799fa56 100644
--- a/core/object/editor_language.h
+++ b/core/object/editor_language.h
@@ -174,6 +174,7 @@ public:
* @param r_result The returned `LookupResult`.
*/
virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) { return ERR_UNAVAILABLE; }
+ virtual Error lookup_code_for_rename(const String &p_code, const String &p_symbol, const String &p_path, LookupResult &r_result) { return ERR_UNAVAILABLE; }
/**
* Called by the editor to find a top-level function in the given source code.
diff --git a/editor/script/find_in_files.cpp b/editor/script/find_in_files.cpp
index fbb057e0e9e..3aa9ca5f02c 100644
--- a/editor/script/find_in_files.cpp
+++ b/editor/script/find_in_files.cpp
@@ -32,6 +32,7 @@
#include "core/config/project_settings.h"
#include "core/io/dir_access.h"
+#include "core/io/resource_loader.h"
#include "core/object/callable_mp.h"
#include "core/os/os.h"
#include "editor/docks/editor_dock_manager.h"
@@ -40,6 +41,7 @@
#include "editor/file_system/editor_file_system.h"
#include "editor/gui/editor_file_dialog.h"
#include "editor/script/script_editor_plugin.h"
+#include "editor/script/script_text_editor.h"
#include "editor/settings/editor_command_palette.h"
#include "editor/settings/editor_settings.h"
#include "editor/shader/shader_editor_plugin.h"
@@ -420,7 +422,12 @@ void FindInFilesSearch::_bind_methods() {
void FindInFilesSearchPanel::set_finder(FindInFilesSearch *p_finder, bool p_init) {
finder = p_finder;
+
+ default_view->set_visible(!p_finder->is_rename_mode());
+ rename_view->set_visible(p_finder->is_rename_mode());
+
search_text_line_edit->set_text(finder->get_search_text());
+ renamed_symbol_name->set_text(finder->get_search_text());
match_case_checkbox->set_pressed_no_signal(finder->get_match_case());
whole_words_checkbox->set_pressed_no_signal(finder->get_whole_words());
folder_line_edit->set_text(finder->get_folder());
@@ -441,6 +448,7 @@ void FindInFilesSearchPanel::set_finder(FindInFilesSearch *p_finder, bool p_init
}
void FindInFilesSearchPanel::set_search_text(const String &p_text) {
+ renamed_symbol_name->set_text(p_text);
if (!p_text.is_empty()) {
search_text_line_edit->set_text(p_text);
_on_search_submitted();
@@ -450,6 +458,7 @@ void FindInFilesSearchPanel::set_search_text(const String &p_text) {
void FindInFilesSearchPanel::set_replace_text(const String &p_text) {
replace_line_edit->set_text(p_text);
+ rename_line_edit->set_text(p_text);
replace_line_edit->emit_signal(SceneStringName(text_changed), p_text);
}
@@ -458,13 +467,21 @@ void FindInFilesSearchPanel::set_replace(bool p_replace_mode) {
}
String FindInFilesSearchPanel::get_search_text() const {
- return search_text_line_edit->get_text();
+ if (is_rename_mode()) {
+ return renamed_symbol_name->get_text();
+ } else {
+ return search_text_line_edit->get_text();
+ }
}
bool FindInFilesSearchPanel::is_replace_pressed() const {
return toggle_replace_button->is_pressed();
}
+bool FindInFilesSearchPanel::is_rename_mode() const {
+ return rename_view->is_visible();
+}
+
HashSet<String> FindInFilesSearchPanel::get_filter() const {
// Could check the filters_preferences but it might not have been generated yet.
HashSet<String> filters;
@@ -576,6 +593,7 @@ void FindInFilesSearchPanel::_on_folder_selected(String p_path) {
void FindInFilesSearchPanel::_bind_methods() {
ADD_SIGNAL(MethodInfo("find_requested"));
ADD_SIGNAL(MethodInfo("replace_all_requested"));
+ ADD_SIGNAL(MethodInfo("close_tab_requested"));
}
FindInFilesSearchPanel::FindInFilesSearchPanel() {
@@ -588,18 +606,18 @@ FindInFilesSearchPanel::FindInFilesSearchPanel() {
sb->set_content_margin_all(6 * EDSCALE);
add_theme_style_override("panel", sb);
- VBoxContainer *vbc = memnew(VBoxContainer);
- vbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
- add_child(vbc);
+ default_view = memnew(VBoxContainer);
+ default_view->set_h_size_flags(Control::SIZE_EXPAND_FILL);
+ add_child(default_view);
{
Label *find_label = memnew(Label);
find_label->set_text(TTRC("Find"));
- vbc->add_child(find_label);
+ default_view->add_child(find_label);
HBoxContainer *search_hbc = memnew(HBoxContainer);
search_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
- vbc->add_child(search_hbc);
+ default_view->add_child(search_hbc);
search_text_line_edit = memnew(LineEdit);
search_text_line_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
@@ -629,11 +647,11 @@ FindInFilesSearchPanel::FindInFilesSearchPanel() {
toggle_replace_button->set_text(TTRC("Replace"));
toggle_replace_button->set_tooltip_text(TTRC("Toggle Replace Mode"));
toggle_replace_button->connect(SceneStringName(toggled), callable_mp(this, &FindInFilesSearchPanel::_toggle_replace_pressed));
- vbc->add_child(toggle_replace_button);
+ default_view->add_child(toggle_replace_button);
replace_hbox = memnew(HBoxContainer);
replace_hbox->set_visible(toggle_replace_button->is_pressed());
- vbc->add_child(replace_hbox);
+ default_view->add_child(replace_hbox);
replace_line_edit = memnew(LineEdit);
replace_line_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
@@ -668,11 +686,11 @@ FindInFilesSearchPanel::FindInFilesSearchPanel() {
filters_label = memnew(Label);
filters_label->set_text(TTRC("Filters"));
- vbc->add_child(filters_label);
+ default_view->add_child(filters_label);
additional_options_vbc = memnew(VBoxContainer);
additional_options_vbc->add_theme_constant_override("separation", 0);
- vbc->add_child(additional_options_vbc);
+ default_view->add_child(additional_options_vbc);
{
HBoxContainer *hbc = memnew(HBoxContainer);
additional_options_vbc->add_child(hbc);
@@ -742,6 +760,43 @@ FindInFilesSearchPanel::FindInFilesSearchPanel() {
EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &FindInFilesSearchPanel::_update_file_extensions).bind(false));
_update_file_extensions(true);
+ rename_view = memnew(VBoxContainer);
+ rename_view->set_h_size_flags(Control::SIZE_EXPAND_FILL);
+ rename_view->hide();
+ add_child(rename_view);
+
+ {
+ Label *rename_label = memnew(Label);
+ rename_label->set_text(TTRC("Rename Symbol"));
+ rename_view->add_child(rename_label);
+
+ renamed_symbol_name = memnew(LineEdit);
+ renamed_symbol_name->set_accessibility_name(TTRC("Symbol to Rename"));
+ renamed_symbol_name->set_editable(false);
+ rename_view->add_child(renamed_symbol_name);
+
+ Label *new_name_label = memnew(Label);
+ new_name_label->set_text(TTRC("New Name"));
+ rename_view->add_child(new_name_label);
+
+ rename_line_edit = memnew(LineEdit);
+ rename_line_edit->set_accessibility_name(TTRC("New Name"));
+ rename_view->add_child(rename_line_edit);
+ rename_line_edit->connect(SceneStringName(text_submitted), callable_mp(this, &FindInFilesSearchPanel::_on_rename_apply).bind(false).unbind(1));
+
+ HBoxContainer *applydiscard_hb = memnew(HBoxContainer);
+ applydiscard_hb->set_alignment(BoxContainer::ALIGNMENT_CENTER);
+ rename_view->add_child(applydiscard_hb);
+
+ apply_rename = memnew(Button(TTRC("Rename Selected")));
+ applydiscard_hb->add_child(apply_rename);
+ apply_rename->connect(SceneStringName(pressed), callable_mp(this, &FindInFilesSearchPanel::_on_rename_apply).bind(false));
+
+ apply_rename = memnew(Button(TTRC("Cancel")));
+ applydiscard_hb->add_child(apply_rename);
+ apply_rename->connect(SceneStringName(pressed), callable_mp(this, &FindInFilesSearchPanel::_on_rename_discard));
+ }
+
debounce_timer = memnew(Timer);
debounce_timer->set_one_shot(true);
debounce_timer->connect("timeout", callable_mp(this, &FindInFilesSearchPanel::_emit_find_requested));
@@ -772,6 +827,11 @@ void FindInFilesResultsPanel::set_replace_text(const String &p_text) {
}
}
+void FindInFilesResultsPanel::set_symbol_rename(const EditorLanguage::LookupResult &p_symbol) {
+ symbol_rename = p_symbol;
+ set_with_replace(true);
+}
+
void FindInFilesResultsPanel::set_search_labels_visibility(bool p_visible) {
find_label->set_visible(p_visible);
search_text_label->set_visible(p_visible);
@@ -847,6 +907,42 @@ void FindInFilesResultsPanel::update_layout(EditorDock::DockLayout p_layout, int
results_display->set_scroll_hint_mode(p_slot == EditorDock::DOCK_SLOT_BOTTOM ? Tree::SCROLL_HINT_MODE_BOTH : Tree::SCROLL_HINT_MODE_TOP);
}
+void FindInFilesContainer::create_rename_control(const String &p_symbol, const EditorLanguage::LookupResult &p_lookup) {
+ FindInFilesResultsPanel *new_panel = memnew(FindInFilesResultsPanel);
+ tabs->add_child(new_panel);
+ tabs->set_current_tab(tabs->get_tab_count() - 1);
+ _update_bar_visibility();
+
+ new_panel->connect("result_selected", callable_mp(this, &FindInFilesContainer::_result_selected));
+ new_panel->connect("files_modified", callable_mp(this, &FindInFilesContainer::_files_modified));
+ new_panel->get_new_tab_button()->connect(SceneStringName(pressed), callable_mp(this, &FindInFilesContainer::create_new_panel));
+
+ HashSet<String> exts;
+
+ Ref<Script> scr = ResourceLoader::load(p_lookup.script_path);
+ ERR_FAIL_COND(scr.is_null());
+ exts.insert(scr->get_language()->get_extension());
+
+ FindInFilesSearch *finder = new_panel->get_finder();
+ finder->set_search_text(p_symbol);
+ finder->set_match_case(true);
+ finder->set_whole_words(true);
+ finder->set_filter(exts);
+ finder->set_symbol_rename(p_lookup);
+
+ search_control->set_finder(finder, false);
+ search_control->set_search_text(p_symbol);
+ search_control->set_replace(true);
+ search_control->set_replace_text(p_symbol);
+
+ new_panel->set_symbol_rename(p_lookup);
+ new_panel->start_search();
+
+ if (tabs->get_tab_count() == 1) {
+ _update_bar_visibility();
+ }
+}
+
void FindInFilesResultsPanel::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED: {
@@ -942,8 +1038,35 @@ void FindInFilesResultsPanel::_on_result_found(const String &p_fpath, int p_line
if (with_replace) {
_update_replace_item(item, r);
+ bool check_item = false;
+ if (!symbol_rename.script_path.is_empty()) {
+ if (ResourceLoader::exists(p_fpath, "Script")) {
+ Ref<Script> source_script = ResourceLoader::load(p_fpath);
+ if (source_script.is_valid()) {
+ PackedStringArray lines = source_script->get_source_code().split("\n");
+ const String current_line = lines[p_line_number - 1];
+ lines.write[p_line_number - 1] = current_line.insert(p_begin, String::chr(0xFFFF));
+
+ EditorLanguage::LookupResult result;
+ Error err = source_script->get_language()->get_editor_language()->lookup_code_for_rename(String("\n").join(lines), current_line.substr(p_begin, p_end - p_begin), source_script->get_path(), result);
+ if (err == OK) {
+ check_item = result.type == symbol_rename.type &&
+ result.class_name == symbol_rename.class_name &&
+ result.class_member == symbol_rename.class_member &&
+ result.doc_type == symbol_rename.doc_type &&
+ result.enumeration == symbol_rename.enumeration &&
+ result.is_bitfield == symbol_rename.is_bitfield &&
+ result.value == symbol_rename.value &&
+ result.script_path == symbol_rename.script_path &&
+ result.location == symbol_rename.location;
+ }
+ }
+ }
+ } else {
+ check_item = true;
+ }
item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
- item->set_checked(0, true);
+ item->set_checked(0, check_item);
item->set_editable(0, true);
item->add_button(1, replace_texture, FIND_BUTTON_REPLACE, false, TTR("Replace"));
item->add_button(1, remove_texture, FIND_BUTTON_REMOVE, false, TTR("Remove result"));
@@ -1116,6 +1239,14 @@ void FindInFilesResultsPanel::replace_all() {
}
emit_signal(SNAME("files_modified"));
+
+ if (!symbol_rename.script_path.is_empty()) {
+ // Go back to the Script Editor.
+ ScriptTextEditor *script_editor = Object::cast_to<ScriptTextEditor>(ScriptEditor::get_singleton()->get_current_editor());
+ if (script_editor && script_editor->is_visible_in_tree()) {
+ script_editor->get_code_editor()->get_text_editor()->grab_focus();
+ }
+ }
}
void FindInFilesResultsPanel::_on_button_clicked(TreeItem *p_item, int p_column, int p_id, int p_mouse_button_index) {
@@ -1331,9 +1462,10 @@ FindInFilesResultsPanel::FindInFilesResultsPanel() {
//-----------------------------------------------------------------------------
-FindInFilesResultsPanel *FindInFilesContainer::_create_new_panel() {
+FindInFilesResultsPanel *FindInFilesContainer::create_new_panel() {
int index = tabs->get_current_tab();
FindInFilesResultsPanel *old_panel = Object::cast_to<FindInFilesResultsPanel>(tabs->get_current_tab_control());
+ bool copy_old_panel = old_panel && !old_panel->get_finder()->is_rename_mode();
FindInFilesResultsPanel *new_panel = memnew(FindInFilesResultsPanel);
tabs->add_child(new_panel);
@@ -1344,29 +1476,38 @@ FindInFilesResultsPanel *FindInFilesContainer::_create_new_panel() {
new_panel->connect("result_selected", callable_mp(this, &FindInFilesContainer::_result_selected));
new_panel->connect("files_modified", callable_mp(this, &FindInFilesContainer::_files_modified));
- new_panel->get_new_tab_button()->connect(SceneStringName(pressed), callable_mp(this, &FindInFilesContainer::_create_new_panel));
+ new_panel->get_new_tab_button()->connect(SceneStringName(pressed), callable_mp(this, &FindInFilesContainer::create_new_panel));
FindInFilesSearch *new_finder = new_panel->get_finder();
- if (old_panel) {
- if (FindInFilesSearch *old_finder = old_panel->get_finder()) {
- new_finder->copy_from(old_finder);
- }
+ if (copy_old_panel) {
+ new_finder->copy_from(old_panel->get_finder());
search_control->set_replace_text(old_panel->get_replace_text());
+ } else {
+ search_control->set_replace(false);
}
- search_control->set_finder(new_finder, !old_panel);
+ search_control->set_finder(new_finder, !copy_old_panel);
search_control->set_search_text(new_finder->get_search_text());
return new_panel;
}
void FindInFilesContainer::_update_current_title() {
- tabs->set_tab_title(tabs->get_current_tab(), vformat(TTR("Find: %s"), search_control->get_search_text()));
+ FindInFilesResultsPanel *panel = Object::cast_to<FindInFilesResultsPanel>(tabs->get_current_tab_control());
+ if (panel->get_finder()->is_rename_mode()) {
+ tabs->set_tab_title(tabs->get_current_tab(), vformat(TTR("Rename: %s"), search_control->get_search_text()));
+ } else {
+ tabs->set_tab_title(tabs->get_current_tab(), vformat(TTR("Find: %s"), search_control->get_search_text()));
+ }
+}
+
+void FindInFilesContainer::_close_current_tab() {
+ _close_panel(Object::cast_to<FindInFilesResultsPanel>(tabs->get_current_tab_control()));
}
void FindInFilesContainer::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_VISIBILITY_CHANGED: {
if (is_visible() && tabs->get_tab_count() == 0) {
- _create_new_panel();
+ create_new_panel();
}
} break;
case NOTIFICATION_POSTINITIALIZE: {
@@ -1393,6 +1534,9 @@ void FindInFilesContainer::_on_theme_changed() {
void FindInFilesContainer::_on_tab_changed() {
if (FindInFilesResultsPanel *panel = Object::cast_to<FindInFilesResultsPanel>(tabs->get_current_tab_control())) {
+ if (panel->get_finder()->is_rename_mode()) {
+ search_control->set_replace(true);
+ }
search_control->set_replace_text(panel->get_replace_text());
search_control->set_finder(panel->get_finder(), false);
}
@@ -1464,7 +1608,7 @@ void FindInFilesContainer::_update_bar_visibility() {
for (int i = 0; i < tabs->get_tab_count(); i++) {
FindInFilesResultsPanel *panel = Object::cast_to<FindInFilesResultsPanel>(tabs->get_tab_control(i));
if (panel) {
- panel->set_search_labels_visibility(!bar_visible);
+ panel->set_search_labels_visibility(!bar_visible && !panel->get_finder()->is_rename_mode());
panel->get_new_tab_button()->set_visible(!bar_visible);
}
}
@@ -1559,7 +1703,9 @@ FindInFilesContainer::FindInFilesContainer() {
search_control->set_scroll_hint_mode(ScrollContainer::SCROLL_HINT_MODE_BOTTOM_AND_RIGHT);
search_control->connect("find_requested", callable_mp(this, &FindInFilesContainer::_start_find_in_files));
search_control->connect("replace_all_requested", callable_mp(this, &FindInFilesContainer::_replace_all));
+ search_control->connect("close_tab_requested", callable_mp(this, &FindInFilesContainer::_close_current_tab));
search_control->get_replace_line_edit()->connect(SceneStringName(text_changed), callable_mp(this, &FindInFilesContainer::_set_replace_text));
+ search_control->get_rename_line_edit()->connect(SceneStringName(text_changed), callable_mp(this, &FindInFilesContainer::_set_replace_text));
hsplit->add_child(search_control);
tabs = memnew(TabContainer);
@@ -1575,7 +1721,7 @@ FindInFilesContainer::FindInFilesContainer() {
new_tab_button = memnew(Button);
new_tab_button->set_flat(true);
- new_tab_button->connect(SceneStringName(pressed), callable_mp(this, &FindInFilesContainer::_create_new_panel));
+ new_tab_button->connect(SceneStringName(pressed), callable_mp(this, &FindInFilesContainer::create_new_panel));
tabs->get_internal_container()->add_child(new_tab_button);
tabs_context_menu = memnew(PopupMenu);
@@ -1592,6 +1738,9 @@ FindInFilesContainer::FindInFilesContainer() {
void FindInFiles::open_dock(const String &p_initial_text, bool p_replace) {
FindInFilesSearchPanel *search_control = container->get_search_control();
EditorDockManager::get_singleton()->focus_dock(container);
+ if (search_control->is_rename_mode()) {
+ container->create_new_panel();
+ }
search_control->set_search_text(p_initial_text);
search_control->set_replace(p_replace);
}
@@ -1602,3 +1751,27 @@ FindInFiles::FindInFiles() {
EditorDockManager::get_singleton()->add_dock(container);
container->close();
}
+
+void FindInFilesSearchPanel::_on_rename_apply(bool p_confirm) {
+ if (rename_line_edit->get_text().is_empty() || rename_line_edit->get_text() == renamed_symbol_name->get_text()) {
+ return;
+ }
+ if (!p_confirm) {
+ if (!rename_confirm) {
+ rename_confirm = memnew(ConfirmationDialog);
+ rename_confirm->set_autowrap(true);
+ rename_confirm->set_text(TTRC("Symbol renaming is an experimental feature. It can be unreliable, and in worst case, cause data loss. Don't use without version control and/or backup."));
+ rename_confirm->set_ok_button_text(TTRC("I understand the risk"));
+ add_child(rename_confirm);
+ rename_confirm->connect(SceneStringName(confirmed), callable_mp(this, &FindInFilesSearchPanel::_on_rename_apply).bind(true));
+ }
+ rename_confirm->popup_centered(Vector2i(EDSCALE_RND(400), 0));
+ return;
+ }
+ emit_signal("replace_all_requested");
+ emit_signal("close_tab_requested");
+}
+
+void FindInFilesSearchPanel::_on_rename_discard() {
+ emit_signal("close_tab_requested");
+}
diff --git a/editor/script/find_in_files.h b/editor/script/find_in_files.h
index f9d5241662e..d1cf0d28703 100644
--- a/editor/script/find_in_files.h
+++ b/editor/script/find_in_files.h
@@ -30,6 +30,7 @@
#pragma once
+#include "core/object/editor_language.h"
#include "core/templates/hash_map.h"
#include "editor/docks/editor_dock.h"
#include "scene/gui/scroll_container.h"
@@ -46,6 +47,7 @@ class FindInFilesSearch : public Node {
String root_dir;
bool whole_words = false;
bool match_case = false;
+ EditorLanguage::LookupResult symbol_rename;
// State.
HashSet<String> include_wildcards;
@@ -80,6 +82,7 @@ public:
void set_includes(const String &p_include_wildcards);
void set_excludes(const String &p_exclude_wildcards);
void set_folder(const String &p_folder);
+ void set_symbol_rename(const EditorLanguage::LookupResult &p_symbol) { symbol_rename = p_symbol; }
String get_search_text() const { return pattern; }
bool get_whole_words() { return whole_words; }
@@ -88,9 +91,11 @@ public:
String get_includes() { return include_string; }
String get_excludes() { return exclude_string; }
String get_folder() { return root_dir; }
+ EditorLanguage::LookupResult get_symbol_rename() const { return symbol_rename; }
bool is_whole_words() const { return whole_words; }
bool is_match_case() const { return match_case; }
+ bool is_rename_mode() const { return !symbol_rename.script_path.is_empty(); }
void start();
void stop();
@@ -122,6 +127,8 @@ class FindInFilesSearchPanel : public ScrollContainer {
void _emit_find_requested();
void _toggle_replace_pressed(bool p_with_replace);
void _on_replace_all_clicked(bool p_dialog);
+ void _on_rename_apply(bool p_confirm);
+ void _on_rename_discard();
void _on_additional_options_toggled(bool p_toggled);
void _on_add_new_tab_clicked();
void _on_filter_toggled(bool p_pressed, const String &p_ext);
@@ -129,6 +136,8 @@ class FindInFilesSearchPanel : public ScrollContainer {
void _update_file_extensions(bool p_init);
void _highlight_search_text();
+ VBoxContainer *default_view = nullptr;
+
FindInFilesSearch *finder = nullptr;
Timer *debounce_timer = nullptr;
CheckBox *toggle_replace_button = nullptr;
@@ -155,6 +164,14 @@ class FindInFilesSearchPanel : public ScrollContainer {
LineEdit *includes_line_edit = nullptr;
LineEdit *excludes_line_edit = nullptr;
+ VBoxContainer *rename_view = nullptr;
+ LineEdit *renamed_symbol_name = nullptr;
+ LineEdit *rename_line_edit = nullptr;
+ Button *apply_rename = nullptr;
+ Button *discard_rename = nullptr;
+
+ ConfirmationDialog *rename_confirm = nullptr;
+
protected:
void _notification(int p_what);
@@ -169,9 +186,11 @@ public:
String get_search_text() const;
bool is_replace_pressed() const;
+ bool is_rename_mode() const;
HashSet<String> get_filter() const;
LineEdit *get_replace_line_edit() const { return replace_line_edit; }
+ LineEdit *get_rename_line_edit() const { return rename_line_edit; }
FindInFilesSearchPanel();
};
@@ -221,6 +240,7 @@ class FindInFilesResultsPanel : public MarginContainer {
bool with_replace = false;
String replace_text;
+ EditorLanguage::LookupResult symbol_rename;
MarginContainer *results_mc = nullptr;
@@ -255,6 +275,7 @@ public:
void set_with_replace(bool p_with_replace);
void set_replace_text(const String &p_text);
+ void set_symbol_rename(const EditorLanguage::LookupResult &p_symbol);
void set_search_labels_visibility(bool p_visible);
void start_search();
@@ -297,8 +318,8 @@ class FindInFilesContainer : public EditorDock {
void _bar_input(const Ref<InputEvent> &p_input);
void _on_theme_changed();
- FindInFilesResultsPanel *_create_new_panel();
void _update_current_title();
+ void _close_current_tab();
void _result_selected(const String &p_fpath, int p_line_number, int p_begin, int p_end);
void _files_modified();
@@ -316,6 +337,8 @@ public:
virtual void update_layout(EditorDock::DockLayout p_layout, int p_slot) override;
FindInFilesSearchPanel *get_search_control() { return search_control; }
+ FindInFilesResultsPanel *create_new_panel();
+ void create_rename_control(const String &p_symbol, const EditorLanguage::LookupResult &p_lookup);
FindInFilesContainer();
};
@@ -334,5 +357,7 @@ public:
void open_dock(const String &p_initial_text = "", bool p_replace = false);
+ FindInFilesContainer *get_dock() const { return container; }
+
FindInFiles();
};
diff --git a/editor/script/script_editor_plugin.cpp b/editor/script/script_editor_plugin.cpp
index 2463a0cfdf5..19d4a267400 100644
--- a/editor/script/script_editor_plugin.cpp
+++ b/editor/script/script_editor_plugin.cpp
@@ -3726,6 +3726,15 @@ void DocumentEditorContainer::update_docs_from_script(const Ref<Script> &p_scrip
}
}
+void ScriptEditor::rename_symbol(const String &p_symbol, const EditorLanguage::LookupResult &p_lookup) {
+ FindInFiles::get_singleton()->get_container()->create_rename_control(p_symbol, p_lookup);
+ FindInFiles::get_singleton()->get_dock()->make_visible();
+
+ LineEdit *name_edit = FindInFiles::get_singleton()->get_container()->get_search_control()->get_rename_line_edit();
+ callable_mp((Control *)name_edit, &Control::grab_focus).call_deferred(false);
+ callable_mp(name_edit, &LineEdit::select_all).call_deferred();
+}
+
void DocumentEditorContainer::_update_selected_editor_menu() {
for (ScriptEditorBase::EditMenusBase *editor_menu : editor_menus) {
editor_menu->set_visible(editor_menu->handles(_get_current_editor()));
diff --git a/editor/script/script_editor_plugin.h b/editor/script/script_editor_plugin.h
index 3b50a10fe07..48e240bd211 100644
--- a/editor/script/script_editor_plugin.h
+++ b/editor/script/script_editor_plugin.h
@@ -31,6 +31,7 @@
#pragma once
#include "core/error/error_list.h"
+#include "core/object/editor_language.h"
#include "core/object/script_language.h"
#include "editor/docks/editor_dock.h"
#include "editor/plugins/editor_plugin.h"
@@ -571,6 +572,7 @@ public:
void update_doc(const String &p_name) { script_container->update_doc(p_name); }
void clear_docs_from_script(const Ref<Script> &p_script) { script_container->clear_docs_from_script(p_script); }
void update_docs_from_script(const Ref<Script> &p_script) { script_container->update_docs_from_script(p_script); }
+ void rename_symbol(const String &p_symbol, const EditorLanguage::LookupResult &p_lookup);
void trigger_live_script_reload(const String &p_script_path) { script_container->trigger_live_script_reload(p_script_path); }
diff --git a/editor/script/script_text_editor.cpp b/editor/script/script_text_editor.cpp
index 3af5ba422a0..7888c55650e 100644
--- a/editor/script/script_text_editor.cpp
+++ b/editor/script/script_text_editor.cpp
@@ -50,12 +50,14 @@
#include "editor/inspector/editor_context_menu_plugin.h"
#include "editor/inspector/editor_inspector.h"
#include "editor/inspector/multi_node_edit.h"
+#include "editor/script/find_in_files.h"
#include "editor/script/script_editor_navigation_marker.h"
#include "editor/script/syntax_highlighters.h"
#include "editor/settings/editor_command_palette.h"
#include "editor/settings/editor_settings.h"
#include "editor/themes/editor_scale.h"
#include "scene/gui/grid_container.h"
+#include "scene/gui/line_edit.h"
#include "scene/gui/menu_button.h"
#include "scene/gui/rich_text_label.h"
#include "scene/gui/split_container.h"
@@ -1665,6 +1667,12 @@ void ScriptTextEditor::shortcut_input(const Ref<InputEvent> &p_event) {
return;
}
+ if (ED_GET_SHORTCUT("script_text_editor/rename_symbol")->matches_event(p_event)) {
+ _edit_option(RENAME_SYMBOL);
+ accept_event();
+ return;
+ }
+
const Callable custom_callback = EditorContextMenuPluginManager::get_singleton()->match_custom_shortcut(EditorContextMenuPlugin::CONTEXT_SLOT_SCRIPT_EDITOR_CODE, p_event);
if (custom_callback.is_valid()) {
#ifndef DISABLE_DEPRECATED
@@ -1850,6 +1858,39 @@ bool ScriptTextEditor::_edit_option(int p_op) {
_lookup_symbol(text, tx->get_caret_line(0), tx->get_caret_column(0));
}
} break;
+ case RENAME_SYMBOL: {
+ String text = tx->get_word_under_caret(0);
+ if (text.is_empty()) {
+ text = tx->get_selected_text(0);
+ }
+ if (text.is_empty()) {
+ break;
+ }
+ Ref<Script> script = edited_res;
+
+ EditorLanguage::LookupResult result;
+ String code_text = code_editor->get_text_editor()->get_text_with_cursor_char(tx->get_caret_line(0), tx->get_caret_column(0));
+ if (script->get_language()->get_editor_language()->lookup_code_for_rename(code_text, text, script->get_path(), result) != OK) {
+ break;
+ }
+
+ if (result.script_path.is_empty()) {
+ // Don't allow renaming native symbols.
+ break;
+ }
+
+ Vector2 pos = tx->get_screen_position();
+ // Set popup at the beginning of the word.
+ const PackedInt32Array words = TS->shaped_text_get_word_breaks(tx->get_line_data(tx->get_caret_line(0))->get_rid());
+ for (int i = 0; i < words.size(); i = i + 2) {
+ if ((words[i] <= tx->get_caret_column(0) && words[i + 1] >= tx->get_caret_column(0)) || (i == words.size() - 2 && tx->get_caret_column(0) == words[i + 1])) {
+ pos += tx->get_rect_at_line_column(tx->get_caret_line(), words[i] + 1).position;
+ break;
+ }
+ }
+ ScriptEditor::get_singleton()->rename_symbol(text, result);
+ } break;
+
default: {
if (CodeEditorBase::_edit_option(p_op)) {
return true;
@@ -2480,9 +2521,11 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &p_ev) {
bool foldable = tx->can_fold_line(mouse_line) || tx->is_line_folded(mouse_line);
bool open_docs = false;
+ bool allow_rename = false;
if (ScriptServer::is_global_class(word_at_pos) || word_at_pos.is_resource_file()) {
open_docs = true;
+ allow_rename = ScriptServer::is_global_class(word_at_pos);
} else {
Ref<Script> script = edited_res;
Node *base = get_tree()->get_edited_scene_root();
@@ -2492,6 +2535,7 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &p_ev) {
EditorLanguage::LookupResult result;
if (script->get_language()->get_editor_language()->lookup_code(tx->get_text_for_symbol_lookup(), word_at_pos, script->get_path(), base, result) == OK) {
open_docs = true;
+ allow_rename = !result.script_path.is_empty();
}
}
@@ -2519,11 +2563,11 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &p_ev) {
}
}
- _make_ste_context_menu(tx->has_selection(), has_color, foldable, open_docs, local_pos);
+ _make_ste_context_menu(tx->has_selection(), has_color, foldable, open_docs, allow_rename, local_pos);
}
}
-void ScriptTextEditor::_make_ste_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, const Vector2 &p_position) {
+void ScriptTextEditor::_make_ste_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_allow_rename, const Vector2 &p_position) {
CodeEditorBase::_make_context_menu(p_selection, p_foldable, p_position, false);
if (p_selection) {
@@ -2538,6 +2582,10 @@ void ScriptTextEditor::_make_ste_context_menu(bool p_selection, bool p_color, bo
if (p_open_docs) {
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_symbol"), LOOKUP_SYMBOL);
}
+ if (p_allow_rename) {
+ context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/rename_symbol"), RENAME_SYMBOL);
+ context_menu->set_item_text(-1, TTR("Rename Symbol (Experimental)"));
+ }
if (p_color) {
context_menu->add_item(TTRC("Pick Color"), EDIT_PICK_COLOR);
}
@@ -2620,6 +2668,7 @@ void ScriptTextEditor::register_editor() {
ED_SHORTCUT("script_text_editor/goto_line", TTRC("Go to Line..."), KeyModifierMask::CMD_OR_CTRL | Key::G);
ED_SHORTCUT_OVERRIDE("script_text_editor/goto_line", "macos", KeyModifierMask::CMD_OR_CTRL | Key::L);
ED_SHORTCUT("script_text_editor/goto_symbol", TTRC("Lookup Symbol"));
+ ED_SHORTCUT("script_text_editor/rename_symbol", TTRC("Rename Symbol"), Key::F2);
ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTRC("Toggle Breakpoint"), Key::F9);
ED_SHORTCUT_OVERRIDE("script_text_editor/toggle_breakpoint", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::B);
diff --git a/editor/script/script_text_editor.h b/editor/script/script_text_editor.h
index 0147025c76b..d2983522488 100644
--- a/editor/script/script_text_editor.h
+++ b/editor/script/script_text_editor.h
@@ -110,6 +110,7 @@ class ScriptTextEditor : public CodeEditorBase {
SHOW_TOOLTIP_AT_CARET,
HELP_CONTEXTUAL,
LOOKUP_SYMBOL,
+ RENAME_SYMBOL,
};
class EditMenusScTE : public EditMenusCEB {
@@ -196,7 +197,7 @@ protected:
void _goto_line(int p_line);
- void _make_ste_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, const Vector2 &p_pos);
+ void _make_ste_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_allow_rename, const Vector2 &p_pos);
Dictionary _get_context_data() const;
virtual void _text_edit_gui_input(const Ref<InputEvent> &p_ev) override;
diff --git a/modules/gdscript/editor/gdscript_editor_language.h b/modules/gdscript/editor/gdscript_editor_language.h
index f835eb90d70..b495b1482d5 100644
--- a/modules/gdscript/editor/gdscript_editor_language.h
+++ b/modules/gdscript/editor/gdscript_editor_language.h
@@ -41,6 +41,7 @@ public:
virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List<EditorLanguage::CompletionOption> *r_options, bool &r_force, String &r_call_hint) override;
virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) override;
+ virtual Error lookup_code_for_rename(const String &p_code, const String &p_symbol, const String &p_path, LookupResult &r_result) override;
virtual int32_t find_function(const String &p_function, const String &p_code) const override;
diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp
index b714f93e91d..18c6313e8fd 100644
--- a/modules/gdscript/gdscript_editor.cpp
+++ b/modules/gdscript/gdscript_editor.cpp
@@ -4728,4 +4728,8 @@ static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, co
return ERR_CANT_RESOLVE;
}
+Error GDScriptEditorLanguage::lookup_code_for_rename(const String &p_code, const String &p_symbol, const String &p_path, LookupResult &r_result) {
+ return lookup_code(p_code, p_symbol, p_path, nullptr, r_result);
+}
+
#endif // TOOLS_ENABLED
diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h
index f16e3927099..cb1f0351e02 100644
--- a/scene/gui/text_edit.h
+++ b/scene/gui/text_edit.h
@@ -872,6 +872,7 @@ public:
/* Text */
// Text properties.
RID get_text_canvas_item() const;
+ const Ref<TextParagraph> get_line_data(int p_line) const { return text.get_line_data(p_line); }
bool has_ime_text() const;
void cancel_ime();