diff options
Diffstat (limited to 'editor/plugins/script_text_editor.cpp')
-rw-r--r-- | editor/plugins/script_text_editor.cpp | 124 |
1 files changed, 57 insertions, 67 deletions
diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index a642f35d6f..561edcf8bf 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -284,8 +284,7 @@ void ScriptTextEditor::_warning_clicked(const Variant &p_line) { if (prev_line.contains("@warning_ignore")) { const int closing_bracket_idx = prev_line.find(")"); const String text_to_insert = ", " + code.quote(quote_style); - prev_line = prev_line.insert(closing_bracket_idx, text_to_insert); - text_editor->set_line(line - 1, prev_line); + text_editor->insert_text(text_to_insert, line - 1, closing_bracket_idx); } else { const int indent = text_editor->get_indent_level(line) / text_editor->get_indent_size(); String annotation_indent; @@ -352,22 +351,26 @@ void ScriptTextEditor::add_callback(const String &p_function, const PackedString if (!language->can_make_function()) { return; } - + code_editor->get_text_editor()->begin_complex_operation(); + code_editor->get_text_editor()->remove_secondary_carets(); + code_editor->get_text_editor()->deselect(); String code = code_editor->get_text_editor()->get_text(); int pos = language->find_function(p_function, code); - code_editor->get_text_editor()->remove_secondary_carets(); if (pos == -1) { - //does not exist - code_editor->get_text_editor()->deselect(); - pos = code_editor->get_text_editor()->get_line_count() + 2; + // Function does not exist, create it at the end of the file. + int last_line = code_editor->get_text_editor()->get_line_count() - 1; String func = language->make_function("", p_function, p_args); - //code=code+func; - code_editor->get_text_editor()->set_caret_line(pos + 1); - code_editor->get_text_editor()->set_caret_column(1000000); //none shall be that big - code_editor->get_text_editor()->insert_text_at_caret("\n\n" + func); + code_editor->get_text_editor()->insert_text("\n\n" + func, last_line, code_editor->get_text_editor()->get_line(last_line).length()); + pos = last_line + 3; + } + // Put caret on the line after the function, after the indent. + int indent_column = 1; + if (EDITOR_GET("text_editor/behavior/indent/type")) { + indent_column = EDITOR_GET("text_editor/behavior/indent/size"); } - code_editor->get_text_editor()->set_caret_line(pos); - code_editor->get_text_editor()->set_caret_column(1); + code_editor->get_text_editor()->set_caret_line(pos, true, true, -1); + code_editor->get_text_editor()->set_caret_column(indent_column); + code_editor->get_text_editor()->end_complex_operation(); } bool ScriptTextEditor::show_members_overview() { @@ -1335,10 +1338,10 @@ void ScriptTextEditor::_edit_option(int p_op) { callable_mp((Control *)tx, &Control::grab_focus).call_deferred(); } break; case EDIT_MOVE_LINE_UP: { - code_editor->move_lines_up(); + code_editor->get_text_editor()->move_lines_up(); } break; case EDIT_MOVE_LINE_DOWN: { - code_editor->move_lines_down(); + code_editor->get_text_editor()->move_lines_down(); } break; case EDIT_INDENT: { Ref<Script> scr = script; @@ -1355,24 +1358,16 @@ void ScriptTextEditor::_edit_option(int p_op) { tx->unindent_lines(); } break; case EDIT_DELETE_LINE: { - code_editor->delete_lines(); + code_editor->get_text_editor()->delete_lines(); } break; case EDIT_DUPLICATE_SELECTION: { - code_editor->duplicate_selection(); + code_editor->get_text_editor()->duplicate_selection(); } break; case EDIT_DUPLICATE_LINES: { code_editor->get_text_editor()->duplicate_lines(); } break; case EDIT_TOGGLE_FOLD_LINE: { - int prev_line = -1; - for (int caret_idx : tx->get_caret_index_edit_order()) { - int line_idx = tx->get_caret_line(caret_idx); - if (line_idx != prev_line) { - tx->toggle_foldable_line(line_idx); - prev_line = line_idx; - } - } - tx->queue_redraw(); + tx->toggle_foldable_lines_at_carets(); } break; case EDIT_FOLD_ALL_LINES: { tx->fold_all_lines(); @@ -1399,24 +1394,34 @@ void ScriptTextEditor::_edit_option(int p_op) { } tx->begin_complex_operation(); - int begin, end; + tx->begin_multicaret_edit(); + int begin = tx->get_line_count() - 1, end = 0; if (tx->has_selection()) { - begin = tx->get_selection_from_line(); - end = tx->get_selection_to_line(); - // ignore if the cursor is not past the first column - if (tx->get_selection_to_column() == 0) { - end--; + // Auto indent all lines that have a caret or selection on it. + Vector<Point2i> line_ranges = tx->get_line_ranges_from_carets(); + for (Point2i line_range : line_ranges) { + scr->get_language()->auto_indent_code(text, line_range.x, line_range.y); + if (line_range.x < begin) { + begin = line_range.x; + } + if (line_range.y > end) { + end = line_range.y; + } } } else { + // Auto indent entire text. begin = 0; end = tx->get_line_count() - 1; + scr->get_language()->auto_indent_code(text, begin, end); } - scr->get_language()->auto_indent_code(text, begin, end); + + // Apply auto indented code. Vector<String> lines = text.split("\n"); for (int i = begin; i <= end; ++i) { tx->set_line(i, lines[i]); } + tx->end_multicaret_edit(); tx->end_complex_operation(); } break; case EDIT_TRIM_TRAILING_WHITESAPCE: { @@ -1515,13 +1520,12 @@ void ScriptTextEditor::_edit_option(int p_op) { code_editor->remove_all_bookmarks(); } break; case DEBUG_TOGGLE_BREAKPOINT: { - Vector<int> caret_edit_order = tx->get_caret_index_edit_order(); - caret_edit_order.reverse(); + Vector<int> sorted_carets = tx->get_sorted_carets(); int last_line = -1; - for (const int &c : caret_edit_order) { - int from = tx->has_selection(c) ? tx->get_selection_from_line(c) : tx->get_caret_line(c); + for (const int &c : sorted_carets) { + int from = tx->get_selection_from_line(c); from += from == last_line ? 1 : 0; - int to = tx->has_selection(c) ? tx->get_selection_to_line(c) : tx->get_caret_line(c); + int to = tx->get_selection_to_line(c); if (to < from) { continue; } @@ -2008,45 +2012,32 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) { tx->apply_ime(); Point2i pos = tx->get_line_column_at_pos(local_pos); - int row = pos.y; - int col = pos.x; + int mouse_line = pos.y; + int mouse_column = pos.x; tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click")); - int caret_clicked = -1; + int selection_clicked = -1; if (tx->is_move_caret_on_right_click_enabled()) { - if (tx->has_selection()) { - for (int i = 0; i < tx->get_caret_count(); i++) { - int from_line = tx->get_selection_from_line(i); - int to_line = tx->get_selection_to_line(i); - int from_column = tx->get_selection_from_column(i); - int to_column = tx->get_selection_to_column(i); - - if (row >= from_line && row <= to_line && (row != from_line || col >= from_column) && (row != to_line || col <= to_column)) { - // Right click in one of the selected text - caret_clicked = i; - break; - } - } - } - if (caret_clicked < 0) { + selection_clicked = tx->get_selection_at_line_column(mouse_line, mouse_column, true); + if (selection_clicked < 0) { tx->deselect(); tx->remove_secondary_carets(); - caret_clicked = 0; - tx->set_caret_line(row, false, false); - tx->set_caret_column(col); + selection_clicked = 0; + tx->set_caret_line(mouse_line, false, false, -1); + tx->set_caret_column(mouse_column); } } String word_at_pos = tx->get_word_at_pos(local_pos); if (word_at_pos.is_empty()) { - word_at_pos = tx->get_word_under_caret(caret_clicked); + word_at_pos = tx->get_word_under_caret(selection_clicked); } if (word_at_pos.is_empty()) { - word_at_pos = tx->get_selected_text(caret_clicked); + word_at_pos = tx->get_selected_text(selection_clicked); } bool has_color = (word_at_pos == "Color"); - bool foldable = tx->can_fold_line(row) || tx->is_line_folded(row); + bool foldable = tx->can_fold_line(mouse_line) || tx->is_line_folded(mouse_line); bool open_docs = false; bool goto_definition = false; @@ -2064,9 +2055,9 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) { } if (has_color) { - String line = tx->get_line(row); - color_position.x = row; - color_position.y = col; + String line = tx->get_line(mouse_line); + color_position.x = mouse_line; + color_position.y = mouse_column; int begin = -1; int end = -1; @@ -2076,7 +2067,7 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) { COLOR_NAME, // Color.COLOR_NAME } expression_pattern = NOT_PARSED; - for (int i = col; i < line.length(); i++) { + for (int i = mouse_column; i < line.length(); i++) { if (line[i] == '(') { if (expression_pattern == NOT_PARSED) { begin = i; @@ -2155,7 +2146,6 @@ void ScriptTextEditor::_color_changed(const Color &p_color) { code_editor->get_text_editor()->begin_complex_operation(); code_editor->get_text_editor()->set_line(color_position.x, line_with_replaced_args); code_editor->get_text_editor()->end_complex_operation(); - code_editor->get_text_editor()->queue_redraw(); } void ScriptTextEditor::_prepare_edit_menu() { |