diff options
Diffstat (limited to 'editor/code_editor.cpp')
-rw-r--r-- | editor/code_editor.cpp | 199 |
1 files changed, 187 insertions, 12 deletions
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 0b06839ecf..8b3c537fe3 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -30,6 +30,7 @@ #include "code_editor.h" +#include "core/os/input.h" #include "core/os/keyboard.h" #include "core/string_builder.h" #include "editor/editor_scale.h" @@ -432,7 +433,11 @@ void FindReplaceBar::_search_text_changed(const String &p_text) { void FindReplaceBar::_search_text_entered(const String &p_text) { - search_next(); + if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) { + search_prev(); + } else { + search_next(); + } } void FindReplaceBar::_replace_text_entered(const String &p_text) { @@ -582,6 +587,26 @@ FindReplaceBar::FindReplaceBar() { /*** CODE EDITOR ****/ +// This function should be used to handle shortcuts that could otherwise +// be handled too late if they weren't handled here. +void CodeTextEditor::_input(const Ref<InputEvent> &event) { + + const Ref<InputEventKey> key_event = event; + if (!key_event.is_valid() || !key_event->is_pressed()) + return; + + if (ED_IS_SHORTCUT("script_text_editor/move_up", key_event)) { + move_lines_up(); + accept_event(); + return; + } + if (ED_IS_SHORTCUT("script_text_editor/move_down", key_event)) { + move_lines_down(); + accept_event(); + return; + } +} + void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventMouseButton> mb = p_event; @@ -670,14 +695,14 @@ void CodeTextEditor::_line_col_changed() { } } - StringBuilder *sb = memnew(StringBuilder); - sb->append("("); - sb->append(itos(text_editor->cursor_get_line() + 1).lpad(3)); - sb->append(","); - sb->append(itos(positional_column + 1).lpad(3)); - sb->append(")"); + StringBuilder sb; + sb.append("("); + sb.append(itos(text_editor->cursor_get_line() + 1).lpad(3)); + sb.append(","); + sb.append(itos(positional_column + 1).lpad(3)); + sb.append(")"); - line_and_col_txt->set_text(sb->as_string()); + line_and_col_txt->set_text(sb.as_string()); } void CodeTextEditor::_text_changed() { @@ -750,6 +775,7 @@ void CodeTextEditor::update_editor_settings() { text_editor->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size")); text_editor->set_auto_indent(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent")); text_editor->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs")); + text_editor->set_draw_spaces(EditorSettings::get_singleton()->get("text_editor/indent/draw_spaces")); text_editor->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_numbers")); text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_numbers_zero_padded")); text_editor->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_length_guideline")); @@ -759,10 +785,12 @@ void CodeTextEditor::update_editor_settings() { text_editor->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line")); text_editor->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink")); text_editor->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed")); + text_editor->set_bookmark_gutter_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_bookmark_gutter")); text_editor->set_breakpoint_gutter_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_breakpoint_gutter")); text_editor->set_hiding_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/code_folding")); text_editor->set_draw_fold_gutter(EditorSettings::get_singleton()->get("text_editor/line_numbers/code_folding")); text_editor->set_wrap_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/word_wrap")); + text_editor->set_draw_info_gutter(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_info_gutter")); text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret")); text_editor->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/open_scripts/smooth_scrolling")); text_editor->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/open_scripts/v_scroll_speed")); @@ -796,6 +824,24 @@ void CodeTextEditor::trim_trailing_whitespace() { } } +void CodeTextEditor::insert_final_newline() { + int final_line = text_editor->get_line_count() - 1; + + String line = text_editor->get_line(final_line); + + //length 0 means it's already an empty line, + //no need to add a newline + if (line.length() > 0 && !line.ends_with("\n")) { + text_editor->begin_complex_operation(); + + line += "\n"; + text_editor->set_line(final_line, line); + + text_editor->end_complex_operation(); + text_editor->update(); + } +} + void CodeTextEditor::convert_indent_to_spaces() { int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size"); String indent = ""; @@ -904,7 +950,7 @@ void CodeTextEditor::convert_case(CaseStyle p_case) { for (int i = begin; i <= end; i++) { int len = text_editor->get_line(i).length(); if (i == end) - len -= len - end_col; + len = end_col; if (i == begin) len -= begin_col; String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len); @@ -1171,21 +1217,77 @@ void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) { text_editor->select(p_line, p_begin, p_line, p_end); } +void CodeTextEditor::set_executing_line(int p_line) { + text_editor->set_executing_line(p_line); +} + +void CodeTextEditor::clear_executing_line() { + text_editor->clear_executing_line(); +} + Variant CodeTextEditor::get_edit_state() { Dictionary state; state["scroll_position"] = text_editor->get_v_scroll(); + state["h_scroll_position"] = text_editor->get_h_scroll(); state["column"] = text_editor->cursor_get_column(); state["row"] = text_editor->cursor_get_line(); + state["selection"] = get_text_edit()->is_selection_active(); + if (get_text_edit()->is_selection_active()) { + state["selection_from_line"] = text_editor->get_selection_from_line(); + state["selection_from_column"] = text_editor->get_selection_from_column(); + state["selection_to_line"] = text_editor->get_selection_to_line(); + state["selection_to_column"] = text_editor->get_selection_to_column(); + } + + state["folded_lines"] = text_editor->get_folded_lines(); + state["breakpoints"] = text_editor->get_breakpoints_array(); + state["bookmarks"] = text_editor->get_bookmarks_array(); + + state["syntax_highlighter"] = TTR("Standard"); + SyntaxHighlighter *syntax_highlighter = text_editor->_get_syntax_highlighting(); + if (syntax_highlighter) { + state["syntax_highlighter"] = syntax_highlighter->get_name(); + } + return state; } void CodeTextEditor::set_edit_state(const Variant &p_state) { Dictionary state = p_state; - text_editor->cursor_set_column(state["column"]); + + /* update the row first as it sets the column to 0 */ text_editor->cursor_set_line(state["row"]); + text_editor->cursor_set_column(state["column"]); text_editor->set_v_scroll(state["scroll_position"]); + text_editor->set_h_scroll(state["h_scroll_position"]); + + if (state.has("selection")) { + text_editor->select(state["selection_from_line"], state["selection_from_column"], state["selection_to_line"], state["selection_to_column"]); + } + + if (state.has("folded_lines")) { + Vector<int> folded_lines = state["folded_lines"]; + for (int i = 0; i < folded_lines.size(); i++) { + text_editor->fold_line(folded_lines[i]); + } + } + + if (state.has("breakpoints")) { + Array breakpoints = state["breakpoints"]; + for (int i = 0; i < breakpoints.size(); i++) { + text_editor->set_line_as_breakpoint(breakpoints[i], true); + } + } + + if (state.has("bookmarks")) { + Array bookmarks = state["bookmarks"]; + for (int i = 0; i < bookmarks.size(); i++) { + text_editor->set_line_as_bookmark(bookmarks[i], true); + } + } + text_editor->grab_focus(); } @@ -1243,14 +1345,19 @@ void CodeTextEditor::_on_settings_change() { text_editor->set_callhint_settings( EDITOR_DEF("text_editor/completion/put_callhint_tooltip_below_current_line", true), EDITOR_DEF("text_editor/completion/callhint_tooltip_offset", Vector2())); + + idle->set_wait_time(EDITOR_DEF("text_editor/completion/idle_parse_delay", 2.0)); } void CodeTextEditor::_text_changed_idle_timeout() { - _validate_script(); emit_signal("validate_script"); } +void CodeTextEditor::validate_script() { + idle->start(); +} + void CodeTextEditor::_warning_label_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventMouseButton> mb = p_event; if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) { @@ -1288,6 +1395,9 @@ void CodeTextEditor::_notification(int p_what) { warning_button->set_icon(get_icon("NodeWarning", "EditorIcons")); add_constant_override("separation", 4 * EDSCALE); } break; + case NOTIFICATION_VISIBILITY_CHANGED: { + set_process_input(is_visible_in_tree()); + } break; default: break; } @@ -1301,8 +1411,73 @@ void CodeTextEditor::set_warning_nb(int p_warning_nb) { _set_show_warnings_panel(false); } +void CodeTextEditor::toggle_bookmark() { + + int line = text_editor->cursor_get_line(); + text_editor->set_line_as_bookmark(line, !text_editor->is_line_set_as_bookmark(line)); +} + +void CodeTextEditor::goto_next_bookmark() { + + List<int> bmarks; + text_editor->get_bookmarks(&bmarks); + if (bmarks.size() <= 0) { + return; + } + + int line = text_editor->cursor_get_line(); + if (line >= bmarks[bmarks.size() - 1]) { + text_editor->unfold_line(bmarks[0]); + text_editor->cursor_set_line(bmarks[0]); + } else { + for (List<int>::Element *E = bmarks.front(); E; E = E->next()) { + int bline = E->get(); + if (bline > line) { + text_editor->unfold_line(bline); + text_editor->cursor_set_line(bline); + return; + } + } + } +} + +void CodeTextEditor::goto_prev_bookmark() { + + List<int> bmarks; + text_editor->get_bookmarks(&bmarks); + if (bmarks.size() <= 0) { + return; + } + + int line = text_editor->cursor_get_line(); + if (line <= bmarks[0]) { + text_editor->unfold_line(bmarks[bmarks.size() - 1]); + text_editor->cursor_set_line(bmarks[bmarks.size() - 1]); + } else { + for (List<int>::Element *E = bmarks.back(); E; E = E->prev()) { + int bline = E->get(); + if (bline < line) { + text_editor->unfold_line(bline); + text_editor->cursor_set_line(bline); + return; + } + } + } +} + +void CodeTextEditor::remove_all_bookmarks() { + + List<int> bmarks; + text_editor->get_bookmarks(&bmarks); + + for (List<int>::Element *E = bmarks.front(); E; E = E->next()) { + text_editor->set_line_as_bookmark(E->get(), false); + } +} + void CodeTextEditor::_bind_methods() { + ClassDB::bind_method(D_METHOD("_input"), &CodeTextEditor::_input); ClassDB::bind_method("_text_editor_gui_input", &CodeTextEditor::_text_editor_gui_input); ClassDB::bind_method("_line_col_changed", &CodeTextEditor::_line_col_changed); ClassDB::bind_method("_text_changed", &CodeTextEditor::_text_changed); @@ -1357,7 +1532,7 @@ CodeTextEditor::CodeTextEditor() { idle = memnew(Timer); add_child(idle); idle->set_one_shot(true); - idle->set_wait_time(EDITOR_DEF("text_editor/completion/idle_parse_delay", 2)); + idle->set_wait_time(EDITOR_DEF("text_editor/completion/idle_parse_delay", 2.0)); code_complete_timer = memnew(Timer); add_child(code_complete_timer); |