Skip to content

Commit

Permalink
Make Goto line a Popup and allow expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbdev committed Aug 27, 2024
1 parent db24ed4 commit 9862e7f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 48 deletions.
51 changes: 28 additions & 23 deletions editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,37 @@
#include "scene/gui/separator.h"
#include "scene/resources/font.h"

void GotoLineDialog::popup_find_line(CodeEdit *p_edit) {
text_editor = p_edit;
void GotoLinePopup::popup_find_line(CodeTextEditor *p_text_editor) {
text_editor = p_text_editor;

// Add 1 because text_editor->get_caret_line() starts from 0, but the editor user interface starts from 1.
line->set_text(itos(text_editor->get_caret_line() + 1));
line->select_all();
popup_centered(Size2(180, 80) * EDSCALE);
line->grab_focus();
}
TextEdit *text_edit = text_editor->get_text_editor();
int original_line = text_edit->get_caret_line() + 1;
line_input->set_max(text_edit->get_line_count());
line_input->set_value_no_signal(original_line);

int GotoLineDialog::get_line() const {
return line->get_text().to_int();
Rect2i parent_rect = text_editor->get_global_rect();
Point2i centered_pos(parent_rect.get_center().x - get_contents_minimum_size().x / 2.0, parent_rect.position.y);
popup_on_parent(Rect2i(centered_pos, Size2()));
reset_size();
line_input->setup_and_show();
}

void GotoLineDialog::ok_pressed() {
void GotoLinePopup::_goto_line() {
// Subtract 1 because the editor user interface starts from 1, but text_editor->set_caret_line(n) starts from 0.
const int line_number = get_line() - 1;
if (line_number < 0 || line_number >= text_editor->get_line_count()) {
const int line_number = line_input->get_value() - 1;
if (line_number < 0 || line_number >= text_editor->get_text_editor()->get_line_count()) {
return;
}
text_editor->remove_secondary_carets();
text_editor->unfold_line(line_number);
text_editor->set_caret_line(line_number);
text_editor->goto_line_centered(line_number);
}

void GotoLinePopup::_submit() {
_goto_line();
hide();
}

GotoLineDialog::GotoLineDialog() {
GotoLinePopup::GotoLinePopup() {
set_title(TTR("Go to Line"));

VBoxContainer *vbc = memnew(VBoxContainer);
Expand All @@ -83,14 +87,15 @@ GotoLineDialog::GotoLineDialog() {
l->set_text(TTR("Line Number:"));
vbc->add_child(l);

line = memnew(LineEdit);
vbc->add_child(line);
register_text_enter(line);
text_editor = nullptr;

line_label = nullptr;
line_input = memnew(EditorSpinSlider);
line_input->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
line_input->set_step(1);
line_input->set_min(1);
line_input->connect("value_changed", callable_mp(this, &GotoLinePopup::_goto_line).unbind(1));
line_input->get_line_edit()->connect("text_submitted", callable_mp(this, &GotoLinePopup::_submit).unbind(1));
vbc->add_child(line_input);

set_hide_on_ok(false);
text_editor = nullptr;
}

void FindReplaceBar::_notification(int p_what) {
Expand Down
24 changes: 11 additions & 13 deletions editor/code_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,34 @@
#ifndef CODE_EDITOR_H
#define CODE_EDITOR_H

#include "editor/gui/editor_spin_slider.h"
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
#include "scene/gui/check_box.h"
#include "scene/gui/code_edit.h"
#include "scene/gui/dialogs.h"
#include "scene/gui/label.h"
#include "scene/gui/line_edit.h"
#include "scene/gui/popup.h"
#include "scene/main/timer.h"

class MenuButton;
class CodeTextEditor;

class GotoLineDialog : public ConfirmationDialog {
GDCLASS(GotoLineDialog, ConfirmationDialog);

Label *line_label = nullptr;
LineEdit *line = nullptr;
class GotoLinePopup : public PopupPanel {
GDCLASS(GotoLinePopup, PopupPanel);

CodeEdit *text_editor = nullptr;
EditorSpinSlider *line_input = nullptr;
CodeTextEditor *text_editor = nullptr;

virtual void ok_pressed() override;
void _goto_line();
void _submit();

public:
void popup_find_line(CodeEdit *p_edit);
int get_line() const;
void popup_find_line(CodeTextEditor *p_text_editor);

GotoLineDialog();
GotoLinePopup();
};

class CodeTextEditor;

class FindReplaceBar : public HBoxContainer {
GDCLASS(FindReplaceBar, HBoxContainer);

Expand Down
6 changes: 3 additions & 3 deletions editor/plugins/script_text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@ void ScriptTextEditor::_edit_option(int p_op) {
quick_open->set_title(TTR("Go to Function"));
} break;
case SEARCH_GOTO_LINE: {
goto_line_dialog->popup_find_line(tx);
goto_line_popup->popup_find_line(code_editor);
} break;
case BOOKMARK_TOGGLE: {
code_editor->toggle_bookmark();
Expand Down Expand Up @@ -2274,8 +2274,8 @@ void ScriptTextEditor::_enable_code_editor() {
quick_open->connect("goto_line", callable_mp(this, &ScriptTextEditor::_goto_line));
add_child(quick_open);

goto_line_dialog = memnew(GotoLineDialog);
add_child(goto_line_dialog);
goto_line_popup = memnew(GotoLinePopup);
add_child(goto_line_popup);

add_child(connection_info_dialog);

Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/script_text_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ScriptTextEditor : public ScriptEditorBase {
PopupMenu *highlighter_menu = nullptr;
PopupMenu *context_menu = nullptr;

GotoLineDialog *goto_line_dialog = nullptr;
GotoLinePopup *goto_line_popup = nullptr;
ScriptEditorQuickOpen *quick_open = nullptr;
ConnectionInfoDialog *connection_info_dialog = nullptr;

Expand Down
6 changes: 3 additions & 3 deletions editor/plugins/text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ void TextEditor::_edit_option(int p_op) {
emit_signal(SNAME("replace_in_files_requested"), selected_text);
} break;
case SEARCH_GOTO_LINE: {
goto_line_dialog->popup_find_line(tx);
goto_line_popup->popup_find_line(code_editor);
} break;
case BOOKMARK_TOGGLE: {
code_editor->toggle_bookmark();
Expand Down Expand Up @@ -704,8 +704,8 @@ TextEditor::TextEditor() {
bookmarks_menu->connect("about_to_popup", callable_mp(this, &TextEditor::_update_bookmark_list));
bookmarks_menu->connect("index_pressed", callable_mp(this, &TextEditor::_bookmark_item_pressed));

goto_line_dialog = memnew(GotoLineDialog);
add_child(goto_line_dialog);
goto_line_popup = memnew(GotoLinePopup);
add_child(goto_line_popup);
}

TextEditor::~TextEditor() {
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/text_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class TextEditor : public ScriptEditorBase {
PopupMenu *bookmarks_menu = nullptr;
PopupMenu *context_menu = nullptr;

GotoLineDialog *goto_line_dialog = nullptr;
GotoLinePopup *goto_line_popup = nullptr;

enum {
EDIT_UNDO,
Expand Down
6 changes: 3 additions & 3 deletions editor/plugins/text_shader_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ void TextShaderEditor::_menu_option(int p_option) {
code_editor->get_find_replace_bar()->popup_replace();
} break;
case SEARCH_GOTO_LINE: {
goto_line_dialog->popup_find_line(code_editor->get_text_editor());
goto_line_popup->popup_find_line(code_editor);
} break;
case BOOKMARK_TOGGLE: {
code_editor->toggle_bookmark();
Expand Down Expand Up @@ -1235,8 +1235,8 @@ TextShaderEditor::TextShaderEditor() {
editor_box->add_child(warnings_panel);
code_editor->set_warnings_panel(warnings_panel);

goto_line_dialog = memnew(GotoLineDialog);
add_child(goto_line_dialog);
goto_line_popup = memnew(GotoLinePopup);
add_child(goto_line_popup);

disk_changed = memnew(ConfirmationDialog);

Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/text_shader_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class TextShaderEditor : public ShaderEditor {
RichTextLabel *warnings_panel = nullptr;
uint64_t idle = 0;

GotoLineDialog *goto_line_dialog = nullptr;
GotoLinePopup *goto_line_popup = nullptr;
ConfirmationDialog *erase_tab_confirm = nullptr;
ConfirmationDialog *disk_changed = nullptr;

Expand Down

0 comments on commit 9862e7f

Please sign in to comment.