diff options
author | Matheus Lima Cunha <matheus.limacunha@hotmail.com> | 2019-05-28 18:27:32 -0300 |
---|---|---|
committer | Matheus Lima Cunha <matheus.limacunha@hotmail.com> | 2019-05-29 18:13:29 -0300 |
commit | 7fbb6d986ffe6101af1e062daeb9e1aa203684a6 (patch) | |
tree | 7062d63f37b3dd58f9a817e6577d4dbf59652edd | |
parent | 06e580f5ba801cf524b1d82753127a9992c7b907 (diff) |
Automatically add new line to scripts
-rw-r--r-- | editor/code_editor.cpp | 18 | ||||
-rw-r--r-- | editor/code_editor.h | 1 | ||||
-rw-r--r-- | editor/plugins/script_editor_plugin.cpp | 11 | ||||
-rw-r--r-- | editor/plugins/script_editor_plugin.h | 1 | ||||
-rw-r--r-- | editor/plugins/script_text_editor.cpp | 5 | ||||
-rw-r--r-- | editor/plugins/script_text_editor.h | 1 | ||||
-rw-r--r-- | editor/plugins/text_editor.cpp | 5 | ||||
-rw-r--r-- | editor/plugins/text_editor.h | 1 | ||||
-rw-r--r-- | modules/visual_script/visual_script_editor.cpp | 3 | ||||
-rw-r--r-- | modules/visual_script/visual_script_editor.h | 1 |
10 files changed, 46 insertions, 1 deletions
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 134384c167..402ce38c0f 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -801,6 +801,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 = ""; diff --git a/editor/code_editor.h b/editor/code_editor.h index e3dbfe1ce0..ad82702d97 100644 --- a/editor/code_editor.h +++ b/editor/code_editor.h @@ -195,6 +195,7 @@ protected: public: void trim_trailing_whitespace(); + void insert_final_newline(); void convert_indent_to_spaces(); void convert_indent_to_tabs(); diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp index d7d4cec07d..98242ec76d 100644 --- a/editor/plugins/script_editor_plugin.cpp +++ b/editor/plugins/script_editor_plugin.cpp @@ -681,6 +681,8 @@ void ScriptEditor::_resave_scripts(const String &p_str) { se->trim_trailing_whitespace(); } + se->insert_final_newline(); + if (convert_indent_on_save) { if (use_space_indentation) { se->convert_indent_to_spaces(); @@ -1033,6 +1035,8 @@ void ScriptEditor::_menu_option(int p_option) { if (trim_trailing_whitespace_on_save) current->trim_trailing_whitespace(); + current->insert_final_newline(); + if (convert_indent_on_save) { if (use_space_indentation) { current->convert_indent_to_spaces(); @@ -1052,7 +1056,10 @@ void ScriptEditor::_menu_option(int p_option) { } break; case FILE_SAVE_AS: { - current->trim_trailing_whitespace(); + if (trim_trailing_whitespace_on_save) + current->trim_trailing_whitespace(); + + current->insert_final_newline(); if (convert_indent_on_save) { if (use_space_indentation) { @@ -2046,6 +2053,8 @@ void ScriptEditor::save_all_scripts() { se->trim_trailing_whitespace(); } + se->insert_final_newline(); + if (!se->is_unsaved()) continue; diff --git a/editor/plugins/script_editor_plugin.h b/editor/plugins/script_editor_plugin.h index 954b014935..d5e2da5c4b 100644 --- a/editor/plugins/script_editor_plugin.h +++ b/editor/plugins/script_editor_plugin.h @@ -97,6 +97,7 @@ public: virtual void set_edit_state(const Variant &p_state) = 0; virtual void goto_line(int p_line, bool p_with_error = false) = 0; virtual void trim_trailing_whitespace() = 0; + virtual void insert_final_newline() = 0; virtual void convert_indent_to_spaces() = 0; virtual void convert_indent_to_tabs() = 0; virtual void ensure_focus() = 0; diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index 543771ad1c..d82207a92e 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -375,6 +375,11 @@ void ScriptTextEditor::trim_trailing_whitespace() { code_editor->trim_trailing_whitespace(); } +void ScriptTextEditor::insert_final_newline() { + + code_editor->insert_final_newline(); +} + void ScriptTextEditor::convert_indent_to_spaces() { code_editor->convert_indent_to_spaces(); diff --git a/editor/plugins/script_text_editor.h b/editor/plugins/script_text_editor.h index b081a31c18..e1e0a7466b 100644 --- a/editor/plugins/script_text_editor.h +++ b/editor/plugins/script_text_editor.h @@ -166,6 +166,7 @@ public: virtual void set_edit_state(const Variant &p_state); virtual void ensure_focus(); virtual void trim_trailing_whitespace(); + virtual void insert_final_newline(); virtual void convert_indent_to_spaces(); virtual void convert_indent_to_tabs(); virtual void tag_saved_version(); diff --git a/editor/plugins/text_editor.cpp b/editor/plugins/text_editor.cpp index fe32c97a64..9c91f3db86 100644 --- a/editor/plugins/text_editor.cpp +++ b/editor/plugins/text_editor.cpp @@ -241,6 +241,11 @@ void TextEditor::trim_trailing_whitespace() { code_editor->trim_trailing_whitespace(); } +void TextEditor::insert_final_newline() { + + code_editor->insert_final_newline(); +} + void TextEditor::convert_indent_to_spaces() { code_editor->convert_indent_to_spaces(); diff --git a/editor/plugins/text_editor.h b/editor/plugins/text_editor.h index 3c136277df..c6be6f2650 100644 --- a/editor/plugins/text_editor.h +++ b/editor/plugins/text_editor.h @@ -124,6 +124,7 @@ public: virtual void get_breakpoints(List<int> *p_breakpoints); virtual void goto_line(int p_line, bool p_with_error = false); virtual void trim_trailing_whitespace(); + virtual void insert_final_newline(); virtual void convert_indent_to_spaces(); virtual void convert_indent_to_tabs(); virtual void ensure_focus(); diff --git a/modules/visual_script/visual_script_editor.cpp b/modules/visual_script/visual_script_editor.cpp index 6e7e566206..5bd152d08d 100644 --- a/modules/visual_script/visual_script_editor.cpp +++ b/modules/visual_script/visual_script_editor.cpp @@ -2102,6 +2102,9 @@ void VisualScriptEditor::goto_line(int p_line, bool p_with_error) { void VisualScriptEditor::trim_trailing_whitespace() { } +void VisualScriptEditor::insert_final_newline() { +} + void VisualScriptEditor::convert_indent_to_spaces() { } diff --git a/modules/visual_script/visual_script_editor.h b/modules/visual_script/visual_script_editor.h index f4b4a6981d..ff9c23d507 100644 --- a/modules/visual_script/visual_script_editor.h +++ b/modules/visual_script/visual_script_editor.h @@ -264,6 +264,7 @@ public: virtual void set_edit_state(const Variant &p_state); virtual void goto_line(int p_line, bool p_with_error = false); virtual void trim_trailing_whitespace(); + virtual void insert_final_newline(); virtual void convert_indent_to_spaces(); virtual void convert_indent_to_tabs(); virtual void ensure_focus(); |