1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-07 12:30:27 +00:00

Automatically add new line to scripts

This commit is contained in:
Matheus Lima Cunha
2019-05-28 18:27:32 -03:00
parent 06e580f5ba
commit 7fbb6d986f
10 changed files with 46 additions and 1 deletions

View File

@@ -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() { void CodeTextEditor::convert_indent_to_spaces() {
int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size"); int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
String indent = ""; String indent = "";

View File

@@ -195,6 +195,7 @@ protected:
public: public:
void trim_trailing_whitespace(); void trim_trailing_whitespace();
void insert_final_newline();
void convert_indent_to_spaces(); void convert_indent_to_spaces();
void convert_indent_to_tabs(); void convert_indent_to_tabs();

View File

@@ -681,6 +681,8 @@ void ScriptEditor::_resave_scripts(const String &p_str) {
se->trim_trailing_whitespace(); se->trim_trailing_whitespace();
} }
se->insert_final_newline();
if (convert_indent_on_save) { if (convert_indent_on_save) {
if (use_space_indentation) { if (use_space_indentation) {
se->convert_indent_to_spaces(); se->convert_indent_to_spaces();
@@ -1033,6 +1035,8 @@ void ScriptEditor::_menu_option(int p_option) {
if (trim_trailing_whitespace_on_save) if (trim_trailing_whitespace_on_save)
current->trim_trailing_whitespace(); current->trim_trailing_whitespace();
current->insert_final_newline();
if (convert_indent_on_save) { if (convert_indent_on_save) {
if (use_space_indentation) { if (use_space_indentation) {
current->convert_indent_to_spaces(); current->convert_indent_to_spaces();
@@ -1052,8 +1056,11 @@ void ScriptEditor::_menu_option(int p_option) {
} break; } break;
case FILE_SAVE_AS: { case FILE_SAVE_AS: {
if (trim_trailing_whitespace_on_save)
current->trim_trailing_whitespace(); current->trim_trailing_whitespace();
current->insert_final_newline();
if (convert_indent_on_save) { if (convert_indent_on_save) {
if (use_space_indentation) { if (use_space_indentation) {
current->convert_indent_to_spaces(); current->convert_indent_to_spaces();
@@ -2046,6 +2053,8 @@ void ScriptEditor::save_all_scripts() {
se->trim_trailing_whitespace(); se->trim_trailing_whitespace();
} }
se->insert_final_newline();
if (!se->is_unsaved()) if (!se->is_unsaved())
continue; continue;

View File

@@ -97,6 +97,7 @@ public:
virtual void set_edit_state(const Variant &p_state) = 0; 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 goto_line(int p_line, bool p_with_error = false) = 0;
virtual void trim_trailing_whitespace() = 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_spaces() = 0;
virtual void convert_indent_to_tabs() = 0; virtual void convert_indent_to_tabs() = 0;
virtual void ensure_focus() = 0; virtual void ensure_focus() = 0;

View File

@@ -375,6 +375,11 @@ void ScriptTextEditor::trim_trailing_whitespace() {
code_editor->trim_trailing_whitespace(); code_editor->trim_trailing_whitespace();
} }
void ScriptTextEditor::insert_final_newline() {
code_editor->insert_final_newline();
}
void ScriptTextEditor::convert_indent_to_spaces() { void ScriptTextEditor::convert_indent_to_spaces() {
code_editor->convert_indent_to_spaces(); code_editor->convert_indent_to_spaces();

View File

@@ -166,6 +166,7 @@ public:
virtual void set_edit_state(const Variant &p_state); virtual void set_edit_state(const Variant &p_state);
virtual void ensure_focus(); virtual void ensure_focus();
virtual void trim_trailing_whitespace(); virtual void trim_trailing_whitespace();
virtual void insert_final_newline();
virtual void convert_indent_to_spaces(); virtual void convert_indent_to_spaces();
virtual void convert_indent_to_tabs(); virtual void convert_indent_to_tabs();
virtual void tag_saved_version(); virtual void tag_saved_version();

View File

@@ -241,6 +241,11 @@ void TextEditor::trim_trailing_whitespace() {
code_editor->trim_trailing_whitespace(); code_editor->trim_trailing_whitespace();
} }
void TextEditor::insert_final_newline() {
code_editor->insert_final_newline();
}
void TextEditor::convert_indent_to_spaces() { void TextEditor::convert_indent_to_spaces() {
code_editor->convert_indent_to_spaces(); code_editor->convert_indent_to_spaces();

View File

@@ -124,6 +124,7 @@ public:
virtual void get_breakpoints(List<int> *p_breakpoints); virtual void get_breakpoints(List<int> *p_breakpoints);
virtual void goto_line(int p_line, bool p_with_error = false); virtual void goto_line(int p_line, bool p_with_error = false);
virtual void trim_trailing_whitespace(); virtual void trim_trailing_whitespace();
virtual void insert_final_newline();
virtual void convert_indent_to_spaces(); virtual void convert_indent_to_spaces();
virtual void convert_indent_to_tabs(); virtual void convert_indent_to_tabs();
virtual void ensure_focus(); virtual void ensure_focus();

View File

@@ -2102,6 +2102,9 @@ void VisualScriptEditor::goto_line(int p_line, bool p_with_error) {
void VisualScriptEditor::trim_trailing_whitespace() { void VisualScriptEditor::trim_trailing_whitespace() {
} }
void VisualScriptEditor::insert_final_newline() {
}
void VisualScriptEditor::convert_indent_to_spaces() { void VisualScriptEditor::convert_indent_to_spaces() {
} }

View File

@@ -264,6 +264,7 @@ public:
virtual void set_edit_state(const Variant &p_state); virtual void set_edit_state(const Variant &p_state);
virtual void goto_line(int p_line, bool p_with_error = false); virtual void goto_line(int p_line, bool p_with_error = false);
virtual void trim_trailing_whitespace(); virtual void trim_trailing_whitespace();
virtual void insert_final_newline();
virtual void convert_indent_to_spaces(); virtual void convert_indent_to_spaces();
virtual void convert_indent_to_tabs(); virtual void convert_indent_to_tabs();
virtual void ensure_focus(); virtual void ensure_focus();