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
|
|
|
/*************************************************************************/
|
2018-01-01 13:40:08 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 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
|
|
|
|
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"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "os/keyboard.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "scene/gui/margin_container.h"
|
|
|
|
#include "scene/gui/separator.h"
|
2016-06-17 19:58:50 +00:00
|
|
|
#include "scene/resources/dynamic_font.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void GotoLineDialog::popup_find_line(TextEdit *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();
|
2017-03-05 15:44:50 +00:00
|
|
|
popup_centered(Size2(180, 80));
|
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() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (get_line() < 1 || get_line() > text_editor->get_line_count())
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
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"));
|
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:"));
|
2017-03-29 15:29:38 +00:00
|
|
|
l->set_position(Point2(5, 5));
|
2014-02-10 01:10:30 +00:00
|
|
|
add_child(l);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
line = memnew(LineEdit);
|
|
|
|
line->set_anchor(MARGIN_RIGHT, ANCHOR_END);
|
|
|
|
line->set_begin(Point2(15, 22));
|
2017-07-06 07:16:27 +00:00
|
|
|
line->set_end(Point2(-15, 35));
|
2014-02-10 01:10:30 +00:00
|
|
|
add_child(line);
|
|
|
|
register_text_enter(line);
|
2017-03-05 15:44:50 +00:00
|
|
|
text_editor = NULL;
|
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) {
|
|
|
|
|
|
|
|
find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
|
|
|
|
find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
|
2017-03-05 15:44:50 +00:00
|
|
|
hide_button->set_normal_texture(get_icon("Close", "EditorIcons"));
|
2017-09-26 04:40:22 +00:00
|
|
|
hide_button->set_hover_texture(get_icon("Close", "EditorIcons"));
|
2017-03-05 15:44:50 +00:00
|
|
|
hide_button->set_pressed_texture(get_icon("Close", "EditorIcons"));
|
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) {
|
|
|
|
|
|
|
|
find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
|
|
|
|
find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
|
|
|
|
hide_button->set_normal_texture(get_icon("Close", "EditorIcons"));
|
2017-09-26 04:40:22 +00:00
|
|
|
hide_button->set_hover_texture(get_icon("Close", "EditorIcons"));
|
2017-08-29 23:03:13 +00:00
|
|
|
hide_button->set_pressed_texture(get_icon("Close", "EditorIcons"));
|
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) {
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventKey> k = p_event;
|
|
|
|
if (k.is_valid()) {
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (k->is_pressed() && (text_edit->has_focus() || text_vbc->is_a_parent_of(get_focus_owner()))) {
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
bool accepted = true;
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
switch (k->get_scancode()) {
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
case KEY_ESCAPE: {
|
|
|
|
|
|
|
|
_hide_bar();
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
|
|
|
|
accepted = false;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accepted) {
|
|
|
|
accept_event();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 16:15:41 +00:00
|
|
|
bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2016-05-30 16:15:41 +00:00
|
|
|
int line, col;
|
2017-03-05 15:44:50 +00:00
|
|
|
String text = get_search_text();
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool found = text_edit->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) {
|
|
|
|
if (!preserve_cursor) {
|
2017-11-12 23:12:17 +00:00
|
|
|
text_edit->unfold_line(line);
|
2016-07-21 01:40:08 +00:00
|
|
|
text_edit->cursor_set_line(line, false);
|
2017-03-05 15:44:50 +00:00
|
|
|
text_edit->cursor_set_column(col + text.length(), false);
|
2016-07-21 01:40:08 +00:00
|
|
|
text_edit->center_viewport_to_cursor();
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
text_edit->set_search_text(text);
|
2016-05-30 16:15:41 +00:00
|
|
|
text_edit->set_search_flags(p_flags);
|
2017-03-05 15:44:50 +00:00
|
|
|
text_edit->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
|
|
|
|
|
|
|
set_error("");
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
result_line = -1;
|
|
|
|
result_col = -1;
|
2016-05-28 16:25:45 +00:00
|
|
|
text_edit->set_search_text("");
|
2017-03-05 15:44:50 +00:00
|
|
|
set_error(text.empty() ? "" : TTR("No Matches"));
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_replace() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (result_line != -1 && result_col != -1) {
|
2016-05-30 16:15:41 +00:00
|
|
|
text_edit->begin_complex_operation();
|
|
|
|
|
2017-11-12 23:12:17 +00:00
|
|
|
text_edit->unfold_line(result_line);
|
2017-03-05 15:44:50 +00:00
|
|
|
text_edit->select(result_line, result_col, result_line, result_col + get_search_text().length());
|
2016-05-28 16:25:45 +00:00
|
|
|
text_edit->insert_text_at_cursor(get_replace_text());
|
2016-05-30 16:15:41 +00:00
|
|
|
|
|
|
|
text_edit->end_complex_operation();
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
search_current();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_replace_all() {
|
|
|
|
|
|
|
|
// line as x so it gets priority in comparison, column as y
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2i orig_cursor(text_edit->cursor_get_line(), text_edit->cursor_get_column());
|
|
|
|
Point2i prev_match = Point2(-1, -1);
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
bool selection_enabled = text_edit->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) {
|
2017-03-05 15:44:50 +00:00
|
|
|
selection_begin = Point2i(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
|
|
|
|
selection_end = Point2i(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int vsval = text_edit->get_v_scroll();
|
|
|
|
|
|
|
|
text_edit->cursor_set_line(0);
|
|
|
|
text_edit->cursor_set_column(0);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
text_edit->begin_complex_operation();
|
|
|
|
|
2016-05-30 16:15:41 +00:00
|
|
|
while (search_next()) {
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
// replace area
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2i match_from(result_line, result_col);
|
|
|
|
Point2i match_to(result_line, result_col + search_text_len);
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
if (match_from < prev_match)
|
|
|
|
break; // done
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
prev_match = Point2i(result_line, result_col + replace_text.length());
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2017-11-12 23:12:17 +00:00
|
|
|
text_edit->unfold_line(result_line);
|
2017-03-05 15:44:50 +00:00
|
|
|
text_edit->select(result_line, result_col, result_line, match_to.y);
|
2016-05-30 16:15:41 +00:00
|
|
|
|
2016-05-28 16:25:45 +00:00
|
|
|
if (selection_enabled && is_selection_only()) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (match_from < selection_begin || match_to > selection_end)
|
2016-05-28 16:25:45 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// replace but adjust selection bounds
|
2016-07-29 23:11:02 +00:00
|
|
|
text_edit->insert_text_at_cursor(replace_text);
|
2017-03-05 15:44:50 +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
|
|
|
} else {
|
2016-05-30 16:15:41 +00:00
|
|
|
// just replace
|
2016-07-29 23:11:02 +00:00
|
|
|
text_edit->insert_text_at_cursor(replace_text);
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
text_edit->end_complex_operation();
|
|
|
|
|
|
|
|
replace_all_mode = false;
|
|
|
|
|
|
|
|
// restore editor state (selection, cursor, scroll)
|
|
|
|
text_edit->cursor_set_line(orig_cursor.x);
|
|
|
|
text_edit->cursor_set_column(orig_cursor.y);
|
|
|
|
|
|
|
|
if (selection_enabled && is_selection_only()) {
|
|
|
|
// reselect
|
2017-03-05 15:44:50 +00:00
|
|
|
text_edit->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
|
2016-05-28 16:25:45 +00:00
|
|
|
} else {
|
|
|
|
text_edit->deselect();
|
|
|
|
}
|
|
|
|
|
|
|
|
text_edit->set_v_scroll(vsval);
|
2017-03-24 20:45:31 +00:00
|
|
|
set_error(vformat(TTR("Replaced %d occurrence(s)."), rc));
|
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) {
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
r_line = text_edit->cursor_get_line();
|
|
|
|
r_col = text_edit->cursor_get_column();
|
2016-05-30 16:15:41 +00:00
|
|
|
|
|
|
|
if (text_edit->is_selection_active() && !replace_all_mode) {
|
2016-06-11 19:07:04 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int selection_line = text_edit->get_selection_from_line();
|
2016-06-11 19:07:04 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (text_edit->get_selection_text() == get_search_text() && r_line == selection_line) {
|
2016-06-11 19:07:04 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int selection_from_col = text_edit->get_selection_from_column();
|
2016-06-11 19:07:04 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (r_col >= selection_from_col && r_col <= text_edit->get_selection_to_column()) {
|
|
|
|
r_col = selection_from_col;
|
2016-06-11 19:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-30 16:15:41 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-30 16:15:41 +00:00
|
|
|
bool FindReplaceBar::search_current() {
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t flags = 0;
|
2016-05-30 16:15:41 +00:00
|
|
|
|
|
|
|
if (is_whole_words())
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_WHOLE_WORDS;
|
2016-05-30 16:15:41 +00:00
|
|
|
if (is_case_sensitive())
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_MATCH_CASE;
|
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() {
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (is_whole_words())
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_WHOLE_WORDS;
|
2016-05-30 16:15:41 +00:00
|
|
|
if (is_case_sensitive())
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_MATCH_CASE;
|
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);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
col -= text.length();
|
|
|
|
if (col < 0) {
|
|
|
|
line -= 1;
|
|
|
|
if (line < 0)
|
|
|
|
line = text_edit->get_line_count() - 1;
|
|
|
|
col = text_edit->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() {
|
2016-05-28 16:25:45 +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();
|
|
|
|
|
|
|
|
if (is_whole_words())
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_WHOLE_WORDS;
|
2016-05-30 16:15:41 +00:00
|
|
|
if (is_case_sensitive())
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_MATCH_CASE;
|
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();
|
|
|
|
if (col > text_edit->get_line(line).length()) {
|
|
|
|
line += 1;
|
|
|
|
if (line >= text_edit->get_line_count())
|
|
|
|
line = 0;
|
|
|
|
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() {
|
|
|
|
|
2016-05-30 16:15:41 +00:00
|
|
|
if (replace_text->has_focus() || search_text->has_focus())
|
|
|
|
text_edit->grab_focus();
|
|
|
|
|
2016-05-28 16:25:45 +00:00
|
|
|
text_edit->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
|
|
|
replace_hbc->hide();
|
|
|
|
replace_options_hbc->hide();
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_show_search() {
|
|
|
|
|
|
|
|
show();
|
|
|
|
search_text->grab_focus();
|
|
|
|
|
2016-09-11 13:01:52 +00:00
|
|
|
if (text_edit->is_selection_active() && !selection_only->is_pressed()) {
|
2016-05-28 16:25:45 +00:00
|
|
|
search_text->set_text(text_edit->get_selection_text());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!get_search_text().empty()) {
|
|
|
|
search_text->select_all();
|
2017-09-10 13:37:49 +00:00
|
|
|
search_text->set_cursor_position(search_text->get_text().length());
|
2016-05-28 16:25:45 +00:00
|
|
|
search_current();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::popup_search() {
|
|
|
|
|
|
|
|
replace_hbc->hide();
|
|
|
|
replace_options_hbc->hide();
|
|
|
|
_show_search();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::popup_replace() {
|
|
|
|
|
2017-01-13 13:45:50 +00:00
|
|
|
if (!replace_hbc->is_visible_in_tree() || !replace_options_hbc->is_visible_in_tree()) {
|
2016-05-28 16:25:45 +00:00
|
|
|
replace_text->clear();
|
|
|
|
replace_hbc->show();
|
|
|
|
replace_options_hbc->show();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
selection_only->set_pressed((text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line()));
|
2016-09-11 13:01:52 +00:00
|
|
|
|
2016-05-28 16:25:45 +00:00
|
|
|
_show_search();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::_search_options_changed(bool p_pressed) {
|
|
|
|
|
|
|
|
search_current();
|
|
|
|
}
|
|
|
|
|
2016-05-30 16:15:41 +00:00
|
|
|
void FindReplaceBar::_editor_text_changed() {
|
|
|
|
|
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) {
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
search_current();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void FindReplaceBar::_search_text_entered(const String &p_text) {
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
search_next();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void FindReplaceBar::_replace_text_entered(const String &p_text) {
|
2016-09-11 13:01:52 +00:00
|
|
|
|
|
|
|
if (selection_only->is_pressed() && text_edit->is_selection_active()) {
|
|
|
|
_replace_all();
|
|
|
|
_hide_bar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
error_label->set_text(p_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) {
|
|
|
|
|
|
|
|
text_edit = p_text_edit;
|
2017-03-05 15:44:50 +00:00
|
|
|
text_edit->connect("text_changed", this, "_editor_text_changed");
|
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);
|
2017-01-03 02:03:46 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method("_editor_text_changed", &FindReplaceBar::_editor_text_changed);
|
|
|
|
ClassDB::bind_method("_search_text_changed", &FindReplaceBar::_search_text_changed);
|
|
|
|
ClassDB::bind_method("_search_text_entered", &FindReplaceBar::_search_text_entered);
|
|
|
|
ClassDB::bind_method("_replace_text_entered", &FindReplaceBar::_replace_text_entered);
|
|
|
|
ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
|
|
|
|
ClassDB::bind_method("_search_next", &FindReplaceBar::search_next);
|
|
|
|
ClassDB::bind_method("_search_prev", &FindReplaceBar::search_prev);
|
|
|
|
ClassDB::bind_method("_replace_pressed", &FindReplaceBar::_replace);
|
|
|
|
ClassDB::bind_method("_replace_all_pressed", &FindReplaceBar::_replace_all);
|
|
|
|
ClassDB::bind_method("_search_options_changed", &FindReplaceBar::_search_options_changed);
|
|
|
|
ClassDB::bind_method("_hide_pressed", &FindReplaceBar::_hide_bar);
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("search"));
|
|
|
|
}
|
|
|
|
|
|
|
|
FindReplaceBar::FindReplaceBar() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
replace_all_mode = false;
|
|
|
|
preserve_cursor = false;
|
2016-05-30 16:15:41 +00:00
|
|
|
|
2016-05-28 16:25:45 +00:00
|
|
|
text_vbc = memnew(VBoxContainer);
|
|
|
|
add_child(text_vbc);
|
|
|
|
|
|
|
|
HBoxContainer *search_hbc = memnew(HBoxContainer);
|
|
|
|
text_vbc->add_child(search_hbc);
|
|
|
|
|
|
|
|
search_text = memnew(LineEdit);
|
|
|
|
search_hbc->add_child(search_text);
|
|
|
|
search_text->set_custom_minimum_size(Size2(200, 0));
|
2017-03-05 15:44:50 +00:00
|
|
|
search_text->connect("text_changed", this, "_search_text_changed");
|
|
|
|
search_text->connect("text_entered", this, "_search_text_entered");
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
find_prev = memnew(ToolButton);
|
|
|
|
search_hbc->add_child(find_prev);
|
|
|
|
find_prev->set_focus_mode(FOCUS_NONE);
|
2017-03-05 15:44:50 +00:00
|
|
|
find_prev->connect("pressed", this, "_search_prev");
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
find_next = memnew(ToolButton);
|
|
|
|
search_hbc->add_child(find_next);
|
|
|
|
find_next->set_focus_mode(FOCUS_NONE);
|
2017-03-05 15:44:50 +00:00
|
|
|
find_next->connect("pressed", this, "_search_next");
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
replace_hbc = memnew(HBoxContainer);
|
|
|
|
text_vbc->add_child(replace_hbc);
|
|
|
|
replace_hbc->hide();
|
|
|
|
|
|
|
|
replace_text = memnew(LineEdit);
|
|
|
|
replace_hbc->add_child(replace_text);
|
|
|
|
replace_text->set_custom_minimum_size(Size2(200, 0));
|
2017-03-05 15:44:50 +00:00
|
|
|
replace_text->connect("text_entered", this, "_replace_text_entered");
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2016-09-11 13:01:52 +00:00
|
|
|
replace = memnew(Button);
|
2016-05-28 16:25:45 +00:00
|
|
|
replace_hbc->add_child(replace);
|
|
|
|
replace->set_text(TTR("Replace"));
|
2016-09-11 13:01:52 +00:00
|
|
|
//replace->set_focus_mode(FOCUS_NONE);
|
2017-03-05 15:44:50 +00:00
|
|
|
replace->connect("pressed", this, "_replace_pressed");
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2016-09-11 13:01:52 +00:00
|
|
|
replace_all = memnew(Button);
|
2016-05-28 16:25:45 +00:00
|
|
|
replace_hbc->add_child(replace_all);
|
|
|
|
replace_all->set_text(TTR("Replace All"));
|
2016-09-11 13:01:52 +00:00
|
|
|
//replace_all->set_focus_mode(FOCUS_NONE);
|
2017-03-05 15:44:50 +00:00
|
|
|
replace_all->connect("pressed", this, "_replace_all_pressed");
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Control *spacer_split = memnew(Control);
|
2016-05-28 16:25:45 +00:00
|
|
|
spacer_split->set_custom_minimum_size(Size2(0, 1));
|
|
|
|
text_vbc->add_child(spacer_split);
|
|
|
|
|
|
|
|
VBoxContainer *options_vbc = memnew(VBoxContainer);
|
|
|
|
add_child(options_vbc);
|
|
|
|
options_vbc->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
|
|
|
|
HBoxContainer *search_options = memnew(HBoxContainer);
|
|
|
|
options_vbc->add_child(search_options);
|
|
|
|
|
|
|
|
case_sensitive = memnew(CheckBox);
|
|
|
|
search_options->add_child(case_sensitive);
|
|
|
|
case_sensitive->set_text(TTR("Match Case"));
|
|
|
|
case_sensitive->set_focus_mode(FOCUS_NONE);
|
2017-03-05 15:44:50 +00:00
|
|
|
case_sensitive->connect("toggled", this, "_search_options_changed");
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
whole_words = memnew(CheckBox);
|
|
|
|
search_options->add_child(whole_words);
|
|
|
|
whole_words->set_text(TTR("Whole Words"));
|
|
|
|
whole_words->set_focus_mode(FOCUS_NONE);
|
2017-03-05 15:44:50 +00:00
|
|
|
whole_words->connect("toggled", this, "_search_options_changed");
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
error_label = memnew(Label);
|
|
|
|
search_options->add_child(error_label);
|
2017-08-15 17:30:53 +00:00
|
|
|
error_label->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("error_color", "Editor"));
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
search_options->add_spacer();
|
|
|
|
|
|
|
|
hide_button = memnew(TextureButton);
|
|
|
|
search_options->add_child(hide_button);
|
|
|
|
hide_button->set_focus_mode(FOCUS_NONE);
|
2017-03-05 15:44:50 +00:00
|
|
|
hide_button->connect("pressed", this, "_hide_pressed");
|
2016-05-28 16:25:45 +00:00
|
|
|
|
|
|
|
replace_options_hbc = memnew(HBoxContainer);
|
|
|
|
options_vbc->add_child(replace_options_hbc);
|
|
|
|
replace_options_hbc->hide();
|
|
|
|
|
|
|
|
selection_only = memnew(CheckBox);
|
|
|
|
replace_options_hbc->add_child(selection_only);
|
|
|
|
selection_only->set_text(TTR("Selection Only"));
|
|
|
|
selection_only->set_focus_mode(FOCUS_NONE);
|
2017-03-05 15:44:50 +00:00
|
|
|
selection_only->connect("toggled", this, "_search_options_changed");
|
2016-05-28 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void FindReplaceDialog::popup_search() {
|
|
|
|
|
2016-05-04 01:25:37 +00:00
|
|
|
set_title(TTR("Search"));
|
2014-02-10 01:10:30 +00:00
|
|
|
replace_mc->hide();
|
|
|
|
replace_label->hide();
|
|
|
|
replace_vb->hide();
|
|
|
|
skip->hide();
|
2017-03-05 15:44:50 +00:00
|
|
|
popup_centered(Point2(300, 190));
|
2016-05-04 01:25:37 +00:00
|
|
|
get_ok()->set_text(TTR("Find"));
|
2016-03-08 23:00:52 +00:00
|
|
|
search_text->grab_focus();
|
2017-03-05 15:44:50 +00:00
|
|
|
if (text_edit->is_selection_active() && (text_edit->get_selection_from_line() == text_edit->get_selection_to_line())) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
search_text->set_text(text_edit->get_selection_text());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
search_text->select_all();
|
|
|
|
|
|
|
|
error_label->set_text("");
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::popup_replace() {
|
|
|
|
|
2016-05-04 01:25:37 +00:00
|
|
|
set_title(TTR("Replace"));
|
2017-03-05 15:44:50 +00:00
|
|
|
bool do_selection = (text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line());
|
2016-09-11 13:01:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
set_replace_selection_only(do_selection);
|
|
|
|
|
2014-06-13 03:31:16 +00:00
|
|
|
if (!do_selection && text_edit->is_selection_active()) {
|
|
|
|
search_text->set_text(text_edit->get_selection_text());
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
replace_mc->show();
|
|
|
|
replace_label->show();
|
|
|
|
replace_vb->show();
|
2017-03-05 15:44:50 +00:00
|
|
|
popup_centered(Point2(300, 300));
|
|
|
|
if (search_text->get_text() != "" && replace_text->get_text() == "") {
|
|
|
|
search_text->select(0, 0);
|
2014-06-13 03:31:16 +00:00
|
|
|
replace_text->grab_focus();
|
|
|
|
} else {
|
|
|
|
search_text->grab_focus();
|
|
|
|
search_text->select_all();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
error_label->set_text("");
|
|
|
|
|
|
|
|
if (prompt->is_pressed()) {
|
|
|
|
skip->show();
|
2016-05-04 01:25:37 +00:00
|
|
|
get_ok()->set_text(TTR("Next"));
|
2014-02-10 01:10:30 +00:00
|
|
|
selection_only->set_disabled(true);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
skip->hide();
|
2016-05-04 01:25:37 +00:00
|
|
|
get_ok()->set_text(TTR("Replace"));
|
2014-02-10 01:10:30 +00:00
|
|
|
selection_only->set_disabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::_search_callback() {
|
|
|
|
|
|
|
|
if (is_replace_mode())
|
|
|
|
_replace();
|
|
|
|
else
|
|
|
|
_search();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::_replace_skip_callback() {
|
|
|
|
|
|
|
|
_search();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::_replace() {
|
|
|
|
|
2016-04-09 00:27:14 +00:00
|
|
|
text_edit->begin_complex_operation();
|
2014-02-10 01:10:30 +00:00
|
|
|
if (is_replace_all_mode()) {
|
|
|
|
|
|
|
|
//line as x so it gets priority in comparison, column as y
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2i orig_cursor(text_edit->cursor_get_line(), text_edit->cursor_get_column());
|
|
|
|
Point2i prev_match = Point2(-1, -1);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
bool selection_enabled = text_edit->is_selection_active();
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2i selection_begin, selection_end;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (selection_enabled) {
|
2017-03-05 15:44:50 +00:00
|
|
|
selection_begin = Point2i(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
|
|
|
|
selection_end = Point2i(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
int vsval = text_edit->get_v_scroll();
|
|
|
|
//int hsval = text_edit->get_h_scroll();
|
|
|
|
|
|
|
|
text_edit->cursor_set_line(0);
|
|
|
|
text_edit->cursor_set_column(0);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int rc = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (_search()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!text_edit->is_selection_active()) {
|
|
|
|
//search selects
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//replace area
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2i match_from(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
|
|
|
|
Point2i match_to(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (match_from < prev_match)
|
|
|
|
break; //done
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
prev_match = match_to;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (selection_enabled && is_replace_selection_only()) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (match_from < selection_begin || match_to > selection_end)
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
//replace but adjust selection bounds
|
|
|
|
|
|
|
|
text_edit->insert_text_at_cursor(get_replace_text());
|
2017-03-05 15:44:50 +00:00
|
|
|
if (match_to.x == selection_end.x)
|
|
|
|
selection_end.y += get_replace_text().length() - get_search_text().length();
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
//just replace
|
|
|
|
text_edit->insert_text_at_cursor(get_replace_text());
|
|
|
|
}
|
|
|
|
rc++;
|
|
|
|
}
|
|
|
|
//restore editor state (selection, cursor, scroll)
|
|
|
|
text_edit->cursor_set_line(orig_cursor.x);
|
|
|
|
text_edit->cursor_set_column(orig_cursor.y);
|
|
|
|
|
|
|
|
if (selection_enabled && is_replace_selection_only()) {
|
|
|
|
//reselect
|
2017-03-05 15:44:50 +00:00
|
|
|
text_edit->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
text_edit->deselect();
|
|
|
|
}
|
|
|
|
|
|
|
|
text_edit->set_v_scroll(vsval);
|
2017-01-14 11:26:56 +00:00
|
|
|
//text_edit->set_h_scroll(hsval);
|
2017-03-24 20:45:31 +00:00
|
|
|
error_label->set_text(vformat(TTR("Replaced %d occurrence(s)."), rc));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//hide();
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (text_edit->get_selection_text() == get_search_text()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
text_edit->insert_text_at_cursor(get_replace_text());
|
|
|
|
}
|
|
|
|
|
|
|
|
_search();
|
|
|
|
}
|
2016-04-09 00:27:14 +00:00
|
|
|
text_edit->end_complex_operation();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FindReplaceDialog::_search() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String text = get_search_text();
|
|
|
|
uint32_t flags = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (is_whole_words())
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_WHOLE_WORDS;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (is_case_sensitive())
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_MATCH_CASE;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (is_backwards())
|
2017-03-05 15:44:50 +00:00
|
|
|
flags |= TextEdit::SEARCH_BACKWARDS;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int line = text_edit->cursor_get_line(), col = text_edit->cursor_get_column();
|
2014-09-21 04:43:42 +00:00
|
|
|
|
|
|
|
if (is_backwards()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
col -= 1;
|
|
|
|
if (col < 0) {
|
|
|
|
line -= 1;
|
|
|
|
if (line < 0) {
|
|
|
|
line = text_edit->get_line_count() - 1;
|
2014-09-21 04:43:42 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
col = text_edit->get_line(line).length();
|
2014-09-21 04:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
bool found = text_edit->search(text, flags, line, col, line, col);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (found) {
|
2014-05-07 11:42:55 +00:00
|
|
|
// print_line("found");
|
2017-11-12 23:12:17 +00:00
|
|
|
text_edit->unfold_line(line);
|
2014-02-10 01:10:30 +00:00
|
|
|
text_edit->cursor_set_line(line);
|
2014-09-21 04:43:42 +00:00
|
|
|
if (is_backwards())
|
|
|
|
text_edit->cursor_set_column(col);
|
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
text_edit->cursor_set_column(col + text.length());
|
|
|
|
text_edit->select(line, col, line, col + text.length());
|
2014-02-10 01:10:30 +00:00
|
|
|
set_error("");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
|
2016-05-18 22:08:12 +00:00
|
|
|
set_error(TTR("Not found!"));
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::_prompt_changed() {
|
|
|
|
|
|
|
|
if (prompt->is_pressed()) {
|
|
|
|
skip->show();
|
2016-05-04 01:25:37 +00:00
|
|
|
get_ok()->set_text(TTR("Next"));
|
2014-02-10 01:10:30 +00:00
|
|
|
selection_only->set_disabled(true);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
skip->hide();
|
2016-05-04 01:25:37 +00:00
|
|
|
get_ok()->set_text(TTR("Replace"));
|
2014-02-10 01:10:30 +00:00
|
|
|
selection_only->set_disabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::_skip_pressed() {
|
|
|
|
|
|
|
|
_replace_skip_callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindReplaceDialog::is_replace_mode() const {
|
|
|
|
|
2017-01-13 13:45:50 +00:00
|
|
|
return replace_text->is_visible_in_tree();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FindReplaceDialog::is_replace_all_mode() const {
|
|
|
|
|
|
|
|
return !prompt->is_pressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindReplaceDialog::is_replace_selection_only() const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return selection_only->is_pressed();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
void FindReplaceDialog::set_replace_selection_only(bool p_enable) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
selection_only->set_pressed(p_enable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::ok_pressed() {
|
|
|
|
|
|
|
|
_search_callback();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void FindReplaceDialog::_search_text_entered(const String &p_text) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-13 13:45:50 +00:00
|
|
|
if (replace_text->is_visible_in_tree())
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
emit_signal("search");
|
|
|
|
_search();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void FindReplaceDialog::_replace_text_entered(const String &p_text) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-13 13:45:50 +00:00
|
|
|
if (!replace_text->is_visible_in_tree())
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
emit_signal("search");
|
|
|
|
_replace();
|
|
|
|
}
|
|
|
|
|
|
|
|
String FindReplaceDialog::get_search_text() const {
|
|
|
|
|
|
|
|
return search_text->get_text();
|
|
|
|
}
|
|
|
|
String FindReplaceDialog::get_replace_text() const {
|
|
|
|
|
|
|
|
return replace_text->get_text();
|
|
|
|
}
|
|
|
|
bool FindReplaceDialog::is_whole_words() const {
|
|
|
|
|
|
|
|
return whole_words->is_pressed();
|
|
|
|
}
|
|
|
|
bool FindReplaceDialog::is_case_sensitive() const {
|
|
|
|
|
|
|
|
return case_sensitive->is_pressed();
|
|
|
|
}
|
|
|
|
bool FindReplaceDialog::is_backwards() const {
|
|
|
|
|
|
|
|
return backwards->is_pressed();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void FindReplaceDialog::set_error(const String &p_error) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
error_label->set_text(p_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::set_text_edit(TextEdit *p_text_edit) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
text_edit = p_text_edit;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::search_next() {
|
|
|
|
_search();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method("_search_text_entered", &FindReplaceDialog::_search_text_entered);
|
|
|
|
ClassDB::bind_method("_replace_text_entered", &FindReplaceDialog::_replace_text_entered);
|
|
|
|
ClassDB::bind_method("_prompt_changed", &FindReplaceDialog::_prompt_changed);
|
|
|
|
ClassDB::bind_method("_skip_pressed", &FindReplaceDialog::_skip_pressed);
|
2014-02-10 01:10:30 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("search"));
|
|
|
|
ADD_SIGNAL(MethodInfo("skip"));
|
|
|
|
}
|
|
|
|
|
|
|
|
FindReplaceDialog::FindReplaceDialog() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
set_self_modulate(Color(1, 1, 1, 0.8));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VBoxContainer *vb = memnew(VBoxContainer);
|
2014-02-10 01:10:30 +00:00
|
|
|
add_child(vb);
|
2017-01-10 04:49:55 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
search_text = memnew(LineEdit);
|
|
|
|
vb->add_margin_child(TTR("Search"), search_text);
|
|
|
|
search_text->connect("text_entered", this, "_search_text_entered");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
replace_label = memnew(Label);
|
2016-05-04 01:25:37 +00:00
|
|
|
replace_label->set_text(TTR("Replace By"));
|
2014-02-10 01:10:30 +00:00
|
|
|
vb->add_child(replace_label);
|
2017-03-05 15:44:50 +00:00
|
|
|
replace_mc = memnew(MarginContainer);
|
2014-02-10 01:10:30 +00:00
|
|
|
vb->add_child(replace_mc);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
replace_text = memnew(LineEdit);
|
|
|
|
replace_text->set_anchor(MARGIN_RIGHT, ANCHOR_END);
|
|
|
|
replace_text->set_begin(Point2(15, 132));
|
2017-07-06 07:16:27 +00:00
|
|
|
replace_text->set_end(Point2(-15, 135));
|
2017-08-15 17:30:53 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
replace_mc->add_child(replace_text);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
replace_text->connect("text_entered", this, "_replace_text_entered");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
MarginContainer *opt_mg = memnew(MarginContainer);
|
2014-02-10 01:10:30 +00:00
|
|
|
vb->add_child(opt_mg);
|
2017-03-05 15:44:50 +00:00
|
|
|
VBoxContainer *svb = memnew(VBoxContainer);
|
2014-02-10 01:10:30 +00:00
|
|
|
opt_mg->add_child(svb);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
svb->add_child(memnew(Label));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
whole_words = memnew(CheckButton);
|
2016-05-04 01:25:37 +00:00
|
|
|
whole_words->set_text(TTR("Whole Words"));
|
2014-02-10 01:10:30 +00:00
|
|
|
svb->add_child(whole_words);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
case_sensitive = memnew(CheckButton);
|
2016-05-04 01:25:37 +00:00
|
|
|
case_sensitive->set_text(TTR("Case Sensitive"));
|
2014-02-10 01:10:30 +00:00
|
|
|
svb->add_child(case_sensitive);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
backwards = memnew(CheckButton);
|
2016-05-04 01:25:37 +00:00
|
|
|
backwards->set_text(TTR("Backwards"));
|
2014-02-10 01:10:30 +00:00
|
|
|
svb->add_child(backwards);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
opt_mg = memnew(MarginContainer);
|
2014-02-10 01:10:30 +00:00
|
|
|
vb->add_child(opt_mg);
|
2017-03-05 15:44:50 +00:00
|
|
|
VBoxContainer *rvb = memnew(VBoxContainer);
|
2014-02-10 01:10:30 +00:00
|
|
|
opt_mg->add_child(rvb);
|
2017-03-05 15:44:50 +00:00
|
|
|
replace_vb = rvb;
|
2017-01-14 11:26:56 +00:00
|
|
|
//rvb ->add_child(memnew(HSeparator));
|
2017-03-05 15:44:50 +00:00
|
|
|
rvb->add_child(memnew(Label));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
prompt = memnew(CheckButton);
|
2016-05-04 01:25:37 +00:00
|
|
|
prompt->set_text(TTR("Prompt On Replace"));
|
2014-02-10 01:10:30 +00:00
|
|
|
rvb->add_child(prompt);
|
2017-03-05 15:44:50 +00:00
|
|
|
prompt->connect("pressed", this, "_prompt_changed");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
selection_only = memnew(CheckButton);
|
2016-05-04 01:25:37 +00:00
|
|
|
selection_only->set_text(TTR("Selection Only"));
|
2014-02-10 01:10:30 +00:00
|
|
|
rvb->add_child(selection_only);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int margin = get_constant("margin", "Dialogs");
|
|
|
|
int button_margin = get_constant("button_margin", "Dialogs");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
skip = memnew(Button);
|
|
|
|
skip->set_anchor(MARGIN_LEFT, ANCHOR_END);
|
|
|
|
skip->set_anchor(MARGIN_TOP, ANCHOR_END);
|
|
|
|
skip->set_anchor(MARGIN_RIGHT, ANCHOR_END);
|
|
|
|
skip->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
|
2017-07-06 07:16:27 +00:00
|
|
|
skip->set_begin(Point2(-70, -button_margin));
|
|
|
|
skip->set_end(Point2(-10, -margin));
|
2016-05-04 01:25:37 +00:00
|
|
|
skip->set_text(TTR("Skip"));
|
2014-02-10 01:10:30 +00:00
|
|
|
add_child(skip);
|
2017-03-05 15:44:50 +00:00
|
|
|
skip->connect("pressed", this, "_skip_pressed");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
error_label = memnew(Label);
|
2014-02-10 01:10:30 +00:00
|
|
|
error_label->set_align(Label::ALIGN_CENTER);
|
2017-08-15 17:30:53 +00:00
|
|
|
error_label->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("error_color", "Editor"));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
vb->add_child(error_label);
|
|
|
|
|
|
|
|
set_hide_on_ok(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** CODE EDITOR ****/
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
|
2016-06-17 19:58:50 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
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()) {
|
2016-06-17 19:58:50 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mb->is_pressed() && mb->get_command()) {
|
2016-06-17 19:58:50 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mb->get_button_index() == BUTTON_WHEEL_UP) {
|
2016-07-14 19:03:40 +00:00
|
|
|
_zoom_in();
|
2017-05-20 15:38:03 +00:00
|
|
|
} else if (mb->get_button_index() == 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()) {
|
|
|
|
|
|
|
|
Ref<DynamicFont> font = text_editor->get_font("font");
|
|
|
|
|
|
|
|
if (font.is_valid()) {
|
|
|
|
if (font->get_size() != (int)font_size) {
|
|
|
|
font_size = font->get_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
font_size *= powf(magnify_gesture->get_factor(), 0.25);
|
|
|
|
|
|
|
|
_add_font_size((int)font_size - font->get_size());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventKey> k = p_event;
|
|
|
|
|
|
|
|
if (k.is_valid()) {
|
2016-06-17 19:58:50 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
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() {
|
2017-09-19 17:51:38 +00:00
|
|
|
font_resize_val += EDSCALE;
|
2017-11-01 20:49:39 +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() {
|
2017-09-19 17:51:38 +00:00
|
|
|
font_resize_val -= EDSCALE;
|
2017-11-01 20:49:39 +00:00
|
|
|
_zoom_changed();
|
|
|
|
}
|
2016-06-17 19:58:50 +00:00
|
|
|
|
2017-11-01 20:49:39 +00:00
|
|
|
void CodeTextEditor::_zoom_changed() {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (font_resize_timer->get_time_left() == 0)
|
2016-07-14 19:03:40 +00:00
|
|
|
font_resize_timer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_reset_zoom() {
|
|
|
|
Ref<DynamicFont> font = text_editor->get_font("font"); // reset source font size to default
|
|
|
|
|
|
|
|
if (font.is_valid()) {
|
2017-09-29 16:52:56 +00:00
|
|
|
EditorSettings::get_singleton()->set("interface/editor/source_font_size", 14);
|
2016-07-14 19:03:40 +00:00
|
|
|
font->set_size(14);
|
2016-06-17 19:58:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void CodeTextEditor::_line_col_changed() {
|
|
|
|
|
2016-07-24 15:11:32 +00:00
|
|
|
line_nb->set_text(itos(text_editor->cursor_get_line() + 1));
|
2016-10-09 22:10:13 +00:00
|
|
|
col_nb->set_text(itos(text_editor->cursor_get_column() + 1));
|
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() {
|
2017-01-13 13:45:50 +00:00
|
|
|
if (!is_visible_in_tree())
|
2014-12-17 01:31:57 +00:00
|
|
|
return;
|
2014-05-21 22:01:11 +00:00
|
|
|
if (enable_complete_timer)
|
|
|
|
text_editor->query_code_comple();
|
2014-05-04 03:22:49 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 01:31:57 +00:00
|
|
|
void CodeTextEditor::_complete_request() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
List<String> entries;
|
2016-08-02 22:11:05 +00:00
|
|
|
String ctext = text_editor->get_text_for_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
|
|
|
}
|
2014-05-07 11:38:07 +00:00
|
|
|
// print_line("COMPLETE: "+p_request);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (entries.size() == 0)
|
2014-12-17 01:31:57 +00:00
|
|
|
return;
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector<String> strs;
|
|
|
|
strs.resize(entries.size());
|
2017-03-05 15:44:50 +00:00
|
|
|
int i = 0;
|
|
|
|
for (List<String>::Element *E = entries.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
strs[i++] = E->get();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 03:06:56 +00:00
|
|
|
text_editor->code_complete(strs, forced);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 19:58:50 +00:00
|
|
|
void CodeTextEditor::_font_resize_timeout() {
|
|
|
|
|
2017-11-01 20:49:39 +00:00
|
|
|
if (_add_font_size(font_resize_val)) {
|
|
|
|
font_resize_val = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CodeTextEditor::_add_font_size(int p_delta) {
|
|
|
|
|
2016-06-17 19:58:50 +00:00
|
|
|
Ref<DynamicFont> font = text_editor->get_font("font");
|
|
|
|
|
|
|
|
if (font.is_valid()) {
|
2017-11-01 20:49:39 +00:00
|
|
|
int new_size = CLAMP(font->get_size() + p_delta, 8 * EDSCALE, 96 * EDSCALE);
|
2017-09-19 17:51:38 +00:00
|
|
|
if (new_size != font->get_size()) {
|
2017-09-29 16:52:56 +00:00
|
|
|
EditorSettings::get_singleton()->set("interface/editor/source_font_size", new_size / EDSCALE);
|
2017-09-19 17:51:38 +00:00
|
|
|
font->set_size(new_size);
|
2016-06-17 19:58:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 20:49:39 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2016-06-17 19:58:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 08:38:12 +00:00
|
|
|
void CodeTextEditor::update_editor_settings() {
|
|
|
|
|
2017-01-05 22:41:36 +00:00
|
|
|
text_editor->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
|
|
|
|
text_editor->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
|
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"));
|
2017-08-31 18:35:15 +00:00
|
|
|
text_editor->set_auto_indent(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"));
|
|
|
|
text_editor->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_numbers"));
|
|
|
|
text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_numbers_zero_padded"));
|
|
|
|
text_editor->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_length_guideline"));
|
|
|
|
text_editor->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_length_guideline_column"));
|
|
|
|
text_editor->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));
|
|
|
|
text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
|
2017-10-22 12:38:00 +00:00
|
|
|
text_editor->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
|
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"));
|
|
|
|
text_editor->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_breakpoint_gutter"));
|
2017-11-16 04:00:27 +00:00
|
|
|
text_editor->set_hiding_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/code_folding"));
|
2017-11-12 23:12:17 +00:00
|
|
|
text_editor->set_draw_fold_gutter(EditorSettings::get_singleton()->get("text_editor/line_numbers/code_folding"));
|
2017-01-05 22:41:36 +00:00
|
|
|
text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
|
2017-08-19 14:23:45 +00:00
|
|
|
text_editor->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/open_scripts/smooth_scrolling"));
|
2017-08-21 18:13:24 +00:00
|
|
|
text_editor->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/open_scripts/v_scroll_speed"));
|
2016-10-10 08:38:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void CodeTextEditor::set_error(const String &p_error) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-26 12:42:12 +00:00
|
|
|
error->set_text(p_error);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
void CodeTextEditor::_update_font() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-04-30 21:58:26 +00:00
|
|
|
// FONTS
|
2017-01-05 22:41:36 +00:00
|
|
|
String editor_font = EDITOR_DEF("text_editor/theme/font", "");
|
2016-06-17 19:58:50 +00:00
|
|
|
bool font_overridden = false;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (editor_font != "") {
|
2014-04-05 15:39:30 +00:00
|
|
|
Ref<Font> fnt = ResourceLoader::load(editor_font);
|
|
|
|
if (fnt.is_valid()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
text_editor->add_font_override("font", fnt);
|
2016-06-17 19:58:50 +00:00
|
|
|
font_overridden = true;
|
2014-04-05 15:39:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!font_overridden) {
|
2016-09-11 14:28:01 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
text_editor->add_font_override("font", get_font("source", "EditorFonts"));
|
2016-09-11 14:28:01 +00:00
|
|
|
}
|
2016-05-29 14:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_on_settings_change() {
|
|
|
|
|
|
|
|
_update_font();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
|
|
|
// AUTO BRACE COMPLETION
|
2014-04-30 16:21:58 +00:00
|
|
|
text_editor->set_auto_brace_completion(
|
2017-03-05 15:44:50 +00:00
|
|
|
EDITOR_DEF("text_editor/completion/auto_brace_complete", true));
|
2014-05-21 22:01:11 +00:00
|
|
|
|
|
|
|
code_complete_timer->set_wait_time(
|
2017-03-05 15:44:50 +00:00
|
|
|
EDITOR_DEF("text_editor/completion/code_complete_delay", .3f));
|
2014-05-21 22:01:11 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
enable_complete_timer = EDITOR_DEF("text_editor/completion/enable_code_completion_delay", true);
|
2016-03-04 10:05:42 +00:00
|
|
|
|
|
|
|
// call hint settings
|
|
|
|
text_editor->set_callhint_settings(
|
2017-03-05 15:44:50 +00:00
|
|
|
EDITOR_DEF("text_editor/completion/put_callhint_tooltip_below_current_line", true),
|
|
|
|
EDITOR_DEF("text_editor/completion/callhint_tooltip_offset", Vector2()));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_notification(int p_what) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_load_theme_settings();
|
2016-08-02 22:11:05 +00:00
|
|
|
emit_signal("load_theme_settings");
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_THEME_CHANGED) {
|
2016-05-29 14:37:26 +00:00
|
|
|
_update_font();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTextEditor::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method("_text_editor_gui_input", &CodeTextEditor::_text_editor_gui_input);
|
|
|
|
ClassDB::bind_method("_line_col_changed", &CodeTextEditor::_line_col_changed);
|
|
|
|
ClassDB::bind_method("_text_changed", &CodeTextEditor::_text_changed);
|
|
|
|
ClassDB::bind_method("_on_settings_change", &CodeTextEditor::_on_settings_change);
|
|
|
|
ClassDB::bind_method("_text_changed_idle_timeout", &CodeTextEditor::_text_changed_idle_timeout);
|
|
|
|
ClassDB::bind_method("_code_complete_timer_timeout", &CodeTextEditor::_code_complete_timer_timeout);
|
|
|
|
ClassDB::bind_method("_complete_request", &CodeTextEditor::_complete_request);
|
|
|
|
ClassDB::bind_method("_font_resize_timeout", &CodeTextEditor::_font_resize_timeout);
|
2016-08-02 22:11:05 +00:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("validate_script"));
|
|
|
|
ADD_SIGNAL(MethodInfo("load_theme_settings"));
|
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
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
CodeTextEditor::CodeTextEditor() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
code_complete_func = NULL;
|
|
|
|
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
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
find_replace_bar = memnew(FindReplaceBar);
|
2016-05-28 16:25:45 +00:00
|
|
|
add_child(find_replace_bar);
|
|
|
|
find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
find_replace_bar->hide();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
text_editor = memnew(TextEdit);
|
2014-02-10 01:10:30 +00:00
|
|
|
add_child(text_editor);
|
2016-05-28 16:25:45 +00:00
|
|
|
text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
|
|
|
|
find_replace_bar->set_text_edit(text_editor);
|
2015-01-02 16:38:31 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
text_editor->set_show_line_numbers(true);
|
2014-12-17 04:53:34 +00:00
|
|
|
text_editor->set_brace_matching(true);
|
2015-11-29 16:02:35 +00:00
|
|
|
text_editor->set_auto_indent(true);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
HBoxContainer *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);
|
|
|
|
|
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);
|
2017-03-05 15:44:50 +00:00
|
|
|
idle->set_wait_time(EDITOR_DEF("text_editor/completion/idle_parse_delay", 2));
|
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);
|
2017-03-05 15:44:50 +00:00
|
|
|
enable_complete_timer = EDITOR_DEF("text_editor/completion/enable_code_completion_delay", true);
|
2014-05-21 22:01:11 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/completion/code_complete_delay", .3f));
|
2014-05-04 03:22:49 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
error = memnew(Label);
|
2016-05-28 16:25:45 +00:00
|
|
|
status_bar->add_child(error);
|
2017-12-28 17:20:40 +00:00
|
|
|
error->set_autowrap(true);
|
2016-05-28 16:25:45 +00:00
|
|
|
error->set_valign(Label::VALIGN_CENTER);
|
2017-08-15 17:30:53 +00:00
|
|
|
error->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("error_color", "Editor"));
|
2017-09-27 19:44:48 +00:00
|
|
|
error->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
|
2017-08-19 13:50:54 +00:00
|
|
|
error->set_h_size_flags(SIZE_EXPAND_FILL); //required for it to display, given now it's clipping contents, do not touch
|
2016-05-28 16:25:45 +00:00
|
|
|
|
2017-09-27 19:44:48 +00:00
|
|
|
status_bar->add_child(memnew(Label)); //to keep the height if the other labels are not visible
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Label *line_txt = memnew(Label);
|
2016-07-24 15:11:32 +00:00
|
|
|
status_bar->add_child(line_txt);
|
|
|
|
line_txt->set_align(Label::ALIGN_RIGHT);
|
|
|
|
line_txt->set_valign(Label::VALIGN_CENTER);
|
|
|
|
line_txt->set_v_size_flags(SIZE_FILL);
|
|
|
|
line_txt->set_text(TTR("Line:"));
|
2017-09-27 19:44:48 +00:00
|
|
|
line_txt->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
|
2016-07-24 15:11:32 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
line_nb = memnew(Label);
|
2016-07-24 15:11:32 +00:00
|
|
|
status_bar->add_child(line_nb);
|
|
|
|
line_nb->set_valign(Label::VALIGN_CENTER);
|
|
|
|
line_nb->set_v_size_flags(SIZE_FILL);
|
2017-08-19 13:50:54 +00:00
|
|
|
line_nb->set_autowrap(true); // workaround to prevent resizing the label on each change, do not touch
|
|
|
|
line_nb->set_clip_text(true); // workaround to prevent resizing the label on each change, do not touch
|
2017-03-05 15:44:50 +00:00
|
|
|
line_nb->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
|
2017-09-27 19:44:48 +00:00
|
|
|
line_nb->set_align(Label::ALIGN_RIGHT);
|
|
|
|
line_nb->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
|
2016-07-24 15:11:32 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Label *col_txt = memnew(Label);
|
2016-07-24 15:11:32 +00:00
|
|
|
status_bar->add_child(col_txt);
|
|
|
|
col_txt->set_align(Label::ALIGN_RIGHT);
|
|
|
|
col_txt->set_valign(Label::VALIGN_CENTER);
|
|
|
|
col_txt->set_v_size_flags(SIZE_FILL);
|
|
|
|
col_txt->set_text(TTR("Col:"));
|
2017-09-27 19:44:48 +00:00
|
|
|
col_txt->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
|
2016-07-24 15:11:32 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
col_nb = memnew(Label);
|
2016-07-24 15:11:32 +00:00
|
|
|
status_bar->add_child(col_nb);
|
|
|
|
col_nb->set_valign(Label::VALIGN_CENTER);
|
|
|
|
col_nb->set_v_size_flags(SIZE_FILL);
|
2017-08-19 13:50:54 +00:00
|
|
|
col_nb->set_autowrap(true); // workaround to prevent resizing the label on each change, do not touch
|
|
|
|
col_nb->set_clip_text(true); // workaround to prevent resizing the label on each change, do not touch
|
2017-03-05 15:44:50 +00:00
|
|
|
col_nb->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
|
2017-09-27 19:44:48 +00:00
|
|
|
col_nb->set_align(Label::ALIGN_RIGHT);
|
|
|
|
col_nb->set("custom_constants/margin_right", 0);
|
|
|
|
col_nb->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
text_editor->connect("gui_input", this, "_text_editor_gui_input");
|
|
|
|
text_editor->connect("cursor_changed", this, "_line_col_changed");
|
|
|
|
text_editor->connect("text_changed", this, "_text_changed");
|
|
|
|
text_editor->connect("request_completion", this, "_complete_request");
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector<String> cs;
|
|
|
|
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("$");
|
2017-03-05 15:44:50 +00:00
|
|
|
text_editor->set_completion(true, cs);
|
|
|
|
idle->connect("timeout", this, "_text_changed_idle_timeout");
|
2014-05-21 22:01:11 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
code_complete_timer->connect("timeout", this, "_code_complete_timer_timeout");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
font_resize_val = 0;
|
2017-11-01 20:49:39 +00:00
|
|
|
font_size = -1;
|
2016-06-17 19:58:50 +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", this, "_font_resize_timeout");
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
EditorSettings::get_singleton()->connect("settings_changed", this, "_on_settings_change");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|