2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* code_editor.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "code_editor.h"
|
2017-01-16 07:04:19 +00:00
|
|
|
|
2020-04-28 13:19:37 +00:00
|
|
|
#include "core/input/input.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/keyboard.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/string/string_builder.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "editor/editor_scale.h"
|
2017-08-15 17:30:53 +00:00
|
|
|
#include "editor_node.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "editor_settings.h"
|
|
|
|
#include "scene/gui/margin_container.h"
|
|
|
|
#include "scene/gui/separator.h"
|
2020-09-03 11:22:16 +00:00
|
|
|
#include "scene/resources/font.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
void GotoLineDialog::popup_find_line(CodeEdit *p_edit) {
|
2017-03-05 15:44:50 +00:00
|
|
|
text_editor = p_edit;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
line->set_text(itos(text_editor->cursor_get_line()));
|
|
|
|
line->select_all();
|
2018-09-18 14:10:36 +00:00
|
|
|
popup_centered(Size2(180, 80) * EDSCALE);
|
2014-02-10 01:10:30 +00:00
|
|
|
line->grab_focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
int GotoLineDialog::get_line() const {
|
|
|
|
return line->get_text().to_int();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GotoLineDialog::ok_pressed() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (get_line() < 1 || get_line() > text_editor->get_line_count()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-11-12 23:12:17 +00:00
|
|
|
text_editor->unfold_line(get_line() - 1);
|
2017-03-05 15:44:50 +00:00
|
|
|
text_editor->cursor_set_line(get_line() - 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
GotoLineDialog::GotoLineDialog() {
|
2016-05-04 01:25:37 +00:00
|
|
|
set_title(TTR("Go to Line"));
|
2018-09-18 14:10:36 +00:00
|
|
|
|
|
|
|
VBoxContainer *vbc = memnew(VBoxContainer);
|
2020-12-22 16:24:29 +00: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 14:10:36 +00:00
|
|
|
add_child(vbc);
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Label *l = memnew(Label);
|
2016-05-04 01:25:37 +00:00
|
|
|
l->set_text(TTR("Line Number:"));
|
2018-09-18 14:10:36 +00:00
|
|
|
vbc->add_child(l);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
line = memnew(LineEdit);
|
2018-09-18 14:10:36 +00:00
|
|
|
vbc->add_child(line);
|
2014-02-10 01:10:30 +00:00
|
|
|
register_text_enter(line);
|
2020-04-01 23:20:12 +00:00
|
|
|
text_editor = nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-11-24 09:12:55 +00:00
|
|
|
line_label = nullptr;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
set_hide_on_ok(false);
|
|
|
|
}
|
|
|
|
|
2016-05-28 16:25:45 +00:00
|
|
|
void FindReplaceBar::_notification(int p_what) {
|
|
|
|
if (p_what == NOTIFICATION_READY) {
|
2020-03-12 12:37:40 +00:00
|
|
|
find_prev->set_icon(get_theme_icon("MoveUp", "EditorIcons"));
|
|
|
|
find_next->set_icon(get_theme_icon("MoveDown", "EditorIcons"));
|
|
|
|
hide_button->set_normal_texture(get_theme_icon("Close", "EditorIcons"));
|
|
|
|
hide_button->set_hover_texture(get_theme_icon("Close", "EditorIcons"));
|
|
|
|
hide_button->set_pressed_texture(get_theme_icon("Close", "EditorIcons"));
|
2018-01-17 23:52:17 +00:00
|
|
|
hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
|
2016-05-28 16:25:45 +00:00
|
|
|
} else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
|
2017-01-13 13:45:50 +00:00
|
|
|
set_process_unhandled_input(is_visible_in_tree());
|
2017-08-29 23:03:13 +00:00
|
|
|
} else if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
|
2020-03-12 12:37:40 +00:00
|
|
|
find_prev->set_icon(get_theme_icon("MoveUp", "EditorIcons"));
|
|
|
|
find_next->set_icon(get_theme_icon("MoveDown", "EditorIcons"));
|
|
|
|
hide_button->set_normal_texture(get_theme_icon("Close", "EditorIcons"));
|
|
|
|
hide_button->set_hover_texture(get_theme_icon("Close", "EditorIcons"));
|
|
|
|
hide_button->set_pressed_texture(get_theme_icon("Close", "EditorIcons"));
|
2018-01-17 23:52:17 +00:00
|
|
|
hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
|
2019-11-14 10:16:25 +00:00
|
|
|
} else if (p_what == NOTIFICATION_THEME_CHANGED) {
|
2020-03-12 12:37:40 +00:00
|
|
|
matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color("font_color", "Label") : get_theme_color("error_color", "Editor"));
|
2021-06-24 13:38:29 +00:00
|
|
|
} else if (p_what == NOTIFICATION_PREDELETE) {
|
|
|
|
if (base_text_editor) {
|
|
|
|
base_text_editor->remove_find_replace_bar();
|
|
|
|
base_text_editor = nullptr;
|
|
|
|
}
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
void FindReplaceBar::_unhandled_input(const Ref<InputEvent> &p_event) {
|
2021-04-05 06:52:21 +00:00
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventKey> k = p_event;
|
2020-07-06 14:13:04 +00:00
|
|
|
if (!k.is_valid() || !k->is_pressed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Control *focus_owner = get_focus_owner();
|
2021-06-18 22:02:50 +00:00
|
|
|
if (text_editor->has_focus() || (focus_owner && vbc_lineedit->is_ancestor_of(focus_owner))) {
|
2020-07-06 14:13:04 +00:00
|
|
|
bool accepted = true;
|
|
|
|
|
|
|
|
switch (k->get_keycode()) {
|
|
|
|
case KEY_ESCAPE: {
|
|
|
|
_hide_bar();
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
accepted = false;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accepted) {
|
|
|
|
accept_event();
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 16:15:41 +00:00
|
|
|
bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
|
|
|
|
int line, col;
|
2017-03-05 15:44:50 +00:00
|
|
|
String text = get_search_text();
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
bool found = text_editor->search(text, p_flags, p_from_line, p_from_col, line, col);
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2016-05-30 16:15:41 +00:00
|
|
|
if (found) {
|
2021-04-04 15:26:54 +00:00
|
|
|
if (!preserve_cursor && !is_selection_only()) {
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->unfold_line(line);
|
|
|
|
text_editor->cursor_set_line(line, false);
|
|
|
|
text_editor->cursor_set_column(col + text.length(), false);
|
|
|
|
text_editor->center_viewport_to_cursor();
|
|
|
|
text_editor->select(line, col, line, col + text.length());
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->set_search_text(text);
|
|
|
|
text_editor->set_search_flags(p_flags);
|
|
|
|
text_editor->set_current_search_result(line, col);
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
result_line = line;
|
|
|
|
result_col = col;
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2019-07-08 17:33:04 +00:00
|
|
|
_update_results_count();
|
2016-05-28 16:25:45 +00:00
|
|
|
} else {
|
2019-08-14 21:31:07 +00:00
|
|
|
results_count = 0;
|
2017-03-05 15:44:50 +00:00
|
|
|
result_line = -1;
|
|
|
|
result_col = -1;
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->set_search_text("");
|
|
|
|
text_editor->set_search_flags(p_flags);
|
|
|
|
text_editor->set_current_search_result(line, col);
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 06:47:09 +00:00
|
|
|
_update_matches_label();
|
|
|
|
|
2016-05-28 16:25:45 +00:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_replace() {
|
2020-07-24 14:50:35 +00:00
|
|
|
bool selection_enabled = text_editor->is_selection_active();
|
2020-06-16 12:04:36 +00:00
|
|
|
Point2i selection_begin, selection_end;
|
|
|
|
if (selection_enabled) {
|
2020-07-24 14:50:35 +00:00
|
|
|
selection_begin = Point2i(text_editor->get_selection_from_line(), text_editor->get_selection_from_column());
|
|
|
|
selection_end = Point2i(text_editor->get_selection_to_line(), text_editor->get_selection_to_column());
|
2020-06-16 12:04:36 +00:00
|
|
|
}
|
2016-05-30 16:15:41 +00:00
|
|
|
|
2020-06-16 12:04:36 +00:00
|
|
|
String replace_text = get_replace_text();
|
|
|
|
int search_text_len = get_search_text().length();
|
2016-05-30 16:15:41 +00:00
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->begin_complex_operation();
|
2020-06-16 12:04:36 +00:00
|
|
|
if (selection_enabled && is_selection_only()) { // To restrict search_current() to selected region
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->cursor_set_line(selection_begin.width);
|
|
|
|
text_editor->cursor_set_column(selection_begin.height);
|
2020-06-16 12:04:36 +00:00
|
|
|
}
|
2019-07-08 17:33:04 +00:00
|
|
|
|
2020-06-16 12:04:36 +00:00
|
|
|
if (search_current()) {
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->unfold_line(result_line);
|
|
|
|
text_editor->select(result_line, result_col, result_line, result_col + search_text_len);
|
2020-06-16 12:04:36 +00: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)) {
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->insert_text_at_cursor(replace_text);
|
2020-06-16 12:04:36 +00:00
|
|
|
if (match_to.x == selection_end.x) { // Adjust selection bounds if necessary
|
|
|
|
selection_end.y += replace_text.length() - search_text_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->insert_text_at_cursor(replace_text);
|
2020-06-16 12:04:36 +00:00
|
|
|
}
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->end_complex_operation();
|
2020-06-16 12:04:36 +00:00
|
|
|
results_count = -1;
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2020-06-16 12:04:36 +00:00
|
|
|
if (selection_enabled && is_selection_only()) {
|
|
|
|
// Reselect in order to keep 'Replace' restricted to selection
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
|
2020-06-16 12:04:36 +00:00
|
|
|
} else {
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->deselect();
|
2019-10-14 11:59:26 +00:00
|
|
|
}
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_replace_all() {
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->disconnect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
|
2019-08-14 21:31:07 +00:00
|
|
|
// Line as x so it gets priority in comparison, column as y.
|
2020-07-24 14:50:35 +00:00
|
|
|
Point2i orig_cursor(text_editor->cursor_get_line(), text_editor->cursor_get_column());
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2i prev_match = Point2(-1, -1);
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
bool selection_enabled = text_editor->is_selection_active();
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2i selection_begin, selection_end;
|
2016-05-28 16:25:45 +00:00
|
|
|
if (selection_enabled) {
|
2020-07-24 14:50:35 +00:00
|
|
|
selection_begin = Point2i(text_editor->get_selection_from_line(), text_editor->get_selection_from_column());
|
|
|
|
selection_end = Point2i(text_editor->get_selection_to_line(), text_editor->get_selection_to_column());
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
int vsval = text_editor->get_v_scroll();
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->cursor_set_line(0);
|
|
|
|
text_editor->cursor_set_column(0);
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String replace_text = get_replace_text();
|
|
|
|
int search_text_len = get_search_text().length();
|
2016-07-29 23:11:02 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int rc = 0;
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
replace_all_mode = true;
|
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->begin_complex_operation();
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2020-03-30 21:27:15 +00:00
|
|
|
if (selection_enabled && is_selection_only()) {
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->cursor_set_line(selection_begin.width);
|
|
|
|
text_editor->cursor_set_column(selection_begin.height);
|
2020-03-30 21:27:15 +00:00
|
|
|
}
|
2018-03-25 07:39:37 +00:00
|
|
|
if (search_current()) {
|
|
|
|
do {
|
|
|
|
// replace area
|
|
|
|
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 21:31:07 +00:00
|
|
|
break; // Done.
|
2018-03-25 07:39:37 +00:00
|
|
|
}
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2018-03-25 07:39:37 +00:00
|
|
|
prev_match = Point2i(result_line, result_col + replace_text.length());
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->unfold_line(result_line);
|
|
|
|
text_editor->select(result_line, result_col, result_line, match_to.y);
|
2016-05-30 16:15:41 +00:00
|
|
|
|
2018-03-25 07:39:37 +00:00
|
|
|
if (selection_enabled && is_selection_only()) {
|
|
|
|
if (match_from < selection_begin || match_to > selection_end) {
|
2020-03-30 21:27:15 +00:00
|
|
|
break; // Done.
|
2018-03-25 07:39:37 +00:00
|
|
|
}
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2019-08-14 21:31:07 +00:00
|
|
|
// Replace but adjust selection bounds.
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->insert_text_at_cursor(replace_text);
|
2018-03-25 07:39:37 +00:00
|
|
|
if (match_to.x == selection_end.x) {
|
|
|
|
selection_end.y += replace_text.length() - search_text_len;
|
|
|
|
}
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2018-03-25 07:39:37 +00:00
|
|
|
} else {
|
2019-08-14 21:31:07 +00:00
|
|
|
// Just replace.
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->insert_text_at_cursor(replace_text);
|
2018-03-25 07:39:37 +00:00
|
|
|
}
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2018-03-25 07:39:37 +00:00
|
|
|
rc++;
|
|
|
|
} while (search_next());
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->end_complex_operation();
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
replace_all_mode = false;
|
|
|
|
|
2019-08-14 21:31:07 +00:00
|
|
|
// Restore editor state (selection, cursor, scroll).
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->cursor_set_line(orig_cursor.x);
|
|
|
|
text_editor->cursor_set_column(orig_cursor.y);
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
if (selection_enabled && is_selection_only()) {
|
2019-08-14 21:31:07 +00:00
|
|
|
// Reselect.
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
|
2016-05-28 16:25:45 +00:00
|
|
|
} else {
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->deselect();
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->set_v_scroll(vsval);
|
2020-03-12 12:37:40 +00:00
|
|
|
matches_label->add_theme_color_override("font_color", rc > 0 ? get_theme_color("font_color", "Label") : get_theme_color("error_color", "Editor"));
|
2019-11-15 12:47:27 +00:00
|
|
|
matches_label->set_text(vformat(TTR("%d replaced."), rc));
|
2018-01-17 23:52:17 +00:00
|
|
|
|
2021-05-28 09:12:58 +00:00
|
|
|
text_editor->call_deferred("connect", "text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
|
2019-07-08 17:33:04 +00:00
|
|
|
results_count = -1;
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void FindReplaceBar::_get_search_from(int &r_line, int &r_col) {
|
2020-07-24 14:50:35 +00:00
|
|
|
r_line = text_editor->cursor_get_line();
|
|
|
|
r_col = text_editor->cursor_get_column();
|
2016-05-30 16:15:41 +00:00
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
if (text_editor->is_selection_active() && is_selection_only()) {
|
2020-03-30 21:27:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
|
|
|
|
r_col = result_col;
|
2016-05-30 16:15:41 +00:00
|
|
|
}
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 17:33:04 +00:00
|
|
|
void FindReplaceBar::_update_results_count() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (results_count != -1) {
|
2019-07-08 17:33:04 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-07-08 17:33:04 +00:00
|
|
|
|
|
|
|
results_count = 0;
|
|
|
|
|
|
|
|
String searched = get_search_text();
|
2020-12-15 12:04:21 +00:00
|
|
|
if (searched.is_empty()) {
|
2020-05-10 10:56:01 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-07-08 17:33:04 +00:00
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
String full_text = text_editor->get_text();
|
2019-07-08 17:33:04 +00:00
|
|
|
|
|
|
|
int from_pos = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
int pos = is_case_sensitive() ? full_text.find(searched, from_pos) : full_text.findn(searched, from_pos);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (pos == -1) {
|
2020-05-10 10:56:01 +00:00
|
|
|
break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-07-08 17:33:04 +00:00
|
|
|
|
2020-06-15 03:39:36 +00:00
|
|
|
int pos_subsequent = pos + searched.length();
|
2019-07-08 17:33:04 +00:00
|
|
|
if (is_whole_words()) {
|
2020-06-12 09:28:43 +00:00
|
|
|
from_pos = pos + 1; // Making sure we won't hit the same match next time, if we get out via a continue.
|
2020-06-15 03:39:36 +00:00
|
|
|
if (pos > 0 && !(is_symbol(full_text[pos - 1]) || full_text[pos - 1] == '\n')) {
|
2019-07-08 17:33:04 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-06-15 03:39:36 +00:00
|
|
|
if (pos_subsequent < full_text.length() && !(is_symbol(full_text[pos_subsequent]) || full_text[pos_subsequent] == '\n')) {
|
2019-07-08 17:33:04 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-07-08 17:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
results_count++;
|
2020-06-15 03:39:36 +00:00
|
|
|
from_pos = pos_subsequent;
|
2019-07-08 17:33:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 06:47:09 +00:00
|
|
|
void FindReplaceBar::_update_matches_label() {
|
2020-12-15 12:04:21 +00:00
|
|
|
if (search_text->get_text().is_empty() || results_count == -1) {
|
2019-08-09 06:47:09 +00:00
|
|
|
matches_label->hide();
|
|
|
|
} else {
|
|
|
|
matches_label->show();
|
|
|
|
|
2020-03-12 12:37:40 +00:00
|
|
|
matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color("font_color", "Label") : get_theme_color("error_color", "Editor"));
|
2019-08-09 06:47:09 +00:00
|
|
|
matches_label->set_text(vformat(results_count == 1 ? TTR("%d match.") : TTR("%d matches."), results_count));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 16:15:41 +00:00
|
|
|
bool FindReplaceBar::search_current() {
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t flags = 0;
|
2016-05-30 16:15:41 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (is_whole_words()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_WHOLE_WORDS;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (is_case_sensitive()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_MATCH_CASE;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-05-30 16:15:41 +00:00
|
|
|
|
|
|
|
int line, col;
|
|
|
|
_get_search_from(line, col);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return _search(flags, line, col);
|
2016-05-30 16:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FindReplaceBar::search_prev() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_visible()) {
|
2019-08-12 18:31:53 +00:00
|
|
|
popup_search(true);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-08-12 18:31:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t flags = 0;
|
2016-05-30 16:15:41 +00:00
|
|
|
String text = get_search_text();
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (is_whole_words()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_WHOLE_WORDS;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (is_case_sensitive()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_MATCH_CASE;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-05-30 16:15:41 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_BACKWARDS;
|
2016-05-30 16:15:41 +00:00
|
|
|
|
|
|
|
int line, col;
|
|
|
|
_get_search_from(line, col);
|
2020-07-24 14:50:35 +00:00
|
|
|
if (text_editor->is_selection_active()) {
|
2019-08-14 21:31:07 +00:00
|
|
|
col--; // Skip currently selected word.
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-05-30 16:15:41 +00:00
|
|
|
|
2019-09-02 12:15:55 +00:00
|
|
|
col -= text.length();
|
|
|
|
if (col < 0) {
|
|
|
|
line -= 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (line < 0) {
|
2020-07-24 14:50:35 +00:00
|
|
|
line = text_editor->get_line_count() - 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-07-24 14:50:35 +00:00
|
|
|
col = text_editor->get_line(line).length();
|
2016-05-30 16:15:41 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return _search(flags, line, col);
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 16:15:41 +00:00
|
|
|
bool FindReplaceBar::search_next() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_visible()) {
|
2019-08-12 18:31:53 +00:00
|
|
|
popup_search(true);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-08-12 18:31:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t flags = 0;
|
2019-07-04 20:43:03 +00:00
|
|
|
String text;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (replace_all_mode) {
|
2019-07-04 20:43:03 +00:00
|
|
|
text = get_replace_text();
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2019-07-04 20:43:03 +00:00
|
|
|
text = get_search_text();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-05-30 16:15:41 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (is_whole_words()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_WHOLE_WORDS;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (is_case_sensitive()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_MATCH_CASE;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-05-30 16:15:41 +00:00
|
|
|
|
|
|
|
int line, col;
|
|
|
|
_get_search_from(line, col);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (line == result_line && col == result_col) {
|
|
|
|
col += text.length();
|
2020-07-24 14:50:35 +00:00
|
|
|
if (col > text_editor->get_line(line).length()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
line += 1;
|
2020-07-24 14:50:35 +00:00
|
|
|
if (line >= text_editor->get_line_count()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
line = 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
col = 0;
|
2016-05-30 16:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return _search(flags, line, col);
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_hide_bar() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (replace_text->has_focus() || search_text->has_focus()) {
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->grab_focus();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-05-30 16:15:41 +00:00
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->set_search_text("");
|
2016-05-30 16:15:41 +00:00
|
|
|
result_line = -1;
|
|
|
|
result_col = -1;
|
2016-05-28 16:25:45 +00:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:31:53 +00:00
|
|
|
void FindReplaceBar::_show_search(bool p_focus_replace, bool p_show_only) {
|
2016-05-28 16:25:45 +00:00
|
|
|
show();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_show_only) {
|
2019-08-12 18:31:53 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-08-12 18:31:53 +00:00
|
|
|
|
|
|
|
if (p_focus_replace) {
|
|
|
|
search_text->deselect();
|
|
|
|
replace_text->call_deferred("grab_focus");
|
|
|
|
} else {
|
|
|
|
replace_text->deselect();
|
|
|
|
search_text->call_deferred("grab_focus");
|
|
|
|
}
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
if (text_editor->is_selection_active() && !selection_only->is_pressed()) {
|
|
|
|
search_text->set_text(text_editor->get_selection_text());
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 12:04:21 +00:00
|
|
|
if (!get_search_text().is_empty()) {
|
2019-08-12 18:31:53 +00:00
|
|
|
if (p_focus_replace) {
|
|
|
|
replace_text->select_all();
|
2021-03-28 18:31:25 +00:00
|
|
|
replace_text->set_caret_column(replace_text->get_text().length());
|
2019-08-12 18:31:53 +00:00
|
|
|
} else {
|
|
|
|
search_text->select_all();
|
2021-03-28 18:31:25 +00:00
|
|
|
search_text->set_caret_column(search_text->get_text().length());
|
2019-08-12 18:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
results_count = -1;
|
|
|
|
_update_results_count();
|
|
|
|
_update_matches_label();
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:31:53 +00:00
|
|
|
void FindReplaceBar::popup_search(bool p_show_only) {
|
2019-10-01 12:57:11 +00:00
|
|
|
replace_text->hide();
|
2018-01-17 23:52:17 +00:00
|
|
|
hbc_button_replace->hide();
|
|
|
|
hbc_option_replace->hide();
|
2019-10-01 12:57:11 +00:00
|
|
|
|
2019-08-12 18:31:53 +00:00
|
|
|
_show_search(false, p_show_only);
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::popup_replace() {
|
2018-01-17 23:52:17 +00:00
|
|
|
if (!replace_text->is_visible_in_tree()) {
|
|
|
|
replace_text->show();
|
|
|
|
hbc_button_replace->show();
|
|
|
|
hbc_option_replace->show();
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
selection_only->set_pressed((text_editor->is_selection_active() && text_editor->get_selection_from_line() < text_editor->get_selection_to_line()));
|
2016-09-11 13:01:52 +00:00
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
_show_search(is_visible() || text_editor->is_selection_active());
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_search_options_changed(bool p_pressed) {
|
2019-07-08 17:33:04 +00:00
|
|
|
results_count = -1;
|
2016-05-28 16:25:45 +00:00
|
|
|
search_current();
|
|
|
|
}
|
|
|
|
|
2016-05-30 16:15:41 +00:00
|
|
|
void FindReplaceBar::_editor_text_changed() {
|
2019-07-08 17:33:04 +00:00
|
|
|
results_count = -1;
|
2017-01-13 13:45:50 +00:00
|
|
|
if (is_visible_in_tree()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
preserve_cursor = true;
|
2016-05-30 16:15:41 +00:00
|
|
|
search_current();
|
2017-03-05 15:44:50 +00:00
|
|
|
preserve_cursor = false;
|
2016-05-30 16:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void FindReplaceBar::_search_text_changed(const String &p_text) {
|
2019-07-08 17:33:04 +00:00
|
|
|
results_count = -1;
|
2016-05-28 16:25:45 +00:00
|
|
|
search_current();
|
|
|
|
}
|
|
|
|
|
2021-06-16 16:43:34 +00:00
|
|
|
void FindReplaceBar::_search_text_submitted(const String &p_text) {
|
2020-04-28 13:19:37 +00:00
|
|
|
if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
|
2019-04-05 23:19:00 +00:00
|
|
|
search_prev();
|
|
|
|
} else {
|
|
|
|
search_next();
|
|
|
|
}
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 16:43:34 +00:00
|
|
|
void FindReplaceBar::_replace_text_submitted(const String &p_text) {
|
2020-07-24 14:50:35 +00:00
|
|
|
if (selection_only->is_pressed() && text_editor->is_selection_active()) {
|
2016-09-11 13:01:52 +00:00
|
|
|
_replace_all();
|
|
|
|
_hide_bar();
|
2020-04-28 13:19:37 +00:00
|
|
|
} else if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
|
2020-02-08 20:46:04 +00:00
|
|
|
_replace();
|
|
|
|
search_prev();
|
|
|
|
} else {
|
|
|
|
_replace();
|
2016-09-11 13:01:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 16:25:45 +00: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) {
|
2018-01-17 23:52:17 +00:00
|
|
|
emit_signal("error", p_label);
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 23:05:41 +00: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 13:38:29 +00:00
|
|
|
if (!p_text_editor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-08 17:33:04 +00:00
|
|
|
results_count = -1;
|
2021-06-02 23:05:41 +00:00
|
|
|
base_text_editor = p_text_editor;
|
|
|
|
text_editor = base_text_editor->get_text_editor();
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor->connect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
|
2021-06-02 23:05:41 +00:00
|
|
|
|
|
|
|
_update_results_count();
|
|
|
|
_update_matches_label();
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method("_unhandled_input", &FindReplaceBar::_unhandled_input);
|
|
|
|
ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("search"));
|
2018-01-17 23:52:17 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("error"));
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FindReplaceBar::FindReplaceBar() {
|
2019-07-08 17:33:04 +00:00
|
|
|
results_count = -1;
|
2017-03-05 15:44:50 +00:00
|
|
|
replace_all_mode = false;
|
|
|
|
preserve_cursor = false;
|
2016-05-30 16:15:41 +00:00
|
|
|
|
2018-01-17 23:52:17 +00:00
|
|
|
vbc_lineedit = memnew(VBoxContainer);
|
2019-02-10 16:17:29 +00:00
|
|
|
add_child(vbc_lineedit);
|
2019-08-09 06:47:09 +00:00
|
|
|
vbc_lineedit->set_alignment(ALIGN_CENTER);
|
2018-01-17 23:52:17 +00:00
|
|
|
vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
VBoxContainer *vbc_button = memnew(VBoxContainer);
|
2019-02-10 16:17:29 +00:00
|
|
|
add_child(vbc_button);
|
2018-01-17 23:52:17 +00:00
|
|
|
VBoxContainer *vbc_option = memnew(VBoxContainer);
|
2019-02-10 16:17:29 +00:00
|
|
|
add_child(vbc_option);
|
2018-01-17 23:52:17 +00:00
|
|
|
|
|
|
|
HBoxContainer *hbc_button_search = memnew(HBoxContainer);
|
|
|
|
vbc_button->add_child(hbc_button_search);
|
2019-08-09 06:47:09 +00:00
|
|
|
hbc_button_search->set_alignment(ALIGN_END);
|
2018-01-17 23:52:17 +00:00
|
|
|
hbc_button_replace = memnew(HBoxContainer);
|
|
|
|
vbc_button->add_child(hbc_button_replace);
|
2019-08-09 06:47:09 +00:00
|
|
|
hbc_button_replace->set_alignment(ALIGN_END);
|
2018-01-17 23:52:17 +00: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);
|
|
|
|
|
|
|
|
// search toolbar
|
2016-05-28 16:25:45 +00:00
|
|
|
search_text = memnew(LineEdit);
|
2018-01-17 23:52:17 +00:00
|
|
|
vbc_lineedit->add_child(search_text);
|
|
|
|
search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
|
2020-02-21 17:28:45 +00:00
|
|
|
search_text->connect("text_changed", callable_mp(this, &FindReplaceBar::_search_text_changed));
|
2021-06-16 16:43:34 +00:00
|
|
|
search_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_search_text_submitted));
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2019-08-09 06:47:09 +00:00
|
|
|
matches_label = memnew(Label);
|
|
|
|
hbc_button_search->add_child(matches_label);
|
|
|
|
matches_label->hide();
|
|
|
|
|
2020-06-19 18:49:04 +00:00
|
|
|
find_prev = memnew(Button);
|
|
|
|
find_prev->set_flat(true);
|
2018-01-17 23:52:17 +00:00
|
|
|
hbc_button_search->add_child(find_prev);
|
2016-05-28 16:25:45 +00:00
|
|
|
find_prev->set_focus_mode(FOCUS_NONE);
|
2020-02-21 17:28:45 +00:00
|
|
|
find_prev->connect("pressed", callable_mp(this, &FindReplaceBar::search_prev));
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2020-06-19 18:49:04 +00:00
|
|
|
find_next = memnew(Button);
|
|
|
|
find_next->set_flat(true);
|
2018-01-17 23:52:17 +00:00
|
|
|
hbc_button_search->add_child(find_next);
|
2016-05-28 16:25:45 +00:00
|
|
|
find_next->set_focus_mode(FOCUS_NONE);
|
2020-02-21 17:28:45 +00:00
|
|
|
find_next->connect("pressed", callable_mp(this, &FindReplaceBar::search_next));
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
case_sensitive = memnew(CheckBox);
|
2018-01-17 23:52:17 +00:00
|
|
|
hbc_option_search->add_child(case_sensitive);
|
2016-05-28 16:25:45 +00:00
|
|
|
case_sensitive->set_text(TTR("Match Case"));
|
|
|
|
case_sensitive->set_focus_mode(FOCUS_NONE);
|
2020-02-21 17:28:45 +00:00
|
|
|
case_sensitive->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
whole_words = memnew(CheckBox);
|
2018-01-17 23:52:17 +00:00
|
|
|
hbc_option_search->add_child(whole_words);
|
2016-05-28 16:25:45 +00:00
|
|
|
whole_words->set_text(TTR("Whole Words"));
|
|
|
|
whole_words->set_focus_mode(FOCUS_NONE);
|
2020-02-21 17:28:45 +00:00
|
|
|
whole_words->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2018-01-17 23:52:17 +00:00
|
|
|
// replace toolbar
|
|
|
|
replace_text = memnew(LineEdit);
|
|
|
|
vbc_lineedit->add_child(replace_text);
|
|
|
|
replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
|
2021-06-16 16:43:34 +00:00
|
|
|
replace_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_replace_text_submitted));
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2018-01-17 23:52:17 +00:00
|
|
|
replace = memnew(Button);
|
|
|
|
hbc_button_replace->add_child(replace);
|
|
|
|
replace->set_text(TTR("Replace"));
|
2020-02-21 17:28:45 +00:00
|
|
|
replace->connect("pressed", callable_mp(this, &FindReplaceBar::_replace));
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2018-01-17 23:52:17 +00:00
|
|
|
replace_all = memnew(Button);
|
|
|
|
hbc_button_replace->add_child(replace_all);
|
|
|
|
replace_all->set_text(TTR("Replace All"));
|
2020-02-21 17:28:45 +00:00
|
|
|
replace_all->connect("pressed", callable_mp(this, &FindReplaceBar::_replace_all));
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
selection_only = memnew(CheckBox);
|
2018-01-17 23:52:17 +00:00
|
|
|
hbc_option_replace->add_child(selection_only);
|
2016-05-28 16:25:45 +00:00
|
|
|
selection_only->set_text(TTR("Selection Only"));
|
|
|
|
selection_only->set_focus_mode(FOCUS_NONE);
|
2020-02-21 17:28:45 +00:00
|
|
|
selection_only->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
|
2018-01-17 23:52:17 +00:00
|
|
|
|
|
|
|
hide_button = memnew(TextureButton);
|
|
|
|
add_child(hide_button);
|
|
|
|
hide_button->set_focus_mode(FOCUS_NONE);
|
2020-02-21 17:28:45 +00:00
|
|
|
hide_button->connect("pressed", callable_mp(this, &FindReplaceBar::_hide_bar));
|
2018-05-16 07:31:40 +00:00
|
|
|
hide_button->set_v_size_flags(SIZE_SHRINK_CENTER);
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
/*** CODE EDITOR ****/
|
|
|
|
|
2019-05-09 16:26:33 +00:00
|
|
|
// This function should be used to handle shortcuts that could otherwise
|
|
|
|
// be handled too late if they weren't handled here.
|
|
|
|
void CodeTextEditor::_input(const Ref<InputEvent> &event) {
|
2021-04-05 06:52:21 +00:00
|
|
|
ERR_FAIL_COND(event.is_null());
|
|
|
|
|
2019-05-09 16:26:33 +00:00
|
|
|
const Ref<InputEventKey> key_event = event;
|
2020-04-25 15:28:22 +00:00
|
|
|
if (!key_event.is_valid() || !key_event->is_pressed() || !text_editor->has_focus()) {
|
2019-05-09 16:26:33 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-05-09 16:26:33 +00: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 21:09:55 +00:00
|
|
|
if (ED_IS_SHORTCUT("script_text_editor/delete_line", key_event)) {
|
|
|
|
delete_lines();
|
|
|
|
accept_event();
|
|
|
|
return;
|
|
|
|
}
|
2021-06-29 10:09:07 +00:00
|
|
|
if (ED_IS_SHORTCUT("script_text_editor/duplicate_selection", key_event)) {
|
|
|
|
duplicate_selection();
|
2020-02-16 21:09:55 +00:00
|
|
|
accept_event();
|
|
|
|
return;
|
|
|
|
}
|
2019-05-09 16:26:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
|
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2016-06-17 19:58:50 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mb.is_valid()) {
|
2021-04-24 20:33:50 +00:00
|
|
|
if (mb->is_pressed() && mb->is_command_pressed()) {
|
2021-01-08 03:37:37 +00:00
|
|
|
if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_UP) {
|
2016-07-14 19:03:40 +00:00
|
|
|
_zoom_in();
|
2021-01-08 03:37:37 +00:00
|
|
|
} else if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_DOWN) {
|
2016-07-14 19:03:40 +00:00
|
|
|
_zoom_out();
|
2016-06-17 19:58:50 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-20 15:38:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 20:49:39 +00:00
|
|
|
Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
|
|
|
|
if (magnify_gesture.is_valid()) {
|
2020-09-18 07:36:10 +00:00
|
|
|
font_size = text_editor->get_theme_font_size("font_size");
|
|
|
|
font_size *= powf(magnify_gesture->get_factor(), 0.25);
|
2017-11-01 20:49:39 +00:00
|
|
|
|
2020-09-18 07:36:10 +00:00
|
|
|
_add_font_size((int)font_size - text_editor->get_theme_font_size("font_size"));
|
2017-11-01 20:49:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventKey> k = p_event;
|
|
|
|
|
|
|
|
if (k.is_valid()) {
|
|
|
|
if (k->is_pressed()) {
|
2016-07-14 19:03:40 +00:00
|
|
|
if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
|
|
|
|
_zoom_in();
|
|
|
|
}
|
|
|
|
if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
|
|
|
|
_zoom_out();
|
|
|
|
}
|
|
|
|
if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
|
|
|
|
_reset_zoom();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-17 19:58:50 +00:00
|
|
|
|
2016-07-14 19:03:40 +00:00
|
|
|
void CodeTextEditor::_zoom_in() {
|
2018-05-05 07:40:35 +00:00
|
|
|
font_resize_val += MAX(EDSCALE, 1.0f);
|
2020-09-18 07:36:10 +00:00
|
|
|
_zoom_changed();
|
2016-07-14 19:03:40 +00:00
|
|
|
}
|
2016-06-17 19:58:50 +00:00
|
|
|
|
2016-07-14 19:03:40 +00:00
|
|
|
void CodeTextEditor::_zoom_out() {
|
2018-05-05 07:40:35 +00:00
|
|
|
font_resize_val -= MAX(EDSCALE, 1.0f);
|
2020-09-18 07:36:10 +00:00
|
|
|
_zoom_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_zoom_changed() {
|
|
|
|
if (font_resize_timer->get_time_left() == 0) {
|
|
|
|
font_resize_timer->start();
|
|
|
|
}
|
2016-07-14 19:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_reset_zoom() {
|
2020-09-18 07:36:10 +00:00
|
|
|
EditorSettings::get_singleton()->set("interface/editor/code_font_size", 14);
|
|
|
|
text_editor->add_theme_font_size_override("font_size", 14 * EDSCALE);
|
2016-06-17 19:58:50 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void CodeTextEditor::_line_col_changed() {
|
2018-05-03 17:03:20 +00:00
|
|
|
String line = text_editor->get_line(text_editor->cursor_get_line());
|
|
|
|
|
|
|
|
int positional_column = 0;
|
|
|
|
for (int i = 0; i < text_editor->cursor_get_column(); i++) {
|
|
|
|
if (line[i] == '\t') {
|
|
|
|
positional_column += text_editor->get_indent_size(); //tab size
|
|
|
|
} else {
|
|
|
|
positional_column += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 19:31:05 +00:00
|
|
|
StringBuilder sb;
|
|
|
|
sb.append("(");
|
|
|
|
sb.append(itos(text_editor->cursor_get_line() + 1).lpad(3));
|
|
|
|
sb.append(",");
|
|
|
|
sb.append(itos(positional_column + 1).lpad(3));
|
|
|
|
sb.append(")");
|
|
|
|
|
|
|
|
line_and_col_txt->set_text(sb.as_string());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_text_changed() {
|
2017-09-06 01:02:51 +00:00
|
|
|
if (text_editor->is_insert_text_operation()) {
|
|
|
|
code_complete_timer->start();
|
|
|
|
}
|
2017-09-28 16:30:20 +00:00
|
|
|
|
|
|
|
idle->start();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2014-05-04 03:22:49 +00:00
|
|
|
void CodeTextEditor::_code_complete_timer_timeout() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_visible_in_tree()) {
|
2014-12-17 01:31:57 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-09-13 20:14:20 +00:00
|
|
|
text_editor->request_code_completion();
|
2014-05-04 03:22:49 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 01:31:57 +00:00
|
|
|
void CodeTextEditor::_complete_request() {
|
2019-06-13 09:17:20 +00:00
|
|
|
List<ScriptCodeCompletionOption> entries;
|
2020-09-13 20:14:20 +00:00
|
|
|
String ctext = text_editor->get_text_for_code_completion();
|
2017-03-05 15:44:50 +00:00
|
|
|
_code_complete_script(ctext, &entries);
|
2017-08-24 03:06:56 +00:00
|
|
|
bool forced = false;
|
2016-08-02 22:11:05 +00:00
|
|
|
if (code_complete_func) {
|
2017-08-24 03:06:56 +00:00
|
|
|
code_complete_func(code_complete_ud, ctext, &entries, forced);
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
if (entries.size() == 0) {
|
2014-12-17 01:31:57 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-06-13 09:17:20 +00:00
|
|
|
for (List<ScriptCodeCompletionOption>::Element *E = entries.front(); E; E = E->next()) {
|
2020-09-13 20:14:20 +00:00
|
|
|
ScriptCodeCompletionOption &e = E->get();
|
|
|
|
|
|
|
|
Color font_color = completion_font_color;
|
|
|
|
if (e.insert_text.begins_with("\"") || e.insert_text.begins_with("\'")) {
|
|
|
|
font_color = completion_string_color;
|
|
|
|
} else if (e.insert_text.begins_with("#") || e.insert_text.begins_with("//")) {
|
|
|
|
font_color = completion_comment_color;
|
2020-05-03 16:08:15 +00:00
|
|
|
}
|
2020-09-13 20:14:20 +00: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);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-09-13 20:14:20 +00:00
|
|
|
text_editor->update_code_completion_options(forced);
|
2019-06-13 09:32:03 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptCodeCompletionOption &p_option) {
|
|
|
|
Ref<Texture2D> tex;
|
2019-06-13 09:32:03 +00:00
|
|
|
switch (p_option.kind) {
|
|
|
|
case ScriptCodeCompletionOption::KIND_CLASS: {
|
2020-03-12 12:37:40 +00:00
|
|
|
if (has_theme_icon(p_option.display, "EditorIcons")) {
|
|
|
|
tex = get_theme_icon(p_option.display, "EditorIcons");
|
2019-06-13 09:32:03 +00:00
|
|
|
} else {
|
2020-03-12 12:37:40 +00:00
|
|
|
tex = get_theme_icon("Object", "EditorIcons");
|
2019-06-13 09:32:03 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case ScriptCodeCompletionOption::KIND_ENUM:
|
2020-03-12 12:37:40 +00:00
|
|
|
tex = get_theme_icon("Enum", "EditorIcons");
|
2019-06-13 09:32:03 +00:00
|
|
|
break;
|
|
|
|
case ScriptCodeCompletionOption::KIND_FILE_PATH:
|
2020-03-12 12:37:40 +00:00
|
|
|
tex = get_theme_icon("File", "EditorIcons");
|
2019-06-13 09:32:03 +00:00
|
|
|
break;
|
|
|
|
case ScriptCodeCompletionOption::KIND_NODE_PATH:
|
2020-03-12 12:37:40 +00:00
|
|
|
tex = get_theme_icon("NodePath", "EditorIcons");
|
2019-06-13 09:32:03 +00:00
|
|
|
break;
|
|
|
|
case ScriptCodeCompletionOption::KIND_VARIABLE:
|
2020-03-12 12:37:40 +00:00
|
|
|
tex = get_theme_icon("Variant", "EditorIcons");
|
2019-06-13 09:32:03 +00:00
|
|
|
break;
|
|
|
|
case ScriptCodeCompletionOption::KIND_CONSTANT:
|
2020-03-12 12:37:40 +00:00
|
|
|
tex = get_theme_icon("MemberConstant", "EditorIcons");
|
2019-06-13 09:32:03 +00:00
|
|
|
break;
|
|
|
|
case ScriptCodeCompletionOption::KIND_MEMBER:
|
2020-03-12 12:37:40 +00:00
|
|
|
tex = get_theme_icon("MemberProperty", "EditorIcons");
|
2019-06-13 09:32:03 +00:00
|
|
|
break;
|
|
|
|
case ScriptCodeCompletionOption::KIND_SIGNAL:
|
2020-03-12 12:37:40 +00:00
|
|
|
tex = get_theme_icon("MemberSignal", "EditorIcons");
|
2019-06-13 09:32:03 +00:00
|
|
|
break;
|
|
|
|
case ScriptCodeCompletionOption::KIND_FUNCTION:
|
2020-03-12 12:37:40 +00:00
|
|
|
tex = get_theme_icon("MemberMethod", "EditorIcons");
|
2019-06-13 09:32:03 +00:00
|
|
|
break;
|
|
|
|
case ScriptCodeCompletionOption::KIND_PLAIN_TEXT:
|
2020-12-04 15:39:45 +00:00
|
|
|
tex = get_theme_icon("BoxMesh", "EditorIcons");
|
2019-06-13 09:32:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
2020-03-12 12:37:40 +00:00
|
|
|
tex = get_theme_icon("String", "EditorIcons");
|
2019-06-13 09:32:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return tex;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 07:36:10 +00: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) {
|
|
|
|
int old_size = text_editor->get_theme_font_size("font_size");
|
|
|
|
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);
|
|
|
|
text_editor->add_theme_font_size_override("font_size", new_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-10 08:38:12 +00:00
|
|
|
void CodeTextEditor::update_editor_settings() {
|
2020-05-03 16:08:15 +00:00
|
|
|
completion_font_color = EDITOR_GET("text_editor/highlighting/completion_font_color");
|
|
|
|
completion_string_color = EDITOR_GET("text_editor/highlighting/string_color");
|
|
|
|
completion_comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
|
|
|
|
|
2019-09-01 11:28:57 +00:00
|
|
|
text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
|
|
|
|
text_editor->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
|
2017-04-26 11:14:03 +00:00
|
|
|
text_editor->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type"));
|
2017-04-17 13:24:30 +00:00
|
|
|
text_editor->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
|
2021-06-15 14:05:01 +00:00
|
|
|
text_editor->set_auto_indent_enabled(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent"));
|
2017-01-05 22:41:36 +00:00
|
|
|
text_editor->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
|
2019-04-23 23:33:20 +00:00
|
|
|
text_editor->set_draw_spaces(EditorSettings::get_singleton()->get("text_editor/indent/draw_spaces"));
|
2019-09-01 11:28:57 +00:00
|
|
|
text_editor->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/navigation/smooth_scrolling"));
|
|
|
|
text_editor->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/navigation/v_scroll_speed"));
|
|
|
|
text_editor->set_draw_minimap(EditorSettings::get_singleton()->get("text_editor/navigation/show_minimap"));
|
2020-01-11 04:43:32 +00:00
|
|
|
text_editor->set_minimap_width((int)EditorSettings::get_singleton()->get("text_editor/navigation/minimap_width") * EDSCALE);
|
2020-07-25 17:27:35 +00:00
|
|
|
text_editor->set_draw_line_numbers(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_numbers"));
|
2019-09-01 11:28:57 +00:00
|
|
|
text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/appearance/line_numbers_zero_padded"));
|
2020-07-26 14:57:23 +00:00
|
|
|
text_editor->set_draw_bookmarks_gutter(EditorSettings::get_singleton()->get("text_editor/appearance/show_bookmark_gutter"));
|
2021-03-13 14:09:49 +00:00
|
|
|
text_editor->set_line_folding_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/code_folding"));
|
2019-09-01 11:28:57 +00:00
|
|
|
text_editor->set_draw_fold_gutter(EditorSettings::get_singleton()->get("text_editor/appearance/code_folding"));
|
|
|
|
text_editor->set_wrap_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/word_wrap"));
|
2020-02-15 01:59:59 +00:00
|
|
|
text_editor->set_show_line_length_guidelines(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_length_guidelines"));
|
|
|
|
text_editor->set_line_length_guideline_soft_column(EditorSettings::get_singleton()->get("text_editor/appearance/line_length_guideline_soft_column"));
|
|
|
|
text_editor->set_line_length_guideline_hard_column(EditorSettings::get_singleton()->get("text_editor/appearance/line_length_guideline_hard_column"));
|
2019-09-01 11:28:57 +00:00
|
|
|
text_editor->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
|
|
|
|
text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
|
2017-01-05 22:41:36 +00:00
|
|
|
text_editor->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
|
|
|
|
text_editor->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed"));
|
2019-09-01 11:28:57 +00:00
|
|
|
text_editor->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
|
2016-10-10 08:38:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 23:05:41 +00: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-25 22:49:35 +00:00
|
|
|
void CodeTextEditor::trim_trailing_whitespace() {
|
|
|
|
bool trimed_whitespace = false;
|
|
|
|
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")) {
|
|
|
|
if (!trimed_whitespace) {
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
trimed_whitespace = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trimed_whitespace) {
|
|
|
|
text_editor->end_complex_operation();
|
|
|
|
text_editor->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 21:27:32 +00:00
|
|
|
void CodeTextEditor::insert_final_newline() {
|
|
|
|
int final_line = text_editor->get_line_count() - 1;
|
|
|
|
|
|
|
|
String line = text_editor->get_line(final_line);
|
|
|
|
|
|
|
|
//length 0 means it's already an empty line,
|
|
|
|
//no need to add a newline
|
|
|
|
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();
|
|
|
|
text_editor->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 22:49:35 +00:00
|
|
|
void CodeTextEditor::convert_indent_to_spaces() {
|
|
|
|
int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
|
|
|
|
String indent = "";
|
|
|
|
|
|
|
|
for (int i = 0; i < indent_size; i++) {
|
|
|
|
indent += " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
int cursor_line = text_editor->cursor_get_line();
|
|
|
|
int cursor_column = text_editor->cursor_get_column();
|
|
|
|
|
|
|
|
bool changed_indentation = false;
|
|
|
|
for (int i = 0; i < text_editor->get_line_count(); i++) {
|
|
|
|
String line = text_editor->get_line(i);
|
|
|
|
|
|
|
|
if (line.length() <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int j = 0;
|
|
|
|
while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
|
|
|
|
if (line[j] == '\t') {
|
|
|
|
if (!changed_indentation) {
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
changed_indentation = true;
|
|
|
|
}
|
|
|
|
if (cursor_line == i && cursor_column > j) {
|
|
|
|
cursor_column += indent_size - 1;
|
|
|
|
}
|
2020-02-13 15:42:49 +00:00
|
|
|
line = line.left(j) + indent + line.substr(j + 1);
|
2018-05-25 22:49:35 +00:00
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
if (changed_indentation) {
|
|
|
|
text_editor->set_line(i, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed_indentation) {
|
|
|
|
text_editor->cursor_set_column(cursor_column);
|
|
|
|
text_editor->end_complex_operation();
|
|
|
|
text_editor->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::convert_indent_to_tabs() {
|
|
|
|
int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
|
|
|
|
indent_size -= 1;
|
|
|
|
|
|
|
|
int cursor_line = text_editor->cursor_get_line();
|
|
|
|
int cursor_column = text_editor->cursor_get_column();
|
|
|
|
|
|
|
|
bool changed_indentation = false;
|
|
|
|
for (int i = 0; i < text_editor->get_line_count(); i++) {
|
|
|
|
String line = text_editor->get_line(i);
|
|
|
|
|
|
|
|
if (line.length() <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int j = 0;
|
|
|
|
int space_count = -1;
|
|
|
|
while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
|
|
|
|
if (line[j] != '\t') {
|
|
|
|
space_count++;
|
|
|
|
|
|
|
|
if (space_count == indent_size) {
|
|
|
|
if (!changed_indentation) {
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
changed_indentation = true;
|
|
|
|
}
|
|
|
|
if (cursor_line == i && cursor_column > j) {
|
|
|
|
cursor_column -= indent_size;
|
|
|
|
}
|
2020-02-13 15:42:49 +00:00
|
|
|
line = line.left(j - indent_size) + "\t" + line.substr(j + 1);
|
2018-05-25 22:49:35 +00:00
|
|
|
j = 0;
|
|
|
|
space_count = -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
space_count = -1;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
if (changed_indentation) {
|
|
|
|
text_editor->set_line(i, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed_indentation) {
|
|
|
|
text_editor->cursor_set_column(cursor_column);
|
|
|
|
text_editor->end_complex_operation();
|
|
|
|
text_editor->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::convert_case(CaseStyle p_case) {
|
|
|
|
if (!text_editor->is_selection_active()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
|
|
|
|
int begin = text_editor->get_selection_from_line();
|
|
|
|
int end = text_editor->get_selection_to_line();
|
|
|
|
int begin_col = text_editor->get_selection_from_column();
|
|
|
|
int end_col = text_editor->get_selection_to_column();
|
|
|
|
|
|
|
|
for (int i = begin; i <= end; i++) {
|
|
|
|
int len = text_editor->get_line(i).length();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (i == end) {
|
2019-06-03 19:52:50 +00:00
|
|
|
len = end_col;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (i == begin) {
|
2018-05-25 22:49:35 +00:00
|
|
|
len -= begin_col;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-25 22:49:35 +00:00
|
|
|
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) {
|
2020-02-13 15:42:49 +00:00
|
|
|
new_line = new_line + text_editor->get_line(i).substr(end_col);
|
2018-05-25 22:49:35 +00:00
|
|
|
}
|
|
|
|
text_editor->set_line(i, new_line);
|
|
|
|
}
|
|
|
|
text_editor->end_complex_operation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::move_lines_up() {
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
if (text_editor->is_selection_active()) {
|
|
|
|
int from_line = text_editor->get_selection_from_line();
|
|
|
|
int from_col = text_editor->get_selection_from_column();
|
|
|
|
int to_line = text_editor->get_selection_to_line();
|
|
|
|
int to_column = text_editor->get_selection_to_column();
|
2020-11-11 10:29:32 +00:00
|
|
|
int cursor_line = text_editor->cursor_get_line();
|
2018-05-25 22:49:35 +00:00
|
|
|
|
|
|
|
for (int i = from_line; i <= to_line; i++) {
|
|
|
|
int line_id = i;
|
|
|
|
int next_id = i - 1;
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (line_id == 0 || next_id < 0) {
|
2018-05-25 22:49:35 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-25 22:49:35 +00:00
|
|
|
|
|
|
|
text_editor->unfold_line(line_id);
|
|
|
|
text_editor->unfold_line(next_id);
|
|
|
|
|
|
|
|
text_editor->swap_lines(line_id, next_id);
|
|
|
|
text_editor->cursor_set_line(next_id);
|
|
|
|
}
|
|
|
|
int from_line_up = from_line > 0 ? from_line - 1 : from_line;
|
|
|
|
int to_line_up = to_line > 0 ? to_line - 1 : to_line;
|
2020-11-11 10:29:32 +00:00
|
|
|
int cursor_line_up = cursor_line > 0 ? cursor_line - 1 : cursor_line;
|
2018-05-25 22:49:35 +00:00
|
|
|
text_editor->select(from_line_up, from_col, to_line_up, to_column);
|
2020-11-11 10:29:32 +00:00
|
|
|
text_editor->cursor_set_line(cursor_line_up);
|
2018-05-25 22:49:35 +00:00
|
|
|
} else {
|
|
|
|
int line_id = text_editor->cursor_get_line();
|
|
|
|
int next_id = line_id - 1;
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (line_id == 0 || next_id < 0) {
|
2018-05-25 22:49:35 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-25 22:49:35 +00:00
|
|
|
|
|
|
|
text_editor->unfold_line(line_id);
|
|
|
|
text_editor->unfold_line(next_id);
|
|
|
|
|
|
|
|
text_editor->swap_lines(line_id, next_id);
|
|
|
|
text_editor->cursor_set_line(next_id);
|
|
|
|
}
|
|
|
|
text_editor->end_complex_operation();
|
|
|
|
text_editor->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::move_lines_down() {
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
if (text_editor->is_selection_active()) {
|
|
|
|
int from_line = text_editor->get_selection_from_line();
|
|
|
|
int from_col = text_editor->get_selection_from_column();
|
|
|
|
int to_line = text_editor->get_selection_to_line();
|
|
|
|
int to_column = text_editor->get_selection_to_column();
|
2020-11-11 10:29:32 +00:00
|
|
|
int cursor_line = text_editor->cursor_get_line();
|
2018-05-25 22:49:35 +00:00
|
|
|
|
|
|
|
for (int i = to_line; i >= from_line; i--) {
|
|
|
|
int line_id = i;
|
|
|
|
int next_id = i + 1;
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count()) {
|
2018-05-25 22:49:35 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-25 22:49:35 +00:00
|
|
|
|
|
|
|
text_editor->unfold_line(line_id);
|
|
|
|
text_editor->unfold_line(next_id);
|
|
|
|
|
|
|
|
text_editor->swap_lines(line_id, next_id);
|
|
|
|
text_editor->cursor_set_line(next_id);
|
|
|
|
}
|
|
|
|
int from_line_down = from_line < text_editor->get_line_count() ? from_line + 1 : from_line;
|
|
|
|
int to_line_down = to_line < text_editor->get_line_count() ? to_line + 1 : to_line;
|
2020-11-11 10:29:32 +00:00
|
|
|
int cursor_line_down = cursor_line < text_editor->get_line_count() ? cursor_line + 1 : cursor_line;
|
2018-05-25 22:49:35 +00:00
|
|
|
text_editor->select(from_line_down, from_col, to_line_down, to_column);
|
2020-11-11 10:29:32 +00:00
|
|
|
text_editor->cursor_set_line(cursor_line_down);
|
2018-05-25 22:49:35 +00:00
|
|
|
} else {
|
|
|
|
int line_id = text_editor->cursor_get_line();
|
|
|
|
int next_id = line_id + 1;
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count()) {
|
2018-05-25 22:49:35 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-25 22:49:35 +00:00
|
|
|
|
|
|
|
text_editor->unfold_line(line_id);
|
|
|
|
text_editor->unfold_line(next_id);
|
|
|
|
|
|
|
|
text_editor->swap_lines(line_id, next_id);
|
|
|
|
text_editor->cursor_set_line(next_id);
|
|
|
|
}
|
|
|
|
text_editor->end_complex_operation();
|
|
|
|
text_editor->update();
|
|
|
|
}
|
|
|
|
|
2019-12-09 06:29:24 +00:00
|
|
|
void CodeTextEditor::_delete_line(int p_line) {
|
|
|
|
// this is currently intended to be called within delete_lines()
|
2020-01-14 23:28:53 +00:00
|
|
|
// so `begin_complex_operation` is omitted here
|
2019-12-09 06:29:24 +00:00
|
|
|
text_editor->set_line(p_line, "");
|
|
|
|
if (p_line == 0 && text_editor->get_line_count() > 1) {
|
|
|
|
text_editor->cursor_set_line(1);
|
|
|
|
text_editor->cursor_set_column(0);
|
|
|
|
}
|
2021-06-15 14:05:01 +00:00
|
|
|
text_editor->backspace();
|
2019-12-09 06:29:24 +00:00
|
|
|
text_editor->unfold_line(p_line);
|
|
|
|
text_editor->cursor_set_line(p_line);
|
|
|
|
}
|
|
|
|
|
2018-05-25 22:49:35 +00:00
|
|
|
void CodeTextEditor::delete_lines() {
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
if (text_editor->is_selection_active()) {
|
|
|
|
int to_line = text_editor->get_selection_to_line();
|
|
|
|
int from_line = text_editor->get_selection_from_line();
|
|
|
|
int count = Math::abs(to_line - from_line) + 1;
|
2018-10-13 17:30:23 +00:00
|
|
|
|
2019-12-09 06:29:24 +00:00
|
|
|
text_editor->cursor_set_line(from_line, false);
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
_delete_line(from_line);
|
2018-05-25 22:49:35 +00:00
|
|
|
}
|
|
|
|
text_editor->deselect();
|
|
|
|
} else {
|
2019-12-09 06:29:24 +00:00
|
|
|
_delete_line(text_editor->cursor_get_line());
|
2018-05-25 22:49:35 +00:00
|
|
|
}
|
|
|
|
text_editor->end_complex_operation();
|
|
|
|
}
|
|
|
|
|
2021-06-29 10:09:07 +00:00
|
|
|
void CodeTextEditor::duplicate_selection() {
|
2018-12-24 00:14:05 +00:00
|
|
|
const int cursor_column = text_editor->cursor_get_column();
|
2018-05-25 22:49:35 +00:00
|
|
|
int from_line = text_editor->cursor_get_line();
|
|
|
|
int to_line = text_editor->cursor_get_line();
|
2018-12-24 00:14:05 +00:00
|
|
|
int from_column = 0;
|
|
|
|
int to_column = 0;
|
|
|
|
int cursor_new_line = to_line + 1;
|
|
|
|
int cursor_new_column = text_editor->cursor_get_column();
|
|
|
|
String new_text = "\n" + text_editor->get_line(from_line);
|
|
|
|
bool selection_active = false;
|
|
|
|
|
|
|
|
text_editor->cursor_set_column(text_editor->get_line(from_line).length());
|
2018-05-25 22:49:35 +00:00
|
|
|
if (text_editor->is_selection_active()) {
|
2018-12-24 00:14:05 +00:00
|
|
|
from_column = text_editor->get_selection_from_column();
|
|
|
|
to_column = text_editor->get_selection_to_column();
|
|
|
|
|
2018-05-25 22:49:35 +00:00
|
|
|
from_line = text_editor->get_selection_from_line();
|
|
|
|
to_line = text_editor->get_selection_to_line();
|
2018-12-24 00:14:05 +00:00
|
|
|
cursor_new_line = to_line + text_editor->cursor_get_line() - from_line;
|
|
|
|
cursor_new_column = to_column == cursor_column ? 2 * to_column - from_column : to_column;
|
|
|
|
new_text = text_editor->get_selection_text();
|
|
|
|
selection_active = true;
|
|
|
|
|
|
|
|
text_editor->cursor_set_line(to_line);
|
|
|
|
text_editor->cursor_set_column(to_column);
|
2018-05-25 22:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
text_editor->begin_complex_operation();
|
2018-12-24 00:14:05 +00:00
|
|
|
|
2018-05-25 22:49:35 +00:00
|
|
|
for (int i = from_line; i <= to_line; i++) {
|
|
|
|
text_editor->unfold_line(i);
|
|
|
|
}
|
2018-12-24 00:14:05 +00:00
|
|
|
text_editor->deselect();
|
|
|
|
text_editor->insert_text_at_cursor(new_text);
|
|
|
|
text_editor->cursor_set_line(cursor_new_line);
|
|
|
|
text_editor->cursor_set_column(cursor_new_column);
|
|
|
|
if (selection_active) {
|
2020-02-27 21:28:17 +00:00
|
|
|
text_editor->select(to_line, to_column, 2 * to_line - from_line, to_line == from_line ? 2 * to_column - from_column : to_column);
|
2018-05-25 22:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
text_editor->end_complex_operation();
|
|
|
|
text_editor->update();
|
|
|
|
}
|
|
|
|
|
2019-02-04 19:17:44 +00:00
|
|
|
void CodeTextEditor::toggle_inline_comment(const String &delimiter) {
|
|
|
|
text_editor->begin_complex_operation();
|
|
|
|
if (text_editor->is_selection_active()) {
|
|
|
|
int begin = text_editor->get_selection_from_line();
|
|
|
|
int end = text_editor->get_selection_to_line();
|
|
|
|
|
|
|
|
// End of selection ends on the first column of the last line, ignore it.
|
2020-05-14 14:41:43 +00:00
|
|
|
if (text_editor->get_selection_to_column() == 0) {
|
2019-02-04 19:17:44 +00:00
|
|
|
end -= 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-02-04 19:17:44 +00:00
|
|
|
|
|
|
|
int col_to = text_editor->get_selection_to_column();
|
|
|
|
int cursor_pos = text_editor->cursor_get_column();
|
|
|
|
|
2019-08-14 21:31:07 +00:00
|
|
|
// Check if all lines in the selected block are commented.
|
2019-02-04 19:17:44 +00:00
|
|
|
bool is_commented = true;
|
|
|
|
for (int i = begin; i <= end; i++) {
|
|
|
|
if (!text_editor->get_line(i).begins_with(delimiter)) {
|
|
|
|
is_commented = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = begin; i <= end; i++) {
|
|
|
|
String line_text = text_editor->get_line(i);
|
|
|
|
|
2020-12-15 12:04:21 +00:00
|
|
|
if (line_text.strip_edges().is_empty()) {
|
2019-02-04 19:17:44 +00:00
|
|
|
line_text = delimiter;
|
|
|
|
} else {
|
|
|
|
if (is_commented) {
|
|
|
|
line_text = line_text.substr(delimiter.length(), line_text.length());
|
|
|
|
} else {
|
|
|
|
line_text = delimiter + line_text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
text_editor->set_line(i, line_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust selection & cursor position.
|
2019-02-04 19:23:48 +00:00
|
|
|
int offset = (is_commented ? -1 : 1) * delimiter.length();
|
2019-02-04 19:17:44 +00:00
|
|
|
int col_from = text_editor->get_selection_from_column() > 0 ? text_editor->get_selection_from_column() + offset : 0;
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (is_commented && text_editor->cursor_get_column() == text_editor->get_line(text_editor->cursor_get_line()).length() + 1) {
|
2019-02-04 19:17:44 +00:00
|
|
|
cursor_pos += 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-02-04 19:17:44 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (text_editor->get_selection_to_column() != 0 && col_to != text_editor->get_line(text_editor->get_selection_to_line()).length() + 1) {
|
2019-02-04 19:17:44 +00:00
|
|
|
col_to += offset;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-02-04 19:17:44 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (text_editor->cursor_get_column() != 0) {
|
2019-02-04 19:17:44 +00:00
|
|
|
cursor_pos += offset;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-02-04 19:17:44 +00:00
|
|
|
|
|
|
|
text_editor->select(begin, col_from, text_editor->get_selection_to_line(), col_to);
|
|
|
|
text_editor->cursor_set_column(cursor_pos);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
int begin = text_editor->cursor_get_line();
|
|
|
|
String line_text = text_editor->get_line(begin);
|
2019-02-04 19:23:48 +00:00
|
|
|
int delimiter_length = delimiter.length();
|
2019-02-04 19:17:44 +00:00
|
|
|
|
|
|
|
int col = text_editor->cursor_get_column();
|
|
|
|
if (line_text.begins_with(delimiter)) {
|
2019-02-04 19:23:48 +00:00
|
|
|
line_text = line_text.substr(delimiter_length, line_text.length());
|
|
|
|
col -= delimiter_length;
|
2019-02-04 19:17:44 +00:00
|
|
|
} else {
|
|
|
|
line_text = delimiter + line_text;
|
2019-02-04 19:23:48 +00:00
|
|
|
col += delimiter_length;
|
2019-02-04 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
text_editor->set_line(begin, line_text);
|
|
|
|
text_editor->cursor_set_column(col);
|
|
|
|
}
|
|
|
|
text_editor->end_complex_operation();
|
|
|
|
text_editor->update();
|
|
|
|
}
|
|
|
|
|
2018-05-25 22:49:35 +00:00
|
|
|
void CodeTextEditor::goto_line(int p_line) {
|
|
|
|
text_editor->deselect();
|
|
|
|
text_editor->unfold_line(p_line);
|
|
|
|
text_editor->call_deferred("cursor_set_line", p_line);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
|
|
|
|
text_editor->unfold_line(p_line);
|
|
|
|
text_editor->call_deferred("cursor_set_line", p_line);
|
|
|
|
text_editor->call_deferred("cursor_set_column", p_begin);
|
|
|
|
text_editor->select(p_line, p_begin, p_line, p_end);
|
|
|
|
}
|
|
|
|
|
2019-01-04 12:09:01 +00:00
|
|
|
void CodeTextEditor::goto_line_centered(int p_line) {
|
|
|
|
goto_line(p_line);
|
|
|
|
text_editor->call_deferred("center_viewport_to_cursor");
|
|
|
|
}
|
|
|
|
|
2019-04-22 16:20:27 +00:00
|
|
|
void CodeTextEditor::set_executing_line(int p_line) {
|
2020-07-26 14:57:23 +00:00
|
|
|
text_editor->set_line_as_executing(p_line, true);
|
2019-04-22 16:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::clear_executing_line() {
|
2020-07-26 14:57:23 +00:00
|
|
|
text_editor->clear_executing_lines();
|
2019-04-22 16:20:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 22:49:35 +00:00
|
|
|
Variant CodeTextEditor::get_edit_state() {
|
|
|
|
Dictionary state;
|
|
|
|
|
|
|
|
state["scroll_position"] = text_editor->get_v_scroll();
|
2019-04-13 11:43:35 +00:00
|
|
|
state["h_scroll_position"] = text_editor->get_h_scroll();
|
2018-05-25 22:49:35 +00:00
|
|
|
state["column"] = text_editor->cursor_get_column();
|
|
|
|
state["row"] = text_editor->cursor_get_line();
|
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
state["selection"] = get_text_editor()->is_selection_active();
|
|
|
|
if (get_text_editor()->is_selection_active()) {
|
2019-04-13 11:43:35 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
state["folded_lines"] = text_editor->get_folded_lines();
|
2020-07-26 14:57:23 +00:00
|
|
|
state["breakpoints"] = text_editor->get_breakpointed_lines();
|
|
|
|
state["bookmarks"] = text_editor->get_bookmarked_lines();
|
2019-04-13 11:43:35 +00:00
|
|
|
|
2020-05-03 16:08:15 +00:00
|
|
|
Ref<EditorSyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighter();
|
|
|
|
state["syntax_highlighter"] = syntax_highlighter->_get_name();
|
2019-04-13 11:43:35 +00:00
|
|
|
|
2018-05-25 22:49:35 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::set_edit_state(const Variant &p_state) {
|
|
|
|
Dictionary state = p_state;
|
2019-04-13 11:43:35 +00:00
|
|
|
|
|
|
|
/* update the row first as it sets the column to 0 */
|
2018-05-25 22:49:35 +00:00
|
|
|
text_editor->cursor_set_line(state["row"]);
|
2019-04-13 11:43:35 +00:00
|
|
|
text_editor->cursor_set_column(state["column"]);
|
2018-05-25 22:49:35 +00:00
|
|
|
text_editor->set_v_scroll(state["scroll_position"]);
|
2019-04-13 11:43:35 +00:00
|
|
|
text_editor->set_h_scroll(state["h_scroll_position"]);
|
|
|
|
|
|
|
|
if (state.has("selection")) {
|
|
|
|
text_editor->select(state["selection_from_line"], state["selection_from_column"], state["selection_to_line"], state["selection_to_column"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-19 23:51:25 +00:00
|
|
|
if (state.has("bookmarks")) {
|
|
|
|
Array bookmarks = state["bookmarks"];
|
|
|
|
for (int i = 0; i < bookmarks.size(); i++) {
|
2020-07-26 14:57:23 +00:00
|
|
|
text_editor->set_line_as_bookmarked(bookmarks[i], true);
|
2019-04-19 23:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-25 22:49:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void CodeTextEditor::set_error(const String &p_error) {
|
2017-08-26 12:42:12 +00:00
|
|
|
error->set_text(p_error);
|
2018-12-21 08:05:29 +00:00
|
|
|
if (p_error != "") {
|
|
|
|
error->set_default_cursor_shape(CURSOR_POINTING_HAND);
|
|
|
|
} else {
|
|
|
|
error->set_default_cursor_shape(CURSOR_ARROW);
|
|
|
|
}
|
2018-11-21 10:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::set_error_pos(int p_line, int p_column) {
|
|
|
|
error_line = p_line;
|
|
|
|
error_column = p_column;
|
|
|
|
}
|
|
|
|
|
2018-12-21 08:05:29 +00:00
|
|
|
void CodeTextEditor::goto_error() {
|
|
|
|
if (error->get_text() != "") {
|
2021-03-13 14:09:49 +00:00
|
|
|
text_editor->unfold_line(error_line);
|
2018-12-21 08:05:29 +00:00
|
|
|
text_editor->cursor_set_line(error_line);
|
|
|
|
text_editor->cursor_set_column(error_column);
|
|
|
|
text_editor->center_viewport_to_cursor();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 20:25:00 +00:00
|
|
|
void CodeTextEditor::_update_text_editor_theme() {
|
|
|
|
text_editor->add_theme_color_override("background_color", EDITOR_GET("text_editor/highlighting/background_color"));
|
|
|
|
text_editor->add_theme_color_override("completion_background_color", EDITOR_GET("text_editor/highlighting/completion_background_color"));
|
|
|
|
text_editor->add_theme_color_override("completion_selected_color", EDITOR_GET("text_editor/highlighting/completion_selected_color"));
|
|
|
|
text_editor->add_theme_color_override("completion_existing_color", EDITOR_GET("text_editor/highlighting/completion_existing_color"));
|
|
|
|
text_editor->add_theme_color_override("completion_scroll_color", EDITOR_GET("text_editor/highlighting/completion_scroll_color"));
|
|
|
|
text_editor->add_theme_color_override("completion_font_color", EDITOR_GET("text_editor/highlighting/completion_font_color"));
|
|
|
|
text_editor->add_theme_color_override("font_color", EDITOR_GET("text_editor/highlighting/text_color"));
|
|
|
|
text_editor->add_theme_color_override("line_number_color", EDITOR_GET("text_editor/highlighting/line_number_color"));
|
|
|
|
text_editor->add_theme_color_override("caret_color", EDITOR_GET("text_editor/highlighting/caret_color"));
|
|
|
|
text_editor->add_theme_color_override("caret_background_color", EDITOR_GET("text_editor/highlighting/caret_background_color"));
|
|
|
|
text_editor->add_theme_color_override("font_selected_color", EDITOR_GET("text_editor/highlighting/text_selected_color"));
|
|
|
|
text_editor->add_theme_color_override("selection_color", EDITOR_GET("text_editor/highlighting/selection_color"));
|
|
|
|
text_editor->add_theme_color_override("brace_mismatch_color", EDITOR_GET("text_editor/highlighting/brace_mismatch_color"));
|
|
|
|
text_editor->add_theme_color_override("current_line_color", EDITOR_GET("text_editor/highlighting/current_line_color"));
|
|
|
|
text_editor->add_theme_color_override("line_length_guideline_color", EDITOR_GET("text_editor/highlighting/line_length_guideline_color"));
|
|
|
|
text_editor->add_theme_color_override("word_highlighted_color", EDITOR_GET("text_editor/highlighting/word_highlighted_color"));
|
|
|
|
text_editor->add_theme_color_override("bookmark_color", EDITOR_GET("text_editor/highlighting/bookmark_color"));
|
|
|
|
text_editor->add_theme_color_override("breakpoint_color", EDITOR_GET("text_editor/highlighting/breakpoint_color"));
|
|
|
|
text_editor->add_theme_color_override("executing_line_color", EDITOR_GET("text_editor/highlighting/executing_line_color"));
|
|
|
|
text_editor->add_theme_color_override("code_folding_color", EDITOR_GET("text_editor/highlighting/code_folding_color"));
|
|
|
|
text_editor->add_theme_color_override("search_result_color", EDITOR_GET("text_editor/highlighting/search_result_color"));
|
|
|
|
text_editor->add_theme_color_override("search_result_border_color", EDITOR_GET("text_editor/highlighting/search_result_border_color"));
|
|
|
|
text_editor->add_theme_constant_override("line_spacing", EDITOR_DEF("text_editor/theme/line_spacing", 6));
|
|
|
|
emit_signal("load_theme_settings");
|
|
|
|
_load_theme_settings();
|
|
|
|
}
|
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
void CodeTextEditor::_update_font() {
|
2020-03-12 12:37:40 +00:00
|
|
|
text_editor->add_theme_font_override("font", get_theme_font("source", "EditorFonts"));
|
2020-09-03 11:22:16 +00:00
|
|
|
text_editor->add_theme_font_size_override("font_size", get_theme_font_size("source_size", "EditorFonts"));
|
2018-02-08 12:04:39 +00:00
|
|
|
|
2020-03-12 12:37:40 +00:00
|
|
|
error->add_theme_font_override("font", get_theme_font("status_source", "EditorFonts"));
|
2020-09-03 11:22:16 +00:00
|
|
|
error->add_theme_font_size_override("font_size", get_theme_font_size("status_source_size", "EditorFonts"));
|
2020-03-12 12:37:40 +00:00
|
|
|
error->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
|
2019-11-14 10:16:25 +00:00
|
|
|
|
2020-03-12 12:37:40 +00:00
|
|
|
Ref<Font> status_bar_font = get_theme_font("status_source", "EditorFonts");
|
2020-09-03 11:22:16 +00:00
|
|
|
int status_bar_font_size = get_theme_font_size("status_source_size", "EditorFonts");
|
2020-03-12 12:37:40 +00:00
|
|
|
error->add_theme_font_override("font", status_bar_font);
|
2020-09-03 11:22:16 +00:00
|
|
|
error->add_theme_font_size_override("font_size", status_bar_font_size);
|
2018-02-08 12:04:39 +00: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 14:41:43 +00:00
|
|
|
if (n) {
|
2020-03-12 12:37:40 +00:00
|
|
|
n->add_theme_font_override("font", status_bar_font);
|
2020-09-03 11:22:16 +00:00
|
|
|
n->add_theme_font_size_override("font_size", status_bar_font_size);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-08 12:04:39 +00:00
|
|
|
}
|
2016-05-29 14:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_on_settings_change() {
|
2020-09-10 20:25:00 +00:00
|
|
|
_update_text_editor_theme();
|
2016-05-29 14:37:26 +00:00
|
|
|
_update_font();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-11-22 15:21:07 +00:00
|
|
|
font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size");
|
|
|
|
|
2020-09-18 07:36:10 +00:00
|
|
|
int ot_mode = EditorSettings::get_singleton()->get("interface/editor/code_font_contextual_ligatures");
|
|
|
|
switch (ot_mode) {
|
|
|
|
case 1: { // Disable ligatures.
|
|
|
|
text_editor->clear_opentype_features();
|
|
|
|
text_editor->set_opentype_feature("calt", 0);
|
|
|
|
} break;
|
|
|
|
case 2: { // Custom.
|
|
|
|
text_editor->clear_opentype_features();
|
|
|
|
Vector<String> subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_opentype_features")).split(",");
|
|
|
|
Dictionary ftrs;
|
|
|
|
for (int i = 0; i < subtag.size(); i++) {
|
|
|
|
Vector<String> subtag_a = subtag[i].split("=");
|
|
|
|
if (subtag_a.size() == 2) {
|
|
|
|
text_editor->set_opentype_feature(subtag_a[0], subtag_a[1].to_int());
|
|
|
|
} else if (subtag_a.size() == 1) {
|
|
|
|
text_editor->set_opentype_feature(subtag_a[0], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default: { // Default.
|
|
|
|
text_editor->clear_opentype_features();
|
|
|
|
text_editor->set_opentype_feature("calt", 1);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:31:07 +00:00
|
|
|
// Auto brace completion.
|
2014-04-30 16:21:58 +00:00
|
|
|
text_editor->set_auto_brace_completion(
|
2019-07-18 19:34:28 +00:00
|
|
|
EDITOR_GET("text_editor/completion/auto_brace_complete"));
|
2014-05-21 22:01:11 +00:00
|
|
|
|
|
|
|
code_complete_timer->set_wait_time(
|
2019-07-18 19:34:28 +00:00
|
|
|
EDITOR_GET("text_editor/completion/code_complete_delay"));
|
2014-05-21 22:01:11 +00:00
|
|
|
|
2019-08-14 21:31:07 +00:00
|
|
|
// Call hint settings.
|
2020-09-18 19:45:47 +00:00
|
|
|
text_editor->set_code_hint_draw_below(EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"));
|
2019-04-25 21:48:59 +00:00
|
|
|
|
2019-07-18 19:34:28 +00:00
|
|
|
idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
|
2014-04-05 15:39:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void CodeTextEditor::_text_changed_idle_timeout() {
|
|
|
|
_validate_script();
|
2016-08-02 22:11:05 +00:00
|
|
|
emit_signal("validate_script");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 16:49:49 +00:00
|
|
|
void CodeTextEditor::validate_script() {
|
|
|
|
idle->start();
|
|
|
|
}
|
|
|
|
|
2021-05-18 03:09:19 +00:00
|
|
|
void CodeTextEditor::_error_button_pressed() {
|
|
|
|
_set_show_errors_panel(!is_errors_panel_opened);
|
|
|
|
_set_show_warnings_panel(false);
|
2019-02-01 16:20:59 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-02-01 16:20:59 +00:00
|
|
|
void CodeTextEditor::_warning_button_pressed() {
|
2019-02-13 09:12:31 +00:00
|
|
|
_set_show_warnings_panel(!is_warnings_panel_opened);
|
2021-05-18 03:09:19 +00:00
|
|
|
_set_show_errors_panel(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_set_show_errors_panel(bool p_show) {
|
|
|
|
is_errors_panel_opened = p_show;
|
|
|
|
emit_signal("show_errors_panel", p_show);
|
2019-02-13 09:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_set_show_warnings_panel(bool p_show) {
|
|
|
|
is_warnings_panel_opened = p_show;
|
|
|
|
emit_signal("show_warnings_panel", p_show);
|
2019-02-01 16:20:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 13:35:16 +00:00
|
|
|
void CodeTextEditor::_toggle_scripts_pressed() {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (is_layout_rtl()) {
|
|
|
|
toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->toggle_scripts_panel() ? get_theme_icon("Forward", "EditorIcons") : get_theme_icon("Back", "EditorIcons"));
|
2020-11-30 14:33:42 +00:00
|
|
|
} else {
|
|
|
|
toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->toggle_scripts_panel() ? get_theme_icon("Back", "EditorIcons") : get_theme_icon("Forward", "EditorIcons"));
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
2019-12-15 13:35:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 16:20:59 +00:00
|
|
|
void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) {
|
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2021-01-08 03:37:37 +00:00
|
|
|
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_LEFT) {
|
2020-06-23 07:59:43 +00:00
|
|
|
goto_error();
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
2019-02-01 16:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2019-12-17 08:16:17 +00:00
|
|
|
if (toggle_scripts_button->is_visible()) {
|
2019-12-17 13:51:49 +00:00
|
|
|
update_toggle_scripts_button();
|
2019-12-17 08:16:17 +00:00
|
|
|
}
|
2020-09-10 20:25:00 +00:00
|
|
|
_update_text_editor_theme();
|
2019-02-01 16:20:59 +00:00
|
|
|
_update_font();
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2021-05-18 03:09:19 +00:00
|
|
|
error_button->set_icon(get_theme_icon("StatusError", "EditorIcons"));
|
2020-03-12 12:37:40 +00:00
|
|
|
warning_button->set_icon(get_theme_icon("NodeWarning", "EditorIcons"));
|
|
|
|
add_theme_constant_override("separation", 4 * EDSCALE);
|
2019-02-01 16:20:59 +00:00
|
|
|
} break;
|
2019-05-09 16:26:33 +00:00
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
2019-12-17 13:51:49 +00:00
|
|
|
if (toggle_scripts_button->is_visible()) {
|
|
|
|
update_toggle_scripts_button();
|
|
|
|
}
|
2019-05-09 16:26:33 +00:00
|
|
|
set_process_input(is_visible_in_tree());
|
|
|
|
} break;
|
2021-06-24 13:38:29 +00:00
|
|
|
case NOTIFICATION_PREDELETE: {
|
|
|
|
if (find_replace_bar) {
|
|
|
|
find_replace_bar->set_text_edit(nullptr);
|
|
|
|
}
|
|
|
|
} break;
|
2019-02-01 16:20:59 +00:00
|
|
|
default:
|
|
|
|
break;
|
2016-05-29 14:37:26 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 03:09:19 +00: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 09:12:31 +00:00
|
|
|
_set_show_warnings_panel(false);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-02-01 16:20:59 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 23:51:25 +00:00
|
|
|
void CodeTextEditor::toggle_bookmark() {
|
|
|
|
int line = text_editor->cursor_get_line();
|
2020-07-26 14:57:23 +00:00
|
|
|
text_editor->set_line_as_bookmarked(line, !text_editor->is_line_bookmarked(line));
|
2019-04-19 23:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::goto_next_bookmark() {
|
2020-07-26 14:57:23 +00:00
|
|
|
Array bmarks = text_editor->get_bookmarked_lines();
|
2019-04-19 23:51:25 +00:00
|
|
|
if (bmarks.size() <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int line = text_editor->cursor_get_line();
|
2020-07-26 14:57:23 +00:00
|
|
|
if (line >= (int)bmarks[bmarks.size() - 1]) {
|
2019-04-19 23:51:25 +00:00
|
|
|
text_editor->unfold_line(bmarks[0]);
|
|
|
|
text_editor->cursor_set_line(bmarks[0]);
|
2019-12-18 20:58:18 +00:00
|
|
|
text_editor->center_viewport_to_cursor();
|
2019-04-19 23:51:25 +00:00
|
|
|
} else {
|
2020-07-26 14:57:23 +00:00
|
|
|
for (int i = 0; i < bmarks.size(); i++) {
|
|
|
|
int bmark_line = bmarks[i];
|
|
|
|
if (bmark_line > line) {
|
|
|
|
text_editor->unfold_line(bmark_line);
|
|
|
|
text_editor->cursor_set_line(bmark_line);
|
2019-12-01 17:35:07 +00:00
|
|
|
text_editor->center_viewport_to_cursor();
|
2019-04-19 23:51:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::goto_prev_bookmark() {
|
2020-07-26 14:57:23 +00:00
|
|
|
Array bmarks = text_editor->get_bookmarked_lines();
|
2019-04-19 23:51:25 +00:00
|
|
|
if (bmarks.size() <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int line = text_editor->cursor_get_line();
|
2020-07-26 14:57:23 +00:00
|
|
|
if (line <= (int)bmarks[0]) {
|
2019-04-19 23:51:25 +00:00
|
|
|
text_editor->unfold_line(bmarks[bmarks.size() - 1]);
|
|
|
|
text_editor->cursor_set_line(bmarks[bmarks.size() - 1]);
|
2019-12-18 20:58:18 +00:00
|
|
|
text_editor->center_viewport_to_cursor();
|
2019-04-19 23:51:25 +00:00
|
|
|
} else {
|
2020-07-26 14:57:23 +00:00
|
|
|
for (int i = bmarks.size(); i >= 0; i--) {
|
|
|
|
int bmark_line = bmarks[i];
|
|
|
|
if (bmark_line < line) {
|
|
|
|
text_editor->unfold_line(bmark_line);
|
|
|
|
text_editor->cursor_set_line(bmark_line);
|
2019-12-01 17:35:07 +00:00
|
|
|
text_editor->center_viewport_to_cursor();
|
2019-04-19 23:51:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::remove_all_bookmarks() {
|
2020-07-26 14:57:23 +00:00
|
|
|
text_editor->clear_bookmarked_lines();
|
2019-04-19 23:51:25 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void CodeTextEditor::_bind_methods() {
|
2019-05-09 16:26:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_input"), &CodeTextEditor::_input);
|
2016-08-02 22:11:05 +00:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("validate_script"));
|
|
|
|
ADD_SIGNAL(MethodInfo("load_theme_settings"));
|
2021-05-18 03:09:19 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("show_errors_panel"));
|
2019-02-13 09:12:31 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("show_warnings_panel"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00: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;
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 08:16:17 +00:00
|
|
|
void CodeTextEditor::show_toggle_scripts_button() {
|
|
|
|
toggle_scripts_button->show();
|
|
|
|
}
|
|
|
|
|
2019-12-17 13:51:49 +00:00
|
|
|
void CodeTextEditor::update_toggle_scripts_button() {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (is_layout_rtl()) {
|
|
|
|
toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? get_theme_icon("Forward", "EditorIcons") : get_theme_icon("Back", "EditorIcons"));
|
2020-11-30 14:33:42 +00:00
|
|
|
} else {
|
|
|
|
toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? get_theme_icon("Back", "EditorIcons") : get_theme_icon("Forward", "EditorIcons"));
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
2019-12-18 09:00:02 +00:00
|
|
|
toggle_scripts_button->set_tooltip(TTR("Toggle Scripts Panel") + " (" + ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text() + ")");
|
2019-12-17 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
CodeTextEditor::CodeTextEditor() {
|
2020-04-01 23:20:12 +00:00
|
|
|
code_complete_func = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL);
|
|
|
|
ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS);
|
|
|
|
ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KEY_MASK_CMD | KEY_0);
|
2016-07-14 19:03:40 +00:00
|
|
|
|
2020-07-24 14:50:35 +00:00
|
|
|
text_editor = memnew(CodeEdit);
|
2019-01-31 14:24:33 +00:00
|
|
|
add_child(text_editor);
|
|
|
|
text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
|
2020-09-18 07:36:10 +00:00
|
|
|
int ot_mode = EditorSettings::get_singleton()->get("interface/editor/code_font_contextual_ligatures");
|
|
|
|
switch (ot_mode) {
|
|
|
|
case 1: { // Disable ligatures.
|
|
|
|
text_editor->clear_opentype_features();
|
|
|
|
text_editor->set_opentype_feature("calt", 0);
|
|
|
|
} break;
|
|
|
|
case 2: { // Custom.
|
|
|
|
text_editor->clear_opentype_features();
|
|
|
|
Vector<String> subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_opentype_features")).split(",");
|
|
|
|
Dictionary ftrs;
|
|
|
|
for (int i = 0; i < subtag.size(); i++) {
|
|
|
|
Vector<String> subtag_a = subtag[i].split("=");
|
|
|
|
if (subtag_a.size() == 2) {
|
|
|
|
text_editor->set_opentype_feature(subtag_a[0], subtag_a[1].to_int());
|
|
|
|
} else if (subtag_a.size() == 1) {
|
|
|
|
text_editor->set_opentype_feature(subtag_a[0], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default: { // Default.
|
|
|
|
text_editor->clear_opentype_features();
|
|
|
|
text_editor->set_opentype_feature("calt", 1);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2020-07-25 17:27:35 +00:00
|
|
|
text_editor->set_draw_line_numbers(true);
|
2014-12-17 04:53:34 +00:00
|
|
|
text_editor->set_brace_matching(true);
|
2021-06-15 14:05:01 +00:00
|
|
|
text_editor->set_auto_indent_enabled(true);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-02-08 12:04:39 +00:00
|
|
|
status_bar = memnew(HBoxContainer);
|
2017-09-27 19:44:48 +00:00
|
|
|
add_child(status_bar);
|
2016-05-28 16:25:45 +00:00
|
|
|
status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
|
2019-02-10 16:17:29 +00:00
|
|
|
status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the warning icon.
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
idle = memnew(Timer);
|
2014-02-10 01:10:30 +00:00
|
|
|
add_child(idle);
|
|
|
|
idle->set_one_shot(true);
|
2019-07-18 19:34:28 +00:00
|
|
|
idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-05-04 03:22:49 +00:00
|
|
|
code_complete_timer = memnew(Timer);
|
|
|
|
add_child(code_complete_timer);
|
|
|
|
code_complete_timer->set_one_shot(true);
|
2019-07-18 19:34:28 +00:00
|
|
|
code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay"));
|
2014-05-04 03:22:49 +00:00
|
|
|
|
2018-11-21 10:56:02 +00:00
|
|
|
error_line = 0;
|
|
|
|
error_column = 0;
|
|
|
|
|
2020-06-19 18:49:04 +00:00
|
|
|
toggle_scripts_button = memnew(Button);
|
|
|
|
toggle_scripts_button->set_flat(true);
|
2020-02-21 17:28:45 +00:00
|
|
|
toggle_scripts_button->connect("pressed", callable_mp(this, &CodeTextEditor::_toggle_scripts_pressed));
|
2019-12-15 13:35:16 +00:00
|
|
|
status_bar->add_child(toggle_scripts_button);
|
2019-12-17 08:16:17 +00:00
|
|
|
toggle_scripts_button->hide();
|
2019-12-15 13:35:16 +00:00
|
|
|
|
2019-02-01 16:20:59 +00:00
|
|
|
// Error
|
|
|
|
ScrollContainer *scroll = memnew(ScrollContainer);
|
|
|
|
scroll->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
scroll->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
scroll->set_enable_v_scroll(false);
|
|
|
|
status_bar->add_child(scroll);
|
|
|
|
|
2018-12-21 08:05:29 +00:00
|
|
|
error = memnew(Label);
|
2019-02-01 16:20:59 +00:00
|
|
|
scroll->add_child(error);
|
|
|
|
error->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
|
2018-12-21 08:05:29 +00:00
|
|
|
error->set_mouse_filter(MOUSE_FILTER_STOP);
|
2020-02-21 17:28:45 +00:00
|
|
|
error->connect("gui_input", callable_mp(this, &CodeTextEditor::_error_pressed));
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2021-05-18 03:09:19 +00: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));
|
|
|
|
error_button->set_tooltip(TTR("Errors"));
|
|
|
|
|
|
|
|
error_button->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color("error_color", "Editor"));
|
|
|
|
error_button->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts"));
|
|
|
|
error_button->add_theme_font_size_override("font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("status_source_size", "EditorFonts"));
|
|
|
|
|
|
|
|
is_errors_panel_opened = false;
|
|
|
|
set_error_count(0);
|
|
|
|
|
2019-02-01 16:20:59 +00:00
|
|
|
// Warnings
|
2020-06-19 18:49:04 +00:00
|
|
|
warning_button = memnew(Button);
|
|
|
|
warning_button->set_flat(true);
|
2019-02-01 16:20:59 +00: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 17:28:45 +00:00
|
|
|
warning_button->connect("pressed", callable_mp(this, &CodeTextEditor::_warning_button_pressed));
|
2019-02-01 16:20:59 +00:00
|
|
|
warning_button->set_tooltip(TTR("Warnings"));
|
2018-07-01 16:17:40 +00:00
|
|
|
|
2021-05-18 03:09:19 +00:00
|
|
|
warning_button->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color("warning_color", "Editor"));
|
|
|
|
warning_button->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts"));
|
|
|
|
warning_button->add_theme_font_size_override("font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("status_source_size", "EditorFonts"));
|
2019-02-01 16:20:59 +00:00
|
|
|
|
2019-02-13 09:12:31 +00:00
|
|
|
is_warnings_panel_opened = false;
|
2021-05-18 03:09:19 +00:00
|
|
|
set_warning_count(0);
|
2019-02-01 16:20:59 +00: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);
|
2020-03-12 12:37:40 +00:00
|
|
|
line_and_col_txt->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts"));
|
2020-09-03 11:22:16 +00:00
|
|
|
line_and_col_txt->add_theme_font_size_override("font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("status_source_size", "EditorFonts"));
|
2019-02-10 16:17:29 +00:00
|
|
|
line_and_col_txt->set_tooltip(TTR("Line and column numbers."));
|
2019-02-01 16:20:59 +00:00
|
|
|
line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-02-21 17:28:45 +00:00
|
|
|
text_editor->connect("gui_input", callable_mp(this, &CodeTextEditor::_text_editor_gui_input));
|
|
|
|
text_editor->connect("cursor_changed", callable_mp(this, &CodeTextEditor::_line_col_changed));
|
|
|
|
text_editor->connect("text_changed", callable_mp(this, &CodeTextEditor::_text_changed));
|
2020-09-13 20:14:20 +00:00
|
|
|
text_editor->connect("request_code_completion", callable_mp(this, &CodeTextEditor::_complete_request));
|
|
|
|
TypedArray<String> cs;
|
2014-02-10 01:10:30 +00:00
|
|
|
cs.push_back(".");
|
2014-12-17 01:31:57 +00:00
|
|
|
cs.push_back(",");
|
|
|
|
cs.push_back("(");
|
2017-08-24 03:06:56 +00:00
|
|
|
cs.push_back("=");
|
2017-01-08 06:01:52 +00:00
|
|
|
cs.push_back("$");
|
2020-07-06 15:24:24 +00:00
|
|
|
cs.push_back("@");
|
2020-09-13 20:14:20 +00:00
|
|
|
cs.push_back("\"");
|
|
|
|
cs.push_back("\'");
|
|
|
|
text_editor->set_code_completion_prefixes(cs);
|
2020-02-21 17:28:45 +00:00
|
|
|
idle->connect("timeout", callable_mp(this, &CodeTextEditor::_text_changed_idle_timeout));
|
2014-05-21 22:01:11 +00:00
|
|
|
|
2020-02-21 17:28:45 +00:00
|
|
|
code_complete_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_code_complete_timer_timeout));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
font_resize_val = 0;
|
2018-01-31 15:05:28 +00:00
|
|
|
font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size");
|
2020-09-18 07:36:10 +00: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 19:58:50 +00:00
|
|
|
|
2020-02-21 17:28:45 +00:00
|
|
|
EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &CodeTextEditor::_on_settings_change));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|