| 1 | /**************************************************************************/ |
| 2 | /* code_editor.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "code_editor.h" |
| 32 | |
| 33 | #include "core/input/input.h" |
| 34 | #include "core/os/keyboard.h" |
| 35 | #include "core/string/string_builder.h" |
| 36 | #include "core/templates/pair.h" |
| 37 | #include "editor/editor_scale.h" |
| 38 | #include "editor/editor_settings.h" |
| 39 | #include "editor/editor_string_names.h" |
| 40 | #include "editor/plugins/script_editor_plugin.h" |
| 41 | #include "scene/resources/font.h" |
| 42 | |
| 43 | void GotoLineDialog::(CodeEdit *p_edit) { |
| 44 | text_editor = p_edit; |
| 45 | |
| 46 | // Add 1 because text_editor->get_caret_line() starts from 0, but the editor user interface starts from 1. |
| 47 | line->set_text(itos(text_editor->get_caret_line() + 1)); |
| 48 | line->select_all(); |
| 49 | popup_centered(Size2(180, 80) * EDSCALE); |
| 50 | line->grab_focus(); |
| 51 | } |
| 52 | |
| 53 | int GotoLineDialog::get_line() const { |
| 54 | return line->get_text().to_int(); |
| 55 | } |
| 56 | |
| 57 | void GotoLineDialog::ok_pressed() { |
| 58 | // Subtract 1 because the editor user interface starts from 1, but text_editor->set_caret_line(n) starts from 0. |
| 59 | const int line_number = get_line() - 1; |
| 60 | if (line_number < 0 || line_number >= text_editor->get_line_count()) { |
| 61 | return; |
| 62 | } |
| 63 | text_editor->remove_secondary_carets(); |
| 64 | text_editor->unfold_line(line_number); |
| 65 | text_editor->set_caret_line(line_number); |
| 66 | hide(); |
| 67 | } |
| 68 | |
| 69 | GotoLineDialog::GotoLineDialog() { |
| 70 | set_title(TTR("Go to Line" )); |
| 71 | |
| 72 | VBoxContainer *vbc = memnew(VBoxContainer); |
| 73 | vbc->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 8 * EDSCALE); |
| 74 | vbc->set_anchor_and_offset(SIDE_TOP, Control::ANCHOR_BEGIN, 8 * EDSCALE); |
| 75 | vbc->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -8 * EDSCALE); |
| 76 | vbc->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, -8 * EDSCALE); |
| 77 | add_child(vbc); |
| 78 | |
| 79 | Label *l = memnew(Label); |
| 80 | l->set_text(TTR("Line Number:" )); |
| 81 | vbc->add_child(l); |
| 82 | |
| 83 | line = memnew(LineEdit); |
| 84 | vbc->add_child(line); |
| 85 | register_text_enter(line); |
| 86 | text_editor = nullptr; |
| 87 | |
| 88 | line_label = nullptr; |
| 89 | |
| 90 | set_hide_on_ok(false); |
| 91 | } |
| 92 | |
| 93 | void FindReplaceBar::_notification(int p_what) { |
| 94 | switch (p_what) { |
| 95 | case NOTIFICATION_READY: |
| 96 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 97 | find_prev->set_icon(get_editor_theme_icon(SNAME("MoveUp" ))); |
| 98 | find_next->set_icon(get_editor_theme_icon(SNAME("MoveDown" ))); |
| 99 | hide_button->set_texture_normal(get_editor_theme_icon(SNAME("Close" ))); |
| 100 | hide_button->set_texture_hover(get_editor_theme_icon(SNAME("Close" ))); |
| 101 | hide_button->set_texture_pressed(get_editor_theme_icon(SNAME("Close" ))); |
| 102 | hide_button->set_custom_minimum_size(hide_button->get_texture_normal()->get_size()); |
| 103 | } break; |
| 104 | |
| 105 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 106 | set_process_unhandled_input(is_visible_in_tree()); |
| 107 | } break; |
| 108 | |
| 109 | case NOTIFICATION_THEME_CHANGED: { |
| 110 | matches_label->add_theme_color_override("font_color" , results_count > 0 ? get_theme_color(SNAME("font_color" ), SNAME("Label" )) : get_theme_color(SNAME("error_color" ), EditorStringName(Editor))); |
| 111 | } break; |
| 112 | |
| 113 | case NOTIFICATION_PREDELETE: { |
| 114 | if (base_text_editor) { |
| 115 | base_text_editor->remove_find_replace_bar(); |
| 116 | base_text_editor = nullptr; |
| 117 | } |
| 118 | } break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void FindReplaceBar::unhandled_input(const Ref<InputEvent> &p_event) { |
| 123 | ERR_FAIL_COND(p_event.is_null()); |
| 124 | |
| 125 | Ref<InputEventKey> k = p_event; |
| 126 | |
| 127 | if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel" ), false, true)) { |
| 128 | Control *focus_owner = get_viewport()->gui_get_focus_owner(); |
| 129 | |
| 130 | if (text_editor->has_focus() || (focus_owner && is_ancestor_of(focus_owner))) { |
| 131 | _hide_bar(); |
| 132 | accept_event(); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) { |
| 138 | if (!preserve_cursor) { |
| 139 | text_editor->remove_secondary_carets(); |
| 140 | } |
| 141 | String text = get_search_text(); |
| 142 | Point2i pos = text_editor->search(text, p_flags, p_from_line, p_from_col); |
| 143 | |
| 144 | if (pos.x != -1) { |
| 145 | if (!preserve_cursor && !is_selection_only()) { |
| 146 | text_editor->unfold_line(pos.y); |
| 147 | text_editor->set_caret_line(pos.y, false); |
| 148 | text_editor->set_caret_column(pos.x + text.length(), false); |
| 149 | text_editor->center_viewport_to_caret(0); |
| 150 | text_editor->select(pos.y, pos.x, pos.y, pos.x + text.length()); |
| 151 | |
| 152 | line_col_changed_for_result = true; |
| 153 | } |
| 154 | |
| 155 | text_editor->set_search_text(text); |
| 156 | text_editor->set_search_flags(p_flags); |
| 157 | |
| 158 | result_line = pos.y; |
| 159 | result_col = pos.x; |
| 160 | |
| 161 | _update_results_count(); |
| 162 | } else { |
| 163 | results_count = 0; |
| 164 | result_line = -1; |
| 165 | result_col = -1; |
| 166 | text_editor->set_search_text("" ); |
| 167 | text_editor->set_search_flags(p_flags); |
| 168 | } |
| 169 | |
| 170 | _update_matches_label(); |
| 171 | |
| 172 | return pos.x != -1; |
| 173 | } |
| 174 | |
| 175 | void FindReplaceBar::_replace() { |
| 176 | text_editor->remove_secondary_carets(); |
| 177 | bool selection_enabled = text_editor->has_selection(0); |
| 178 | Point2i selection_begin, selection_end; |
| 179 | if (selection_enabled) { |
| 180 | selection_begin = Point2i(text_editor->get_selection_from_line(0), text_editor->get_selection_from_column(0)); |
| 181 | selection_end = Point2i(text_editor->get_selection_to_line(0), text_editor->get_selection_to_column(0)); |
| 182 | } |
| 183 | |
| 184 | String repl_text = get_replace_text(); |
| 185 | int search_text_len = get_search_text().length(); |
| 186 | |
| 187 | text_editor->begin_complex_operation(); |
| 188 | if (selection_enabled && is_selection_only()) { |
| 189 | // Restrict search_current() to selected region. |
| 190 | text_editor->set_caret_line(selection_begin.width, false, true, 0, 0); |
| 191 | text_editor->set_caret_column(selection_begin.height, true, 0); |
| 192 | } |
| 193 | |
| 194 | if (search_current()) { |
| 195 | text_editor->unfold_line(result_line); |
| 196 | text_editor->select(result_line, result_col, result_line, result_col + search_text_len, 0); |
| 197 | |
| 198 | if (selection_enabled && is_selection_only()) { |
| 199 | Point2i match_from(result_line, result_col); |
| 200 | Point2i match_to(result_line, result_col + search_text_len); |
| 201 | if (!(match_from < selection_begin || match_to > selection_end)) { |
| 202 | text_editor->insert_text_at_caret(repl_text, 0); |
| 203 | if (match_to.x == selection_end.x) { |
| 204 | // Adjust selection bounds if necessary. |
| 205 | selection_end.y += repl_text.length() - search_text_len; |
| 206 | } |
| 207 | } |
| 208 | } else { |
| 209 | text_editor->insert_text_at_caret(repl_text, 0); |
| 210 | } |
| 211 | } |
| 212 | text_editor->end_complex_operation(); |
| 213 | results_count = -1; |
| 214 | results_count_to_current = -1; |
| 215 | needs_to_count_results = true; |
| 216 | |
| 217 | if (selection_enabled && is_selection_only()) { |
| 218 | // Reselect in order to keep 'Replace' restricted to selection. |
| 219 | text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y, 0); |
| 220 | } else { |
| 221 | text_editor->deselect(0); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void FindReplaceBar::_replace_all() { |
| 226 | text_editor->remove_secondary_carets(); |
| 227 | text_editor->disconnect("text_changed" , callable_mp(this, &FindReplaceBar::_editor_text_changed)); |
| 228 | // Line as x so it gets priority in comparison, column as y. |
| 229 | Point2i orig_cursor(text_editor->get_caret_line(0), text_editor->get_caret_column(0)); |
| 230 | Point2i prev_match = Point2(-1, -1); |
| 231 | |
| 232 | bool selection_enabled = text_editor->has_selection(0); |
| 233 | if (!is_selection_only()) { |
| 234 | text_editor->deselect(); |
| 235 | selection_enabled = false; |
| 236 | } else { |
| 237 | result_line = -1; |
| 238 | result_col = -1; |
| 239 | } |
| 240 | |
| 241 | Point2i selection_begin, selection_end; |
| 242 | if (selection_enabled) { |
| 243 | selection_begin = Point2i(text_editor->get_selection_from_line(0), text_editor->get_selection_from_column(0)); |
| 244 | selection_end = Point2i(text_editor->get_selection_to_line(0), text_editor->get_selection_to_column(0)); |
| 245 | } |
| 246 | |
| 247 | int vsval = text_editor->get_v_scroll(); |
| 248 | |
| 249 | String repl_text = get_replace_text(); |
| 250 | int search_text_len = get_search_text().length(); |
| 251 | |
| 252 | int rc = 0; |
| 253 | |
| 254 | replace_all_mode = true; |
| 255 | |
| 256 | text_editor->begin_complex_operation(); |
| 257 | |
| 258 | if (selection_enabled && is_selection_only()) { |
| 259 | text_editor->set_caret_line(selection_begin.width, false, true, 0, 0); |
| 260 | text_editor->set_caret_column(selection_begin.height, true, 0); |
| 261 | } else { |
| 262 | text_editor->set_caret_line(0, false, true, 0, 0); |
| 263 | text_editor->set_caret_column(0, true, 0); |
| 264 | } |
| 265 | |
| 266 | if (search_current()) { |
| 267 | do { |
| 268 | // Replace area. |
| 269 | Point2i match_from(result_line, result_col); |
| 270 | Point2i match_to(result_line, result_col + search_text_len); |
| 271 | |
| 272 | if (match_from < prev_match) { |
| 273 | break; // Done. |
| 274 | } |
| 275 | |
| 276 | prev_match = Point2i(result_line, result_col + repl_text.length()); |
| 277 | |
| 278 | text_editor->unfold_line(result_line); |
| 279 | text_editor->select(result_line, result_col, result_line, match_to.y, 0); |
| 280 | |
| 281 | if (selection_enabled) { |
| 282 | if (match_from < selection_begin || match_to > selection_end) { |
| 283 | break; // Done. |
| 284 | } |
| 285 | |
| 286 | // Replace but adjust selection bounds. |
| 287 | text_editor->insert_text_at_caret(repl_text, 0); |
| 288 | if (match_to.x == selection_end.x) { |
| 289 | selection_end.y += repl_text.length() - search_text_len; |
| 290 | } |
| 291 | |
| 292 | } else { |
| 293 | // Just replace. |
| 294 | text_editor->insert_text_at_caret(repl_text, 0); |
| 295 | } |
| 296 | |
| 297 | rc++; |
| 298 | } while (search_next()); |
| 299 | } |
| 300 | |
| 301 | text_editor->end_complex_operation(); |
| 302 | |
| 303 | replace_all_mode = false; |
| 304 | |
| 305 | // Restore editor state (selection, cursor, scroll). |
| 306 | text_editor->set_caret_line(orig_cursor.x, false, true, 0, 0); |
| 307 | text_editor->set_caret_column(orig_cursor.y, true, 0); |
| 308 | |
| 309 | if (selection_enabled) { |
| 310 | // Reselect. |
| 311 | text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y, 0); |
| 312 | } |
| 313 | |
| 314 | text_editor->set_v_scroll(vsval); |
| 315 | matches_label->add_theme_color_override("font_color" , rc > 0 ? get_theme_color(SNAME("font_color" ), SNAME("Label" )) : get_theme_color(SNAME("error_color" ), EditorStringName(Editor))); |
| 316 | matches_label->set_text(vformat(TTR("%d replaced." ), rc)); |
| 317 | |
| 318 | text_editor->call_deferred(SNAME("connect" ), "text_changed" , callable_mp(this, &FindReplaceBar::_editor_text_changed)); |
| 319 | results_count = -1; |
| 320 | results_count_to_current = -1; |
| 321 | needs_to_count_results = true; |
| 322 | } |
| 323 | |
| 324 | void FindReplaceBar::_get_search_from(int &r_line, int &r_col, bool p_is_searching_next) { |
| 325 | if (!text_editor->has_selection(0) || is_selection_only()) { |
| 326 | r_line = text_editor->get_caret_line(0); |
| 327 | r_col = text_editor->get_caret_column(0); |
| 328 | |
| 329 | if (!p_is_searching_next && r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) { |
| 330 | r_col = result_col; |
| 331 | } |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | if (p_is_searching_next) { |
| 336 | r_line = text_editor->get_selection_to_line(); |
| 337 | r_col = text_editor->get_selection_to_column(); |
| 338 | } else { |
| 339 | r_line = text_editor->get_selection_from_line(); |
| 340 | r_col = text_editor->get_selection_from_column(); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | void FindReplaceBar::_update_results_count() { |
| 345 | if (!needs_to_count_results && (result_line != -1) && results_count_to_current > 0) { |
| 346 | results_count_to_current += (flags & TextEdit::SEARCH_BACKWARDS) ? -1 : 1; |
| 347 | |
| 348 | if (results_count_to_current > results_count) { |
| 349 | results_count_to_current = results_count_to_current - results_count; |
| 350 | } else if (results_count_to_current <= 0) { |
| 351 | results_count_to_current = results_count; |
| 352 | } |
| 353 | |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | String searched = get_search_text(); |
| 358 | if (searched.is_empty()) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | needs_to_count_results = false; |
| 363 | |
| 364 | results_count = 0; |
| 365 | |
| 366 | for (int i = 0; i < text_editor->get_line_count(); i++) { |
| 367 | String line_text = text_editor->get_line(i); |
| 368 | |
| 369 | int col_pos = 0; |
| 370 | |
| 371 | while (true) { |
| 372 | col_pos = is_case_sensitive() ? line_text.find(searched, col_pos) : line_text.findn(searched, col_pos); |
| 373 | |
| 374 | if (col_pos == -1) { |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | if (is_whole_words()) { |
| 379 | if (col_pos > 0 && !is_symbol(line_text[col_pos - 1])) { |
| 380 | col_pos += searched.length(); |
| 381 | continue; |
| 382 | } |
| 383 | if (col_pos + searched.length() < line_text.length() && !is_symbol(line_text[col_pos + searched.length()])) { |
| 384 | col_pos += searched.length(); |
| 385 | continue; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | results_count++; |
| 390 | |
| 391 | if (i == result_line) { |
| 392 | if (col_pos == result_col) { |
| 393 | results_count_to_current = results_count; |
| 394 | } else if (col_pos < result_col && col_pos + searched.length() > result_col) { |
| 395 | col_pos = result_col; |
| 396 | results_count_to_current = results_count; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | col_pos += searched.length(); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | void FindReplaceBar::_update_matches_label() { |
| 406 | if (search_text->get_text().is_empty() || results_count == -1) { |
| 407 | matches_label->hide(); |
| 408 | } else { |
| 409 | matches_label->show(); |
| 410 | |
| 411 | matches_label->add_theme_color_override("font_color" , results_count > 0 ? get_theme_color(SNAME("font_color" ), SNAME("Label" )) : get_theme_color(SNAME("error_color" ), EditorStringName(Editor))); |
| 412 | |
| 413 | if (results_count == 0) { |
| 414 | matches_label->set_text(TTR("No match" )); |
| 415 | } else if (results_count_to_current == -1) { |
| 416 | matches_label->set_text(vformat(TTRN("%d match" , "%d matches" , results_count), results_count)); |
| 417 | } else { |
| 418 | matches_label->set_text(vformat(TTRN("%d of %d match" , "%d of %d matches" , results_count), results_count_to_current, results_count)); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | bool FindReplaceBar::search_current() { |
| 424 | flags = 0; |
| 425 | |
| 426 | if (is_whole_words()) { |
| 427 | flags |= TextEdit::SEARCH_WHOLE_WORDS; |
| 428 | } |
| 429 | if (is_case_sensitive()) { |
| 430 | flags |= TextEdit::SEARCH_MATCH_CASE; |
| 431 | } |
| 432 | |
| 433 | int line, col; |
| 434 | _get_search_from(line, col); |
| 435 | |
| 436 | return _search(flags, line, col); |
| 437 | } |
| 438 | |
| 439 | bool FindReplaceBar::search_prev() { |
| 440 | if (is_selection_only() && !replace_all_mode) { |
| 441 | return false; |
| 442 | } |
| 443 | |
| 444 | if (!is_visible()) { |
| 445 | popup_search(true); |
| 446 | } |
| 447 | |
| 448 | flags = 0; |
| 449 | String text = get_search_text(); |
| 450 | |
| 451 | if (is_whole_words()) { |
| 452 | flags |= TextEdit::SEARCH_WHOLE_WORDS; |
| 453 | } |
| 454 | if (is_case_sensitive()) { |
| 455 | flags |= TextEdit::SEARCH_MATCH_CASE; |
| 456 | } |
| 457 | |
| 458 | flags |= TextEdit::SEARCH_BACKWARDS; |
| 459 | |
| 460 | int line, col; |
| 461 | _get_search_from(line, col); |
| 462 | |
| 463 | col -= text.length(); |
| 464 | if (col < 0) { |
| 465 | line -= 1; |
| 466 | if (line < 0) { |
| 467 | line = text_editor->get_line_count() - 1; |
| 468 | } |
| 469 | col = text_editor->get_line(line).length(); |
| 470 | } |
| 471 | |
| 472 | return _search(flags, line, col); |
| 473 | } |
| 474 | |
| 475 | bool FindReplaceBar::search_next() { |
| 476 | if (is_selection_only() && !replace_all_mode) { |
| 477 | return false; |
| 478 | } |
| 479 | |
| 480 | if (!is_visible()) { |
| 481 | popup_search(true); |
| 482 | } |
| 483 | |
| 484 | flags = 0; |
| 485 | |
| 486 | if (is_whole_words()) { |
| 487 | flags |= TextEdit::SEARCH_WHOLE_WORDS; |
| 488 | } |
| 489 | if (is_case_sensitive()) { |
| 490 | flags |= TextEdit::SEARCH_MATCH_CASE; |
| 491 | } |
| 492 | |
| 493 | int line, col; |
| 494 | _get_search_from(line, col, true); |
| 495 | |
| 496 | return _search(flags, line, col); |
| 497 | } |
| 498 | |
| 499 | void FindReplaceBar::_hide_bar() { |
| 500 | if (replace_text->has_focus() || search_text->has_focus()) { |
| 501 | text_editor->grab_focus(); |
| 502 | } |
| 503 | |
| 504 | text_editor->set_search_text("" ); |
| 505 | result_line = -1; |
| 506 | result_col = -1; |
| 507 | hide(); |
| 508 | } |
| 509 | |
| 510 | void FindReplaceBar::_show_search(bool p_focus_replace, bool p_show_only) { |
| 511 | show(); |
| 512 | if (p_show_only) { |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | if (p_focus_replace) { |
| 517 | search_text->deselect(); |
| 518 | replace_text->call_deferred(SNAME("grab_focus" )); |
| 519 | } else { |
| 520 | replace_text->deselect(); |
| 521 | search_text->call_deferred(SNAME("grab_focus" )); |
| 522 | } |
| 523 | |
| 524 | if (text_editor->has_selection(0) && !is_selection_only()) { |
| 525 | search_text->set_text(text_editor->get_selected_text(0)); |
| 526 | result_line = text_editor->get_selection_from_line(); |
| 527 | result_col = text_editor->get_selection_from_column(); |
| 528 | } |
| 529 | |
| 530 | if (!get_search_text().is_empty()) { |
| 531 | if (p_focus_replace) { |
| 532 | replace_text->select_all(); |
| 533 | replace_text->set_caret_column(replace_text->get_text().length()); |
| 534 | } else { |
| 535 | search_text->select_all(); |
| 536 | search_text->set_caret_column(search_text->get_text().length()); |
| 537 | } |
| 538 | |
| 539 | results_count = -1; |
| 540 | results_count_to_current = -1; |
| 541 | needs_to_count_results = true; |
| 542 | _update_results_count(); |
| 543 | _update_matches_label(); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | void FindReplaceBar::(bool p_show_only) { |
| 548 | replace_text->hide(); |
| 549 | hbc_button_replace->hide(); |
| 550 | hbc_option_replace->hide(); |
| 551 | selection_only->set_pressed(false); |
| 552 | |
| 553 | _show_search(false, p_show_only); |
| 554 | } |
| 555 | |
| 556 | void FindReplaceBar::() { |
| 557 | if (!replace_text->is_visible_in_tree()) { |
| 558 | replace_text->show(); |
| 559 | hbc_button_replace->show(); |
| 560 | hbc_option_replace->show(); |
| 561 | } |
| 562 | |
| 563 | selection_only->set_pressed((text_editor->has_selection(0) && text_editor->get_selection_from_line(0) < text_editor->get_selection_to_line(0))); |
| 564 | |
| 565 | _show_search(is_visible() || text_editor->has_selection(0)); |
| 566 | } |
| 567 | |
| 568 | void FindReplaceBar::_search_options_changed(bool p_pressed) { |
| 569 | results_count = -1; |
| 570 | results_count_to_current = -1; |
| 571 | needs_to_count_results = true; |
| 572 | search_current(); |
| 573 | } |
| 574 | |
| 575 | void FindReplaceBar::_editor_text_changed() { |
| 576 | results_count = -1; |
| 577 | results_count_to_current = -1; |
| 578 | needs_to_count_results = true; |
| 579 | if (is_visible_in_tree()) { |
| 580 | preserve_cursor = true; |
| 581 | search_current(); |
| 582 | preserve_cursor = false; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | void FindReplaceBar::_search_text_changed(const String &p_text) { |
| 587 | results_count = -1; |
| 588 | results_count_to_current = -1; |
| 589 | needs_to_count_results = true; |
| 590 | search_current(); |
| 591 | } |
| 592 | |
| 593 | void FindReplaceBar::_search_text_submitted(const String &p_text) { |
| 594 | if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) { |
| 595 | search_prev(); |
| 596 | } else { |
| 597 | search_next(); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | void FindReplaceBar::_replace_text_submitted(const String &p_text) { |
| 602 | if (selection_only->is_pressed() && text_editor->has_selection(0)) { |
| 603 | _replace_all(); |
| 604 | _hide_bar(); |
| 605 | } else if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) { |
| 606 | _replace(); |
| 607 | search_prev(); |
| 608 | } else { |
| 609 | _replace(); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | String FindReplaceBar::get_search_text() const { |
| 614 | return search_text->get_text(); |
| 615 | } |
| 616 | |
| 617 | String FindReplaceBar::get_replace_text() const { |
| 618 | return replace_text->get_text(); |
| 619 | } |
| 620 | |
| 621 | bool FindReplaceBar::is_case_sensitive() const { |
| 622 | return case_sensitive->is_pressed(); |
| 623 | } |
| 624 | |
| 625 | bool FindReplaceBar::is_whole_words() const { |
| 626 | return whole_words->is_pressed(); |
| 627 | } |
| 628 | |
| 629 | bool FindReplaceBar::is_selection_only() const { |
| 630 | return selection_only->is_pressed(); |
| 631 | } |
| 632 | |
| 633 | void FindReplaceBar::set_error(const String &p_label) { |
| 634 | emit_signal(SNAME("error" ), p_label); |
| 635 | } |
| 636 | |
| 637 | void FindReplaceBar::set_text_edit(CodeTextEditor *p_text_editor) { |
| 638 | if (p_text_editor == base_text_editor) { |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | if (base_text_editor) { |
| 643 | base_text_editor->remove_find_replace_bar(); |
| 644 | base_text_editor = nullptr; |
| 645 | text_editor->disconnect("text_changed" , callable_mp(this, &FindReplaceBar::_editor_text_changed)); |
| 646 | text_editor = nullptr; |
| 647 | } |
| 648 | |
| 649 | if (!p_text_editor) { |
| 650 | return; |
| 651 | } |
| 652 | |
| 653 | results_count = -1; |
| 654 | results_count_to_current = -1; |
| 655 | needs_to_count_results = true; |
| 656 | base_text_editor = p_text_editor; |
| 657 | text_editor = base_text_editor->get_text_editor(); |
| 658 | text_editor->connect("text_changed" , callable_mp(this, &FindReplaceBar::_editor_text_changed)); |
| 659 | |
| 660 | _update_results_count(); |
| 661 | _update_matches_label(); |
| 662 | } |
| 663 | |
| 664 | void FindReplaceBar::_bind_methods() { |
| 665 | ClassDB::bind_method("_search_current" , &FindReplaceBar::search_current); |
| 666 | |
| 667 | ADD_SIGNAL(MethodInfo("error" )); |
| 668 | } |
| 669 | |
| 670 | FindReplaceBar::FindReplaceBar() { |
| 671 | vbc_lineedit = memnew(VBoxContainer); |
| 672 | add_child(vbc_lineedit); |
| 673 | vbc_lineedit->set_alignment(BoxContainer::ALIGNMENT_CENTER); |
| 674 | vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL); |
| 675 | VBoxContainer *vbc_button = memnew(VBoxContainer); |
| 676 | add_child(vbc_button); |
| 677 | VBoxContainer *vbc_option = memnew(VBoxContainer); |
| 678 | add_child(vbc_option); |
| 679 | |
| 680 | HBoxContainer *hbc_button_search = memnew(HBoxContainer); |
| 681 | vbc_button->add_child(hbc_button_search); |
| 682 | hbc_button_search->set_alignment(BoxContainer::ALIGNMENT_END); |
| 683 | hbc_button_replace = memnew(HBoxContainer); |
| 684 | vbc_button->add_child(hbc_button_replace); |
| 685 | hbc_button_replace->set_alignment(BoxContainer::ALIGNMENT_END); |
| 686 | |
| 687 | HBoxContainer *hbc_option_search = memnew(HBoxContainer); |
| 688 | vbc_option->add_child(hbc_option_search); |
| 689 | hbc_option_replace = memnew(HBoxContainer); |
| 690 | vbc_option->add_child(hbc_option_replace); |
| 691 | |
| 692 | // Search toolbar |
| 693 | search_text = memnew(LineEdit); |
| 694 | vbc_lineedit->add_child(search_text); |
| 695 | search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0)); |
| 696 | search_text->connect("text_changed" , callable_mp(this, &FindReplaceBar::_search_text_changed)); |
| 697 | search_text->connect("text_submitted" , callable_mp(this, &FindReplaceBar::_search_text_submitted)); |
| 698 | |
| 699 | matches_label = memnew(Label); |
| 700 | hbc_button_search->add_child(matches_label); |
| 701 | matches_label->hide(); |
| 702 | |
| 703 | find_prev = memnew(Button); |
| 704 | find_prev->set_flat(true); |
| 705 | hbc_button_search->add_child(find_prev); |
| 706 | find_prev->set_focus_mode(FOCUS_NONE); |
| 707 | find_prev->connect("pressed" , callable_mp(this, &FindReplaceBar::search_prev)); |
| 708 | |
| 709 | find_next = memnew(Button); |
| 710 | find_next->set_flat(true); |
| 711 | hbc_button_search->add_child(find_next); |
| 712 | find_next->set_focus_mode(FOCUS_NONE); |
| 713 | find_next->connect("pressed" , callable_mp(this, &FindReplaceBar::search_next)); |
| 714 | |
| 715 | case_sensitive = memnew(CheckBox); |
| 716 | hbc_option_search->add_child(case_sensitive); |
| 717 | case_sensitive->set_text(TTR("Match Case" )); |
| 718 | case_sensitive->set_focus_mode(FOCUS_NONE); |
| 719 | case_sensitive->connect("toggled" , callable_mp(this, &FindReplaceBar::_search_options_changed)); |
| 720 | |
| 721 | whole_words = memnew(CheckBox); |
| 722 | hbc_option_search->add_child(whole_words); |
| 723 | whole_words->set_text(TTR("Whole Words" )); |
| 724 | whole_words->set_focus_mode(FOCUS_NONE); |
| 725 | whole_words->connect("toggled" , callable_mp(this, &FindReplaceBar::_search_options_changed)); |
| 726 | |
| 727 | // Replace toolbar |
| 728 | replace_text = memnew(LineEdit); |
| 729 | vbc_lineedit->add_child(replace_text); |
| 730 | replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0)); |
| 731 | replace_text->connect("text_submitted" , callable_mp(this, &FindReplaceBar::_replace_text_submitted)); |
| 732 | |
| 733 | replace = memnew(Button); |
| 734 | hbc_button_replace->add_child(replace); |
| 735 | replace->set_text(TTR("Replace" )); |
| 736 | replace->connect("pressed" , callable_mp(this, &FindReplaceBar::_replace)); |
| 737 | |
| 738 | replace_all = memnew(Button); |
| 739 | hbc_button_replace->add_child(replace_all); |
| 740 | replace_all->set_text(TTR("Replace All" )); |
| 741 | replace_all->connect("pressed" , callable_mp(this, &FindReplaceBar::_replace_all)); |
| 742 | |
| 743 | selection_only = memnew(CheckBox); |
| 744 | hbc_option_replace->add_child(selection_only); |
| 745 | selection_only->set_text(TTR("Selection Only" )); |
| 746 | selection_only->set_focus_mode(FOCUS_NONE); |
| 747 | selection_only->connect("toggled" , callable_mp(this, &FindReplaceBar::_search_options_changed)); |
| 748 | |
| 749 | hide_button = memnew(TextureButton); |
| 750 | add_child(hide_button); |
| 751 | hide_button->set_focus_mode(FOCUS_NONE); |
| 752 | hide_button->connect("pressed" , callable_mp(this, &FindReplaceBar::_hide_bar)); |
| 753 | hide_button->set_v_size_flags(SIZE_SHRINK_CENTER); |
| 754 | } |
| 755 | |
| 756 | /*** CODE EDITOR ****/ |
| 757 | |
| 758 | // This function should be used to handle shortcuts that could otherwise |
| 759 | // be handled too late if they weren't handled here. |
| 760 | void CodeTextEditor::input(const Ref<InputEvent> &event) { |
| 761 | ERR_FAIL_COND(event.is_null()); |
| 762 | |
| 763 | const Ref<InputEventKey> key_event = event; |
| 764 | |
| 765 | if (!key_event.is_valid()) { |
| 766 | return; |
| 767 | } |
| 768 | if (!key_event->is_pressed()) { |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | if (!text_editor->has_focus()) { |
| 773 | if ((find_replace_bar != nullptr && find_replace_bar->is_visible()) && (find_replace_bar->has_focus() || find_replace_bar->is_ancestor_of(get_viewport()->gui_get_focus_owner()))) { |
| 774 | if (ED_IS_SHORTCUT("script_text_editor/find_next" , key_event)) { |
| 775 | find_replace_bar->search_next(); |
| 776 | accept_event(); |
| 777 | return; |
| 778 | } |
| 779 | if (ED_IS_SHORTCUT("script_text_editor/find_previous" , key_event)) { |
| 780 | find_replace_bar->search_prev(); |
| 781 | accept_event(); |
| 782 | return; |
| 783 | } |
| 784 | } |
| 785 | return; |
| 786 | } |
| 787 | |
| 788 | if (ED_IS_SHORTCUT("script_text_editor/move_up" , key_event)) { |
| 789 | move_lines_up(); |
| 790 | accept_event(); |
| 791 | return; |
| 792 | } |
| 793 | if (ED_IS_SHORTCUT("script_text_editor/move_down" , key_event)) { |
| 794 | move_lines_down(); |
| 795 | accept_event(); |
| 796 | return; |
| 797 | } |
| 798 | if (ED_IS_SHORTCUT("script_text_editor/delete_line" , key_event)) { |
| 799 | delete_lines(); |
| 800 | accept_event(); |
| 801 | return; |
| 802 | } |
| 803 | if (ED_IS_SHORTCUT("script_text_editor/duplicate_selection" , key_event)) { |
| 804 | duplicate_selection(); |
| 805 | accept_event(); |
| 806 | return; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) { |
| 811 | Ref<InputEventMouseButton> mb = p_event; |
| 812 | |
| 813 | if (mb.is_valid()) { |
| 814 | if (mb->is_pressed() && mb->is_command_or_control_pressed()) { |
| 815 | if (mb->get_button_index() == MouseButton::WHEEL_UP) { |
| 816 | _zoom_in(); |
| 817 | } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN) { |
| 818 | _zoom_out(); |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | Ref<InputEventMagnifyGesture> magnify_gesture = p_event; |
| 824 | if (magnify_gesture.is_valid()) { |
| 825 | font_size = text_editor->get_theme_font_size(SNAME("font_size" )); |
| 826 | font_size *= powf(magnify_gesture->get_factor(), 0.25); |
| 827 | |
| 828 | _add_font_size((int)font_size - text_editor->get_theme_font_size(SNAME("font_size" ))); |
| 829 | return; |
| 830 | } |
| 831 | |
| 832 | Ref<InputEventKey> k = p_event; |
| 833 | |
| 834 | if (k.is_valid()) { |
| 835 | if (k->is_pressed()) { |
| 836 | if (ED_IS_SHORTCUT("script_editor/zoom_in" , p_event)) { |
| 837 | _zoom_in(); |
| 838 | accept_event(); |
| 839 | } |
| 840 | if (ED_IS_SHORTCUT("script_editor/zoom_out" , p_event)) { |
| 841 | _zoom_out(); |
| 842 | accept_event(); |
| 843 | } |
| 844 | if (ED_IS_SHORTCUT("script_editor/reset_zoom" , p_event)) { |
| 845 | _reset_zoom(); |
| 846 | accept_event(); |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | void CodeTextEditor::_zoom_in() { |
| 853 | font_resize_val += MAX(EDSCALE, 1.0f); |
| 854 | _zoom_changed(); |
| 855 | } |
| 856 | |
| 857 | void CodeTextEditor::_zoom_out() { |
| 858 | font_resize_val -= MAX(EDSCALE, 1.0f); |
| 859 | _zoom_changed(); |
| 860 | } |
| 861 | |
| 862 | void CodeTextEditor::_zoom_changed() { |
| 863 | if (font_resize_timer->get_time_left() == 0) { |
| 864 | font_resize_timer->start(); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | void CodeTextEditor::_reset_zoom() { |
| 869 | EditorSettings::get_singleton()->set("interface/editor/code_font_size" , 14); |
| 870 | text_editor->add_theme_font_size_override("font_size" , 14 * EDSCALE); |
| 871 | } |
| 872 | |
| 873 | void CodeTextEditor::_line_col_changed() { |
| 874 | if (!code_complete_timer->is_stopped() && code_complete_timer_line != text_editor->get_caret_line()) { |
| 875 | code_complete_timer->stop(); |
| 876 | } |
| 877 | |
| 878 | String line = text_editor->get_line(text_editor->get_caret_line()); |
| 879 | |
| 880 | int positional_column = 0; |
| 881 | for (int i = 0; i < text_editor->get_caret_column(); i++) { |
| 882 | if (line[i] == '\t') { |
| 883 | positional_column += text_editor->get_indent_size(); // Tab size |
| 884 | } else { |
| 885 | positional_column += 1; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | StringBuilder sb; |
| 890 | sb.append(itos(text_editor->get_caret_line() + 1).lpad(4)); |
| 891 | sb.append(" : " ); |
| 892 | sb.append(itos(positional_column + 1).lpad(3)); |
| 893 | |
| 894 | sb.append(" | " ); |
| 895 | sb.append(text_editor->is_indent_using_spaces() ? TTR("Spaces" , "Indentation" ) : TTR("Tabs" , "Indentation" )); |
| 896 | |
| 897 | line_and_col_txt->set_text(sb.as_string()); |
| 898 | |
| 899 | if (find_replace_bar) { |
| 900 | if (!find_replace_bar->line_col_changed_for_result) { |
| 901 | find_replace_bar->needs_to_count_results = true; |
| 902 | } |
| 903 | |
| 904 | find_replace_bar->line_col_changed_for_result = false; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | void CodeTextEditor::_text_changed() { |
| 909 | if (code_complete_enabled && text_editor->is_insert_text_operation()) { |
| 910 | code_complete_timer_line = text_editor->get_caret_line(); |
| 911 | code_complete_timer->start(); |
| 912 | } |
| 913 | |
| 914 | idle->start(); |
| 915 | |
| 916 | if (find_replace_bar) { |
| 917 | find_replace_bar->needs_to_count_results = true; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | void CodeTextEditor::_code_complete_timer_timeout() { |
| 922 | if (!is_visible_in_tree()) { |
| 923 | return; |
| 924 | } |
| 925 | text_editor->request_code_completion(); |
| 926 | } |
| 927 | |
| 928 | void CodeTextEditor::_complete_request() { |
| 929 | List<ScriptLanguage::CodeCompletionOption> entries; |
| 930 | String ctext = text_editor->get_text_for_code_completion(); |
| 931 | _code_complete_script(ctext, &entries); |
| 932 | bool forced = false; |
| 933 | if (code_complete_func) { |
| 934 | code_complete_func(code_complete_ud, ctext, &entries, forced); |
| 935 | } |
| 936 | if (entries.size() == 0) { |
| 937 | return; |
| 938 | } |
| 939 | |
| 940 | for (const ScriptLanguage::CodeCompletionOption &e : entries) { |
| 941 | Color font_color = completion_font_color; |
| 942 | if (!e.theme_color_name.is_empty() && EDITOR_GET("text_editor/completion/colorize_suggestions" )) { |
| 943 | font_color = get_theme_color(e.theme_color_name, SNAME("Editor" )); |
| 944 | } else if (e.insert_text.begins_with("\"" ) || e.insert_text.begins_with("\'" )) { |
| 945 | font_color = completion_string_color; |
| 946 | } else if (e.insert_text.begins_with("#" ) || e.insert_text.begins_with("//" )) { |
| 947 | font_color = completion_comment_color; |
| 948 | } |
| 949 | text_editor->add_code_completion_option((CodeEdit::CodeCompletionKind)e.kind, e.display, e.insert_text, font_color, _get_completion_icon(e), e.default_value, e.location); |
| 950 | } |
| 951 | text_editor->update_code_completion_options(forced); |
| 952 | } |
| 953 | |
| 954 | Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptLanguage::CodeCompletionOption &p_option) { |
| 955 | Ref<Texture2D> tex; |
| 956 | switch (p_option.kind) { |
| 957 | case ScriptLanguage::CODE_COMPLETION_KIND_CLASS: { |
| 958 | if (has_theme_icon(p_option.display, EditorStringName(EditorIcons))) { |
| 959 | tex = get_editor_theme_icon(p_option.display); |
| 960 | } else { |
| 961 | tex = get_editor_theme_icon(SNAME("Object" )); |
| 962 | } |
| 963 | } break; |
| 964 | case ScriptLanguage::CODE_COMPLETION_KIND_ENUM: |
| 965 | tex = get_editor_theme_icon(SNAME("Enum" )); |
| 966 | break; |
| 967 | case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH: |
| 968 | tex = get_editor_theme_icon(SNAME("File" )); |
| 969 | break; |
| 970 | case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH: |
| 971 | tex = get_editor_theme_icon(SNAME("NodePath" )); |
| 972 | break; |
| 973 | case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE: |
| 974 | tex = get_editor_theme_icon(SNAME("Variant" )); |
| 975 | break; |
| 976 | case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT: |
| 977 | tex = get_editor_theme_icon(SNAME("MemberConstant" )); |
| 978 | break; |
| 979 | case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER: |
| 980 | tex = get_editor_theme_icon(SNAME("MemberProperty" )); |
| 981 | break; |
| 982 | case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL: |
| 983 | tex = get_editor_theme_icon(SNAME("MemberSignal" )); |
| 984 | break; |
| 985 | case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION: |
| 986 | tex = get_editor_theme_icon(SNAME("MemberMethod" )); |
| 987 | break; |
| 988 | case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT: |
| 989 | tex = get_editor_theme_icon(SNAME("BoxMesh" )); |
| 990 | break; |
| 991 | default: |
| 992 | tex = get_editor_theme_icon(SNAME("String" )); |
| 993 | break; |
| 994 | } |
| 995 | return tex; |
| 996 | } |
| 997 | |
| 998 | void CodeTextEditor::_font_resize_timeout() { |
| 999 | if (_add_font_size(font_resize_val)) { |
| 1000 | font_resize_val = 0; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | bool CodeTextEditor::_add_font_size(int p_delta) { |
| 1005 | int old_size = text_editor->get_theme_font_size(SNAME("font_size" )); |
| 1006 | int new_size = CLAMP(old_size + p_delta, 8 * EDSCALE, 96 * EDSCALE); |
| 1007 | |
| 1008 | if (new_size != old_size) { |
| 1009 | EditorSettings::get_singleton()->set("interface/editor/code_font_size" , new_size / EDSCALE); |
| 1010 | text_editor->add_theme_font_size_override("font_size" , new_size); |
| 1011 | } |
| 1012 | |
| 1013 | return true; |
| 1014 | } |
| 1015 | |
| 1016 | void CodeTextEditor::update_editor_settings() { |
| 1017 | // Theme: Highlighting |
| 1018 | completion_font_color = EDITOR_GET("text_editor/theme/highlighting/completion_font_color" ); |
| 1019 | completion_string_color = EDITOR_GET("text_editor/theme/highlighting/string_color" ); |
| 1020 | completion_comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color" ); |
| 1021 | |
| 1022 | // Appearance: Caret |
| 1023 | text_editor->set_caret_type((TextEdit::CaretType)EDITOR_GET("text_editor/appearance/caret/type" ).operator int()); |
| 1024 | text_editor->set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink" )); |
| 1025 | text_editor->set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval" )); |
| 1026 | text_editor->set_highlight_current_line(EDITOR_GET("text_editor/appearance/caret/highlight_current_line" )); |
| 1027 | text_editor->set_highlight_all_occurrences(EDITOR_GET("text_editor/appearance/caret/highlight_all_occurrences" )); |
| 1028 | |
| 1029 | // Appearance: Gutters |
| 1030 | text_editor->set_draw_line_numbers(EDITOR_GET("text_editor/appearance/gutters/show_line_numbers" )); |
| 1031 | text_editor->set_line_numbers_zero_padded(EDITOR_GET("text_editor/appearance/gutters/line_numbers_zero_padded" )); |
| 1032 | |
| 1033 | // Appearance: Minimap |
| 1034 | text_editor->set_draw_minimap(EDITOR_GET("text_editor/appearance/minimap/show_minimap" )); |
| 1035 | text_editor->set_minimap_width((int)EDITOR_GET("text_editor/appearance/minimap/minimap_width" ) * EDSCALE); |
| 1036 | |
| 1037 | // Appearance: Lines |
| 1038 | text_editor->set_line_folding_enabled(EDITOR_GET("text_editor/appearance/lines/code_folding" )); |
| 1039 | text_editor->set_draw_fold_gutter(EDITOR_GET("text_editor/appearance/lines/code_folding" )); |
| 1040 | text_editor->set_line_wrapping_mode((TextEdit::LineWrappingMode)EDITOR_GET("text_editor/appearance/lines/word_wrap" ).operator int()); |
| 1041 | text_editor->set_autowrap_mode((TextServer::AutowrapMode)EDITOR_GET("text_editor/appearance/lines/autowrap_mode" ).operator int()); |
| 1042 | |
| 1043 | // Appearance: Whitespace |
| 1044 | text_editor->set_draw_tabs(EDITOR_GET("text_editor/appearance/whitespace/draw_tabs" )); |
| 1045 | text_editor->set_draw_spaces(EDITOR_GET("text_editor/appearance/whitespace/draw_spaces" )); |
| 1046 | text_editor->add_theme_constant_override("line_spacing" , EDITOR_GET("text_editor/appearance/whitespace/line_spacing" )); |
| 1047 | |
| 1048 | // Behavior: Navigation |
| 1049 | text_editor->set_scroll_past_end_of_file_enabled(EDITOR_GET("text_editor/behavior/navigation/scroll_past_end_of_file" )); |
| 1050 | text_editor->set_smooth_scroll_enabled(EDITOR_GET("text_editor/behavior/navigation/smooth_scrolling" )); |
| 1051 | text_editor->set_v_scroll_speed(EDITOR_GET("text_editor/behavior/navigation/v_scroll_speed" )); |
| 1052 | text_editor->set_drag_and_drop_selection_enabled(EDITOR_GET("text_editor/behavior/navigation/drag_and_drop_selection" )); |
| 1053 | |
| 1054 | // Behavior: indent |
| 1055 | text_editor->set_indent_using_spaces(EDITOR_GET("text_editor/behavior/indent/type" )); |
| 1056 | text_editor->set_indent_size(EDITOR_GET("text_editor/behavior/indent/size" )); |
| 1057 | text_editor->set_auto_indent_enabled(EDITOR_GET("text_editor/behavior/indent/auto_indent" )); |
| 1058 | |
| 1059 | // Completion |
| 1060 | text_editor->set_auto_brace_completion_enabled(EDITOR_GET("text_editor/completion/auto_brace_complete" )); |
| 1061 | |
| 1062 | // Appearance: Guidelines |
| 1063 | if (EDITOR_GET("text_editor/appearance/guidelines/show_line_length_guidelines" )) { |
| 1064 | TypedArray<int> guideline_cols; |
| 1065 | guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_hard_column" )); |
| 1066 | if (EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column" ) != guideline_cols[0]) { |
| 1067 | guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column" )); |
| 1068 | } |
| 1069 | text_editor->set_line_length_guidelines(guideline_cols); |
| 1070 | } else { |
| 1071 | text_editor->set_line_length_guidelines(TypedArray<int>()); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | void CodeTextEditor::set_find_replace_bar(FindReplaceBar *p_bar) { |
| 1076 | if (find_replace_bar) { |
| 1077 | return; |
| 1078 | } |
| 1079 | |
| 1080 | find_replace_bar = p_bar; |
| 1081 | find_replace_bar->set_text_edit(this); |
| 1082 | find_replace_bar->connect("error" , callable_mp(error, &Label::set_text)); |
| 1083 | } |
| 1084 | |
| 1085 | void CodeTextEditor::remove_find_replace_bar() { |
| 1086 | if (!find_replace_bar) { |
| 1087 | return; |
| 1088 | } |
| 1089 | |
| 1090 | find_replace_bar->disconnect("error" , callable_mp(error, &Label::set_text)); |
| 1091 | find_replace_bar = nullptr; |
| 1092 | } |
| 1093 | |
| 1094 | void CodeTextEditor::trim_trailing_whitespace() { |
| 1095 | bool trimmed_whitespace = false; |
| 1096 | for (int i = 0; i < text_editor->get_line_count(); i++) { |
| 1097 | String line = text_editor->get_line(i); |
| 1098 | if (line.ends_with(" " ) || line.ends_with("\t" )) { |
| 1099 | if (!trimmed_whitespace) { |
| 1100 | text_editor->begin_complex_operation(); |
| 1101 | trimmed_whitespace = true; |
| 1102 | } |
| 1103 | |
| 1104 | int end = 0; |
| 1105 | for (int j = line.length() - 1; j > -1; j--) { |
| 1106 | if (line[j] != ' ' && line[j] != '\t') { |
| 1107 | end = j + 1; |
| 1108 | break; |
| 1109 | } |
| 1110 | } |
| 1111 | text_editor->set_line(i, line.substr(0, end)); |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | if (trimmed_whitespace) { |
| 1116 | text_editor->merge_overlapping_carets(); |
| 1117 | text_editor->end_complex_operation(); |
| 1118 | text_editor->queue_redraw(); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | void CodeTextEditor::insert_final_newline() { |
| 1123 | int final_line = text_editor->get_line_count() - 1; |
| 1124 | |
| 1125 | String line = text_editor->get_line(final_line); |
| 1126 | |
| 1127 | // Length 0 means it's already an empty line, no need to add a newline. |
| 1128 | if (line.length() > 0 && !line.ends_with("\n" )) { |
| 1129 | text_editor->begin_complex_operation(); |
| 1130 | |
| 1131 | line += "\n" ; |
| 1132 | text_editor->set_line(final_line, line); |
| 1133 | |
| 1134 | text_editor->end_complex_operation(); |
| 1135 | text_editor->queue_redraw(); |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | void CodeTextEditor::convert_case(CaseStyle p_case) { |
| 1140 | if (!text_editor->has_selection()) { |
| 1141 | return; |
| 1142 | } |
| 1143 | text_editor->begin_complex_operation(); |
| 1144 | |
| 1145 | Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order(); |
| 1146 | for (const int &c : caret_edit_order) { |
| 1147 | if (!text_editor->has_selection(c)) { |
| 1148 | continue; |
| 1149 | } |
| 1150 | |
| 1151 | int begin = text_editor->get_selection_from_line(c); |
| 1152 | int end = text_editor->get_selection_to_line(c); |
| 1153 | int begin_col = text_editor->get_selection_from_column(c); |
| 1154 | int end_col = text_editor->get_selection_to_column(c); |
| 1155 | |
| 1156 | for (int i = begin; i <= end; i++) { |
| 1157 | int len = text_editor->get_line(i).length(); |
| 1158 | if (i == end) { |
| 1159 | len = end_col; |
| 1160 | } |
| 1161 | if (i == begin) { |
| 1162 | len -= begin_col; |
| 1163 | } |
| 1164 | String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len); |
| 1165 | |
| 1166 | switch (p_case) { |
| 1167 | case UPPER: { |
| 1168 | new_line = new_line.to_upper(); |
| 1169 | } break; |
| 1170 | case LOWER: { |
| 1171 | new_line = new_line.to_lower(); |
| 1172 | } break; |
| 1173 | case CAPITALIZE: { |
| 1174 | new_line = new_line.capitalize(); |
| 1175 | } break; |
| 1176 | } |
| 1177 | |
| 1178 | if (i == begin) { |
| 1179 | new_line = text_editor->get_line(i).left(begin_col) + new_line; |
| 1180 | } |
| 1181 | if (i == end) { |
| 1182 | new_line = new_line + text_editor->get_line(i).substr(end_col); |
| 1183 | } |
| 1184 | text_editor->set_line(i, new_line); |
| 1185 | } |
| 1186 | } |
| 1187 | text_editor->end_complex_operation(); |
| 1188 | } |
| 1189 | |
| 1190 | void CodeTextEditor::move_lines_up() { |
| 1191 | text_editor->begin_complex_operation(); |
| 1192 | |
| 1193 | Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order(); |
| 1194 | |
| 1195 | // Lists of carets representing each group. |
| 1196 | Vector<Vector<int>> caret_groups; |
| 1197 | Vector<Pair<int, int>> group_borders; |
| 1198 | |
| 1199 | // Search for groups of carets and their selections residing on the same lines. |
| 1200 | for (int i = 0; i < caret_edit_order.size(); i++) { |
| 1201 | int c = caret_edit_order[i]; |
| 1202 | |
| 1203 | Vector<int> new_group{ c }; |
| 1204 | Pair<int, int> group_border; |
| 1205 | group_border.first = _get_affected_lines_from(c); |
| 1206 | group_border.second = _get_affected_lines_to(c); |
| 1207 | |
| 1208 | for (int j = i; j < caret_edit_order.size() - 1; j++) { |
| 1209 | int c_current = caret_edit_order[j]; |
| 1210 | int c_next = caret_edit_order[j + 1]; |
| 1211 | |
| 1212 | int next_start_pos = _get_affected_lines_from(c_next); |
| 1213 | int next_end_pos = _get_affected_lines_to(c_next); |
| 1214 | |
| 1215 | int current_start_pos = text_editor->has_selection(c_current) ? text_editor->get_selection_from_line(c_current) : text_editor->get_caret_line(c_current); |
| 1216 | |
| 1217 | i = j; |
| 1218 | if (next_end_pos != current_start_pos && next_end_pos + 1 != current_start_pos) { |
| 1219 | break; |
| 1220 | } |
| 1221 | group_border.first = next_start_pos; |
| 1222 | new_group.push_back(c_next); |
| 1223 | // If the last caret is added to the current group there is no need to process it again. |
| 1224 | if (j + 1 == caret_edit_order.size() - 1) { |
| 1225 | i++; |
| 1226 | } |
| 1227 | } |
| 1228 | group_borders.push_back(group_border); |
| 1229 | caret_groups.push_back(new_group); |
| 1230 | } |
| 1231 | |
| 1232 | for (int i = group_borders.size() - 1; i >= 0; i--) { |
| 1233 | if (group_borders[i].first - 1 < 0) { |
| 1234 | continue; |
| 1235 | } |
| 1236 | |
| 1237 | // If the group starts overlapping with the upper group don't move it. |
| 1238 | if (i < group_borders.size() - 1 && group_borders[i].first - 1 <= group_borders[i + 1].second) { |
| 1239 | continue; |
| 1240 | } |
| 1241 | |
| 1242 | // We have to remember caret positions and selections prior to line swapping. |
| 1243 | Vector<Vector<int>> caret_group_parameters; |
| 1244 | |
| 1245 | for (int j = 0; j < caret_groups[i].size(); j++) { |
| 1246 | int c = caret_groups[i][j]; |
| 1247 | int cursor_line = text_editor->get_caret_line(c); |
| 1248 | int cursor_column = text_editor->get_caret_column(c); |
| 1249 | |
| 1250 | if (!text_editor->has_selection(c)) { |
| 1251 | caret_group_parameters.push_back(Vector<int>{ -1, -1, -1, -1, cursor_line, cursor_column }); |
| 1252 | continue; |
| 1253 | } |
| 1254 | int from_line = text_editor->get_selection_from_line(c); |
| 1255 | int from_col = text_editor->get_selection_from_column(c); |
| 1256 | int to_line = text_editor->get_selection_to_line(c); |
| 1257 | int to_column = text_editor->get_selection_to_column(c); |
| 1258 | caret_group_parameters.push_back(Vector<int>{ from_line, from_col, to_line, to_column, cursor_line, cursor_column }); |
| 1259 | } |
| 1260 | |
| 1261 | for (int line_id = group_borders[i].first; line_id <= group_borders[i].second; line_id++) { |
| 1262 | text_editor->unfold_line(line_id); |
| 1263 | text_editor->unfold_line(line_id - 1); |
| 1264 | |
| 1265 | text_editor->swap_lines(line_id - 1, line_id); |
| 1266 | } |
| 1267 | |
| 1268 | for (int j = 0; j < caret_groups[i].size(); j++) { |
| 1269 | int c = caret_groups[i][j]; |
| 1270 | Vector<int> caret_parameters = caret_group_parameters[j]; |
| 1271 | text_editor->set_caret_line(caret_parameters[4] - 1, c == 0, true, 0, c); |
| 1272 | text_editor->set_caret_column(caret_parameters[5], c == 0, c); |
| 1273 | |
| 1274 | if (caret_parameters[0] >= 0) { |
| 1275 | text_editor->select(caret_parameters[0] - 1, caret_parameters[1], caret_parameters[2] - 1, caret_parameters[3], c); |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | text_editor->end_complex_operation(); |
| 1281 | text_editor->merge_overlapping_carets(); |
| 1282 | text_editor->queue_redraw(); |
| 1283 | } |
| 1284 | |
| 1285 | void CodeTextEditor::move_lines_down() { |
| 1286 | text_editor->begin_complex_operation(); |
| 1287 | |
| 1288 | Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order(); |
| 1289 | |
| 1290 | // Lists of carets representing each group. |
| 1291 | Vector<Vector<int>> caret_groups; |
| 1292 | Vector<Pair<int, int>> group_borders; |
| 1293 | Vector<int> group_border_ends; |
| 1294 | // Search for groups of carets and their selections residing on the same lines. |
| 1295 | for (int i = 0; i < caret_edit_order.size(); i++) { |
| 1296 | int c = caret_edit_order[i]; |
| 1297 | |
| 1298 | Vector<int> new_group{ c }; |
| 1299 | Pair<int, int> group_border; |
| 1300 | group_border.first = _get_affected_lines_from(c); |
| 1301 | group_border.second = _get_affected_lines_to(c); |
| 1302 | |
| 1303 | for (int j = i; j < caret_edit_order.size() - 1; j++) { |
| 1304 | int c_current = caret_edit_order[j]; |
| 1305 | int c_next = caret_edit_order[j + 1]; |
| 1306 | |
| 1307 | int next_start_pos = _get_affected_lines_from(c_next); |
| 1308 | int next_end_pos = _get_affected_lines_to(c_next); |
| 1309 | |
| 1310 | int current_start_pos = text_editor->has_selection(c_current) ? text_editor->get_selection_from_line(c_current) : text_editor->get_caret_line(c_current); |
| 1311 | |
| 1312 | i = j; |
| 1313 | if (next_end_pos == current_start_pos || next_end_pos + 1 == current_start_pos) { |
| 1314 | group_border.first = next_start_pos; |
| 1315 | new_group.push_back(c_next); |
| 1316 | // If the last caret is added to the current group there is no need to process it again. |
| 1317 | if (j + 1 == caret_edit_order.size() - 1) { |
| 1318 | i++; |
| 1319 | } |
| 1320 | } else { |
| 1321 | break; |
| 1322 | } |
| 1323 | } |
| 1324 | group_borders.push_back(group_border); |
| 1325 | group_border_ends.push_back(text_editor->has_selection(c) ? text_editor->get_selection_to_line(c) : text_editor->get_caret_line(c)); |
| 1326 | caret_groups.push_back(new_group); |
| 1327 | } |
| 1328 | |
| 1329 | for (int i = 0; i < group_borders.size(); i++) { |
| 1330 | if (group_border_ends[i] + 1 > text_editor->get_line_count() - 1) { |
| 1331 | continue; |
| 1332 | } |
| 1333 | |
| 1334 | // If the group starts overlapping with the upper group don't move it. |
| 1335 | if (i > 0 && group_border_ends[i] + 1 >= group_borders[i - 1].first) { |
| 1336 | continue; |
| 1337 | } |
| 1338 | |
| 1339 | // We have to remember caret positions and selections prior to line swapping. |
| 1340 | Vector<Vector<int>> caret_group_parameters; |
| 1341 | |
| 1342 | for (int j = 0; j < caret_groups[i].size(); j++) { |
| 1343 | int c = caret_groups[i][j]; |
| 1344 | int cursor_line = text_editor->get_caret_line(c); |
| 1345 | int cursor_column = text_editor->get_caret_column(c); |
| 1346 | |
| 1347 | if (!text_editor->has_selection(c)) { |
| 1348 | caret_group_parameters.push_back(Vector<int>{ -1, -1, -1, -1, cursor_line, cursor_column }); |
| 1349 | continue; |
| 1350 | } |
| 1351 | int from_line = text_editor->get_selection_from_line(c); |
| 1352 | int from_col = text_editor->get_selection_from_column(c); |
| 1353 | int to_line = text_editor->get_selection_to_line(c); |
| 1354 | int to_column = text_editor->get_selection_to_column(c); |
| 1355 | caret_group_parameters.push_back(Vector<int>{ from_line, from_col, to_line, to_column, cursor_line, cursor_column }); |
| 1356 | } |
| 1357 | |
| 1358 | for (int line_id = group_borders[i].second; line_id >= group_borders[i].first; line_id--) { |
| 1359 | text_editor->unfold_line(line_id); |
| 1360 | text_editor->unfold_line(line_id + 1); |
| 1361 | |
| 1362 | text_editor->swap_lines(line_id + 1, line_id); |
| 1363 | } |
| 1364 | |
| 1365 | for (int j = 0; j < caret_groups[i].size(); j++) { |
| 1366 | int c = caret_groups[i][j]; |
| 1367 | Vector<int> caret_parameters = caret_group_parameters[j]; |
| 1368 | text_editor->set_caret_line(caret_parameters[4] + 1, c == 0, true, 0, c); |
| 1369 | text_editor->set_caret_column(caret_parameters[5], c == 0, c); |
| 1370 | |
| 1371 | if (caret_parameters[0] >= 0) { |
| 1372 | text_editor->select(caret_parameters[0] + 1, caret_parameters[1], caret_parameters[2] + 1, caret_parameters[3], c); |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | text_editor->merge_overlapping_carets(); |
| 1378 | text_editor->end_complex_operation(); |
| 1379 | text_editor->queue_redraw(); |
| 1380 | } |
| 1381 | |
| 1382 | void CodeTextEditor::delete_lines() { |
| 1383 | text_editor->begin_complex_operation(); |
| 1384 | |
| 1385 | Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order(); |
| 1386 | Vector<int> lines; |
| 1387 | int last_line = INT_MAX; |
| 1388 | for (const int &c : caret_edit_order) { |
| 1389 | for (int line = _get_affected_lines_to(c); line >= _get_affected_lines_from(c); line--) { |
| 1390 | if (line >= last_line) { |
| 1391 | continue; |
| 1392 | } |
| 1393 | last_line = line; |
| 1394 | lines.append(line); |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | for (const int &line : lines) { |
| 1399 | if (line != text_editor->get_line_count() - 1) { |
| 1400 | text_editor->remove_text(line, 0, line + 1, 0); |
| 1401 | } else { |
| 1402 | text_editor->remove_text(line - 1, text_editor->get_line(line - 1).length(), line, text_editor->get_line(line).length()); |
| 1403 | } |
| 1404 | // Readjust carets. |
| 1405 | int new_line = MIN(line, text_editor->get_line_count() - 1); |
| 1406 | text_editor->unfold_line(new_line); |
| 1407 | for (const int &c : caret_edit_order) { |
| 1408 | if (text_editor->get_caret_line(c) == line || (text_editor->get_caret_line(c) == line + 1 && text_editor->get_caret_column(c) == 0)) { |
| 1409 | text_editor->deselect(c); |
| 1410 | text_editor->set_caret_line(new_line, c == 0, true, 0, c); |
| 1411 | continue; |
| 1412 | } |
| 1413 | if (text_editor->get_caret_line(c) > line) { |
| 1414 | text_editor->set_caret_line(text_editor->get_caret_line(c) - 1, c == 0, true, 0, c); |
| 1415 | continue; |
| 1416 | } |
| 1417 | break; |
| 1418 | } |
| 1419 | } |
| 1420 | text_editor->merge_overlapping_carets(); |
| 1421 | text_editor->end_complex_operation(); |
| 1422 | } |
| 1423 | |
| 1424 | void CodeTextEditor::duplicate_selection() { |
| 1425 | text_editor->begin_complex_operation(); |
| 1426 | |
| 1427 | Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order(); |
| 1428 | for (const int &c : caret_edit_order) { |
| 1429 | const int cursor_column = text_editor->get_caret_column(c); |
| 1430 | int from_line = text_editor->get_caret_line(c); |
| 1431 | int to_line = text_editor->get_caret_line(c); |
| 1432 | int from_column = 0; |
| 1433 | int to_column = 0; |
| 1434 | int cursor_new_line = to_line + 1; |
| 1435 | int cursor_new_column = text_editor->get_caret_column(c); |
| 1436 | String new_text = "\n" + text_editor->get_line(from_line); |
| 1437 | bool selection_active = false; |
| 1438 | |
| 1439 | text_editor->set_caret_column(text_editor->get_line(from_line).length(), c == 0, c); |
| 1440 | if (text_editor->has_selection(c)) { |
| 1441 | from_column = text_editor->get_selection_from_column(c); |
| 1442 | to_column = text_editor->get_selection_to_column(c); |
| 1443 | |
| 1444 | from_line = text_editor->get_selection_from_line(c); |
| 1445 | to_line = text_editor->get_selection_to_line(c); |
| 1446 | cursor_new_line = to_line + text_editor->get_caret_line(c) - from_line; |
| 1447 | cursor_new_column = to_column == cursor_column ? 2 * to_column - from_column : to_column; |
| 1448 | new_text = text_editor->get_selected_text(c); |
| 1449 | selection_active = true; |
| 1450 | |
| 1451 | text_editor->set_caret_line(to_line, c == 0, true, 0, c); |
| 1452 | text_editor->set_caret_column(to_column, c == 0, c); |
| 1453 | } |
| 1454 | |
| 1455 | for (int i = from_line; i <= to_line; i++) { |
| 1456 | text_editor->unfold_line(i); |
| 1457 | } |
| 1458 | text_editor->deselect(c); |
| 1459 | text_editor->insert_text_at_caret(new_text, c); |
| 1460 | text_editor->set_caret_line(cursor_new_line, c == 0, true, 0, c); |
| 1461 | text_editor->set_caret_column(cursor_new_column, c == 0, c); |
| 1462 | if (selection_active) { |
| 1463 | text_editor->select(to_line, to_column, 2 * to_line - from_line, to_line == from_line ? 2 * to_column - from_column : to_column, c); |
| 1464 | } |
| 1465 | } |
| 1466 | text_editor->merge_overlapping_carets(); |
| 1467 | text_editor->end_complex_operation(); |
| 1468 | text_editor->queue_redraw(); |
| 1469 | } |
| 1470 | |
| 1471 | void CodeTextEditor::(const String &delimiter) { |
| 1472 | text_editor->begin_complex_operation(); |
| 1473 | |
| 1474 | Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order(); |
| 1475 | caret_edit_order.reverse(); |
| 1476 | int last_line = -1; |
| 1477 | int folded_to = 0; |
| 1478 | for (const int &c1 : caret_edit_order) { |
| 1479 | int from = _get_affected_lines_from(c1); |
| 1480 | from += from == last_line ? 1 + folded_to : 0; |
| 1481 | int to = _get_affected_lines_to(c1); |
| 1482 | last_line = to; |
| 1483 | // If last line is folded, extends to the end of the folded section |
| 1484 | if (text_editor->is_line_folded(to)) { |
| 1485 | folded_to = text_editor->get_next_visible_line_offset_from(to + 1, 1) - 1; |
| 1486 | to += folded_to; |
| 1487 | } |
| 1488 | // Check first if there's any uncommented lines in selection. |
| 1489 | bool = true; |
| 1490 | for (int line = from; line <= to; line++) { |
| 1491 | // `+ delimiter.length()` here because comment delimiter is not actually `in comment` so we check first character after it |
| 1492 | int delimiter_idx = text_editor->is_in_comment(line, text_editor->get_first_non_whitespace_column(line) + delimiter.length()); |
| 1493 | if (delimiter_idx == -1 || text_editor->get_delimiter_start_key(delimiter_idx) != delimiter) { |
| 1494 | is_commented = false; |
| 1495 | break; |
| 1496 | } |
| 1497 | } |
| 1498 | // Caret positions need to be saved since they could be moved at the eol. |
| 1499 | Vector<int> caret_cols; |
| 1500 | Vector<int> selection_to_cols; |
| 1501 | for (const int &c2 : caret_edit_order) { |
| 1502 | if (text_editor->get_caret_line(c2) >= from && text_editor->get_caret_line(c2) <= to) { |
| 1503 | caret_cols.append(text_editor->get_caret_column(c2)); |
| 1504 | } |
| 1505 | if (text_editor->has_selection(c2) && text_editor->get_selection_to_line(c2) >= from && text_editor->get_selection_to_line(c2) <= to) { |
| 1506 | selection_to_cols.append(text_editor->get_selection_to_column(c2)); |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | // Comment/uncomment. |
| 1511 | for (int line = from; line <= to; line++) { |
| 1512 | String line_text = text_editor->get_line(line); |
| 1513 | if (line_text.strip_edges().is_empty()) { |
| 1514 | text_editor->set_line(line, delimiter); |
| 1515 | continue; |
| 1516 | } |
| 1517 | if (is_commented) { |
| 1518 | text_editor->set_line(line, line_text.replace_first(delimiter, "" )); |
| 1519 | } else { |
| 1520 | text_editor->set_line(line, line_text.insert(text_editor->get_first_non_whitespace_column(line), delimiter)); |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | // Readjust carets and selections. |
| 1525 | int caret_i = 0; |
| 1526 | int selection_i = 0; |
| 1527 | int offset = (is_commented ? -1 : 1) * delimiter.length(); |
| 1528 | for (const int &c2 : caret_edit_order) { |
| 1529 | bool is_line_selection = text_editor->has_selection(c2) && text_editor->get_selection_from_line(c2) < text_editor->get_selection_to_line(c2); |
| 1530 | if (text_editor->get_caret_line(c2) >= from && text_editor->get_caret_line(c2) <= to) { |
| 1531 | int caret_col = caret_cols[caret_i++]; |
| 1532 | caret_col += (is_line_selection && caret_col == 0) ? 0 : offset; |
| 1533 | text_editor->set_caret_column(caret_col, c2 == 0, c2); |
| 1534 | } |
| 1535 | if (text_editor->has_selection(c2) && text_editor->get_selection_to_line(c2) >= from && text_editor->get_selection_to_line(c2) <= to) { |
| 1536 | int from_col = text_editor->get_selection_from_column(c2); |
| 1537 | from_col += (is_line_selection && from_col == 0) ? 0 : offset; |
| 1538 | int to_col = selection_to_cols[selection_i++]; |
| 1539 | to_col += (to_col == 0) ? 0 : offset; |
| 1540 | text_editor->select( |
| 1541 | text_editor->get_selection_from_line(c2), from_col, |
| 1542 | text_editor->get_selection_to_line(c2), to_col, c2); |
| 1543 | } |
| 1544 | } |
| 1545 | } |
| 1546 | text_editor->merge_overlapping_carets(); |
| 1547 | text_editor->end_complex_operation(); |
| 1548 | text_editor->queue_redraw(); |
| 1549 | } |
| 1550 | |
| 1551 | void CodeTextEditor::goto_line(int p_line) { |
| 1552 | text_editor->remove_secondary_carets(); |
| 1553 | text_editor->deselect(); |
| 1554 | text_editor->unfold_line(p_line); |
| 1555 | text_editor->call_deferred(SNAME("set_caret_line" ), p_line); |
| 1556 | } |
| 1557 | |
| 1558 | void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) { |
| 1559 | text_editor->remove_secondary_carets(); |
| 1560 | text_editor->unfold_line(p_line); |
| 1561 | text_editor->call_deferred(SNAME("set_caret_line" ), p_line); |
| 1562 | text_editor->call_deferred(SNAME("set_caret_column" ), p_begin); |
| 1563 | text_editor->select(p_line, p_begin, p_line, p_end); |
| 1564 | } |
| 1565 | |
| 1566 | void CodeTextEditor::goto_line_centered(int p_line) { |
| 1567 | goto_line(p_line); |
| 1568 | text_editor->call_deferred(SNAME("center_viewport_to_caret" )); |
| 1569 | } |
| 1570 | |
| 1571 | void CodeTextEditor::set_executing_line(int p_line) { |
| 1572 | text_editor->set_line_as_executing(p_line, true); |
| 1573 | } |
| 1574 | |
| 1575 | void CodeTextEditor::clear_executing_line() { |
| 1576 | text_editor->clear_executing_lines(); |
| 1577 | } |
| 1578 | |
| 1579 | Variant CodeTextEditor::get_edit_state() { |
| 1580 | Dictionary state; |
| 1581 | state.merge(get_navigation_state()); |
| 1582 | |
| 1583 | state["folded_lines" ] = text_editor->get_folded_lines(); |
| 1584 | state["breakpoints" ] = text_editor->get_breakpointed_lines(); |
| 1585 | state["bookmarks" ] = text_editor->get_bookmarked_lines(); |
| 1586 | |
| 1587 | Ref<EditorSyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighter(); |
| 1588 | state["syntax_highlighter" ] = syntax_highlighter->_get_name(); |
| 1589 | |
| 1590 | return state; |
| 1591 | } |
| 1592 | |
| 1593 | void CodeTextEditor::set_edit_state(const Variant &p_state) { |
| 1594 | Dictionary state = p_state; |
| 1595 | |
| 1596 | /* update the row first as it sets the column to 0 */ |
| 1597 | text_editor->set_caret_line(state["row" ]); |
| 1598 | text_editor->set_caret_column(state["column" ]); |
| 1599 | text_editor->set_v_scroll(state["scroll_position" ]); |
| 1600 | text_editor->set_h_scroll(state["h_scroll_position" ]); |
| 1601 | |
| 1602 | if (state.get("selection" , false)) { |
| 1603 | text_editor->select(state["selection_from_line" ], state["selection_from_column" ], state["selection_to_line" ], state["selection_to_column" ]); |
| 1604 | } else { |
| 1605 | text_editor->deselect(); |
| 1606 | } |
| 1607 | |
| 1608 | if (state.has("folded_lines" )) { |
| 1609 | Vector<int> folded_lines = state["folded_lines" ]; |
| 1610 | for (int i = 0; i < folded_lines.size(); i++) { |
| 1611 | text_editor->fold_line(folded_lines[i]); |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | if (state.has("breakpoints" )) { |
| 1616 | Array breakpoints = state["breakpoints" ]; |
| 1617 | for (int i = 0; i < breakpoints.size(); i++) { |
| 1618 | text_editor->set_line_as_breakpoint(breakpoints[i], true); |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | if (state.has("bookmarks" )) { |
| 1623 | Array bookmarks = state["bookmarks" ]; |
| 1624 | for (int i = 0; i < bookmarks.size(); i++) { |
| 1625 | text_editor->set_line_as_bookmarked(bookmarks[i], true); |
| 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | Variant CodeTextEditor::get_navigation_state() { |
| 1631 | Dictionary state; |
| 1632 | |
| 1633 | state["scroll_position" ] = text_editor->get_v_scroll(); |
| 1634 | state["h_scroll_position" ] = text_editor->get_h_scroll(); |
| 1635 | state["column" ] = text_editor->get_caret_column(); |
| 1636 | state["row" ] = text_editor->get_caret_line(); |
| 1637 | |
| 1638 | state["selection" ] = get_text_editor()->has_selection(); |
| 1639 | if (get_text_editor()->has_selection()) { |
| 1640 | state["selection_from_line" ] = text_editor->get_selection_from_line(); |
| 1641 | state["selection_from_column" ] = text_editor->get_selection_from_column(); |
| 1642 | state["selection_to_line" ] = text_editor->get_selection_to_line(); |
| 1643 | state["selection_to_column" ] = text_editor->get_selection_to_column(); |
| 1644 | } |
| 1645 | |
| 1646 | return state; |
| 1647 | } |
| 1648 | |
| 1649 | void CodeTextEditor::set_error(const String &p_error) { |
| 1650 | error->set_text(p_error); |
| 1651 | if (!p_error.is_empty()) { |
| 1652 | error->set_default_cursor_shape(CURSOR_POINTING_HAND); |
| 1653 | } else { |
| 1654 | error->set_default_cursor_shape(CURSOR_ARROW); |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | void CodeTextEditor::set_error_pos(int p_line, int p_column) { |
| 1659 | error_line = p_line; |
| 1660 | error_column = p_column; |
| 1661 | } |
| 1662 | |
| 1663 | Point2i CodeTextEditor::get_error_pos() const { |
| 1664 | return Point2i(error_line, error_column); |
| 1665 | } |
| 1666 | |
| 1667 | void CodeTextEditor::goto_error() { |
| 1668 | if (!error->get_text().is_empty()) { |
| 1669 | if (text_editor->get_line_count() != error_line) { |
| 1670 | text_editor->unfold_line(error_line); |
| 1671 | } |
| 1672 | text_editor->remove_secondary_carets(); |
| 1673 | text_editor->set_caret_line(error_line); |
| 1674 | text_editor->set_caret_column(error_column); |
| 1675 | text_editor->center_viewport_to_caret(); |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | void CodeTextEditor::_update_text_editor_theme() { |
| 1680 | emit_signal(SNAME("load_theme_settings" )); |
| 1681 | |
| 1682 | error->begin_bulk_theme_override(); |
| 1683 | error->add_theme_font_override(SNAME("font" ), get_theme_font(SNAME("status_source" ), EditorStringName(EditorFonts))); |
| 1684 | error->add_theme_font_size_override(SNAME("font_size" ), get_theme_font_size(SNAME("status_source_size" ), EditorStringName(EditorFonts))); |
| 1685 | error->add_theme_color_override(SNAME("font_color" ), get_theme_color(SNAME("error_color" ), EditorStringName(Editor))); |
| 1686 | |
| 1687 | Ref<Font> status_bar_font = get_theme_font(SNAME("status_source" ), EditorStringName(EditorFonts)); |
| 1688 | int status_bar_font_size = get_theme_font_size(SNAME("status_source_size" ), EditorStringName(EditorFonts)); |
| 1689 | error->add_theme_font_override("font" , status_bar_font); |
| 1690 | error->add_theme_font_size_override("font_size" , status_bar_font_size); |
| 1691 | int count = status_bar->get_child_count(); |
| 1692 | for (int i = 0; i < count; i++) { |
| 1693 | Control *n = Object::cast_to<Control>(status_bar->get_child(i)); |
| 1694 | if (n) { |
| 1695 | n->add_theme_font_override("font" , status_bar_font); |
| 1696 | n->add_theme_font_size_override("font_size" , status_bar_font_size); |
| 1697 | } |
| 1698 | } |
| 1699 | error->end_bulk_theme_override(); |
| 1700 | } |
| 1701 | |
| 1702 | void CodeTextEditor::_on_settings_change() { |
| 1703 | _apply_settings_change(); |
| 1704 | } |
| 1705 | |
| 1706 | void CodeTextEditor::_apply_settings_change() { |
| 1707 | _update_text_editor_theme(); |
| 1708 | |
| 1709 | font_size = EDITOR_GET("interface/editor/code_font_size" ); |
| 1710 | int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures" ); |
| 1711 | |
| 1712 | Ref<FontVariation> fc = text_editor->get_theme_font(SNAME("font" )); |
| 1713 | if (fc.is_valid()) { |
| 1714 | switch (ot_mode) { |
| 1715 | case 1: { // Disable ligatures. |
| 1716 | Dictionary ftrs; |
| 1717 | ftrs[TS->name_to_tag("calt" )] = 0; |
| 1718 | fc->set_opentype_features(ftrs); |
| 1719 | } break; |
| 1720 | case 2: { // Custom. |
| 1721 | Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features" )).split("," ); |
| 1722 | Dictionary ftrs; |
| 1723 | for (int i = 0; i < subtag.size(); i++) { |
| 1724 | Vector<String> subtag_a = subtag[i].split("=" ); |
| 1725 | if (subtag_a.size() == 2) { |
| 1726 | ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int(); |
| 1727 | } else if (subtag_a.size() == 1) { |
| 1728 | ftrs[TS->name_to_tag(subtag_a[0])] = 1; |
| 1729 | } |
| 1730 | } |
| 1731 | fc->set_opentype_features(ftrs); |
| 1732 | } break; |
| 1733 | default: { // Enabled. |
| 1734 | Dictionary ftrs; |
| 1735 | ftrs[TS->name_to_tag("calt" )] = 1; |
| 1736 | fc->set_opentype_features(ftrs); |
| 1737 | } break; |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | text_editor->set_code_hint_draw_below(EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line" )); |
| 1742 | |
| 1743 | code_complete_enabled = EDITOR_GET("text_editor/completion/code_complete_enabled" ); |
| 1744 | code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay" )); |
| 1745 | idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay" )); |
| 1746 | } |
| 1747 | |
| 1748 | void CodeTextEditor::_text_changed_idle_timeout() { |
| 1749 | _validate_script(); |
| 1750 | emit_signal(SNAME("validate_script" )); |
| 1751 | } |
| 1752 | |
| 1753 | void CodeTextEditor::validate_script() { |
| 1754 | idle->start(); |
| 1755 | } |
| 1756 | |
| 1757 | void CodeTextEditor::_error_button_pressed() { |
| 1758 | _set_show_errors_panel(!is_errors_panel_opened); |
| 1759 | _set_show_warnings_panel(false); |
| 1760 | } |
| 1761 | |
| 1762 | void CodeTextEditor::_warning_button_pressed() { |
| 1763 | _set_show_warnings_panel(!is_warnings_panel_opened); |
| 1764 | _set_show_errors_panel(false); |
| 1765 | } |
| 1766 | |
| 1767 | void CodeTextEditor::_set_show_errors_panel(bool p_show) { |
| 1768 | is_errors_panel_opened = p_show; |
| 1769 | emit_signal(SNAME("show_errors_panel" ), p_show); |
| 1770 | } |
| 1771 | |
| 1772 | void CodeTextEditor::_set_show_warnings_panel(bool p_show) { |
| 1773 | is_warnings_panel_opened = p_show; |
| 1774 | emit_signal(SNAME("show_warnings_panel" ), p_show); |
| 1775 | } |
| 1776 | |
| 1777 | void CodeTextEditor::_toggle_scripts_pressed() { |
| 1778 | ScriptEditor::get_singleton()->toggle_scripts_panel(); |
| 1779 | update_toggle_scripts_button(); |
| 1780 | } |
| 1781 | |
| 1782 | int CodeTextEditor::_get_affected_lines_from(int p_caret) { |
| 1783 | return text_editor->has_selection(p_caret) ? text_editor->get_selection_from_line(p_caret) : text_editor->get_caret_line(p_caret); |
| 1784 | } |
| 1785 | |
| 1786 | int CodeTextEditor::_get_affected_lines_to(int p_caret) { |
| 1787 | if (!text_editor->has_selection(p_caret)) { |
| 1788 | return text_editor->get_caret_line(p_caret); |
| 1789 | } |
| 1790 | int line = text_editor->get_selection_to_line(p_caret); |
| 1791 | // Don't affect a line with no selected characters. |
| 1792 | if (text_editor->get_selection_to_column(p_caret) == 0) { |
| 1793 | line--; |
| 1794 | } |
| 1795 | return line; |
| 1796 | } |
| 1797 | |
| 1798 | void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) { |
| 1799 | Ref<InputEventMouseButton> mb = p_event; |
| 1800 | if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) { |
| 1801 | goto_error(); |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | void CodeTextEditor::_update_status_bar_theme() { |
| 1806 | error_button->set_icon(get_editor_theme_icon(SNAME("StatusError" ))); |
| 1807 | error_button->add_theme_color_override("font_color" , get_theme_color(SNAME("error_color" ), EditorStringName(Editor))); |
| 1808 | error_button->add_theme_font_override("font" , get_theme_font(SNAME("status_source" ), EditorStringName(EditorFonts))); |
| 1809 | error_button->add_theme_font_size_override("font_size" , get_theme_font_size(SNAME("status_source_size" ), EditorStringName(EditorFonts))); |
| 1810 | |
| 1811 | warning_button->set_icon(get_editor_theme_icon(SNAME("NodeWarning" ))); |
| 1812 | warning_button->add_theme_color_override("font_color" , get_theme_color(SNAME("warning_color" ), EditorStringName(Editor))); |
| 1813 | warning_button->add_theme_font_override("font" , get_theme_font(SNAME("status_source" ), EditorStringName(EditorFonts))); |
| 1814 | warning_button->add_theme_font_size_override("font_size" , get_theme_font_size(SNAME("status_source_size" ), EditorStringName(EditorFonts))); |
| 1815 | |
| 1816 | line_and_col_txt->add_theme_font_override("font" , get_theme_font(SNAME("status_source" ), EditorStringName(EditorFonts))); |
| 1817 | line_and_col_txt->add_theme_font_size_override("font_size" , get_theme_font_size(SNAME("status_source_size" ), EditorStringName(EditorFonts))); |
| 1818 | } |
| 1819 | |
| 1820 | void CodeTextEditor::_notification(int p_what) { |
| 1821 | switch (p_what) { |
| 1822 | case NOTIFICATION_ENTER_TREE: { |
| 1823 | _update_status_bar_theme(); |
| 1824 | } break; |
| 1825 | |
| 1826 | case NOTIFICATION_THEME_CHANGED: { |
| 1827 | _update_status_bar_theme(); |
| 1828 | if (toggle_scripts_button->is_visible()) { |
| 1829 | update_toggle_scripts_button(); |
| 1830 | } |
| 1831 | _update_text_editor_theme(); |
| 1832 | } break; |
| 1833 | |
| 1834 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 1835 | if (toggle_scripts_button->is_visible()) { |
| 1836 | update_toggle_scripts_button(); |
| 1837 | } |
| 1838 | set_process_input(is_visible_in_tree()); |
| 1839 | } break; |
| 1840 | |
| 1841 | case NOTIFICATION_PREDELETE: { |
| 1842 | if (find_replace_bar) { |
| 1843 | find_replace_bar->set_text_edit(nullptr); |
| 1844 | } |
| 1845 | } break; |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | void CodeTextEditor::set_error_count(int p_error_count) { |
| 1850 | error_button->set_text(itos(p_error_count)); |
| 1851 | error_button->set_visible(p_error_count > 0); |
| 1852 | if (!p_error_count) { |
| 1853 | _set_show_errors_panel(false); |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | void CodeTextEditor::set_warning_count(int p_warning_count) { |
| 1858 | warning_button->set_text(itos(p_warning_count)); |
| 1859 | warning_button->set_visible(p_warning_count > 0); |
| 1860 | if (!p_warning_count) { |
| 1861 | _set_show_warnings_panel(false); |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | void CodeTextEditor::toggle_bookmark() { |
| 1866 | Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order(); |
| 1867 | caret_edit_order.reverse(); |
| 1868 | int last_line = -1; |
| 1869 | for (const int &c : caret_edit_order) { |
| 1870 | int from = text_editor->has_selection(c) ? text_editor->get_selection_from_line(c) : text_editor->get_caret_line(c); |
| 1871 | from += from == last_line ? 1 : 0; |
| 1872 | int to = text_editor->has_selection(c) ? text_editor->get_selection_to_line(c) : text_editor->get_caret_line(c); |
| 1873 | if (to < from) { |
| 1874 | continue; |
| 1875 | } |
| 1876 | // Check first if there's any bookmarked lines in the selection. |
| 1877 | bool selection_has_bookmarks = false; |
| 1878 | for (int line = from; line <= to; line++) { |
| 1879 | if (text_editor->is_line_bookmarked(line)) { |
| 1880 | selection_has_bookmarks = true; |
| 1881 | break; |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | // Set bookmark on caret or remove all bookmarks from the selection. |
| 1886 | if (!selection_has_bookmarks) { |
| 1887 | if (text_editor->get_caret_line(c) != last_line) { |
| 1888 | text_editor->set_line_as_bookmarked(text_editor->get_caret_line(c), true); |
| 1889 | } |
| 1890 | } else { |
| 1891 | for (int line = from; line <= to; line++) { |
| 1892 | text_editor->set_line_as_bookmarked(line, false); |
| 1893 | } |
| 1894 | } |
| 1895 | last_line = to; |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | void CodeTextEditor::goto_next_bookmark() { |
| 1900 | PackedInt32Array bmarks = text_editor->get_bookmarked_lines(); |
| 1901 | if (bmarks.size() <= 0) { |
| 1902 | return; |
| 1903 | } |
| 1904 | |
| 1905 | int current_line = text_editor->get_caret_line(); |
| 1906 | int bmark_idx = 0; |
| 1907 | if (current_line < (int)bmarks[bmarks.size() - 1]) { |
| 1908 | while (bmark_idx < bmarks.size() && bmarks[bmark_idx] <= current_line) { |
| 1909 | bmark_idx++; |
| 1910 | } |
| 1911 | } |
| 1912 | goto_line_centered(bmarks[bmark_idx]); |
| 1913 | } |
| 1914 | |
| 1915 | void CodeTextEditor::goto_prev_bookmark() { |
| 1916 | PackedInt32Array bmarks = text_editor->get_bookmarked_lines(); |
| 1917 | if (bmarks.size() <= 0) { |
| 1918 | return; |
| 1919 | } |
| 1920 | |
| 1921 | int current_line = text_editor->get_caret_line(); |
| 1922 | int bmark_idx = bmarks.size() - 1; |
| 1923 | if (current_line > (int)bmarks[0]) { |
| 1924 | while (bmark_idx >= 0 && bmarks[bmark_idx] >= current_line) { |
| 1925 | bmark_idx--; |
| 1926 | } |
| 1927 | } |
| 1928 | goto_line_centered(bmarks[bmark_idx]); |
| 1929 | } |
| 1930 | |
| 1931 | void CodeTextEditor::remove_all_bookmarks() { |
| 1932 | text_editor->clear_bookmarked_lines(); |
| 1933 | } |
| 1934 | |
| 1935 | void CodeTextEditor::_bind_methods() { |
| 1936 | ADD_SIGNAL(MethodInfo("validate_script" )); |
| 1937 | ADD_SIGNAL(MethodInfo("load_theme_settings" )); |
| 1938 | ADD_SIGNAL(MethodInfo("show_errors_panel" )); |
| 1939 | ADD_SIGNAL(MethodInfo("show_warnings_panel" )); |
| 1940 | } |
| 1941 | |
| 1942 | void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) { |
| 1943 | code_complete_func = p_code_complete_func; |
| 1944 | code_complete_ud = p_ud; |
| 1945 | } |
| 1946 | |
| 1947 | void CodeTextEditor::show_toggle_scripts_button() { |
| 1948 | toggle_scripts_button->show(); |
| 1949 | } |
| 1950 | |
| 1951 | void CodeTextEditor::update_toggle_scripts_button() { |
| 1952 | if (is_layout_rtl()) { |
| 1953 | toggle_scripts_button->set_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Forward" ) : SNAME("Back" ))); |
| 1954 | } else { |
| 1955 | toggle_scripts_button->set_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Back" ) : SNAME("Forward" ))); |
| 1956 | } |
| 1957 | toggle_scripts_button->set_tooltip_text(vformat("%s (%s)" , TTR("Toggle Scripts Panel" ), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel" )->get_as_text())); |
| 1958 | } |
| 1959 | |
| 1960 | CodeTextEditor::CodeTextEditor() { |
| 1961 | code_complete_func = nullptr; |
| 1962 | ED_SHORTCUT("script_editor/zoom_in" , TTR("Zoom In" ), KeyModifierMask::CMD_OR_CTRL | Key::EQUAL); |
| 1963 | ED_SHORTCUT("script_editor/zoom_out" , TTR("Zoom Out" ), KeyModifierMask::CMD_OR_CTRL | Key::MINUS); |
| 1964 | ED_SHORTCUT_ARRAY("script_editor/reset_zoom" , TTR("Reset Zoom" ), |
| 1965 | { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KEY_0), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_0) }); |
| 1966 | |
| 1967 | text_editor = memnew(CodeEdit); |
| 1968 | add_child(text_editor); |
| 1969 | text_editor->set_v_size_flags(SIZE_EXPAND_FILL); |
| 1970 | text_editor->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_GDSCRIPT); |
| 1971 | text_editor->set_draw_bookmarks_gutter(true); |
| 1972 | |
| 1973 | int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures" ); |
| 1974 | Ref<FontVariation> fc = text_editor->get_theme_font(SNAME("font" )); |
| 1975 | if (fc.is_valid()) { |
| 1976 | switch (ot_mode) { |
| 1977 | case 1: { // Disable ligatures. |
| 1978 | Dictionary ftrs; |
| 1979 | ftrs[TS->name_to_tag("calt" )] = 0; |
| 1980 | fc->set_opentype_features(ftrs); |
| 1981 | } break; |
| 1982 | case 2: { // Custom. |
| 1983 | Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features" )).split("," ); |
| 1984 | Dictionary ftrs; |
| 1985 | for (int i = 0; i < subtag.size(); i++) { |
| 1986 | Vector<String> subtag_a = subtag[i].split("=" ); |
| 1987 | if (subtag_a.size() == 2) { |
| 1988 | ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int(); |
| 1989 | } else if (subtag_a.size() == 1) { |
| 1990 | ftrs[TS->name_to_tag(subtag_a[0])] = 1; |
| 1991 | } |
| 1992 | } |
| 1993 | fc->set_opentype_features(ftrs); |
| 1994 | } break; |
| 1995 | default: { // Enabled. |
| 1996 | Dictionary ftrs; |
| 1997 | ftrs[TS->name_to_tag("calt" )] = 1; |
| 1998 | fc->set_opentype_features(ftrs); |
| 1999 | } break; |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | text_editor->set_draw_line_numbers(true); |
| 2004 | text_editor->set_highlight_matching_braces_enabled(true); |
| 2005 | text_editor->set_auto_indent_enabled(true); |
| 2006 | text_editor->set_deselect_on_focus_loss_enabled(false); |
| 2007 | |
| 2008 | status_bar = memnew(HBoxContainer); |
| 2009 | add_child(status_bar); |
| 2010 | status_bar->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2011 | status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the warning icon. |
| 2012 | |
| 2013 | idle = memnew(Timer); |
| 2014 | add_child(idle); |
| 2015 | idle->set_one_shot(true); |
| 2016 | idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay" )); |
| 2017 | |
| 2018 | code_complete_enabled = EDITOR_GET("text_editor/completion/code_complete_enabled" ); |
| 2019 | code_complete_timer = memnew(Timer); |
| 2020 | add_child(code_complete_timer); |
| 2021 | code_complete_timer->set_one_shot(true); |
| 2022 | code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay" )); |
| 2023 | |
| 2024 | error_line = 0; |
| 2025 | error_column = 0; |
| 2026 | |
| 2027 | toggle_scripts_button = memnew(Button); |
| 2028 | toggle_scripts_button->set_flat(true); |
| 2029 | toggle_scripts_button->connect("pressed" , callable_mp(this, &CodeTextEditor::_toggle_scripts_pressed)); |
| 2030 | status_bar->add_child(toggle_scripts_button); |
| 2031 | toggle_scripts_button->hide(); |
| 2032 | |
| 2033 | // Error |
| 2034 | ScrollContainer *scroll = memnew(ScrollContainer); |
| 2035 | scroll->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2036 | scroll->set_v_size_flags(SIZE_EXPAND_FILL); |
| 2037 | scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); |
| 2038 | status_bar->add_child(scroll); |
| 2039 | |
| 2040 | error = memnew(Label); |
| 2041 | scroll->add_child(error); |
| 2042 | error->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER); |
| 2043 | error->set_mouse_filter(MOUSE_FILTER_STOP); |
| 2044 | error->connect("gui_input" , callable_mp(this, &CodeTextEditor::_error_pressed)); |
| 2045 | |
| 2046 | // Errors |
| 2047 | error_button = memnew(Button); |
| 2048 | error_button->set_flat(true); |
| 2049 | status_bar->add_child(error_button); |
| 2050 | error_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER); |
| 2051 | error_button->set_default_cursor_shape(CURSOR_POINTING_HAND); |
| 2052 | error_button->connect("pressed" , callable_mp(this, &CodeTextEditor::_error_button_pressed)); |
| 2053 | error_button->set_tooltip_text(TTR("Errors" )); |
| 2054 | set_error_count(0); |
| 2055 | |
| 2056 | // Warnings |
| 2057 | warning_button = memnew(Button); |
| 2058 | warning_button->set_flat(true); |
| 2059 | status_bar->add_child(warning_button); |
| 2060 | warning_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER); |
| 2061 | warning_button->set_default_cursor_shape(CURSOR_POINTING_HAND); |
| 2062 | warning_button->connect("pressed" , callable_mp(this, &CodeTextEditor::_warning_button_pressed)); |
| 2063 | warning_button->set_tooltip_text(TTR("Warnings" )); |
| 2064 | set_warning_count(0); |
| 2065 | |
| 2066 | // Line and column |
| 2067 | line_and_col_txt = memnew(Label); |
| 2068 | status_bar->add_child(line_and_col_txt); |
| 2069 | line_and_col_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER); |
| 2070 | line_and_col_txt->set_tooltip_text(TTR("Line and column numbers." )); |
| 2071 | line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP); |
| 2072 | |
| 2073 | text_editor->connect("gui_input" , callable_mp(this, &CodeTextEditor::_text_editor_gui_input)); |
| 2074 | text_editor->connect("caret_changed" , callable_mp(this, &CodeTextEditor::_line_col_changed)); |
| 2075 | text_editor->connect("text_changed" , callable_mp(this, &CodeTextEditor::_text_changed)); |
| 2076 | text_editor->connect("code_completion_requested" , callable_mp(this, &CodeTextEditor::_complete_request)); |
| 2077 | TypedArray<String> cs; |
| 2078 | cs.push_back("." ); |
| 2079 | cs.push_back("," ); |
| 2080 | cs.push_back("(" ); |
| 2081 | cs.push_back("=" ); |
| 2082 | cs.push_back("$" ); |
| 2083 | cs.push_back("@" ); |
| 2084 | cs.push_back("\"" ); |
| 2085 | cs.push_back("\'" ); |
| 2086 | text_editor->set_code_completion_prefixes(cs); |
| 2087 | idle->connect("timeout" , callable_mp(this, &CodeTextEditor::_text_changed_idle_timeout)); |
| 2088 | |
| 2089 | code_complete_timer->connect("timeout" , callable_mp(this, &CodeTextEditor::_code_complete_timer_timeout)); |
| 2090 | |
| 2091 | font_resize_val = 0; |
| 2092 | font_size = EDITOR_GET("interface/editor/code_font_size" ); |
| 2093 | font_resize_timer = memnew(Timer); |
| 2094 | add_child(font_resize_timer); |
| 2095 | font_resize_timer->set_one_shot(true); |
| 2096 | font_resize_timer->set_wait_time(0.07); |
| 2097 | font_resize_timer->connect("timeout" , callable_mp(this, &CodeTextEditor::_font_resize_timeout)); |
| 2098 | |
| 2099 | EditorSettings::get_singleton()->connect("settings_changed" , callable_mp(this, &CodeTextEditor::_on_settings_change)); |
| 2100 | add_theme_constant_override("separation" , 4 * EDSCALE); |
| 2101 | } |
| 2102 | |