Skip to content

Commit 98c5267

Browse files
committed
Merge pull request #91388 from kitbdev/fix-goto-line
Make Goto line a Popup and column input
2 parents 01a7c81 + f41cd8a commit 98c5267

8 files changed

+109
-52
lines changed

editor/code_editor.cpp

+74-28
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,72 @@
3939
#include "editor/plugins/script_editor_plugin.h"
4040
#include "editor/themes/editor_scale.h"
4141
#include "editor/themes/editor_theme_manager.h"
42+
#include "scene/gui/line_edit.h"
4243
#include "scene/gui/menu_button.h"
4344
#include "scene/gui/separator.h"
4445
#include "scene/resources/font.h"
4546

46-
void GotoLineDialog::popup_find_line(CodeEdit *p_edit) {
47-
text_editor = p_edit;
47+
void GotoLinePopup::popup_find_line(CodeTextEditor *p_text_editor) {
48+
text_editor = p_text_editor;
4849

49-
// Add 1 because text_editor->get_caret_line() starts from 0, but the editor user interface starts from 1.
50-
line->set_text(itos(text_editor->get_caret_line() + 1));
51-
line->select_all();
52-
popup_centered(Size2(180, 80) * EDSCALE);
53-
line->grab_focus();
54-
}
50+
original_state = text_editor->get_navigation_state();
51+
52+
// Add 1 because the TextEdit starts from 0, but the editor user interface starts from 1.
53+
TextEdit *text_edit = text_editor->get_text_editor();
54+
int original_line = text_edit->get_caret_line() + 1;
55+
line_input->set_text(itos(original_line));
56+
text_editor->set_preview_navigation_change(true);
5557

56-
int GotoLineDialog::get_line() const {
57-
return line->get_text().to_int();
58+
Rect2i parent_rect = text_editor->get_global_rect();
59+
Point2i centered_pos(parent_rect.get_center().x - get_contents_minimum_size().x / 2.0, parent_rect.position.y);
60+
popup_on_parent(Rect2i(centered_pos, Size2()));
61+
reset_size();
62+
line_input->grab_focus();
5863
}
5964

60-
void GotoLineDialog::ok_pressed() {
61-
// Subtract 1 because the editor user interface starts from 1, but text_editor->set_caret_line(n) starts from 0.
62-
const int line_number = get_line() - 1;
63-
if (line_number < 0 || line_number >= text_editor->get_line_count()) {
65+
void GotoLinePopup::_goto_line() {
66+
if (line_input->get_text().is_empty()) {
6467
return;
6568
}
66-
text_editor->remove_secondary_carets();
67-
text_editor->unfold_line(line_number);
68-
text_editor->set_caret_line(line_number);
69-
text_editor->set_code_hint("");
70-
text_editor->cancel_code_completion();
69+
70+
PackedStringArray line_col_strings = line_input->get_text().split(":");
71+
// Subtract 1 because the editor user interface starts from 1, but the TextEdit starts from 0.
72+
const int line_number = line_col_strings[0].to_int() - 1;
73+
if (line_number < 0 || line_number >= text_editor->get_text_editor()->get_line_count()) {
74+
return;
75+
}
76+
77+
int column_number = 0;
78+
if (line_col_strings.size() >= 2) {
79+
column_number = line_col_strings[1].to_int() - 1;
80+
}
81+
text_editor->goto_line_centered(line_number, column_number);
82+
}
83+
84+
void GotoLinePopup::_submit() {
85+
_goto_line();
7186
hide();
7287
}
7388

74-
GotoLineDialog::GotoLineDialog() {
89+
void GotoLinePopup::_notification(int p_what) {
90+
switch (p_what) {
91+
case NOTIFICATION_VISIBILITY_CHANGED: {
92+
if (!is_visible()) {
93+
text_editor->set_preview_navigation_change(false);
94+
}
95+
} break;
96+
}
97+
}
98+
99+
void GotoLinePopup::_input_from_window(const Ref<InputEvent> &p_event) {
100+
if (p_event->is_action_pressed(SNAME("ui_cancel"), false, true)) {
101+
// Cancelled, go back to original state.
102+
text_editor->set_edit_state(original_state);
103+
}
104+
PopupPanel::_input_from_window(p_event);
105+
}
106+
107+
GotoLinePopup::GotoLinePopup() {
75108
set_title(TTR("Go to Line"));
76109

77110
VBoxContainer *vbc = memnew(VBoxContainer);
@@ -85,14 +118,12 @@ GotoLineDialog::GotoLineDialog() {
85118
l->set_text(TTR("Line Number:"));
86119
vbc->add_child(l);
87120

88-
line = memnew(LineEdit);
89-
vbc->add_child(line);
90-
register_text_enter(line);
91-
text_editor = nullptr;
92-
93-
line_label = nullptr;
94-
95-
set_hide_on_ok(false);
121+
line_input = memnew(LineEdit);
122+
line_input->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
123+
line_input->set_select_all_on_focus(true);
124+
line_input->connect(SceneStringName(text_changed), callable_mp(this, &GotoLinePopup::_goto_line).unbind(1));
125+
line_input->connect(SceneStringName(text_submitted), callable_mp(this, &GotoLinePopup::_submit).unbind(1));
126+
vbc->add_child(line_input);
96127
}
97128

98129
void FindReplaceBar::_notification(int p_what) {
@@ -1393,6 +1424,20 @@ void CodeTextEditor::store_previous_state() {
13931424
previous_state = get_navigation_state();
13941425
}
13951426

1427+
bool CodeTextEditor::is_previewing_navigation_change() const {
1428+
return preview_navigation_change;
1429+
}
1430+
1431+
void CodeTextEditor::set_preview_navigation_change(bool p_preview) {
1432+
if (preview_navigation_change == p_preview) {
1433+
return;
1434+
}
1435+
preview_navigation_change = p_preview;
1436+
if (!preview_navigation_change) {
1437+
emit_signal("navigation_preview_ended");
1438+
}
1439+
}
1440+
13961441
void CodeTextEditor::set_edit_state(const Variant &p_state) {
13971442
Dictionary state = p_state;
13981443

@@ -1761,6 +1806,7 @@ void CodeTextEditor::_bind_methods() {
17611806
ADD_SIGNAL(MethodInfo("load_theme_settings"));
17621807
ADD_SIGNAL(MethodInfo("show_errors_panel"));
17631808
ADD_SIGNAL(MethodInfo("show_warnings_panel"));
1809+
ADD_SIGNAL(MethodInfo("navigation_preview_ended"));
17641810
ADD_SIGNAL(MethodInfo("zoomed", PropertyInfo(Variant::FLOAT, "p_zoom_factor")));
17651811
}
17661812

editor/code_editor.h

+19-12
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,33 @@
3737
#include "scene/gui/code_edit.h"
3838
#include "scene/gui/dialogs.h"
3939
#include "scene/gui/label.h"
40-
#include "scene/gui/line_edit.h"
40+
#include "scene/gui/popup.h"
4141
#include "scene/main/timer.h"
4242

4343
class MenuButton;
44+
class CodeTextEditor;
45+
class LineEdit;
4446

45-
class GotoLineDialog : public ConfirmationDialog {
46-
GDCLASS(GotoLineDialog, ConfirmationDialog);
47+
class GotoLinePopup : public PopupPanel {
48+
GDCLASS(GotoLinePopup, PopupPanel);
4749

48-
Label *line_label = nullptr;
49-
LineEdit *line = nullptr;
50+
Variant original_state;
51+
LineEdit *line_input = nullptr;
52+
CodeTextEditor *text_editor = nullptr;
5053

51-
CodeEdit *text_editor = nullptr;
54+
void _goto_line();
55+
void _submit();
5256

53-
virtual void ok_pressed() override;
57+
protected:
58+
void _notification(int p_what);
59+
virtual void _input_from_window(const Ref<InputEvent> &p_event) override;
5460

5561
public:
56-
void popup_find_line(CodeEdit *p_edit);
57-
int get_line() const;
62+
void popup_find_line(CodeTextEditor *p_text_editor);
5863

59-
GotoLineDialog();
64+
GotoLinePopup();
6065
};
6166

62-
class CodeTextEditor;
63-
6467
class FindReplaceBar : public HBoxContainer {
6568
GDCLASS(FindReplaceBar, HBoxContainer);
6669

@@ -186,6 +189,7 @@ class CodeTextEditor : public VBoxContainer {
186189
int error_line;
187190
int error_column;
188191

192+
bool preview_navigation_change = false;
189193
Dictionary previous_state;
190194

191195
void _update_text_editor_theme();
@@ -264,6 +268,9 @@ class CodeTextEditor : public VBoxContainer {
264268
Variant get_previous_state();
265269
void store_previous_state();
266270

271+
bool is_previewing_navigation_change() const;
272+
void set_preview_navigation_change(bool p_preview);
273+
267274
void set_error_count(int p_error_count);
268275
void set_warning_count(int p_warning_count);
269276

editor/plugins/script_text_editor.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,9 @@ void ScriptTextEditor::_breakpoint_toggled(int p_row) {
929929
}
930930

931931
void ScriptTextEditor::_on_caret_moved() {
932+
if (code_editor->is_previewing_navigation_change()) {
933+
return;
934+
}
932935
int current_line = code_editor->get_text_editor()->get_caret_line();
933936
if (ABS(current_line - previous_line) >= 10) {
934937
Dictionary nav_state = get_navigation_state();
@@ -1617,7 +1620,7 @@ void ScriptTextEditor::_edit_option(int p_op) {
16171620
quick_open->set_title(TTR("Go to Function"));
16181621
} break;
16191622
case SEARCH_GOTO_LINE: {
1620-
goto_line_dialog->popup_find_line(tx);
1623+
goto_line_popup->popup_find_line(code_editor);
16211624
} break;
16221625
case BOOKMARK_TOGGLE: {
16231626
code_editor->toggle_bookmark();
@@ -2372,8 +2375,8 @@ void ScriptTextEditor::_enable_code_editor() {
23722375
quick_open->connect("goto_line", callable_mp(this, &ScriptTextEditor::_goto_line));
23732376
add_child(quick_open);
23742377

2375-
goto_line_dialog = memnew(GotoLineDialog);
2376-
add_child(goto_line_dialog);
2378+
goto_line_popup = memnew(GotoLinePopup);
2379+
add_child(goto_line_popup);
23772380

23782381
add_child(connection_info_dialog);
23792382

@@ -2481,6 +2484,7 @@ ScriptTextEditor::ScriptTextEditor() {
24812484
code_editor->get_text_editor()->set_draw_executing_lines_gutter(true);
24822485
code_editor->get_text_editor()->connect("breakpoint_toggled", callable_mp(this, &ScriptTextEditor::_breakpoint_toggled));
24832486
code_editor->get_text_editor()->connect("caret_changed", callable_mp(this, &ScriptTextEditor::_on_caret_moved));
2487+
code_editor->connect("navigation_preview_ended", callable_mp(this, &ScriptTextEditor::_on_caret_moved));
24842488

24852489
connection_gutter = 1;
24862490
code_editor->get_text_editor()->add_gutter(connection_gutter);

editor/plugins/script_text_editor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class ScriptTextEditor : public ScriptEditorBase {
8585
PopupMenu *highlighter_menu = nullptr;
8686
PopupMenu *context_menu = nullptr;
8787

88-
GotoLineDialog *goto_line_dialog = nullptr;
88+
GotoLinePopup *goto_line_popup = nullptr;
8989
ScriptEditorQuickOpen *quick_open = nullptr;
9090
ConnectionInfoDialog *connection_info_dialog = nullptr;
9191

editor/plugins/text_editor.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ void TextEditor::_edit_option(int p_op) {
467467
emit_signal(SNAME("replace_in_files_requested"), selected_text);
468468
} break;
469469
case SEARCH_GOTO_LINE: {
470-
goto_line_dialog->popup_find_line(tx);
470+
goto_line_popup->popup_find_line(code_editor);
471471
} break;
472472
case BOOKMARK_TOGGLE: {
473473
code_editor->toggle_bookmark();
@@ -705,8 +705,8 @@ TextEditor::TextEditor() {
705705
bookmarks_menu->connect("about_to_popup", callable_mp(this, &TextEditor::_update_bookmark_list));
706706
bookmarks_menu->connect("index_pressed", callable_mp(this, &TextEditor::_bookmark_item_pressed));
707707

708-
goto_line_dialog = memnew(GotoLineDialog);
709-
add_child(goto_line_dialog);
708+
goto_line_popup = memnew(GotoLinePopup);
709+
add_child(goto_line_popup);
710710
}
711711

712712
TextEditor::~TextEditor() {

editor/plugins/text_editor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class TextEditor : public ScriptEditorBase {
5353
PopupMenu *bookmarks_menu = nullptr;
5454
PopupMenu *context_menu = nullptr;
5555

56-
GotoLineDialog *goto_line_dialog = nullptr;
56+
GotoLinePopup *goto_line_popup = nullptr;
5757

5858
enum {
5959
EDIT_UNDO,

editor/plugins/text_shader_editor.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ void TextShaderEditor::_menu_option(int p_option) {
712712
code_editor->get_find_replace_bar()->popup_replace();
713713
} break;
714714
case SEARCH_GOTO_LINE: {
715-
goto_line_dialog->popup_find_line(code_editor->get_text_editor());
715+
goto_line_popup->popup_find_line(code_editor);
716716
} break;
717717
case BOOKMARK_TOGGLE: {
718718
code_editor->toggle_bookmark();
@@ -1235,8 +1235,8 @@ TextShaderEditor::TextShaderEditor() {
12351235
editor_box->add_child(warnings_panel);
12361236
code_editor->set_warnings_panel(warnings_panel);
12371237

1238-
goto_line_dialog = memnew(GotoLineDialog);
1239-
add_child(goto_line_dialog);
1238+
goto_line_popup = memnew(GotoLinePopup);
1239+
add_child(goto_line_popup);
12401240

12411241
disk_changed = memnew(ConfirmationDialog);
12421242

editor/plugins/text_shader_editor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class TextShaderEditor : public ShaderEditor {
144144
RichTextLabel *warnings_panel = nullptr;
145145
uint64_t idle = 0;
146146

147-
GotoLineDialog *goto_line_dialog = nullptr;
147+
GotoLinePopup *goto_line_popup = nullptr;
148148
ConfirmationDialog *erase_tab_confirm = nullptr;
149149
ConfirmationDialog *disk_changed = nullptr;
150150

0 commit comments

Comments
 (0)