2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* code_editor.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. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "code_editor.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2020-04-28 15:19:37 +02:00
|
|
|
#include "core/input/input.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/keyboard.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/string/string_builder.h"
|
2022-12-05 08:43:27 +01:00
|
|
|
#include "core/templates/pair.h"
|
2017-03-05 14:21:25 +01:00
|
|
|
#include "editor/editor_scale.h"
|
2022-02-12 02:46:22 +01:00
|
|
|
#include "editor/editor_settings.h"
|
2023-08-13 02:33:39 +02:00
|
|
|
#include "editor/editor_string_names.h"
|
2022-02-14 14:00:03 +01:00
|
|
|
#include "editor/plugins/script_editor_plugin.h"
|
2020-09-03 13:22:16 +02:00
|
|
|
#include "scene/resources/font.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-07-24 16:50:35 +02:00
|
|
|
void GotoLineDialog::popup_find_line(CodeEdit *p_edit) {
|
2014-02-10 02:10:30 +01:00
|
|
|
text_editor = p_edit;
|
|
|
|
|
2023-03-31 08:39:44 +02:00
|
|
|
// 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));
|
2014-02-10 02:10:30 +01:00
|
|
|
line->select_all();
|
2018-09-18 16:10:36 +02:00
|
|
|
popup_centered(Size2(180, 80) * EDSCALE);
|
2014-02-10 02:10:30 +01:00
|
|
|
line->grab_focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
int GotoLineDialog::get_line() const {
|
|
|
|
return line->get_text().to_int();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GotoLineDialog::ok_pressed() {
|
2023-03-31 08:39:44 +02:00
|
|
|
// 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()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->remove_secondary_carets();
|
2023-03-31 08:39:44 +02:00
|
|
|
text_editor->unfold_line(line_number);
|
|
|
|
text_editor->set_caret_line(line_number);
|
2014-02-10 02:10:30 +01:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
GotoLineDialog::GotoLineDialog() {
|
2016-05-04 03:25:37 +02:00
|
|
|
set_title(TTR("Go to Line"));
|
2018-09-18 16:10:36 +02:00
|
|
|
|
|
|
|
VBoxContainer *vbc = memnew(VBoxContainer);
|
2020-12-22 17:24:29 +01:00
|
|
|
vbc->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 8 * EDSCALE);
|
|
|
|
vbc->set_anchor_and_offset(SIDE_TOP, Control::ANCHOR_BEGIN, 8 * EDSCALE);
|
|
|
|
vbc->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -8 * EDSCALE);
|
|
|
|
vbc->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, -8 * EDSCALE);
|
2018-09-18 16:10:36 +02:00
|
|
|
add_child(vbc);
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Label *l = memnew(Label);
|
2016-05-04 03:25:37 +02:00
|
|
|
l->set_text(TTR("Line Number:"));
|
2018-09-18 16:10:36 +02:00
|
|
|
vbc->add_child(l);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
line = memnew(LineEdit);
|
2018-09-18 16:10:36 +02:00
|
|
|
vbc->add_child(line);
|
2014-02-10 02:10:30 +01:00
|
|
|
register_text_enter(line);
|
2020-04-02 01:20:12 +02:00
|
|
|
text_editor = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-11-24 10:12:55 +01:00
|
|
|
line_label = nullptr;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
set_hide_on_ok(false);
|
|
|
|
}
|
|
|
|
|
2016-05-28 18:25:45 +02:00
|
|
|
void FindReplaceBar::_notification(int p_what) {
|
2022-02-16 00:52:32 +01:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_READY:
|
|
|
|
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
|
2023-08-13 02:33:39 +02:00
|
|
|
find_prev->set_icon(get_editor_theme_icon(SNAME("MoveUp")));
|
|
|
|
find_next->set_icon(get_editor_theme_icon(SNAME("MoveDown")));
|
|
|
|
hide_button->set_texture_normal(get_editor_theme_icon(SNAME("Close")));
|
|
|
|
hide_button->set_texture_hover(get_editor_theme_icon(SNAME("Close")));
|
|
|
|
hide_button->set_texture_pressed(get_editor_theme_icon(SNAME("Close")));
|
2022-11-07 09:09:13 +01:00
|
|
|
hide_button->set_custom_minimum_size(hide_button->get_texture_normal()->get_size());
|
2022-02-16 00:52:32 +01:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
set_process_unhandled_input(is_visible_in_tree());
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2023-08-13 02:33:39 +02:00
|
|
|
matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
|
2022-02-16 00:52:32 +01:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_PREDELETE: {
|
|
|
|
if (base_text_editor) {
|
|
|
|
base_text_editor->remove_find_replace_bar();
|
|
|
|
base_text_editor = nullptr;
|
|
|
|
}
|
|
|
|
} break;
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 17:37:22 +02:00
|
|
|
void FindReplaceBar::unhandled_input(const Ref<InputEvent> &p_event) {
|
2021-04-05 08:52:21 +02:00
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
Ref<InputEventKey> k = p_event;
|
2020-07-06 16:13:04 +02:00
|
|
|
|
2023-05-21 02:26:13 +02:00
|
|
|
if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true)) {
|
|
|
|
Control *focus_owner = get_viewport()->gui_get_focus_owner();
|
2020-07-06 16:13:04 +02:00
|
|
|
|
2023-07-05 23:05:01 +02:00
|
|
|
if (text_editor->has_focus() || (focus_owner && is_ancestor_of(focus_owner))) {
|
2023-05-21 02:26:13 +02:00
|
|
|
_hide_bar();
|
2020-07-06 16:13:04 +02:00
|
|
|
accept_event();
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-14 16:16:13 +02:00
|
|
|
void FindReplaceBar::_focus_lost() {
|
|
|
|
if (Input::get_singleton()->is_action_pressed(SNAME("ui_cancel"))) {
|
|
|
|
// Unfocused after pressing Escape, so hide the bar.
|
|
|
|
_hide_bar(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 18:15:41 +02:00
|
|
|
bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
|
2023-01-28 17:08:10 +01:00
|
|
|
if (!preserve_cursor) {
|
|
|
|
text_editor->remove_secondary_carets();
|
|
|
|
}
|
2016-05-28 18:25:45 +02:00
|
|
|
String text = get_search_text();
|
2021-07-10 12:41:38 +02:00
|
|
|
Point2i pos = text_editor->search(text, p_flags, p_from_line, p_from_col);
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2021-07-10 12:41:38 +02:00
|
|
|
if (pos.x != -1) {
|
2021-04-04 17:26:54 +02:00
|
|
|
if (!preserve_cursor && !is_selection_only()) {
|
2021-07-10 12:41:38 +02:00
|
|
|
text_editor->unfold_line(pos.y);
|
|
|
|
text_editor->set_caret_line(pos.y, false);
|
|
|
|
text_editor->set_caret_column(pos.x + text.length(), false);
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->center_viewport_to_caret(0);
|
2021-07-10 12:41:38 +02:00
|
|
|
text_editor->select(pos.y, pos.x, pos.y, pos.x + text.length());
|
2022-04-27 14:21:28 +02:00
|
|
|
|
|
|
|
line_col_changed_for_result = true;
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->set_search_text(text);
|
|
|
|
text_editor->set_search_flags(p_flags);
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2021-07-10 12:41:38 +02:00
|
|
|
result_line = pos.y;
|
|
|
|
result_col = pos.x;
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2019-07-08 19:33:04 +02:00
|
|
|
_update_results_count();
|
2016-05-28 18:25:45 +02:00
|
|
|
} else {
|
2019-08-14 23:31:07 +02:00
|
|
|
results_count = 0;
|
2016-05-30 18:15:41 +02:00
|
|
|
result_line = -1;
|
|
|
|
result_col = -1;
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->set_search_text("");
|
|
|
|
text_editor->set_search_flags(p_flags);
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 08:47:09 +02:00
|
|
|
_update_matches_label();
|
|
|
|
|
2021-07-10 12:41:38 +02:00
|
|
|
return pos.x != -1;
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_replace() {
|
2023-01-14 16:58:15 +01:00
|
|
|
text_editor->remove_secondary_carets();
|
2022-06-08 23:41:38 +02:00
|
|
|
bool selection_enabled = text_editor->has_selection(0);
|
2020-06-16 14:04:36 +02:00
|
|
|
Point2i selection_begin, selection_end;
|
|
|
|
if (selection_enabled) {
|
2022-06-08 23:41:38 +02:00
|
|
|
selection_begin = Point2i(text_editor->get_selection_from_line(0), text_editor->get_selection_from_column(0));
|
|
|
|
selection_end = Point2i(text_editor->get_selection_to_line(0), text_editor->get_selection_to_column(0));
|
2020-06-16 14:04:36 +02:00
|
|
|
}
|
2016-05-30 18:15:41 +02:00
|
|
|
|
2022-09-29 11:53:28 +02:00
|
|
|
String repl_text = get_replace_text();
|
2020-06-16 14:04:36 +02:00
|
|
|
int search_text_len = get_search_text().length();
|
2016-05-30 18:15:41 +02:00
|
|
|
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->begin_complex_operation();
|
2023-03-08 23:00:27 +01:00
|
|
|
if (selection_enabled && is_selection_only()) {
|
|
|
|
// Restrict search_current() to selected region.
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->set_caret_line(selection_begin.width, false, true, 0, 0);
|
|
|
|
text_editor->set_caret_column(selection_begin.height, true, 0);
|
2020-06-16 14:04:36 +02:00
|
|
|
}
|
2019-07-08 19:33:04 +02:00
|
|
|
|
2020-06-16 14:04:36 +02:00
|
|
|
if (search_current()) {
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->unfold_line(result_line);
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->select(result_line, result_col, result_line, result_col + search_text_len, 0);
|
2020-06-16 14:04:36 +02:00
|
|
|
|
|
|
|
if (selection_enabled && is_selection_only()) {
|
|
|
|
Point2i match_from(result_line, result_col);
|
|
|
|
Point2i match_to(result_line, result_col + search_text_len);
|
|
|
|
if (!(match_from < selection_begin || match_to > selection_end)) {
|
2022-09-29 11:53:28 +02:00
|
|
|
text_editor->insert_text_at_caret(repl_text, 0);
|
2023-03-08 23:00:27 +01:00
|
|
|
if (match_to.x == selection_end.x) {
|
|
|
|
// Adjust selection bounds if necessary.
|
2022-09-29 11:53:28 +02:00
|
|
|
selection_end.y += repl_text.length() - search_text_len;
|
2020-06-16 14:04:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-29 11:53:28 +02:00
|
|
|
text_editor->insert_text_at_caret(repl_text, 0);
|
2020-06-16 14:04:36 +02:00
|
|
|
}
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->end_complex_operation();
|
2020-06-16 14:04:36 +02:00
|
|
|
results_count = -1;
|
2022-04-27 14:21:28 +02:00
|
|
|
results_count_to_current = -1;
|
|
|
|
needs_to_count_results = true;
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2020-06-16 14:04:36 +02:00
|
|
|
if (selection_enabled && is_selection_only()) {
|
2023-03-08 23:00:27 +01:00
|
|
|
// Reselect in order to keep 'Replace' restricted to selection.
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y, 0);
|
2020-06-16 14:04:36 +02:00
|
|
|
} else {
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->deselect(0);
|
2019-10-14 13:59:26 +02:00
|
|
|
}
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_replace_all() {
|
2023-01-14 16:58:15 +01:00
|
|
|
text_editor->remove_secondary_carets();
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->disconnect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
|
2019-08-14 23:31:07 +02:00
|
|
|
// Line as x so it gets priority in comparison, column as y.
|
2022-06-08 23:41:38 +02:00
|
|
|
Point2i orig_cursor(text_editor->get_caret_line(0), text_editor->get_caret_column(0));
|
2016-05-28 18:25:45 +02:00
|
|
|
Point2i prev_match = Point2(-1, -1);
|
|
|
|
|
2022-06-08 23:41:38 +02:00
|
|
|
bool selection_enabled = text_editor->has_selection(0);
|
2022-06-06 11:47:57 +02:00
|
|
|
if (!is_selection_only()) {
|
|
|
|
text_editor->deselect();
|
|
|
|
selection_enabled = false;
|
|
|
|
} else {
|
|
|
|
result_line = -1;
|
|
|
|
result_col = -1;
|
|
|
|
}
|
|
|
|
|
2016-05-28 18:25:45 +02:00
|
|
|
Point2i selection_begin, selection_end;
|
|
|
|
if (selection_enabled) {
|
2022-06-08 23:41:38 +02:00
|
|
|
selection_begin = Point2i(text_editor->get_selection_from_line(0), text_editor->get_selection_from_column(0));
|
|
|
|
selection_end = Point2i(text_editor->get_selection_to_line(0), text_editor->get_selection_to_column(0));
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 16:50:35 +02:00
|
|
|
int vsval = text_editor->get_v_scroll();
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2022-09-29 11:53:28 +02:00
|
|
|
String repl_text = get_replace_text();
|
2016-05-30 18:15:41 +02:00
|
|
|
int search_text_len = get_search_text().length();
|
2016-07-30 01:11:02 +02:00
|
|
|
|
2016-05-28 18:25:45 +02:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
replace_all_mode = true;
|
|
|
|
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->begin_complex_operation();
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2020-03-30 23:27:15 +02:00
|
|
|
if (selection_enabled && is_selection_only()) {
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->set_caret_line(selection_begin.width, false, true, 0, 0);
|
|
|
|
text_editor->set_caret_column(selection_begin.height, true, 0);
|
2022-06-06 11:47:57 +02:00
|
|
|
} else {
|
|
|
|
text_editor->set_caret_line(0, false, true, 0, 0);
|
|
|
|
text_editor->set_caret_column(0, true, 0);
|
2020-03-30 23:27:15 +02:00
|
|
|
}
|
2022-06-06 11:47:57 +02:00
|
|
|
|
2018-03-25 09:39:37 +02:00
|
|
|
if (search_current()) {
|
|
|
|
do {
|
2023-03-08 23:00:27 +01:00
|
|
|
// Replace area.
|
2018-03-25 09:39:37 +02:00
|
|
|
Point2i match_from(result_line, result_col);
|
|
|
|
Point2i match_to(result_line, result_col + search_text_len);
|
|
|
|
|
|
|
|
if (match_from < prev_match) {
|
2019-08-14 23:31:07 +02:00
|
|
|
break; // Done.
|
2018-03-25 09:39:37 +02:00
|
|
|
}
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2022-09-29 11:53:28 +02:00
|
|
|
prev_match = Point2i(result_line, result_col + repl_text.length());
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->unfold_line(result_line);
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->select(result_line, result_col, result_line, match_to.y, 0);
|
2016-05-30 18:15:41 +02:00
|
|
|
|
2022-06-06 11:47:57 +02:00
|
|
|
if (selection_enabled) {
|
2018-03-25 09:39:37 +02:00
|
|
|
if (match_from < selection_begin || match_to > selection_end) {
|
2020-03-30 23:27:15 +02:00
|
|
|
break; // Done.
|
2018-03-25 09:39:37 +02:00
|
|
|
}
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2019-08-14 23:31:07 +02:00
|
|
|
// Replace but adjust selection bounds.
|
2022-09-29 11:53:28 +02:00
|
|
|
text_editor->insert_text_at_caret(repl_text, 0);
|
2018-03-25 09:39:37 +02:00
|
|
|
if (match_to.x == selection_end.x) {
|
2022-09-29 11:53:28 +02:00
|
|
|
selection_end.y += repl_text.length() - search_text_len;
|
2018-03-25 09:39:37 +02:00
|
|
|
}
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2018-03-25 09:39:37 +02:00
|
|
|
} else {
|
2019-08-14 23:31:07 +02:00
|
|
|
// Just replace.
|
2022-09-29 11:53:28 +02:00
|
|
|
text_editor->insert_text_at_caret(repl_text, 0);
|
2018-03-25 09:39:37 +02:00
|
|
|
}
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2018-03-25 09:39:37 +02:00
|
|
|
rc++;
|
|
|
|
} while (search_next());
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->end_complex_operation();
|
2016-05-28 18:25:45 +02:00
|
|
|
|
|
|
|
replace_all_mode = false;
|
|
|
|
|
2019-08-14 23:31:07 +02:00
|
|
|
// Restore editor state (selection, cursor, scroll).
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->set_caret_line(orig_cursor.x, false, true, 0, 0);
|
|
|
|
text_editor->set_caret_column(orig_cursor.y, true, 0);
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2022-06-06 11:47:57 +02:00
|
|
|
if (selection_enabled) {
|
2019-08-14 23:31:07 +02:00
|
|
|
// Reselect.
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y, 0);
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->set_v_scroll(vsval);
|
2023-08-13 02:33:39 +02:00
|
|
|
matches_label->add_theme_color_override("font_color", rc > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
|
2019-11-15 13:47:27 +01:00
|
|
|
matches_label->set_text(vformat(TTR("%d replaced."), rc));
|
2018-01-18 00:52:17 +01:00
|
|
|
|
2021-07-17 23:22:52 +02:00
|
|
|
text_editor->call_deferred(SNAME("connect"), "text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
|
2019-07-08 19:33:04 +02:00
|
|
|
results_count = -1;
|
2022-04-27 14:21:28 +02:00
|
|
|
results_count_to_current = -1;
|
|
|
|
needs_to_count_results = true;
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 11:47:57 +02:00
|
|
|
void FindReplaceBar::_get_search_from(int &r_line, int &r_col, bool p_is_searching_next) {
|
|
|
|
if (!text_editor->has_selection(0) || is_selection_only()) {
|
|
|
|
r_line = text_editor->get_caret_line(0);
|
|
|
|
r_col = text_editor->get_caret_column(0);
|
2016-05-30 18:15:41 +02:00
|
|
|
|
2022-06-06 11:47:57 +02:00
|
|
|
if (!p_is_searching_next && r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
|
|
|
|
r_col = result_col;
|
|
|
|
}
|
2020-03-30 23:27:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:47:57 +02:00
|
|
|
if (p_is_searching_next) {
|
|
|
|
r_line = text_editor->get_selection_to_line();
|
|
|
|
r_col = text_editor->get_selection_to_column();
|
|
|
|
} else {
|
|
|
|
r_line = text_editor->get_selection_from_line();
|
|
|
|
r_col = text_editor->get_selection_from_column();
|
2016-05-30 18:15:41 +02:00
|
|
|
}
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2019-07-08 19:33:04 +02:00
|
|
|
void FindReplaceBar::_update_results_count() {
|
2022-06-06 11:47:57 +02:00
|
|
|
if (!needs_to_count_results && (result_line != -1) && results_count_to_current > 0) {
|
2022-04-27 14:21:28 +02:00
|
|
|
results_count_to_current += (flags & TextEdit::SEARCH_BACKWARDS) ? -1 : 1;
|
|
|
|
|
|
|
|
if (results_count_to_current > results_count) {
|
|
|
|
results_count_to_current = results_count_to_current - results_count;
|
2022-07-12 15:14:43 +02:00
|
|
|
} else if (results_count_to_current <= 0) {
|
2022-04-27 14:21:28 +02:00
|
|
|
results_count_to_current = results_count;
|
|
|
|
}
|
|
|
|
|
2019-07-08 19:33:04 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-08 19:33:04 +02:00
|
|
|
|
|
|
|
String searched = get_search_text();
|
2020-12-15 13:04:21 +01:00
|
|
|
if (searched.is_empty()) {
|
2019-07-08 19:33:04 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-08 19:33:04 +02:00
|
|
|
|
2022-04-27 14:21:28 +02:00
|
|
|
needs_to_count_results = false;
|
2019-07-08 19:33:04 +02:00
|
|
|
|
2022-06-06 11:47:57 +02:00
|
|
|
results_count = 0;
|
|
|
|
|
2022-04-27 14:21:28 +02:00
|
|
|
for (int i = 0; i < text_editor->get_line_count(); i++) {
|
|
|
|
String line_text = text_editor->get_line(i);
|
2019-07-08 19:33:04 +02:00
|
|
|
|
2022-04-27 14:21:28 +02:00
|
|
|
int col_pos = 0;
|
|
|
|
|
2023-10-02 22:30:23 +02:00
|
|
|
bool searched_start_is_symbol = is_symbol(searched[0]);
|
|
|
|
bool searched_end_is_symbol = is_symbol(searched[searched.length() - 1]);
|
|
|
|
|
2022-04-27 14:21:28 +02:00
|
|
|
while (true) {
|
|
|
|
col_pos = is_case_sensitive() ? line_text.find(searched, col_pos) : line_text.findn(searched, col_pos);
|
2019-07-08 19:33:04 +02:00
|
|
|
|
2022-04-27 14:21:28 +02:00
|
|
|
if (col_pos == -1) {
|
|
|
|
break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2022-04-27 14:21:28 +02:00
|
|
|
|
|
|
|
if (is_whole_words()) {
|
2023-10-02 22:30:23 +02:00
|
|
|
if (!searched_start_is_symbol && col_pos > 0 && !is_symbol(line_text[col_pos - 1])) {
|
2022-11-14 12:18:21 +01:00
|
|
|
col_pos += searched.length();
|
|
|
|
continue;
|
2022-04-27 14:21:28 +02:00
|
|
|
}
|
2023-10-02 22:30:23 +02:00
|
|
|
if (!searched_end_is_symbol && col_pos + searched.length() < line_text.length() && !is_symbol(line_text[col_pos + searched.length()])) {
|
2022-11-14 12:18:21 +01:00
|
|
|
col_pos += searched.length();
|
|
|
|
continue;
|
2022-04-27 14:21:28 +02:00
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-08 19:33:04 +02:00
|
|
|
|
2022-04-27 14:21:28 +02:00
|
|
|
results_count++;
|
|
|
|
|
2022-06-06 11:47:57 +02:00
|
|
|
if (i == result_line) {
|
|
|
|
if (col_pos == result_col) {
|
|
|
|
results_count_to_current = results_count;
|
|
|
|
} else if (col_pos < result_col && col_pos + searched.length() > result_col) {
|
|
|
|
col_pos = result_col;
|
|
|
|
results_count_to_current = results_count;
|
|
|
|
}
|
2022-04-27 14:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
col_pos += searched.length();
|
|
|
|
}
|
2019-07-08 19:33:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 08:47:09 +02:00
|
|
|
void FindReplaceBar::_update_matches_label() {
|
2020-12-15 13:04:21 +01:00
|
|
|
if (search_text->get_text().is_empty() || results_count == -1) {
|
2019-08-09 08:47:09 +02:00
|
|
|
matches_label->hide();
|
|
|
|
} else {
|
|
|
|
matches_label->show();
|
|
|
|
|
2023-08-13 02:33:39 +02:00
|
|
|
matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
|
2022-04-27 14:21:28 +02:00
|
|
|
|
|
|
|
if (results_count == 0) {
|
2023-07-02 10:48:37 +02:00
|
|
|
matches_label->set_text(TTR("No match"));
|
2022-06-06 11:47:57 +02:00
|
|
|
} else if (results_count_to_current == -1) {
|
|
|
|
matches_label->set_text(vformat(TTRN("%d match", "%d matches", results_count), results_count));
|
2022-04-27 14:21:28 +02:00
|
|
|
} else {
|
2022-06-06 11:47:57 +02:00
|
|
|
matches_label->set_text(vformat(TTRN("%d of %d match", "%d of %d matches", results_count), results_count_to_current, results_count));
|
2022-04-27 14:21:28 +02:00
|
|
|
}
|
2019-08-09 08:47:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 18:15:41 +02:00
|
|
|
bool FindReplaceBar::search_current() {
|
2022-04-27 14:21:28 +02:00
|
|
|
flags = 0;
|
2016-05-30 18:15:41 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (is_whole_words()) {
|
2016-05-30 18:15:41 +02:00
|
|
|
flags |= TextEdit::SEARCH_WHOLE_WORDS;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (is_case_sensitive()) {
|
2016-05-30 18:15:41 +02:00
|
|
|
flags |= TextEdit::SEARCH_MATCH_CASE;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-05-30 18:15:41 +02:00
|
|
|
|
|
|
|
int line, col;
|
|
|
|
_get_search_from(line, col);
|
|
|
|
|
|
|
|
return _search(flags, line, col);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindReplaceBar::search_prev() {
|
2022-06-06 11:47:57 +02:00
|
|
|
if (is_selection_only() && !replace_all_mode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!is_visible()) {
|
2019-08-12 20:31:53 +02:00
|
|
|
popup_search(true);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-08-12 20:31:53 +02:00
|
|
|
|
2022-04-27 14:21:28 +02:00
|
|
|
flags = 0;
|
2016-05-30 18:15:41 +02:00
|
|
|
String text = get_search_text();
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (is_whole_words()) {
|
2016-05-30 18:15:41 +02:00
|
|
|
flags |= TextEdit::SEARCH_WHOLE_WORDS;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (is_case_sensitive()) {
|
2016-05-30 18:15:41 +02:00
|
|
|
flags |= TextEdit::SEARCH_MATCH_CASE;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-05-30 18:15:41 +02:00
|
|
|
|
|
|
|
flags |= TextEdit::SEARCH_BACKWARDS;
|
|
|
|
|
|
|
|
int line, col;
|
|
|
|
_get_search_from(line, col);
|
|
|
|
|
2019-09-02 14:15:55 +02:00
|
|
|
col -= text.length();
|
|
|
|
if (col < 0) {
|
|
|
|
line -= 1;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (line < 0) {
|
2020-07-24 16:50:35 +02:00
|
|
|
line = text_editor->get_line_count() - 1;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-07-24 16:50:35 +02:00
|
|
|
col = text_editor->get_line(line).length();
|
2016-05-30 18:15:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return _search(flags, line, col);
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2016-05-30 18:15:41 +02:00
|
|
|
bool FindReplaceBar::search_next() {
|
2022-06-06 11:47:57 +02:00
|
|
|
if (is_selection_only() && !replace_all_mode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!is_visible()) {
|
2019-08-12 20:31:53 +02:00
|
|
|
popup_search(true);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-08-12 20:31:53 +02:00
|
|
|
|
2022-04-27 14:21:28 +02:00
|
|
|
flags = 0;
|
2016-05-30 18:15:41 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (is_whole_words()) {
|
2016-05-30 18:15:41 +02:00
|
|
|
flags |= TextEdit::SEARCH_WHOLE_WORDS;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (is_case_sensitive()) {
|
2016-05-30 18:15:41 +02:00
|
|
|
flags |= TextEdit::SEARCH_MATCH_CASE;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-05-30 18:15:41 +02:00
|
|
|
|
|
|
|
int line, col;
|
2022-06-06 11:47:57 +02:00
|
|
|
_get_search_from(line, col, true);
|
2016-05-30 18:15:41 +02:00
|
|
|
|
|
|
|
return _search(flags, line, col);
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 16:16:13 +02:00
|
|
|
void FindReplaceBar::_hide_bar(bool p_force_focus) {
|
|
|
|
if (replace_text->has_focus() || search_text->has_focus() || p_force_focus) {
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->grab_focus();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-05-30 18:15:41 +02:00
|
|
|
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->set_search_text("");
|
2016-05-30 18:15:41 +02:00
|
|
|
result_line = -1;
|
|
|
|
result_col = -1;
|
2016-05-28 18:25:45 +02:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
2019-08-12 20:31:53 +02:00
|
|
|
void FindReplaceBar::_show_search(bool p_focus_replace, bool p_show_only) {
|
2016-05-28 18:25:45 +02:00
|
|
|
show();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_show_only) {
|
2019-08-12 20:31:53 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-08-12 20:31:53 +02:00
|
|
|
|
|
|
|
if (p_focus_replace) {
|
|
|
|
search_text->deselect();
|
2021-07-17 23:22:52 +02:00
|
|
|
replace_text->call_deferred(SNAME("grab_focus"));
|
2019-08-12 20:31:53 +02:00
|
|
|
} else {
|
|
|
|
replace_text->deselect();
|
2021-07-17 23:22:52 +02:00
|
|
|
search_text->call_deferred(SNAME("grab_focus"));
|
2019-08-12 20:31:53 +02:00
|
|
|
}
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2022-06-06 11:47:57 +02:00
|
|
|
if (text_editor->has_selection(0) && !is_selection_only()) {
|
2022-06-08 23:41:38 +02:00
|
|
|
search_text->set_text(text_editor->get_selected_text(0));
|
2022-06-06 11:47:57 +02:00
|
|
|
result_line = text_editor->get_selection_from_line();
|
|
|
|
result_col = text_editor->get_selection_from_column();
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2020-12-15 13:04:21 +01:00
|
|
|
if (!get_search_text().is_empty()) {
|
2019-08-12 20:31:53 +02:00
|
|
|
if (p_focus_replace) {
|
|
|
|
replace_text->select_all();
|
2021-03-28 20:31:25 +02:00
|
|
|
replace_text->set_caret_column(replace_text->get_text().length());
|
2019-08-12 20:31:53 +02:00
|
|
|
} else {
|
|
|
|
search_text->select_all();
|
2021-03-28 20:31:25 +02:00
|
|
|
search_text->set_caret_column(search_text->get_text().length());
|
2019-08-12 20:31:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
results_count = -1;
|
2022-04-27 14:21:28 +02:00
|
|
|
results_count_to_current = -1;
|
|
|
|
needs_to_count_results = true;
|
2019-08-12 20:31:53 +02:00
|
|
|
_update_results_count();
|
|
|
|
_update_matches_label();
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 20:31:53 +02:00
|
|
|
void FindReplaceBar::popup_search(bool p_show_only) {
|
2019-10-01 14:57:11 +02:00
|
|
|
replace_text->hide();
|
2018-01-18 00:52:17 +01:00
|
|
|
hbc_button_replace->hide();
|
|
|
|
hbc_option_replace->hide();
|
2022-06-06 11:47:57 +02:00
|
|
|
selection_only->set_pressed(false);
|
2019-10-01 14:57:11 +02:00
|
|
|
|
2019-08-12 20:31:53 +02:00
|
|
|
_show_search(false, p_show_only);
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::popup_replace() {
|
2018-01-18 00:52:17 +01:00
|
|
|
if (!replace_text->is_visible_in_tree()) {
|
|
|
|
replace_text->show();
|
|
|
|
hbc_button_replace->show();
|
|
|
|
hbc_option_replace->show();
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2022-06-08 23:41:38 +02:00
|
|
|
selection_only->set_pressed((text_editor->has_selection(0) && text_editor->get_selection_from_line(0) < text_editor->get_selection_to_line(0)));
|
2016-09-11 15:01:52 +02:00
|
|
|
|
2022-06-08 23:41:38 +02:00
|
|
|
_show_search(is_visible() || text_editor->has_selection(0));
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_search_options_changed(bool p_pressed) {
|
2019-07-08 19:33:04 +02:00
|
|
|
results_count = -1;
|
2022-04-27 14:21:28 +02:00
|
|
|
results_count_to_current = -1;
|
|
|
|
needs_to_count_results = true;
|
2016-05-28 18:25:45 +02:00
|
|
|
search_current();
|
|
|
|
}
|
|
|
|
|
2016-05-30 18:15:41 +02:00
|
|
|
void FindReplaceBar::_editor_text_changed() {
|
2019-07-08 19:33:04 +02:00
|
|
|
results_count = -1;
|
2022-04-27 14:21:28 +02:00
|
|
|
results_count_to_current = -1;
|
|
|
|
needs_to_count_results = true;
|
2017-01-13 14:45:50 +01:00
|
|
|
if (is_visible_in_tree()) {
|
2016-05-30 18:15:41 +02:00
|
|
|
preserve_cursor = true;
|
|
|
|
search_current();
|
|
|
|
preserve_cursor = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 18:25:45 +02:00
|
|
|
void FindReplaceBar::_search_text_changed(const String &p_text) {
|
2019-07-08 19:33:04 +02:00
|
|
|
results_count = -1;
|
2022-04-27 14:21:28 +02:00
|
|
|
results_count_to_current = -1;
|
|
|
|
needs_to_count_results = true;
|
2016-05-28 18:25:45 +02:00
|
|
|
search_current();
|
|
|
|
}
|
|
|
|
|
2021-06-16 18:43:34 +02:00
|
|
|
void FindReplaceBar::_search_text_submitted(const String &p_text) {
|
2021-08-13 23:31:57 +02:00
|
|
|
if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
|
2019-04-06 01:19:00 +02:00
|
|
|
search_prev();
|
|
|
|
} else {
|
|
|
|
search_next();
|
|
|
|
}
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2021-06-16 18:43:34 +02:00
|
|
|
void FindReplaceBar::_replace_text_submitted(const String &p_text) {
|
2022-06-08 23:41:38 +02:00
|
|
|
if (selection_only->is_pressed() && text_editor->has_selection(0)) {
|
2016-09-11 15:01:52 +02:00
|
|
|
_replace_all();
|
|
|
|
_hide_bar();
|
2021-08-13 23:31:57 +02:00
|
|
|
} else if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
|
2020-02-08 21:46:04 +01:00
|
|
|
_replace();
|
|
|
|
search_prev();
|
|
|
|
} else {
|
|
|
|
_replace();
|
2016-09-11 15:01:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 18:25:45 +02:00
|
|
|
String FindReplaceBar::get_search_text() const {
|
|
|
|
return search_text->get_text();
|
|
|
|
}
|
|
|
|
|
|
|
|
String FindReplaceBar::get_replace_text() const {
|
|
|
|
return replace_text->get_text();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindReplaceBar::is_case_sensitive() const {
|
|
|
|
return case_sensitive->is_pressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindReplaceBar::is_whole_words() const {
|
|
|
|
return whole_words->is_pressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindReplaceBar::is_selection_only() const {
|
|
|
|
return selection_only->is_pressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::set_error(const String &p_label) {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("error"), p_label);
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2021-06-03 01:05:41 +02:00
|
|
|
void FindReplaceBar::set_text_edit(CodeTextEditor *p_text_editor) {
|
|
|
|
if (p_text_editor == base_text_editor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base_text_editor) {
|
|
|
|
base_text_editor->remove_find_replace_bar();
|
|
|
|
base_text_editor = nullptr;
|
|
|
|
text_editor->disconnect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
|
|
|
|
text_editor = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:38:29 +02:00
|
|
|
if (!p_text_editor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-08 19:33:04 +02:00
|
|
|
results_count = -1;
|
2022-04-27 14:21:28 +02:00
|
|
|
results_count_to_current = -1;
|
|
|
|
needs_to_count_results = true;
|
2021-06-03 01:05:41 +02:00
|
|
|
base_text_editor = p_text_editor;
|
|
|
|
text_editor = base_text_editor->get_text_editor();
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor->connect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
|
2021-06-03 01:05:41 +02:00
|
|
|
|
|
|
|
_update_results_count();
|
|
|
|
_update_matches_label();
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_bind_methods() {
|
2017-01-03 03:03:46 +01:00
|
|
|
ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2018-01-18 00:52:17 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("error"));
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FindReplaceBar::FindReplaceBar() {
|
2018-01-18 00:52:17 +01:00
|
|
|
vbc_lineedit = memnew(VBoxContainer);
|
2019-02-10 17:17:29 +01:00
|
|
|
add_child(vbc_lineedit);
|
2021-12-09 11:40:46 +01:00
|
|
|
vbc_lineedit->set_alignment(BoxContainer::ALIGNMENT_CENTER);
|
2018-01-18 00:52:17 +01:00
|
|
|
vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
VBoxContainer *vbc_button = memnew(VBoxContainer);
|
2019-02-10 17:17:29 +01:00
|
|
|
add_child(vbc_button);
|
2018-01-18 00:52:17 +01:00
|
|
|
VBoxContainer *vbc_option = memnew(VBoxContainer);
|
2019-02-10 17:17:29 +01:00
|
|
|
add_child(vbc_option);
|
2018-01-18 00:52:17 +01:00
|
|
|
|
|
|
|
HBoxContainer *hbc_button_search = memnew(HBoxContainer);
|
|
|
|
vbc_button->add_child(hbc_button_search);
|
2021-12-09 11:40:46 +01:00
|
|
|
hbc_button_search->set_alignment(BoxContainer::ALIGNMENT_END);
|
2018-01-18 00:52:17 +01:00
|
|
|
hbc_button_replace = memnew(HBoxContainer);
|
|
|
|
vbc_button->add_child(hbc_button_replace);
|
2021-12-09 11:40:46 +01:00
|
|
|
hbc_button_replace->set_alignment(BoxContainer::ALIGNMENT_END);
|
2018-01-18 00:52:17 +01:00
|
|
|
|
|
|
|
HBoxContainer *hbc_option_search = memnew(HBoxContainer);
|
|
|
|
vbc_option->add_child(hbc_option_search);
|
|
|
|
hbc_option_replace = memnew(HBoxContainer);
|
|
|
|
vbc_option->add_child(hbc_option_replace);
|
|
|
|
|
2023-03-08 23:00:27 +01:00
|
|
|
// Search toolbar
|
2016-05-28 18:25:45 +02:00
|
|
|
search_text = memnew(LineEdit);
|
2018-01-18 00:52:17 +01:00
|
|
|
vbc_lineedit->add_child(search_text);
|
|
|
|
search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
|
2020-02-21 18:28:45 +01:00
|
|
|
search_text->connect("text_changed", callable_mp(this, &FindReplaceBar::_search_text_changed));
|
2021-06-16 18:43:34 +02:00
|
|
|
search_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_search_text_submitted));
|
2023-10-14 16:16:13 +02:00
|
|
|
search_text->connect("focus_exited", callable_mp(this, &FindReplaceBar::_focus_lost));
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2019-08-09 08:47:09 +02:00
|
|
|
matches_label = memnew(Label);
|
|
|
|
hbc_button_search->add_child(matches_label);
|
|
|
|
matches_label->hide();
|
|
|
|
|
2020-06-19 20:49:04 +02:00
|
|
|
find_prev = memnew(Button);
|
|
|
|
find_prev->set_flat(true);
|
2018-01-18 00:52:17 +01:00
|
|
|
hbc_button_search->add_child(find_prev);
|
2016-05-28 18:25:45 +02:00
|
|
|
find_prev->set_focus_mode(FOCUS_NONE);
|
2020-02-21 18:28:45 +01:00
|
|
|
find_prev->connect("pressed", callable_mp(this, &FindReplaceBar::search_prev));
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2020-06-19 20:49:04 +02:00
|
|
|
find_next = memnew(Button);
|
|
|
|
find_next->set_flat(true);
|
2018-01-18 00:52:17 +01:00
|
|
|
hbc_button_search->add_child(find_next);
|
2016-05-28 18:25:45 +02:00
|
|
|
find_next->set_focus_mode(FOCUS_NONE);
|
2020-02-21 18:28:45 +01:00
|
|
|
find_next->connect("pressed", callable_mp(this, &FindReplaceBar::search_next));
|
2016-05-28 18:25:45 +02:00
|
|
|
|
|
|
|
case_sensitive = memnew(CheckBox);
|
2018-01-18 00:52:17 +01:00
|
|
|
hbc_option_search->add_child(case_sensitive);
|
2016-05-28 18:25:45 +02:00
|
|
|
case_sensitive->set_text(TTR("Match Case"));
|
|
|
|
case_sensitive->set_focus_mode(FOCUS_NONE);
|
2020-02-21 18:28:45 +01:00
|
|
|
case_sensitive->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
|
2016-05-28 18:25:45 +02:00
|
|
|
|
|
|
|
whole_words = memnew(CheckBox);
|
2018-01-18 00:52:17 +01:00
|
|
|
hbc_option_search->add_child(whole_words);
|
2016-05-28 18:25:45 +02:00
|
|
|
whole_words->set_text(TTR("Whole Words"));
|
|
|
|
whole_words->set_focus_mode(FOCUS_NONE);
|
2020-02-21 18:28:45 +01:00
|
|
|
whole_words->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2023-03-08 23:00:27 +01:00
|
|
|
// Replace toolbar
|
2018-01-18 00:52:17 +01:00
|
|
|
replace_text = memnew(LineEdit);
|
|
|
|
vbc_lineedit->add_child(replace_text);
|
|
|
|
replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
|
2021-06-16 18:43:34 +02:00
|
|
|
replace_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_replace_text_submitted));
|
2023-10-14 16:16:13 +02:00
|
|
|
replace_text->connect("focus_exited", callable_mp(this, &FindReplaceBar::_focus_lost));
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2018-01-18 00:52:17 +01:00
|
|
|
replace = memnew(Button);
|
|
|
|
hbc_button_replace->add_child(replace);
|
|
|
|
replace->set_text(TTR("Replace"));
|
2020-02-21 18:28:45 +01:00
|
|
|
replace->connect("pressed", callable_mp(this, &FindReplaceBar::_replace));
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2018-01-18 00:52:17 +01:00
|
|
|
replace_all = memnew(Button);
|
|
|
|
hbc_button_replace->add_child(replace_all);
|
|
|
|
replace_all->set_text(TTR("Replace All"));
|
2020-02-21 18:28:45 +01:00
|
|
|
replace_all->connect("pressed", callable_mp(this, &FindReplaceBar::_replace_all));
|
2016-05-28 18:25:45 +02:00
|
|
|
|
|
|
|
selection_only = memnew(CheckBox);
|
2018-01-18 00:52:17 +01:00
|
|
|
hbc_option_replace->add_child(selection_only);
|
2016-05-28 18:25:45 +02:00
|
|
|
selection_only->set_text(TTR("Selection Only"));
|
|
|
|
selection_only->set_focus_mode(FOCUS_NONE);
|
2020-02-21 18:28:45 +01:00
|
|
|
selection_only->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
|
2018-01-18 00:52:17 +01:00
|
|
|
|
|
|
|
hide_button = memnew(TextureButton);
|
|
|
|
add_child(hide_button);
|
|
|
|
hide_button->set_focus_mode(FOCUS_NONE);
|
2023-10-16 21:51:37 +02:00
|
|
|
hide_button->connect("pressed", callable_mp(this, &FindReplaceBar::_hide_bar).bind(false));
|
2018-05-16 09:31:40 +02:00
|
|
|
hide_button->set_v_size_flags(SIZE_SHRINK_CENTER);
|
2016-05-28 18:25:45 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
/*** CODE EDITOR ****/
|
|
|
|
|
2019-05-09 18:26:33 +02:00
|
|
|
// This function should be used to handle shortcuts that could otherwise
|
|
|
|
// be handled too late if they weren't handled here.
|
2021-08-22 17:37:22 +02:00
|
|
|
void CodeTextEditor::input(const Ref<InputEvent> &event) {
|
2021-04-05 08:52:21 +02:00
|
|
|
ERR_FAIL_COND(event.is_null());
|
|
|
|
|
2019-05-09 18:26:33 +02:00
|
|
|
const Ref<InputEventKey> key_event = event;
|
2021-08-30 17:59:56 +02:00
|
|
|
|
|
|
|
if (!key_event.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!key_event->is_pressed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!text_editor->has_focus()) {
|
2023-10-14 16:16:13 +02:00
|
|
|
if ((find_replace_bar != nullptr && find_replace_bar->is_visible()) && (find_replace_bar->has_focus() || (get_viewport()->gui_get_focus_owner() && find_replace_bar->is_ancestor_of(get_viewport()->gui_get_focus_owner())))) {
|
2021-08-30 17:59:56 +02:00
|
|
|
if (ED_IS_SHORTCUT("script_text_editor/find_next", key_event)) {
|
|
|
|
find_replace_bar->search_next();
|
|
|
|
accept_event();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ED_IS_SHORTCUT("script_text_editor/find_previous", key_event)) {
|
|
|
|
find_replace_bar->search_prev();
|
|
|
|
accept_event();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 18:26:33 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-05-09 18:26:33 +02:00
|
|
|
|
|
|
|
if (ED_IS_SHORTCUT("script_text_editor/move_up", key_event)) {
|
|
|
|
move_lines_up();
|
|
|
|
accept_event();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ED_IS_SHORTCUT("script_text_editor/move_down", key_event)) {
|
|
|
|
move_lines_down();
|
|
|
|
accept_event();
|
|
|
|
return;
|
|
|
|
}
|
2020-02-16 22:09:55 +01:00
|
|
|
if (ED_IS_SHORTCUT("script_text_editor/delete_line", key_event)) {
|
|
|
|
delete_lines();
|
|
|
|
accept_event();
|
|
|
|
return;
|
|
|
|
}
|
2021-06-29 12:09:07 +02:00
|
|
|
if (ED_IS_SHORTCUT("script_text_editor/duplicate_selection", key_event)) {
|
|
|
|
duplicate_selection();
|
2020-02-16 22:09:55 +01:00
|
|
|
accept_event();
|
|
|
|
return;
|
|
|
|
}
|
2022-09-28 17:09:45 +02:00
|
|
|
if (ED_IS_SHORTCUT("script_text_editor/duplicate_lines", key_event)) {
|
|
|
|
text_editor->duplicate_lines();
|
|
|
|
accept_event();
|
|
|
|
return;
|
|
|
|
}
|
2019-05-09 18:26:33 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
|
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2016-06-17 21:58:50 +02:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (mb.is_valid()) {
|
2022-09-02 11:37:48 +02:00
|
|
|
if (mb->is_pressed() && mb->is_command_or_control_pressed()) {
|
2021-08-13 23:31:57 +02:00
|
|
|
if (mb->get_button_index() == MouseButton::WHEEL_UP) {
|
2016-07-14 21:03:40 +02:00
|
|
|
_zoom_in();
|
2021-08-13 23:31:57 +02:00
|
|
|
} else if (mb->get_button_index() == MouseButton::WHEEL_DOWN) {
|
2016-07-14 21:03:40 +02:00
|
|
|
_zoom_out();
|
2016-06-17 21:58:50 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-20 17:38:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 21:49:39 +01:00
|
|
|
Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
|
|
|
|
if (magnify_gesture.is_valid()) {
|
2021-07-17 23:22:52 +02:00
|
|
|
font_size = text_editor->get_theme_font_size(SNAME("font_size"));
|
2020-09-18 09:36:10 +02:00
|
|
|
font_size *= powf(magnify_gesture->get_factor(), 0.25);
|
2017-11-01 21:49:39 +01:00
|
|
|
|
2021-07-17 23:22:52 +02:00
|
|
|
_add_font_size((int)font_size - text_editor->get_theme_font_size(SNAME("font_size")));
|
2017-11-01 21:49:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
Ref<InputEventKey> k = p_event;
|
|
|
|
|
|
|
|
if (k.is_valid()) {
|
|
|
|
if (k->is_pressed()) {
|
2016-07-14 21:03:40 +02:00
|
|
|
if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
|
|
|
|
_zoom_in();
|
2022-07-25 13:21:56 +02:00
|
|
|
accept_event();
|
2016-07-14 21:03:40 +02:00
|
|
|
}
|
|
|
|
if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
|
|
|
|
_zoom_out();
|
2022-07-25 13:21:56 +02:00
|
|
|
accept_event();
|
2016-07-14 21:03:40 +02:00
|
|
|
}
|
|
|
|
if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
|
|
|
|
_reset_zoom();
|
2022-07-25 13:21:56 +02:00
|
|
|
accept_event();
|
2016-07-14 21:03:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-17 21:58:50 +02:00
|
|
|
|
2016-07-14 21:03:40 +02:00
|
|
|
void CodeTextEditor::_zoom_in() {
|
2018-05-05 09:40:35 +02:00
|
|
|
font_resize_val += MAX(EDSCALE, 1.0f);
|
2020-09-18 09:36:10 +02:00
|
|
|
_zoom_changed();
|
2016-07-14 21:03:40 +02:00
|
|
|
}
|
2016-06-17 21:58:50 +02:00
|
|
|
|
2016-07-14 21:03:40 +02:00
|
|
|
void CodeTextEditor::_zoom_out() {
|
2018-05-05 09:40:35 +02:00
|
|
|
font_resize_val -= MAX(EDSCALE, 1.0f);
|
2020-09-18 09:36:10 +02:00
|
|
|
_zoom_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_zoom_changed() {
|
|
|
|
if (font_resize_timer->get_time_left() == 0) {
|
|
|
|
font_resize_timer->start();
|
|
|
|
}
|
2016-07-14 21:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_reset_zoom() {
|
2020-09-18 09:36:10 +02:00
|
|
|
EditorSettings::get_singleton()->set("interface/editor/code_font_size", 14);
|
2022-02-08 10:14:58 +01:00
|
|
|
text_editor->add_theme_font_size_override("font_size", 14 * EDSCALE);
|
2016-06-17 21:58:50 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void CodeTextEditor::_line_col_changed() {
|
2022-12-06 10:23:11 +01:00
|
|
|
if (!code_complete_timer->is_stopped() && code_complete_timer_line != text_editor->get_caret_line()) {
|
|
|
|
code_complete_timer->stop();
|
|
|
|
}
|
|
|
|
|
2021-07-08 19:35:56 +02:00
|
|
|
String line = text_editor->get_line(text_editor->get_caret_line());
|
2018-05-03 19:03:20 +02:00
|
|
|
|
|
|
|
int positional_column = 0;
|
2021-07-08 19:35:56 +02:00
|
|
|
for (int i = 0; i < text_editor->get_caret_column(); i++) {
|
2018-05-03 19:03:20 +02:00
|
|
|
if (line[i] == '\t') {
|
2023-03-08 23:00:27 +01:00
|
|
|
positional_column += text_editor->get_indent_size(); // Tab size
|
2018-05-03 19:03:20 +02:00
|
|
|
} else {
|
|
|
|
positional_column += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 21:31:05 +02:00
|
|
|
StringBuilder sb;
|
2021-09-14 22:23:59 +02:00
|
|
|
sb.append(itos(text_editor->get_caret_line() + 1).lpad(4));
|
|
|
|
sb.append(" : ");
|
2019-05-30 21:31:05 +02:00
|
|
|
sb.append(itos(positional_column + 1).lpad(3));
|
|
|
|
|
2023-05-01 22:41:50 +02:00
|
|
|
sb.append(" | ");
|
2023-07-12 03:12:02 +02:00
|
|
|
sb.append(text_editor->is_indent_using_spaces() ? TTR("Spaces", "Indentation") : TTR("Tabs", "Indentation"));
|
2023-05-01 22:41:50 +02:00
|
|
|
|
2019-05-30 21:31:05 +02:00
|
|
|
line_and_col_txt->set_text(sb.as_string());
|
2022-04-27 14:21:28 +02:00
|
|
|
|
|
|
|
if (find_replace_bar) {
|
|
|
|
if (!find_replace_bar->line_col_changed_for_result) {
|
|
|
|
find_replace_bar->needs_to_count_results = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
find_replace_bar->line_col_changed_for_result = false;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_text_changed() {
|
2022-11-01 20:03:43 +01:00
|
|
|
if (code_complete_enabled && text_editor->is_insert_text_operation()) {
|
2022-12-06 10:23:11 +01:00
|
|
|
code_complete_timer_line = text_editor->get_caret_line();
|
2017-09-06 03:02:51 +02:00
|
|
|
code_complete_timer->start();
|
|
|
|
}
|
2017-09-28 18:30:20 +02:00
|
|
|
|
|
|
|
idle->start();
|
2022-04-27 14:21:28 +02:00
|
|
|
|
|
|
|
if (find_replace_bar) {
|
|
|
|
find_replace_bar->needs_to_count_results = true;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-05-04 05:22:49 +02:00
|
|
|
void CodeTextEditor::_code_complete_timer_timeout() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!is_visible_in_tree()) {
|
2014-12-17 02:31:57 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-09-13 22:14:20 +02:00
|
|
|
text_editor->request_code_completion();
|
2014-05-04 05:22:49 +02:00
|
|
|
}
|
|
|
|
|
2014-12-17 02:31:57 +01:00
|
|
|
void CodeTextEditor::_complete_request() {
|
2022-03-26 16:48:43 +01:00
|
|
|
List<ScriptLanguage::CodeCompletionOption> entries;
|
2020-09-13 22:14:20 +02:00
|
|
|
String ctext = text_editor->get_text_for_code_completion();
|
2016-08-03 00:11:05 +02:00
|
|
|
_code_complete_script(ctext, &entries);
|
2017-08-24 05:06:56 +02:00
|
|
|
bool forced = false;
|
2016-08-03 00:11:05 +02:00
|
|
|
if (code_complete_func) {
|
2017-08-24 05:06:56 +02:00
|
|
|
code_complete_func(code_complete_ud, ctext, &entries, forced);
|
2016-08-03 00:11:05 +02:00
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
if (entries.size() == 0) {
|
2014-12-17 02:31:57 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-03-26 16:48:43 +01:00
|
|
|
for (const ScriptLanguage::CodeCompletionOption &e : entries) {
|
2020-09-13 22:14:20 +02:00
|
|
|
Color font_color = completion_font_color;
|
2023-03-12 08:33:38 +01:00
|
|
|
if (!e.theme_color_name.is_empty() && EDITOR_GET("text_editor/completion/colorize_suggestions")) {
|
|
|
|
font_color = get_theme_color(e.theme_color_name, SNAME("Editor"));
|
|
|
|
} else if (e.insert_text.begins_with("\"") || e.insert_text.begins_with("\'")) {
|
2020-09-13 22:14:20 +02:00
|
|
|
font_color = completion_string_color;
|
2023-02-05 10:01:01 +01:00
|
|
|
} else if (e.insert_text.begins_with("##") || e.insert_text.begins_with("///")) {
|
|
|
|
font_color = completion_doc_comment_color;
|
2020-09-13 22:14:20 +02:00
|
|
|
} else if (e.insert_text.begins_with("#") || e.insert_text.begins_with("//")) {
|
|
|
|
font_color = completion_comment_color;
|
2020-05-03 18:08:15 +02:00
|
|
|
}
|
2023-05-23 05:12:34 +02:00
|
|
|
text_editor->add_code_completion_option((CodeEdit::CodeCompletionKind)e.kind, e.display, e.insert_text, font_color, _get_completion_icon(e), e.default_value, e.location);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-09-13 22:14:20 +02:00
|
|
|
text_editor->update_code_completion_options(forced);
|
2019-06-13 11:32:03 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-03-26 16:48:43 +01:00
|
|
|
Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptLanguage::CodeCompletionOption &p_option) {
|
2019-06-11 20:43:37 +02:00
|
|
|
Ref<Texture2D> tex;
|
2019-06-13 11:32:03 +02:00
|
|
|
switch (p_option.kind) {
|
2022-03-26 16:48:43 +01:00
|
|
|
case ScriptLanguage::CODE_COMPLETION_KIND_CLASS: {
|
2023-08-13 02:33:39 +02:00
|
|
|
if (has_theme_icon(p_option.display, EditorStringName(EditorIcons))) {
|
|
|
|
tex = get_editor_theme_icon(p_option.display);
|
2019-06-13 11:32:03 +02:00
|
|
|
} else {
|
2023-08-13 02:33:39 +02:00
|
|
|
tex = get_editor_theme_icon(SNAME("Object"));
|
2019-06-13 11:32:03 +02:00
|
|
|
}
|
|
|
|
} break;
|
2022-03-26 16:48:43 +01:00
|
|
|
case ScriptLanguage::CODE_COMPLETION_KIND_ENUM:
|
2023-08-13 02:33:39 +02:00
|
|
|
tex = get_editor_theme_icon(SNAME("Enum"));
|
2019-06-13 11:32:03 +02:00
|
|
|
break;
|
2022-03-26 16:48:43 +01:00
|
|
|
case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH:
|
2023-08-13 02:33:39 +02:00
|
|
|
tex = get_editor_theme_icon(SNAME("File"));
|
2019-06-13 11:32:03 +02:00
|
|
|
break;
|
2022-03-26 16:48:43 +01:00
|
|
|
case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH:
|
2023-08-13 02:33:39 +02:00
|
|
|
tex = get_editor_theme_icon(SNAME("NodePath"));
|
2019-06-13 11:32:03 +02:00
|
|
|
break;
|
2022-03-26 16:48:43 +01:00
|
|
|
case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE:
|
2023-08-13 02:33:39 +02:00
|
|
|
tex = get_editor_theme_icon(SNAME("Variant"));
|
2019-06-13 11:32:03 +02:00
|
|
|
break;
|
2022-03-26 16:48:43 +01:00
|
|
|
case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT:
|
2023-08-13 02:33:39 +02:00
|
|
|
tex = get_editor_theme_icon(SNAME("MemberConstant"));
|
2019-06-13 11:32:03 +02:00
|
|
|
break;
|
2022-03-26 16:48:43 +01:00
|
|
|
case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER:
|
2023-08-13 02:33:39 +02:00
|
|
|
tex = get_editor_theme_icon(SNAME("MemberProperty"));
|
2019-06-13 11:32:03 +02:00
|
|
|
break;
|
2022-03-26 16:48:43 +01:00
|
|
|
case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL:
|
2023-08-13 02:33:39 +02:00
|
|
|
tex = get_editor_theme_icon(SNAME("MemberSignal"));
|
2019-06-13 11:32:03 +02:00
|
|
|
break;
|
2022-03-26 16:48:43 +01:00
|
|
|
case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION:
|
2023-08-13 02:33:39 +02:00
|
|
|
tex = get_editor_theme_icon(SNAME("MemberMethod"));
|
2019-06-13 11:32:03 +02:00
|
|
|
break;
|
2022-03-26 16:48:43 +01:00
|
|
|
case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT:
|
2023-08-13 02:33:39 +02:00
|
|
|
tex = get_editor_theme_icon(SNAME("BoxMesh"));
|
2019-06-13 11:32:03 +02:00
|
|
|
break;
|
|
|
|
default:
|
2023-08-13 02:33:39 +02:00
|
|
|
tex = get_editor_theme_icon(SNAME("String"));
|
2019-06-13 11:32:03 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return tex;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 09:36:10 +02:00
|
|
|
void CodeTextEditor::_font_resize_timeout() {
|
|
|
|
if (_add_font_size(font_resize_val)) {
|
|
|
|
font_resize_val = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CodeTextEditor::_add_font_size(int p_delta) {
|
2021-07-17 23:22:52 +02:00
|
|
|
int old_size = text_editor->get_theme_font_size(SNAME("font_size"));
|
2020-09-18 09:36:10 +02:00
|
|
|
int new_size = CLAMP(old_size + p_delta, 8 * EDSCALE, 96 * EDSCALE);
|
|
|
|
|
|
|
|
if (new_size != old_size) {
|
|
|
|
EditorSettings::get_singleton()->set("interface/editor/code_font_size", new_size / EDSCALE);
|
2022-02-08 10:14:58 +01:00
|
|
|
text_editor->add_theme_font_size_override("font_size", new_size);
|
2020-09-18 09:36:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-10 10:38:12 +02:00
|
|
|
void CodeTextEditor::update_editor_settings() {
|
2021-08-15 19:14:46 +02:00
|
|
|
// Theme: Highlighting
|
|
|
|
completion_font_color = EDITOR_GET("text_editor/theme/highlighting/completion_font_color");
|
|
|
|
completion_string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
|
|
|
|
completion_comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
|
2023-02-05 10:01:01 +01:00
|
|
|
completion_doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
|
2021-08-15 19:14:46 +02:00
|
|
|
|
|
|
|
// Appearance: Caret
|
2022-10-18 16:43:37 +02:00
|
|
|
text_editor->set_caret_type((TextEdit::CaretType)EDITOR_GET("text_editor/appearance/caret/type").operator int());
|
|
|
|
text_editor->set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
|
|
|
|
text_editor->set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
|
|
|
|
text_editor->set_highlight_current_line(EDITOR_GET("text_editor/appearance/caret/highlight_current_line"));
|
|
|
|
text_editor->set_highlight_all_occurrences(EDITOR_GET("text_editor/appearance/caret/highlight_all_occurrences"));
|
2021-08-15 19:14:46 +02:00
|
|
|
|
|
|
|
// Appearance: Gutters
|
2022-10-18 16:43:37 +02:00
|
|
|
text_editor->set_draw_line_numbers(EDITOR_GET("text_editor/appearance/gutters/show_line_numbers"));
|
|
|
|
text_editor->set_line_numbers_zero_padded(EDITOR_GET("text_editor/appearance/gutters/line_numbers_zero_padded"));
|
2021-08-15 19:14:46 +02:00
|
|
|
|
|
|
|
// Appearance: Minimap
|
2022-10-18 16:43:37 +02:00
|
|
|
text_editor->set_draw_minimap(EDITOR_GET("text_editor/appearance/minimap/show_minimap"));
|
|
|
|
text_editor->set_minimap_width((int)EDITOR_GET("text_editor/appearance/minimap/minimap_width") * EDSCALE);
|
2021-08-15 19:14:46 +02:00
|
|
|
|
|
|
|
// Appearance: Lines
|
2022-10-18 16:43:37 +02:00
|
|
|
text_editor->set_line_folding_enabled(EDITOR_GET("text_editor/appearance/lines/code_folding"));
|
|
|
|
text_editor->set_draw_fold_gutter(EDITOR_GET("text_editor/appearance/lines/code_folding"));
|
|
|
|
text_editor->set_line_wrapping_mode((TextEdit::LineWrappingMode)EDITOR_GET("text_editor/appearance/lines/word_wrap").operator int());
|
2023-03-12 12:21:00 +01:00
|
|
|
text_editor->set_autowrap_mode((TextServer::AutowrapMode)EDITOR_GET("text_editor/appearance/lines/autowrap_mode").operator int());
|
2021-08-15 19:14:46 +02:00
|
|
|
|
|
|
|
// Appearance: Whitespace
|
2022-10-18 16:43:37 +02:00
|
|
|
text_editor->set_draw_tabs(EDITOR_GET("text_editor/appearance/whitespace/draw_tabs"));
|
|
|
|
text_editor->set_draw_spaces(EDITOR_GET("text_editor/appearance/whitespace/draw_spaces"));
|
2023-04-24 11:11:33 +02:00
|
|
|
text_editor->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));
|
2021-08-15 19:14:46 +02:00
|
|
|
|
|
|
|
// Behavior: Navigation
|
2022-10-18 16:43:37 +02:00
|
|
|
text_editor->set_scroll_past_end_of_file_enabled(EDITOR_GET("text_editor/behavior/navigation/scroll_past_end_of_file"));
|
|
|
|
text_editor->set_smooth_scroll_enabled(EDITOR_GET("text_editor/behavior/navigation/smooth_scrolling"));
|
|
|
|
text_editor->set_v_scroll_speed(EDITOR_GET("text_editor/behavior/navigation/v_scroll_speed"));
|
|
|
|
text_editor->set_drag_and_drop_selection_enabled(EDITOR_GET("text_editor/behavior/navigation/drag_and_drop_selection"));
|
2021-08-15 19:14:46 +02:00
|
|
|
|
|
|
|
// Behavior: indent
|
2022-10-18 16:43:37 +02:00
|
|
|
text_editor->set_indent_using_spaces(EDITOR_GET("text_editor/behavior/indent/type"));
|
|
|
|
text_editor->set_indent_size(EDITOR_GET("text_editor/behavior/indent/size"));
|
|
|
|
text_editor->set_auto_indent_enabled(EDITOR_GET("text_editor/behavior/indent/auto_indent"));
|
2021-08-15 19:14:46 +02:00
|
|
|
|
|
|
|
// Completion
|
2022-10-18 16:43:37 +02:00
|
|
|
text_editor->set_auto_brace_completion_enabled(EDITOR_GET("text_editor/completion/auto_brace_complete"));
|
2021-06-30 19:03:32 +02:00
|
|
|
|
2021-08-15 19:14:46 +02:00
|
|
|
// Appearance: Guidelines
|
2022-10-18 16:43:37 +02:00
|
|
|
if (EDITOR_GET("text_editor/appearance/guidelines/show_line_length_guidelines")) {
|
2021-06-30 19:03:32 +02:00
|
|
|
TypedArray<int> guideline_cols;
|
2022-10-18 16:43:37 +02:00
|
|
|
guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_hard_column"));
|
|
|
|
if (EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column") != guideline_cols[0]) {
|
|
|
|
guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column"));
|
2021-06-30 19:03:32 +02:00
|
|
|
}
|
|
|
|
text_editor->set_line_length_guidelines(guideline_cols);
|
2022-08-02 07:38:23 +02:00
|
|
|
} else {
|
|
|
|
text_editor->set_line_length_guidelines(TypedArray<int>());
|
2021-06-30 19:03:32 +02:00
|
|
|
}
|
2016-10-10 10:38:12 +02:00
|
|
|
}
|
|
|
|
|
2021-06-03 01:05:41 +02:00
|
|
|
void CodeTextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {
|
|
|
|
if (find_replace_bar) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
find_replace_bar = p_bar;
|
|
|
|
find_replace_bar->set_text_edit(this);
|
|
|
|
find_replace_bar->connect("error", callable_mp(error, &Label::set_text));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::remove_find_replace_bar() {
|
|
|
|
if (!find_replace_bar) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
find_replace_bar->disconnect("error", callable_mp(error, &Label::set_text));
|
|
|
|
find_replace_bar = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-26 00:49:35 +02:00
|
|
|
void CodeTextEditor::trim_trailing_whitespace() {
|
2023-03-31 08:39:44 +02:00
|
|
|
bool trimmed_whitespace = false;
|
2018-05-26 00:49:35 +02:00
|
|
|
for (int i = 0; i < text_editor->get_line_count(); i++) {
|
|
|
|
String line = text_editor->get_line(i);
|
|
|
|
if (line.ends_with(" ") || line.ends_with("\t")) {
|
2023-03-31 08:39:44 +02:00
|
|
|
if (!trimmed_whitespace) {
|
2018-05-26 00:49:35 +02:00
|
|
|
text_editor->begin_complex_operation();
|
2023-03-31 08:39:44 +02:00
|
|
|
trimmed_whitespace = true;
|
2018-05-26 00:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int end = 0;
|
|
|
|
for (int j = line.length() - 1; j > -1; j--) {
|
|
|
|
if (line[j] != ' ' && line[j] != '\t') {
|
|
|
|
end = j + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
text_editor->set_line(i, line.substr(0, end));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 08:39:44 +02:00
|
|
|
if (trimmed_whitespace) {
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->merge_overlapping_carets();
|
2018-05-26 00:49:35 +02:00
|
|
|
text_editor->end_complex_operation();
|
2022-08-13 23:21:24 +02:00
|
|
|
text_editor->queue_redraw();
|
2018-05-26 00:49:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 23:27:32 +02:00
|
|
|
void CodeTextEditor::insert_final_newline() {
|
|
|
|
int final_line = text_editor->get_line_count() - 1;
|
|
|
|
|
|
|
|
String line = text_editor->get_line(final_line);
|
|
|
|
|
2023-03-08 23:00:27 +01:00
|
|
|
// Length 0 means it's already an empty line, no need to add a newline.
|
2019-05-28 23:27:32 +02:00
|
|
|
if (line.length() > 0 && !line.ends_with("\n")) {
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
|
|
|
|
line += "\n";
|
|
|
|
text_editor->set_line(final_line, line);
|
|
|
|
|
|
|
|
text_editor->end_complex_operation();
|
2022-08-13 23:21:24 +02:00
|
|
|
text_editor->queue_redraw();
|
2019-05-28 23:27:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-26 00:49:35 +02:00
|
|
|
void CodeTextEditor::convert_case(CaseStyle p_case) {
|
2021-07-09 13:42:55 +02:00
|
|
|
if (!text_editor->has_selection()) {
|
2018-05-26 00:49:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
|
2022-06-08 23:41:38 +02:00
|
|
|
Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
|
|
|
|
for (const int &c : caret_edit_order) {
|
|
|
|
if (!text_editor->has_selection(c)) {
|
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-26 00:49:35 +02:00
|
|
|
|
2022-06-08 23:41:38 +02:00
|
|
|
int begin = text_editor->get_selection_from_line(c);
|
|
|
|
int end = text_editor->get_selection_to_line(c);
|
|
|
|
int begin_col = text_editor->get_selection_from_column(c);
|
|
|
|
int end_col = text_editor->get_selection_to_column(c);
|
2018-05-26 00:49:35 +02:00
|
|
|
|
2022-06-08 23:41:38 +02:00
|
|
|
for (int i = begin; i <= end; i++) {
|
|
|
|
int len = text_editor->get_line(i).length();
|
|
|
|
if (i == end) {
|
|
|
|
len = end_col;
|
|
|
|
}
|
|
|
|
if (i == begin) {
|
|
|
|
len -= begin_col;
|
|
|
|
}
|
|
|
|
String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len);
|
|
|
|
|
|
|
|
switch (p_case) {
|
|
|
|
case UPPER: {
|
|
|
|
new_line = new_line.to_upper();
|
|
|
|
} break;
|
|
|
|
case LOWER: {
|
|
|
|
new_line = new_line.to_lower();
|
|
|
|
} break;
|
|
|
|
case CAPITALIZE: {
|
|
|
|
new_line = new_line.capitalize();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == begin) {
|
|
|
|
new_line = text_editor->get_line(i).left(begin_col) + new_line;
|
|
|
|
}
|
|
|
|
if (i == end) {
|
|
|
|
new_line = new_line + text_editor->get_line(i).substr(end_col);
|
|
|
|
}
|
|
|
|
text_editor->set_line(i, new_line);
|
2018-05-26 00:49:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
text_editor->end_complex_operation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::move_lines_up() {
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
|
2022-06-08 23:41:38 +02:00
|
|
|
Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
|
2022-12-05 08:43:27 +01:00
|
|
|
|
2023-03-08 23:00:27 +01:00
|
|
|
// Lists of carets representing each group.
|
2022-12-05 08:43:27 +01:00
|
|
|
Vector<Vector<int>> caret_groups;
|
|
|
|
Vector<Pair<int, int>> group_borders;
|
|
|
|
|
2023-03-08 23:00:27 +01:00
|
|
|
// Search for groups of carets and their selections residing on the same lines.
|
2022-06-08 23:41:38 +02:00
|
|
|
for (int i = 0; i < caret_edit_order.size(); i++) {
|
|
|
|
int c = caret_edit_order[i];
|
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
Vector<int> new_group{ c };
|
|
|
|
Pair<int, int> group_border;
|
2023-02-01 14:35:35 +01:00
|
|
|
group_border.first = _get_affected_lines_from(c);
|
|
|
|
group_border.second = _get_affected_lines_to(c);
|
2022-06-08 23:41:38 +02:00
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
for (int j = i; j < caret_edit_order.size() - 1; j++) {
|
|
|
|
int c_current = caret_edit_order[j];
|
|
|
|
int c_next = caret_edit_order[j + 1];
|
2022-06-08 23:41:38 +02:00
|
|
|
|
2023-02-01 14:35:35 +01:00
|
|
|
int next_start_pos = _get_affected_lines_from(c_next);
|
|
|
|
int next_end_pos = _get_affected_lines_to(c_next);
|
2022-12-05 08:43:27 +01:00
|
|
|
|
|
|
|
int current_start_pos = text_editor->has_selection(c_current) ? text_editor->get_selection_from_line(c_current) : text_editor->get_caret_line(c_current);
|
|
|
|
|
|
|
|
i = j;
|
|
|
|
if (next_end_pos != current_start_pos && next_end_pos + 1 != current_start_pos) {
|
2022-06-08 23:41:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-12-05 08:43:27 +01:00
|
|
|
group_border.first = next_start_pos;
|
|
|
|
new_group.push_back(c_next);
|
2023-03-08 23:00:27 +01:00
|
|
|
// If the last caret is added to the current group there is no need to process it again.
|
2022-12-05 08:43:27 +01:00
|
|
|
if (j + 1 == caret_edit_order.size() - 1) {
|
|
|
|
i++;
|
2022-06-08 23:41:38 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-05 08:43:27 +01:00
|
|
|
group_borders.push_back(group_border);
|
|
|
|
caret_groups.push_back(new_group);
|
|
|
|
}
|
2022-06-08 23:41:38 +02:00
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
for (int i = group_borders.size() - 1; i >= 0; i--) {
|
|
|
|
if (group_borders[i].first - 1 < 0) {
|
2022-06-08 23:41:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:00:27 +01:00
|
|
|
// If the group starts overlapping with the upper group don't move it.
|
2022-12-05 08:43:27 +01:00
|
|
|
if (i < group_borders.size() - 1 && group_borders[i].first - 1 <= group_borders[i + 1].second) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:00:27 +01:00
|
|
|
// We have to remember caret positions and selections prior to line swapping.
|
2022-12-05 08:43:27 +01:00
|
|
|
Vector<Vector<int>> caret_group_parameters;
|
|
|
|
|
|
|
|
for (int j = 0; j < caret_groups[i].size(); j++) {
|
|
|
|
int c = caret_groups[i][j];
|
|
|
|
int cursor_line = text_editor->get_caret_line(c);
|
|
|
|
int cursor_column = text_editor->get_caret_column(c);
|
|
|
|
|
|
|
|
if (!text_editor->has_selection(c)) {
|
|
|
|
caret_group_parameters.push_back(Vector<int>{ -1, -1, -1, -1, cursor_line, cursor_column });
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-08 23:41:38 +02:00
|
|
|
int from_line = text_editor->get_selection_from_line(c);
|
|
|
|
int from_col = text_editor->get_selection_from_column(c);
|
|
|
|
int to_line = text_editor->get_selection_to_line(c);
|
|
|
|
int to_column = text_editor->get_selection_to_column(c);
|
2022-12-05 08:43:27 +01:00
|
|
|
caret_group_parameters.push_back(Vector<int>{ from_line, from_col, to_line, to_column, cursor_line, cursor_column });
|
|
|
|
}
|
2022-06-08 23:41:38 +02:00
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
for (int line_id = group_borders[i].first; line_id <= group_borders[i].second; line_id++) {
|
|
|
|
text_editor->unfold_line(line_id);
|
|
|
|
text_editor->unfold_line(line_id - 1);
|
2022-06-08 23:41:38 +02:00
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
text_editor->swap_lines(line_id - 1, line_id);
|
|
|
|
}
|
2022-06-08 23:41:38 +02:00
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
for (int j = 0; j < caret_groups[i].size(); j++) {
|
|
|
|
int c = caret_groups[i][j];
|
|
|
|
Vector<int> caret_parameters = caret_group_parameters[j];
|
|
|
|
text_editor->set_caret_line(caret_parameters[4] - 1, c == 0, true, 0, c);
|
|
|
|
text_editor->set_caret_column(caret_parameters[5], c == 0, c);
|
2018-05-26 00:49:35 +02:00
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
if (caret_parameters[0] >= 0) {
|
|
|
|
text_editor->select(caret_parameters[0] - 1, caret_parameters[1], caret_parameters[2] - 1, caret_parameters[3], c);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-26 00:49:35 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-05 08:43:27 +01:00
|
|
|
|
2018-05-26 00:49:35 +02:00
|
|
|
text_editor->end_complex_operation();
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->merge_overlapping_carets();
|
2022-08-13 23:21:24 +02:00
|
|
|
text_editor->queue_redraw();
|
2018-05-26 00:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::move_lines_down() {
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
|
2022-06-08 23:41:38 +02:00
|
|
|
Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
|
2022-12-05 08:43:27 +01:00
|
|
|
|
2023-03-08 23:00:27 +01:00
|
|
|
// Lists of carets representing each group.
|
2022-12-05 08:43:27 +01:00
|
|
|
Vector<Vector<int>> caret_groups;
|
|
|
|
Vector<Pair<int, int>> group_borders;
|
2023-02-01 14:35:35 +01:00
|
|
|
Vector<int> group_border_ends;
|
2023-03-08 23:00:27 +01:00
|
|
|
// Search for groups of carets and their selections residing on the same lines.
|
2022-06-08 23:41:38 +02:00
|
|
|
for (int i = 0; i < caret_edit_order.size(); i++) {
|
|
|
|
int c = caret_edit_order[i];
|
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
Vector<int> new_group{ c };
|
|
|
|
Pair<int, int> group_border;
|
2023-02-01 14:35:35 +01:00
|
|
|
group_border.first = _get_affected_lines_from(c);
|
|
|
|
group_border.second = _get_affected_lines_to(c);
|
2022-06-08 23:41:38 +02:00
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
for (int j = i; j < caret_edit_order.size() - 1; j++) {
|
|
|
|
int c_current = caret_edit_order[j];
|
|
|
|
int c_next = caret_edit_order[j + 1];
|
2022-06-08 23:41:38 +02:00
|
|
|
|
2023-02-01 14:35:35 +01:00
|
|
|
int next_start_pos = _get_affected_lines_from(c_next);
|
|
|
|
int next_end_pos = _get_affected_lines_to(c_next);
|
2022-12-05 08:43:27 +01:00
|
|
|
|
|
|
|
int current_start_pos = text_editor->has_selection(c_current) ? text_editor->get_selection_from_line(c_current) : text_editor->get_caret_line(c_current);
|
|
|
|
|
|
|
|
i = j;
|
|
|
|
if (next_end_pos == current_start_pos || next_end_pos + 1 == current_start_pos) {
|
|
|
|
group_border.first = next_start_pos;
|
|
|
|
new_group.push_back(c_next);
|
2023-03-08 23:00:27 +01:00
|
|
|
// If the last caret is added to the current group there is no need to process it again.
|
2022-12-05 08:43:27 +01:00
|
|
|
if (j + 1 == caret_edit_order.size() - 1) {
|
|
|
|
i++;
|
2022-06-08 23:41:38 +02:00
|
|
|
}
|
2022-12-05 08:43:27 +01:00
|
|
|
} else {
|
2022-06-08 23:41:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-12-05 08:43:27 +01:00
|
|
|
group_borders.push_back(group_border);
|
2023-02-01 14:35:35 +01:00
|
|
|
group_border_ends.push_back(text_editor->has_selection(c) ? text_editor->get_selection_to_line(c) : text_editor->get_caret_line(c));
|
2022-12-05 08:43:27 +01:00
|
|
|
caret_groups.push_back(new_group);
|
|
|
|
}
|
2022-06-08 23:41:38 +02:00
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
for (int i = 0; i < group_borders.size(); i++) {
|
2023-02-01 14:35:35 +01:00
|
|
|
if (group_border_ends[i] + 1 > text_editor->get_line_count() - 1) {
|
2022-06-08 23:41:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:00:27 +01:00
|
|
|
// If the group starts overlapping with the upper group don't move it.
|
2023-02-01 14:35:35 +01:00
|
|
|
if (i > 0 && group_border_ends[i] + 1 >= group_borders[i - 1].first) {
|
2022-12-05 08:43:27 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-08 23:41:38 +02:00
|
|
|
|
2023-03-08 23:00:27 +01:00
|
|
|
// We have to remember caret positions and selections prior to line swapping.
|
2022-12-05 08:43:27 +01:00
|
|
|
Vector<Vector<int>> caret_group_parameters;
|
2018-05-26 00:49:35 +02:00
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
for (int j = 0; j < caret_groups[i].size(); j++) {
|
|
|
|
int c = caret_groups[i][j];
|
|
|
|
int cursor_line = text_editor->get_caret_line(c);
|
|
|
|
int cursor_column = text_editor->get_caret_column(c);
|
|
|
|
|
2023-03-08 23:00:27 +01:00
|
|
|
if (!text_editor->has_selection(c)) {
|
2022-12-05 08:43:27 +01:00
|
|
|
caret_group_parameters.push_back(Vector<int>{ -1, -1, -1, -1, cursor_line, cursor_column });
|
2023-03-08 23:00:27 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2023-03-08 23:00:27 +01:00
|
|
|
int from_line = text_editor->get_selection_from_line(c);
|
|
|
|
int from_col = text_editor->get_selection_from_column(c);
|
|
|
|
int to_line = text_editor->get_selection_to_line(c);
|
|
|
|
int to_column = text_editor->get_selection_to_column(c);
|
|
|
|
caret_group_parameters.push_back(Vector<int>{ from_line, from_col, to_line, to_column, cursor_line, cursor_column });
|
2022-12-05 08:43:27 +01:00
|
|
|
}
|
2018-05-26 00:49:35 +02:00
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
for (int line_id = group_borders[i].second; line_id >= group_borders[i].first; line_id--) {
|
2018-05-26 00:49:35 +02:00
|
|
|
text_editor->unfold_line(line_id);
|
2022-12-05 08:43:27 +01:00
|
|
|
text_editor->unfold_line(line_id + 1);
|
2018-05-26 00:49:35 +02:00
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
text_editor->swap_lines(line_id + 1, line_id);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-26 00:49:35 +02:00
|
|
|
|
2022-12-05 08:43:27 +01:00
|
|
|
for (int j = 0; j < caret_groups[i].size(); j++) {
|
|
|
|
int c = caret_groups[i][j];
|
|
|
|
Vector<int> caret_parameters = caret_group_parameters[j];
|
|
|
|
text_editor->set_caret_line(caret_parameters[4] + 1, c == 0, true, 0, c);
|
|
|
|
text_editor->set_caret_column(caret_parameters[5], c == 0, c);
|
|
|
|
|
|
|
|
if (caret_parameters[0] >= 0) {
|
|
|
|
text_editor->select(caret_parameters[0] + 1, caret_parameters[1], caret_parameters[2] + 1, caret_parameters[3], c);
|
|
|
|
}
|
|
|
|
}
|
2018-05-26 00:49:35 +02:00
|
|
|
}
|
2022-06-08 23:41:38 +02:00
|
|
|
|
|
|
|
text_editor->merge_overlapping_carets();
|
2018-05-26 00:49:35 +02:00
|
|
|
text_editor->end_complex_operation();
|
2022-08-13 23:21:24 +02:00
|
|
|
text_editor->queue_redraw();
|
2018-05-26 00:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::delete_lines() {
|
|
|
|
text_editor->begin_complex_operation();
|
2018-10-13 19:30:23 +02:00
|
|
|
|
2022-06-08 23:41:38 +02:00
|
|
|
Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
|
2023-02-01 14:35:35 +01:00
|
|
|
Vector<int> lines;
|
|
|
|
int last_line = INT_MAX;
|
|
|
|
for (const int &c : caret_edit_order) {
|
|
|
|
for (int line = _get_affected_lines_to(c); line >= _get_affected_lines_from(c); line--) {
|
|
|
|
if (line >= last_line) {
|
2022-06-08 23:41:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-01 14:35:35 +01:00
|
|
|
last_line = line;
|
|
|
|
lines.append(line);
|
2018-05-26 00:49:35 +02:00
|
|
|
}
|
2023-02-01 14:35:35 +01:00
|
|
|
}
|
2022-06-08 23:41:38 +02:00
|
|
|
|
2023-02-01 14:35:35 +01:00
|
|
|
for (const int &line : lines) {
|
|
|
|
if (line != text_editor->get_line_count() - 1) {
|
|
|
|
text_editor->remove_text(line, 0, line + 1, 0);
|
|
|
|
} else {
|
|
|
|
text_editor->remove_text(line - 1, text_editor->get_line(line - 1).length(), line, text_editor->get_line(line).length());
|
2022-06-08 23:41:38 +02:00
|
|
|
}
|
2023-02-01 14:35:35 +01:00
|
|
|
// Readjust carets.
|
|
|
|
int new_line = MIN(line, text_editor->get_line_count() - 1);
|
|
|
|
text_editor->unfold_line(new_line);
|
|
|
|
for (const int &c : caret_edit_order) {
|
|
|
|
if (text_editor->get_caret_line(c) == line || (text_editor->get_caret_line(c) == line + 1 && text_editor->get_caret_column(c) == 0)) {
|
|
|
|
text_editor->deselect(c);
|
|
|
|
text_editor->set_caret_line(new_line, c == 0, true, 0, c);
|
|
|
|
continue;
|
2022-06-08 23:41:38 +02:00
|
|
|
}
|
2023-02-01 14:35:35 +01:00
|
|
|
if (text_editor->get_caret_line(c) > line) {
|
|
|
|
text_editor->set_caret_line(text_editor->get_caret_line(c) - 1, c == 0, true, 0, c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2018-05-26 00:49:35 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->merge_overlapping_carets();
|
2018-05-26 00:49:35 +02:00
|
|
|
text_editor->end_complex_operation();
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:09:07 +02:00
|
|
|
void CodeTextEditor::duplicate_selection() {
|
2018-05-26 00:49:35 +02:00
|
|
|
text_editor->begin_complex_operation();
|
2018-12-24 01:14:05 +01:00
|
|
|
|
2022-06-08 23:41:38 +02:00
|
|
|
Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
|
|
|
|
for (const int &c : caret_edit_order) {
|
|
|
|
const int cursor_column = text_editor->get_caret_column(c);
|
|
|
|
int from_line = text_editor->get_caret_line(c);
|
|
|
|
int to_line = text_editor->get_caret_line(c);
|
|
|
|
int from_column = 0;
|
|
|
|
int to_column = 0;
|
|
|
|
int cursor_new_line = to_line + 1;
|
|
|
|
int cursor_new_column = text_editor->get_caret_column(c);
|
|
|
|
String new_text = "\n" + text_editor->get_line(from_line);
|
|
|
|
bool selection_active = false;
|
|
|
|
|
|
|
|
text_editor->set_caret_column(text_editor->get_line(from_line).length(), c == 0, c);
|
|
|
|
if (text_editor->has_selection(c)) {
|
|
|
|
from_column = text_editor->get_selection_from_column(c);
|
|
|
|
to_column = text_editor->get_selection_to_column(c);
|
|
|
|
|
|
|
|
from_line = text_editor->get_selection_from_line(c);
|
|
|
|
to_line = text_editor->get_selection_to_line(c);
|
|
|
|
cursor_new_line = to_line + text_editor->get_caret_line(c) - from_line;
|
|
|
|
cursor_new_column = to_column == cursor_column ? 2 * to_column - from_column : to_column;
|
|
|
|
new_text = text_editor->get_selected_text(c);
|
|
|
|
selection_active = true;
|
|
|
|
|
|
|
|
text_editor->set_caret_line(to_line, c == 0, true, 0, c);
|
|
|
|
text_editor->set_caret_column(to_column, c == 0, c);
|
|
|
|
}
|
2018-05-26 00:49:35 +02:00
|
|
|
|
2022-06-08 23:41:38 +02:00
|
|
|
for (int i = from_line; i <= to_line; i++) {
|
|
|
|
text_editor->unfold_line(i);
|
|
|
|
}
|
|
|
|
text_editor->deselect(c);
|
|
|
|
text_editor->insert_text_at_caret(new_text, c);
|
|
|
|
text_editor->set_caret_line(cursor_new_line, c == 0, true, 0, c);
|
|
|
|
text_editor->set_caret_column(cursor_new_column, c == 0, c);
|
|
|
|
if (selection_active) {
|
|
|
|
text_editor->select(to_line, to_column, 2 * to_line - from_line, to_line == from_line ? 2 * to_column - from_column : to_column, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
text_editor->merge_overlapping_carets();
|
2018-05-26 00:49:35 +02:00
|
|
|
text_editor->end_complex_operation();
|
2022-08-13 23:21:24 +02:00
|
|
|
text_editor->queue_redraw();
|
2018-05-26 00:49:35 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 20:17:44 +01:00
|
|
|
void CodeTextEditor::toggle_inline_comment(const String &delimiter) {
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
|
2022-06-08 23:41:38 +02:00
|
|
|
Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
|
2023-02-01 14:35:35 +01:00
|
|
|
caret_edit_order.reverse();
|
|
|
|
int last_line = -1;
|
2023-03-18 14:37:08 +01:00
|
|
|
int folded_to = 0;
|
2023-02-01 14:35:35 +01:00
|
|
|
for (const int &c1 : caret_edit_order) {
|
|
|
|
int from = _get_affected_lines_from(c1);
|
2023-03-18 14:37:08 +01:00
|
|
|
from += from == last_line ? 1 + folded_to : 0;
|
2023-02-01 14:35:35 +01:00
|
|
|
int to = _get_affected_lines_to(c1);
|
|
|
|
last_line = to;
|
2023-03-18 14:37:08 +01:00
|
|
|
// If last line is folded, extends to the end of the folded section
|
|
|
|
if (text_editor->is_line_folded(to)) {
|
|
|
|
folded_to = text_editor->get_next_visible_line_offset_from(to + 1, 1) - 1;
|
|
|
|
to += folded_to;
|
|
|
|
}
|
2023-02-01 14:35:35 +01:00
|
|
|
// Check first if there's any uncommented lines in selection.
|
|
|
|
bool is_commented = true;
|
2023-09-09 15:09:58 +02:00
|
|
|
bool is_all_empty = true;
|
2023-02-01 14:35:35 +01:00
|
|
|
for (int line = from; line <= to; line++) {
|
2021-10-29 20:02:54 +02:00
|
|
|
// `+ delimiter.length()` here because comment delimiter is not actually `in comment` so we check first character after it
|
|
|
|
int delimiter_idx = text_editor->is_in_comment(line, text_editor->get_first_non_whitespace_column(line) + delimiter.length());
|
2023-09-09 15:09:58 +02:00
|
|
|
// Empty lines should not be counted.
|
|
|
|
bool is_empty = text_editor->get_line(line).strip_edges().is_empty();
|
|
|
|
is_all_empty = is_all_empty && is_empty;
|
2023-10-15 13:28:43 +02:00
|
|
|
// `.left(1)` here because get_delimiter_start_key will return `##` instead of `#` when there is multiple comment delimiter in a line.
|
|
|
|
if (!is_empty && (delimiter_idx == -1 || text_editor->get_delimiter_start_key(delimiter_idx).left(1) != delimiter)) {
|
2023-02-01 14:35:35 +01:00
|
|
|
is_commented = false;
|
|
|
|
break;
|
2022-06-08 23:41:38 +02:00
|
|
|
}
|
2023-02-01 14:35:35 +01:00
|
|
|
}
|
2023-09-09 15:09:58 +02:00
|
|
|
|
|
|
|
// Special case for commenting empty lines, treat it/them as uncommented lines.
|
|
|
|
is_commented = is_commented && !is_all_empty;
|
|
|
|
|
2023-02-01 14:35:35 +01:00
|
|
|
// Caret positions need to be saved since they could be moved at the eol.
|
|
|
|
Vector<int> caret_cols;
|
|
|
|
Vector<int> selection_to_cols;
|
|
|
|
for (const int &c2 : caret_edit_order) {
|
|
|
|
if (text_editor->get_caret_line(c2) >= from && text_editor->get_caret_line(c2) <= to) {
|
|
|
|
caret_cols.append(text_editor->get_caret_column(c2));
|
2019-02-04 20:17:44 +01:00
|
|
|
}
|
2023-02-01 14:35:35 +01:00
|
|
|
if (text_editor->has_selection(c2) && text_editor->get_selection_to_line(c2) >= from && text_editor->get_selection_to_line(c2) <= to) {
|
|
|
|
selection_to_cols.append(text_editor->get_selection_to_column(c2));
|
2019-02-04 20:17:44 +01:00
|
|
|
}
|
2023-02-01 14:35:35 +01:00
|
|
|
}
|
2019-02-04 20:17:44 +01:00
|
|
|
|
2023-02-01 14:35:35 +01:00
|
|
|
// Comment/uncomment.
|
|
|
|
for (int line = from; line <= to; line++) {
|
|
|
|
String line_text = text_editor->get_line(line);
|
2023-09-09 15:09:58 +02:00
|
|
|
if (is_all_empty) {
|
2023-02-01 14:35:35 +01:00
|
|
|
text_editor->set_line(line, delimiter);
|
|
|
|
continue;
|
2022-06-08 23:41:38 +02:00
|
|
|
}
|
2023-09-09 15:09:58 +02:00
|
|
|
|
2023-02-01 14:35:35 +01:00
|
|
|
if (is_commented) {
|
2021-10-29 20:02:54 +02:00
|
|
|
text_editor->set_line(line, line_text.replace_first(delimiter, ""));
|
|
|
|
} else {
|
|
|
|
text_editor->set_line(line, line_text.insert(text_editor->get_first_non_whitespace_column(line), delimiter));
|
2022-06-08 23:41:38 +02:00
|
|
|
}
|
2023-02-01 14:35:35 +01:00
|
|
|
}
|
2019-02-04 20:17:44 +01:00
|
|
|
|
2023-02-01 14:35:35 +01:00
|
|
|
// Readjust carets and selections.
|
|
|
|
int caret_i = 0;
|
|
|
|
int selection_i = 0;
|
|
|
|
int offset = (is_commented ? -1 : 1) * delimiter.length();
|
|
|
|
for (const int &c2 : caret_edit_order) {
|
2023-02-16 15:17:24 +01:00
|
|
|
bool is_line_selection = text_editor->has_selection(c2) && text_editor->get_selection_from_line(c2) < text_editor->get_selection_to_line(c2);
|
2023-02-01 14:35:35 +01:00
|
|
|
if (text_editor->get_caret_line(c2) >= from && text_editor->get_caret_line(c2) <= to) {
|
|
|
|
int caret_col = caret_cols[caret_i++];
|
2023-02-16 15:17:24 +01:00
|
|
|
caret_col += (is_line_selection && caret_col == 0) ? 0 : offset;
|
2023-02-01 14:35:35 +01:00
|
|
|
text_editor->set_caret_column(caret_col, c2 == 0, c2);
|
2022-06-08 23:41:38 +02:00
|
|
|
}
|
2023-02-01 14:35:35 +01:00
|
|
|
if (text_editor->has_selection(c2) && text_editor->get_selection_to_line(c2) >= from && text_editor->get_selection_to_line(c2) <= to) {
|
|
|
|
int from_col = text_editor->get_selection_from_column(c2);
|
2023-02-16 15:17:24 +01:00
|
|
|
from_col += (is_line_selection && from_col == 0) ? 0 : offset;
|
2023-02-01 14:35:35 +01:00
|
|
|
int to_col = selection_to_cols[selection_i++];
|
|
|
|
to_col += (to_col == 0) ? 0 : offset;
|
|
|
|
text_editor->select(
|
|
|
|
text_editor->get_selection_from_line(c2), from_col,
|
|
|
|
text_editor->get_selection_to_line(c2), to_col, c2);
|
2022-06-08 23:41:38 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-04 20:17:44 +01:00
|
|
|
}
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->merge_overlapping_carets();
|
2019-02-04 20:17:44 +01:00
|
|
|
text_editor->end_complex_operation();
|
2022-08-13 23:21:24 +02:00
|
|
|
text_editor->queue_redraw();
|
2019-02-04 20:17:44 +01:00
|
|
|
}
|
|
|
|
|
2018-05-26 00:49:35 +02:00
|
|
|
void CodeTextEditor::goto_line(int p_line) {
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->remove_secondary_carets();
|
2018-05-26 00:49:35 +02:00
|
|
|
text_editor->deselect();
|
|
|
|
text_editor->unfold_line(p_line);
|
2021-08-16 23:12:46 +02:00
|
|
|
text_editor->call_deferred(SNAME("set_caret_line"), p_line);
|
2018-05-26 00:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->remove_secondary_carets();
|
2018-05-26 00:49:35 +02:00
|
|
|
text_editor->unfold_line(p_line);
|
2021-08-16 23:12:46 +02:00
|
|
|
text_editor->call_deferred(SNAME("set_caret_line"), p_line);
|
|
|
|
text_editor->call_deferred(SNAME("set_caret_column"), p_begin);
|
2018-05-26 00:49:35 +02:00
|
|
|
text_editor->select(p_line, p_begin, p_line, p_end);
|
|
|
|
}
|
|
|
|
|
2019-01-04 13:09:01 +01:00
|
|
|
void CodeTextEditor::goto_line_centered(int p_line) {
|
|
|
|
goto_line(p_line);
|
2021-07-08 19:35:56 +02:00
|
|
|
text_editor->call_deferred(SNAME("center_viewport_to_caret"));
|
2019-01-04 13:09:01 +01:00
|
|
|
}
|
|
|
|
|
2019-04-22 18:20:27 +02:00
|
|
|
void CodeTextEditor::set_executing_line(int p_line) {
|
2020-07-26 16:57:23 +02:00
|
|
|
text_editor->set_line_as_executing(p_line, true);
|
2019-04-22 18:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::clear_executing_line() {
|
2020-07-26 16:57:23 +02:00
|
|
|
text_editor->clear_executing_lines();
|
2019-04-22 18:20:27 +02:00
|
|
|
}
|
|
|
|
|
2018-05-26 00:49:35 +02:00
|
|
|
Variant CodeTextEditor::get_edit_state() {
|
|
|
|
Dictionary state;
|
2022-09-23 14:46:28 +02:00
|
|
|
state.merge(get_navigation_state());
|
2019-04-13 13:43:35 +02:00
|
|
|
|
|
|
|
state["folded_lines"] = text_editor->get_folded_lines();
|
2020-07-26 16:57:23 +02:00
|
|
|
state["breakpoints"] = text_editor->get_breakpointed_lines();
|
|
|
|
state["bookmarks"] = text_editor->get_bookmarked_lines();
|
2019-04-13 13:43:35 +02:00
|
|
|
|
2020-05-03 18:08:15 +02:00
|
|
|
Ref<EditorSyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighter();
|
|
|
|
state["syntax_highlighter"] = syntax_highlighter->_get_name();
|
2019-04-13 13:43:35 +02:00
|
|
|
|
2018-05-26 00:49:35 +02:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::set_edit_state(const Variant &p_state) {
|
|
|
|
Dictionary state = p_state;
|
2019-04-13 13:43:35 +02:00
|
|
|
|
|
|
|
/* update the row first as it sets the column to 0 */
|
2021-07-08 19:35:56 +02:00
|
|
|
text_editor->set_caret_line(state["row"]);
|
|
|
|
text_editor->set_caret_column(state["column"]);
|
2018-05-26 00:49:35 +02:00
|
|
|
text_editor->set_v_scroll(state["scroll_position"]);
|
2019-04-13 13:43:35 +02:00
|
|
|
text_editor->set_h_scroll(state["h_scroll_position"]);
|
|
|
|
|
2022-09-23 14:46:28 +02:00
|
|
|
if (state.get("selection", false)) {
|
2019-04-13 13:43:35 +02:00
|
|
|
text_editor->select(state["selection_from_line"], state["selection_from_column"], state["selection_to_line"], state["selection_to_column"]);
|
2022-09-23 14:46:28 +02:00
|
|
|
} else {
|
|
|
|
text_editor->deselect();
|
2019-04-13 13:43:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state.has("folded_lines")) {
|
|
|
|
Vector<int> folded_lines = state["folded_lines"];
|
|
|
|
for (int i = 0; i < folded_lines.size(); i++) {
|
|
|
|
text_editor->fold_line(folded_lines[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.has("breakpoints")) {
|
|
|
|
Array breakpoints = state["breakpoints"];
|
|
|
|
for (int i = 0; i < breakpoints.size(); i++) {
|
|
|
|
text_editor->set_line_as_breakpoint(breakpoints[i], true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 01:51:25 +02:00
|
|
|
if (state.has("bookmarks")) {
|
|
|
|
Array bookmarks = state["bookmarks"];
|
|
|
|
for (int i = 0; i < bookmarks.size(); i++) {
|
2020-07-26 16:57:23 +02:00
|
|
|
text_editor->set_line_as_bookmarked(bookmarks[i], true);
|
2019-04-20 01:51:25 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-26 00:49:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 14:46:28 +02:00
|
|
|
Variant CodeTextEditor::get_navigation_state() {
|
|
|
|
Dictionary state;
|
|
|
|
|
|
|
|
state["scroll_position"] = text_editor->get_v_scroll();
|
|
|
|
state["h_scroll_position"] = text_editor->get_h_scroll();
|
|
|
|
state["column"] = text_editor->get_caret_column();
|
|
|
|
state["row"] = text_editor->get_caret_line();
|
|
|
|
|
|
|
|
state["selection"] = get_text_editor()->has_selection();
|
|
|
|
if (get_text_editor()->has_selection()) {
|
|
|
|
state["selection_from_line"] = text_editor->get_selection_from_line();
|
|
|
|
state["selection_from_column"] = text_editor->get_selection_from_column();
|
|
|
|
state["selection_to_line"] = text_editor->get_selection_to_line();
|
|
|
|
state["selection_to_column"] = text_editor->get_selection_to_column();
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void CodeTextEditor::set_error(const String &p_error) {
|
2017-08-26 14:42:12 +02:00
|
|
|
error->set_text(p_error);
|
2021-12-09 10:42:46 +01:00
|
|
|
if (!p_error.is_empty()) {
|
2018-12-21 09:05:29 +01:00
|
|
|
error->set_default_cursor_shape(CURSOR_POINTING_HAND);
|
|
|
|
} else {
|
|
|
|
error->set_default_cursor_shape(CURSOR_ARROW);
|
|
|
|
}
|
2018-11-21 11:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::set_error_pos(int p_line, int p_column) {
|
|
|
|
error_line = p_line;
|
|
|
|
error_column = p_column;
|
|
|
|
}
|
|
|
|
|
2022-03-08 11:39:16 +01:00
|
|
|
Point2i CodeTextEditor::get_error_pos() const {
|
|
|
|
return Point2i(error_line, error_column);
|
|
|
|
}
|
|
|
|
|
2018-12-21 09:05:29 +01:00
|
|
|
void CodeTextEditor::goto_error() {
|
2021-12-09 10:42:46 +01:00
|
|
|
if (!error->get_text().is_empty()) {
|
2022-01-05 06:55:33 +01:00
|
|
|
if (text_editor->get_line_count() != error_line) {
|
|
|
|
text_editor->unfold_line(error_line);
|
|
|
|
}
|
2022-06-08 23:41:38 +02:00
|
|
|
text_editor->remove_secondary_carets();
|
2021-07-08 19:35:56 +02:00
|
|
|
text_editor->set_caret_line(error_line);
|
|
|
|
text_editor->set_caret_column(error_column);
|
|
|
|
text_editor->center_viewport_to_caret();
|
2018-12-21 09:05:29 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-09-10 22:25:00 +02:00
|
|
|
void CodeTextEditor::_update_text_editor_theme() {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("load_theme_settings"));
|
2018-02-08 13:04:39 +01:00
|
|
|
|
2021-08-13 17:32:11 +02:00
|
|
|
error->begin_bulk_theme_override();
|
2023-08-13 02:33:39 +02:00
|
|
|
error->add_theme_font_override(SNAME("font"), get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts)));
|
|
|
|
error->add_theme_font_size_override(SNAME("font_size"), get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts)));
|
|
|
|
error->add_theme_color_override(SNAME("font_color"), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
|
2019-11-14 11:16:25 +01:00
|
|
|
|
2023-08-13 02:33:39 +02:00
|
|
|
Ref<Font> status_bar_font = get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts));
|
|
|
|
int status_bar_font_size = get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts));
|
2022-02-08 10:14:58 +01:00
|
|
|
error->add_theme_font_override("font", status_bar_font);
|
|
|
|
error->add_theme_font_size_override("font_size", status_bar_font_size);
|
2023-10-19 18:05:19 +02:00
|
|
|
error->end_bulk_theme_override();
|
|
|
|
|
2018-02-08 13:04:39 +01:00
|
|
|
int count = status_bar->get_child_count();
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
Control *n = Object::cast_to<Control>(status_bar->get_child(i));
|
2020-05-14 16:41:43 +02:00
|
|
|
if (n) {
|
2022-02-08 10:14:58 +01:00
|
|
|
n->add_theme_font_override("font", status_bar_font);
|
|
|
|
n->add_theme_font_size_override("font_size", status_bar_font_size);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-02-08 13:04:39 +01:00
|
|
|
}
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_on_settings_change() {
|
2021-08-14 08:42:18 +02:00
|
|
|
_apply_settings_change();
|
2021-07-30 19:40:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_apply_settings_change() {
|
2020-09-10 22:25:00 +02:00
|
|
|
_update_text_editor_theme();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2022-10-18 16:43:37 +02:00
|
|
|
font_size = EDITOR_GET("interface/editor/code_font_size");
|
|
|
|
int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures");
|
2022-05-09 11:47:10 +02:00
|
|
|
|
2022-07-07 14:11:08 +02:00
|
|
|
Ref<FontVariation> fc = text_editor->get_theme_font(SNAME("font"));
|
|
|
|
if (fc.is_valid()) {
|
|
|
|
switch (ot_mode) {
|
|
|
|
case 1: { // Disable ligatures.
|
|
|
|
Dictionary ftrs;
|
|
|
|
ftrs[TS->name_to_tag("calt")] = 0;
|
|
|
|
fc->set_opentype_features(ftrs);
|
|
|
|
} break;
|
|
|
|
case 2: { // Custom.
|
2022-10-18 16:43:37 +02:00
|
|
|
Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features")).split(",");
|
2022-07-07 14:11:08 +02:00
|
|
|
Dictionary ftrs;
|
|
|
|
for (int i = 0; i < subtag.size(); i++) {
|
|
|
|
Vector<String> subtag_a = subtag[i].split("=");
|
|
|
|
if (subtag_a.size() == 2) {
|
|
|
|
ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
|
|
|
|
} else if (subtag_a.size() == 1) {
|
|
|
|
ftrs[TS->name_to_tag(subtag_a[0])] = 1;
|
|
|
|
}
|
2020-09-18 09:36:10 +02:00
|
|
|
}
|
2022-07-07 14:11:08 +02:00
|
|
|
fc->set_opentype_features(ftrs);
|
|
|
|
} break;
|
2022-11-12 18:55:57 +01:00
|
|
|
default: { // Enabled.
|
2022-07-07 14:11:08 +02:00
|
|
|
Dictionary ftrs;
|
|
|
|
ftrs[TS->name_to_tag("calt")] = 1;
|
|
|
|
fc->set_opentype_features(ftrs);
|
|
|
|
} break;
|
|
|
|
}
|
2020-09-18 09:36:10 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 21:45:47 +02:00
|
|
|
text_editor->set_code_hint_draw_below(EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"));
|
2019-04-25 23:48:59 +02:00
|
|
|
|
2022-11-01 20:03:43 +01:00
|
|
|
code_complete_enabled = EDITOR_GET("text_editor/completion/code_complete_enabled");
|
2021-06-28 18:14:44 +02:00
|
|
|
code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay"));
|
2019-07-18 21:34:28 +02:00
|
|
|
idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
|
2014-04-05 17:39:30 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void CodeTextEditor::_text_changed_idle_timeout() {
|
|
|
|
_validate_script();
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("validate_script"));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-05-08 18:49:49 +02:00
|
|
|
void CodeTextEditor::validate_script() {
|
|
|
|
idle->start();
|
|
|
|
}
|
|
|
|
|
2021-05-18 05:09:19 +02:00
|
|
|
void CodeTextEditor::_error_button_pressed() {
|
|
|
|
_set_show_errors_panel(!is_errors_panel_opened);
|
|
|
|
_set_show_warnings_panel(false);
|
2019-02-01 17:20:59 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-02-01 17:20:59 +01:00
|
|
|
void CodeTextEditor::_warning_button_pressed() {
|
2019-02-13 10:12:31 +01:00
|
|
|
_set_show_warnings_panel(!is_warnings_panel_opened);
|
2021-05-18 05:09:19 +02:00
|
|
|
_set_show_errors_panel(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_set_show_errors_panel(bool p_show) {
|
|
|
|
is_errors_panel_opened = p_show;
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("show_errors_panel"), p_show);
|
2019-02-13 10:12:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_set_show_warnings_panel(bool p_show) {
|
|
|
|
is_warnings_panel_opened = p_show;
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("show_warnings_panel"), p_show);
|
2019-02-01 17:20:59 +01:00
|
|
|
}
|
|
|
|
|
2019-12-15 14:35:16 +01:00
|
|
|
void CodeTextEditor::_toggle_scripts_pressed() {
|
2021-09-14 11:17:47 +02:00
|
|
|
ScriptEditor::get_singleton()->toggle_scripts_panel();
|
|
|
|
update_toggle_scripts_button();
|
2019-12-15 14:35:16 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:35:35 +01:00
|
|
|
int CodeTextEditor::_get_affected_lines_from(int p_caret) {
|
|
|
|
return text_editor->has_selection(p_caret) ? text_editor->get_selection_from_line(p_caret) : text_editor->get_caret_line(p_caret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CodeTextEditor::_get_affected_lines_to(int p_caret) {
|
|
|
|
if (!text_editor->has_selection(p_caret)) {
|
|
|
|
return text_editor->get_caret_line(p_caret);
|
|
|
|
}
|
|
|
|
int line = text_editor->get_selection_to_line(p_caret);
|
2023-03-08 23:00:27 +01:00
|
|
|
// Don't affect a line with no selected characters.
|
2023-02-01 14:35:35 +01:00
|
|
|
if (text_editor->get_selection_to_column(p_caret) == 0) {
|
|
|
|
line--;
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:20:59 +01:00
|
|
|
void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) {
|
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2021-08-13 23:31:57 +02:00
|
|
|
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
|
2020-06-23 09:59:43 +02:00
|
|
|
goto_error();
|
2016-08-03 00:11:05 +02:00
|
|
|
}
|
2019-02-01 17:20:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-16 00:52:32 +01:00
|
|
|
void CodeTextEditor::_update_status_bar_theme() {
|
2023-08-13 02:33:39 +02:00
|
|
|
error_button->set_icon(get_editor_theme_icon(SNAME("StatusError")));
|
2023-10-19 18:05:19 +02:00
|
|
|
warning_button->set_icon(get_editor_theme_icon(SNAME("NodeWarning")));
|
|
|
|
|
|
|
|
error_button->begin_bulk_theme_override();
|
2023-08-13 02:33:39 +02:00
|
|
|
error_button->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
|
|
|
|
error_button->add_theme_font_override("font", get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts)));
|
|
|
|
error_button->add_theme_font_size_override("font_size", get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts)));
|
2023-10-19 18:05:19 +02:00
|
|
|
error_button->end_bulk_theme_override();
|
2022-01-19 06:31:39 +01:00
|
|
|
|
2023-10-19 18:05:19 +02:00
|
|
|
warning_button->begin_bulk_theme_override();
|
2023-08-13 02:33:39 +02:00
|
|
|
warning_button->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
|
|
|
|
warning_button->add_theme_font_override("font", get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts)));
|
|
|
|
warning_button->add_theme_font_size_override("font_size", get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts)));
|
2023-10-19 18:05:19 +02:00
|
|
|
warning_button->end_bulk_theme_override();
|
2022-01-19 06:31:39 +01:00
|
|
|
|
2023-10-19 18:05:19 +02:00
|
|
|
line_and_col_txt->begin_bulk_theme_override();
|
2023-08-13 02:33:39 +02:00
|
|
|
line_and_col_txt->add_theme_font_override("font", get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts)));
|
|
|
|
line_and_col_txt->add_theme_font_size_override("font_size", get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts)));
|
2023-10-19 18:05:19 +02:00
|
|
|
line_and_col_txt->end_bulk_theme_override();
|
2022-02-16 00:52:32 +01:00
|
|
|
}
|
2022-01-19 06:31:39 +01:00
|
|
|
|
2022-02-16 00:52:32 +01:00
|
|
|
void CodeTextEditor::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
|
|
|
_update_status_bar_theme();
|
2019-12-17 09:16:17 +01:00
|
|
|
if (toggle_scripts_button->is_visible()) {
|
2019-12-17 14:51:49 +01:00
|
|
|
update_toggle_scripts_button();
|
2019-12-17 09:16:17 +01:00
|
|
|
}
|
2020-09-10 22:25:00 +02:00
|
|
|
_update_text_editor_theme();
|
2019-02-01 17:20:59 +01:00
|
|
|
} break;
|
2022-02-16 00:52:32 +01:00
|
|
|
|
2019-05-09 18:26:33 +02:00
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
2019-12-17 14:51:49 +01:00
|
|
|
if (toggle_scripts_button->is_visible()) {
|
|
|
|
update_toggle_scripts_button();
|
|
|
|
}
|
2019-05-09 18:26:33 +02:00
|
|
|
set_process_input(is_visible_in_tree());
|
|
|
|
} break;
|
2022-02-16 00:52:32 +01:00
|
|
|
|
2021-06-24 15:38:29 +02:00
|
|
|
case NOTIFICATION_PREDELETE: {
|
|
|
|
if (find_replace_bar) {
|
|
|
|
find_replace_bar->set_text_edit(nullptr);
|
|
|
|
}
|
|
|
|
} break;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-05-18 05:09:19 +02:00
|
|
|
void CodeTextEditor::set_error_count(int p_error_count) {
|
|
|
|
error_button->set_text(itos(p_error_count));
|
|
|
|
error_button->set_visible(p_error_count > 0);
|
|
|
|
if (!p_error_count) {
|
|
|
|
_set_show_errors_panel(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::set_warning_count(int p_warning_count) {
|
|
|
|
warning_button->set_text(itos(p_warning_count));
|
|
|
|
warning_button->set_visible(p_warning_count > 0);
|
|
|
|
if (!p_warning_count) {
|
2019-02-13 10:12:31 +01:00
|
|
|
_set_show_warnings_panel(false);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-02-01 17:20:59 +01:00
|
|
|
}
|
|
|
|
|
2019-04-20 01:51:25 +02:00
|
|
|
void CodeTextEditor::toggle_bookmark() {
|
2023-02-01 14:35:35 +01:00
|
|
|
Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
|
|
|
|
caret_edit_order.reverse();
|
|
|
|
int last_line = -1;
|
|
|
|
for (const int &c : caret_edit_order) {
|
|
|
|
int from = text_editor->has_selection(c) ? text_editor->get_selection_from_line(c) : text_editor->get_caret_line(c);
|
|
|
|
from += from == last_line ? 1 : 0;
|
|
|
|
int to = text_editor->has_selection(c) ? text_editor->get_selection_to_line(c) : text_editor->get_caret_line(c);
|
|
|
|
if (to < from) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Check first if there's any bookmarked lines in the selection.
|
|
|
|
bool selection_has_bookmarks = false;
|
|
|
|
for (int line = from; line <= to; line++) {
|
|
|
|
if (text_editor->is_line_bookmarked(line)) {
|
|
|
|
selection_has_bookmarks = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set bookmark on caret or remove all bookmarks from the selection.
|
|
|
|
if (!selection_has_bookmarks) {
|
|
|
|
if (text_editor->get_caret_line(c) != last_line) {
|
|
|
|
text_editor->set_line_as_bookmarked(text_editor->get_caret_line(c), true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int line = from; line <= to; line++) {
|
|
|
|
text_editor->set_line_as_bookmarked(line, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_line = to;
|
2022-06-08 23:41:38 +02:00
|
|
|
}
|
2019-04-20 01:51:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::goto_next_bookmark() {
|
2022-08-05 03:41:48 +02:00
|
|
|
PackedInt32Array bmarks = text_editor->get_bookmarked_lines();
|
2019-04-20 01:51:25 +02:00
|
|
|
if (bmarks.size() <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-17 02:50:04 +01:00
|
|
|
int current_line = text_editor->get_caret_line();
|
|
|
|
int bmark_idx = 0;
|
|
|
|
if (current_line < (int)bmarks[bmarks.size() - 1]) {
|
|
|
|
while (bmark_idx < bmarks.size() && bmarks[bmark_idx] <= current_line) {
|
|
|
|
bmark_idx++;
|
2019-04-20 01:51:25 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-17 02:50:04 +01:00
|
|
|
goto_line_centered(bmarks[bmark_idx]);
|
2019-04-20 01:51:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::goto_prev_bookmark() {
|
2022-08-05 03:41:48 +02:00
|
|
|
PackedInt32Array bmarks = text_editor->get_bookmarked_lines();
|
2019-04-20 01:51:25 +02:00
|
|
|
if (bmarks.size() <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-17 02:50:04 +01:00
|
|
|
int current_line = text_editor->get_caret_line();
|
|
|
|
int bmark_idx = bmarks.size() - 1;
|
|
|
|
if (current_line > (int)bmarks[0]) {
|
|
|
|
while (bmark_idx >= 0 && bmarks[bmark_idx] >= current_line) {
|
|
|
|
bmark_idx--;
|
2019-04-20 01:51:25 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-17 02:50:04 +01:00
|
|
|
goto_line_centered(bmarks[bmark_idx]);
|
2019-04-20 01:51:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::remove_all_bookmarks() {
|
2020-07-26 16:57:23 +02:00
|
|
|
text_editor->clear_bookmarked_lines();
|
2019-04-20 01:51:25 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void CodeTextEditor::_bind_methods() {
|
2016-08-03 00:11:05 +02:00
|
|
|
ADD_SIGNAL(MethodInfo("validate_script"));
|
|
|
|
ADD_SIGNAL(MethodInfo("load_theme_settings"));
|
2021-05-18 05:09:19 +02:00
|
|
|
ADD_SIGNAL(MethodInfo("show_errors_panel"));
|
2019-02-13 10:12:31 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("show_warnings_panel"));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-08-03 00:11:05 +02:00
|
|
|
void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
|
|
|
|
code_complete_func = p_code_complete_func;
|
|
|
|
code_complete_ud = p_ud;
|
|
|
|
}
|
|
|
|
|
2019-12-17 09:16:17 +01:00
|
|
|
void CodeTextEditor::show_toggle_scripts_button() {
|
|
|
|
toggle_scripts_button->show();
|
|
|
|
}
|
|
|
|
|
2019-12-17 14:51:49 +01:00
|
|
|
void CodeTextEditor::update_toggle_scripts_button() {
|
2020-09-03 13:22:16 +02:00
|
|
|
if (is_layout_rtl()) {
|
2023-08-13 02:33:39 +02:00
|
|
|
toggle_scripts_button->set_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Forward") : SNAME("Back")));
|
2020-11-30 15:33:42 +01:00
|
|
|
} else {
|
2023-08-13 02:33:39 +02:00
|
|
|
toggle_scripts_button->set_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Back") : SNAME("Forward")));
|
2020-09-03 13:22:16 +02:00
|
|
|
}
|
2022-08-25 12:42:17 +02:00
|
|
|
toggle_scripts_button->set_tooltip_text(vformat("%s (%s)", TTR("Toggle Scripts Panel"), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text()));
|
2019-12-17 14:51:49 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
CodeTextEditor::CodeTextEditor() {
|
2020-04-02 01:20:12 +02:00
|
|
|
code_complete_func = nullptr;
|
2022-09-02 11:37:48 +02:00
|
|
|
ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KeyModifierMask::CMD_OR_CTRL | Key::EQUAL);
|
|
|
|
ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KeyModifierMask::CMD_OR_CTRL | Key::MINUS);
|
2022-08-10 15:07:47 +02:00
|
|
|
ED_SHORTCUT_ARRAY("script_editor/reset_zoom", TTR("Reset Zoom"),
|
2022-09-02 11:37:48 +02:00
|
|
|
{ int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KEY_0), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_0) });
|
2016-07-14 21:03:40 +02:00
|
|
|
|
2020-07-24 16:50:35 +02:00
|
|
|
text_editor = memnew(CodeEdit);
|
2019-01-31 15:24:33 +01:00
|
|
|
add_child(text_editor);
|
|
|
|
text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
|
2023-01-18 08:33:35 +01:00
|
|
|
text_editor->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_GDSCRIPT);
|
2023-06-02 22:36:04 +02:00
|
|
|
text_editor->set_draw_bookmarks_gutter(true);
|
2019-01-31 15:24:33 +01:00
|
|
|
|
2022-10-18 16:43:37 +02:00
|
|
|
int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures");
|
2022-07-07 14:11:08 +02:00
|
|
|
Ref<FontVariation> fc = text_editor->get_theme_font(SNAME("font"));
|
|
|
|
if (fc.is_valid()) {
|
|
|
|
switch (ot_mode) {
|
|
|
|
case 1: { // Disable ligatures.
|
|
|
|
Dictionary ftrs;
|
|
|
|
ftrs[TS->name_to_tag("calt")] = 0;
|
|
|
|
fc->set_opentype_features(ftrs);
|
|
|
|
} break;
|
|
|
|
case 2: { // Custom.
|
2022-10-18 16:43:37 +02:00
|
|
|
Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features")).split(",");
|
2022-07-07 14:11:08 +02:00
|
|
|
Dictionary ftrs;
|
|
|
|
for (int i = 0; i < subtag.size(); i++) {
|
|
|
|
Vector<String> subtag_a = subtag[i].split("=");
|
|
|
|
if (subtag_a.size() == 2) {
|
|
|
|
ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
|
|
|
|
} else if (subtag_a.size() == 1) {
|
|
|
|
ftrs[TS->name_to_tag(subtag_a[0])] = 1;
|
|
|
|
}
|
2020-09-18 09:36:10 +02:00
|
|
|
}
|
2022-07-07 14:11:08 +02:00
|
|
|
fc->set_opentype_features(ftrs);
|
|
|
|
} break;
|
2022-11-12 18:55:57 +01:00
|
|
|
default: { // Enabled.
|
2022-07-07 14:11:08 +02:00
|
|
|
Dictionary ftrs;
|
|
|
|
ftrs[TS->name_to_tag("calt")] = 1;
|
|
|
|
fc->set_opentype_features(ftrs);
|
|
|
|
} break;
|
|
|
|
}
|
2020-09-18 09:36:10 +02:00
|
|
|
}
|
|
|
|
|
2020-07-25 19:27:35 +02:00
|
|
|
text_editor->set_draw_line_numbers(true);
|
2021-06-30 12:58:13 +02:00
|
|
|
text_editor->set_highlight_matching_braces_enabled(true);
|
2021-06-15 16:05:01 +02:00
|
|
|
text_editor->set_auto_indent_enabled(true);
|
2021-10-21 23:02:46 +02:00
|
|
|
text_editor->set_deselect_on_focus_loss_enabled(false);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-02-08 13:04:39 +01:00
|
|
|
status_bar = memnew(HBoxContainer);
|
2017-09-27 21:44:48 +02:00
|
|
|
add_child(status_bar);
|
2016-05-28 18:25:45 +02:00
|
|
|
status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
|
2019-02-10 17:17:29 +01:00
|
|
|
status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the warning icon.
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
idle = memnew(Timer);
|
|
|
|
add_child(idle);
|
|
|
|
idle->set_one_shot(true);
|
2019-07-18 21:34:28 +02:00
|
|
|
idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-11-01 20:03:43 +01:00
|
|
|
code_complete_enabled = EDITOR_GET("text_editor/completion/code_complete_enabled");
|
2014-05-04 05:22:49 +02:00
|
|
|
code_complete_timer = memnew(Timer);
|
|
|
|
add_child(code_complete_timer);
|
|
|
|
code_complete_timer->set_one_shot(true);
|
2019-07-18 21:34:28 +02:00
|
|
|
code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay"));
|
2014-05-04 05:22:49 +02:00
|
|
|
|
2018-11-21 11:56:02 +01:00
|
|
|
error_line = 0;
|
|
|
|
error_column = 0;
|
|
|
|
|
2020-06-19 20:49:04 +02:00
|
|
|
toggle_scripts_button = memnew(Button);
|
|
|
|
toggle_scripts_button->set_flat(true);
|
2020-02-21 18:28:45 +01:00
|
|
|
toggle_scripts_button->connect("pressed", callable_mp(this, &CodeTextEditor::_toggle_scripts_pressed));
|
2019-12-15 14:35:16 +01:00
|
|
|
status_bar->add_child(toggle_scripts_button);
|
2019-12-17 09:16:17 +01:00
|
|
|
toggle_scripts_button->hide();
|
2019-12-15 14:35:16 +01:00
|
|
|
|
2019-02-01 17:20:59 +01:00
|
|
|
// Error
|
|
|
|
ScrollContainer *scroll = memnew(ScrollContainer);
|
|
|
|
scroll->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
scroll->set_v_size_flags(SIZE_EXPAND_FILL);
|
2021-12-07 17:15:18 +01:00
|
|
|
scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
|
2019-02-01 17:20:59 +01:00
|
|
|
status_bar->add_child(scroll);
|
|
|
|
|
2018-12-21 09:05:29 +01:00
|
|
|
error = memnew(Label);
|
2019-02-01 17:20:59 +01:00
|
|
|
scroll->add_child(error);
|
|
|
|
error->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
|
2018-12-21 09:05:29 +01:00
|
|
|
error->set_mouse_filter(MOUSE_FILTER_STOP);
|
2020-02-21 18:28:45 +01:00
|
|
|
error->connect("gui_input", callable_mp(this, &CodeTextEditor::_error_pressed));
|
2016-05-28 18:25:45 +02:00
|
|
|
|
2021-05-18 05:09:19 +02:00
|
|
|
// Errors
|
|
|
|
error_button = memnew(Button);
|
|
|
|
error_button->set_flat(true);
|
|
|
|
status_bar->add_child(error_button);
|
|
|
|
error_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
|
|
|
|
error_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
|
|
|
|
error_button->connect("pressed", callable_mp(this, &CodeTextEditor::_error_button_pressed));
|
2022-08-25 12:42:17 +02:00
|
|
|
error_button->set_tooltip_text(TTR("Errors"));
|
2021-05-18 05:09:19 +02:00
|
|
|
set_error_count(0);
|
|
|
|
|
2019-02-01 17:20:59 +01:00
|
|
|
// Warnings
|
2020-06-19 20:49:04 +02:00
|
|
|
warning_button = memnew(Button);
|
|
|
|
warning_button->set_flat(true);
|
2019-02-01 17:20:59 +01:00
|
|
|
status_bar->add_child(warning_button);
|
|
|
|
warning_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
|
|
|
|
warning_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
|
2020-02-21 18:28:45 +01:00
|
|
|
warning_button->connect("pressed", callable_mp(this, &CodeTextEditor::_warning_button_pressed));
|
2022-08-25 12:42:17 +02:00
|
|
|
warning_button->set_tooltip_text(TTR("Warnings"));
|
2021-05-18 05:09:19 +02:00
|
|
|
set_warning_count(0);
|
2019-02-01 17:20:59 +01:00
|
|
|
|
|
|
|
// Line and column
|
|
|
|
line_and_col_txt = memnew(Label);
|
|
|
|
status_bar->add_child(line_and_col_txt);
|
|
|
|
line_and_col_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
|
2022-08-25 12:42:17 +02:00
|
|
|
line_and_col_txt->set_tooltip_text(TTR("Line and column numbers."));
|
2019-02-01 17:20:59 +01:00
|
|
|
line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-21 18:28:45 +01:00
|
|
|
text_editor->connect("gui_input", callable_mp(this, &CodeTextEditor::_text_editor_gui_input));
|
2021-07-08 19:35:56 +02:00
|
|
|
text_editor->connect("caret_changed", callable_mp(this, &CodeTextEditor::_line_col_changed));
|
2020-02-21 18:28:45 +01:00
|
|
|
text_editor->connect("text_changed", callable_mp(this, &CodeTextEditor::_text_changed));
|
2022-01-22 13:50:20 +01:00
|
|
|
text_editor->connect("code_completion_requested", callable_mp(this, &CodeTextEditor::_complete_request));
|
2020-09-13 22:14:20 +02:00
|
|
|
TypedArray<String> cs;
|
2014-02-10 02:10:30 +01:00
|
|
|
cs.push_back(".");
|
2014-12-17 02:31:57 +01:00
|
|
|
cs.push_back(",");
|
|
|
|
cs.push_back("(");
|
2017-08-24 05:06:56 +02:00
|
|
|
cs.push_back("=");
|
2017-01-08 07:01:52 +01:00
|
|
|
cs.push_back("$");
|
2020-07-06 17:24:24 +02:00
|
|
|
cs.push_back("@");
|
2020-09-13 22:14:20 +02:00
|
|
|
cs.push_back("\"");
|
|
|
|
cs.push_back("\'");
|
|
|
|
text_editor->set_code_completion_prefixes(cs);
|
2020-02-21 18:28:45 +01:00
|
|
|
idle->connect("timeout", callable_mp(this, &CodeTextEditor::_text_changed_idle_timeout));
|
2014-05-22 00:01:11 +02:00
|
|
|
|
2020-02-21 18:28:45 +01:00
|
|
|
code_complete_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_code_complete_timer_timeout));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-17 21:58:50 +02:00
|
|
|
font_resize_val = 0;
|
2022-10-18 16:43:37 +02:00
|
|
|
font_size = EDITOR_GET("interface/editor/code_font_size");
|
2020-09-18 09:36:10 +02:00
|
|
|
font_resize_timer = memnew(Timer);
|
|
|
|
add_child(font_resize_timer);
|
|
|
|
font_resize_timer->set_one_shot(true);
|
|
|
|
font_resize_timer->set_wait_time(0.07);
|
|
|
|
font_resize_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_font_resize_timeout));
|
2016-06-17 21:58:50 +02:00
|
|
|
|
2020-02-21 18:28:45 +01:00
|
|
|
EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &CodeTextEditor::_on_settings_change));
|
2022-02-08 10:14:58 +01:00
|
|
|
add_theme_constant_override("separation", 4 * EDSCALE);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|