diff options
| author | Paulb23 | 2016-04-23 19:21:34 +0100 |
|---|---|---|
| committer | Rémi Verschelde | 2016-04-27 20:17:25 +0200 |
| commit | 73296a9a6da3e4c616cca9ffba62ff8918b9ae19 (patch) | |
| tree | aff914c33881f8317d600bdbd1edcff66aebbf01 /tools/editor/plugins | |
| parent | 93b1f60ca2938adc169cb9618f0193b9f06369e9 (diff) | |
| download | godot-73296a9a6da3e4c616cca9ffba62ff8918b9ae19.tar.gz godot-73296a9a6da3e4c616cca9ffba62ff8918b9ae19.tar.zst godot-73296a9a6da3e4c616cca9ffba62ff8918b9ae19.zip | |
Trim trailing white space on save, issue 4383
(cherry picked from commit f3e6569e00b6fcebe5f932d91cf0df24411ce062)
Diffstat (limited to 'tools/editor/plugins')
| -rw-r--r-- | tools/editor/plugins/script_editor_plugin.cpp | 51 | ||||
| -rw-r--r-- | tools/editor/plugins/script_editor_plugin.h | 5 |
2 files changed, 53 insertions, 3 deletions
diff --git a/tools/editor/plugins/script_editor_plugin.cpp b/tools/editor/plugins/script_editor_plugin.cpp index fba0eb525..04394daab 100644 --- a/tools/editor/plugins/script_editor_plugin.cpp +++ b/tools/editor/plugins/script_editor_plugin.cpp @@ -619,6 +619,34 @@ void ScriptEditor::_script_created(Ref<Script> p_script) { editor->push_item(p_script.operator->()); } +void ScriptEditor::_trim_trailing_whitespace(TextEdit *tx) { + + bool trimed_whitespace = false; + for (int i = 0; i < tx->get_line_count(); i++) { + String line = tx->get_line(i); + if (line.ends_with(" ") || line.ends_with("\t")) { + + if (!trimed_whitespace) { + tx->begin_complex_operation(); + trimed_whitespace = true; + } + + int end = 0; + for (int j = line.length() - 1; j > -1; j--) { + if (line[j] != ' ' && line[j] != '\t') { + end = j+1; + break; + } + } + tx->set_line(i, line.substr(0, end)); + } + } + if (trimed_whitespace) { + tx->end_complex_operation(); + tx->update(); + } +} + void ScriptEditor::_goto_script_line2(int p_line) { int selected = tab_container->get_current_tab(); @@ -777,7 +805,9 @@ void ScriptEditor::_resave_scripts(const String& p_str) { if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) continue; //internal script, who cares - + if (trim_trailing_whitespace_on_save) { + _trim_trailing_whitespace(ste->get_text_edit()); + } editor->save_resource(script); ste->get_text_edit()->tag_saved_version(); } @@ -1028,11 +1058,17 @@ void ScriptEditor::_menu_option(int p_option) { if (_test_script_times_on_disk()) return; + if (trim_trailing_whitespace_on_save) { + _trim_trailing_whitespace(current->get_text_edit()); + } editor->save_resource( current->get_edited_script() ); } break; case FILE_SAVE_AS: { + if (trim_trailing_whitespace_on_save) { + _trim_trailing_whitespace(current->get_text_edit()); + } editor->save_resource_as( current->get_edited_script() ); } break; @@ -1245,7 +1281,7 @@ void ScriptEditor::_menu_option(int p_option) { // End of selection ends on the first column of the last line, ignore it. if(tx->get_selection_to_column() == 0) end -= 1; - + for (int i = begin; i <= end; i++) { String line_text = tx->get_line(i); @@ -1298,6 +1334,9 @@ void ScriptEditor::_menu_option(int p_option) { } break; + case EDIT_TRIM_TRAILING_WHITESAPCE: { + _trim_trailing_whitespace(current->get_text_edit()); + } break; case SEARCH_FIND: { find_replace_dialog->set_text_edit(current->get_text_edit()); @@ -1951,6 +1990,10 @@ void ScriptEditor::save_all_scripts() { if (!ste) continue; + + if (trim_trailing_whitespace_on_save) { + _trim_trailing_whitespace(ste->get_text_edit()); + } if (ste->get_text_edit()->get_version()==ste->get_text_edit()->get_saved_version()) continue; @@ -2047,6 +2090,7 @@ void ScriptEditor::_add_callback(Object *p_obj, const String& p_function, const void ScriptEditor::_editor_settings_changed() { print_line("settings changed"); + trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/trim_trailing_whitespace_on_save"); float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs"); if (autosave_time>0) { autosave_timer->set_wait_time(autosave_time); @@ -2392,6 +2436,7 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) { #else edit_menu->get_popup()->add_item("Complete Symbol",EDIT_COMPLETE,KEY_MASK_CMD|KEY_SPACE); #endif + edit_menu->get_popup()->add_item("Trim Trailing Whitespace", EDIT_TRIM_TRAILING_WHITESAPCE, KEY_MASK_CTRL|KEY_MASK_ALT|KEY_T); edit_menu->get_popup()->add_item("Auto Indent",EDIT_AUTO_INDENT,KEY_MASK_CMD|KEY_I); edit_menu->get_popup()->connect("item_pressed", this,"_menu_option"); @@ -2582,7 +2627,7 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) { // debugger_gui->hide(); edit_pass=0; - + trim_trailing_whitespace_on_save = false; } diff --git a/tools/editor/plugins/script_editor_plugin.h b/tools/editor/plugins/script_editor_plugin.h index 5664b2658..68aef4d39 100644 --- a/tools/editor/plugins/script_editor_plugin.h +++ b/tools/editor/plugins/script_editor_plugin.h @@ -131,6 +131,7 @@ class ScriptEditor : public VBoxContainer { EDIT_SELECT_ALL, EDIT_COMPLETE, EDIT_AUTO_INDENT, + EDIT_TRIM_TRAILING_WHITESAPCE, EDIT_TOGGLE_COMMENT, EDIT_MOVE_LINE_UP, EDIT_MOVE_LINE_DOWN, @@ -238,6 +239,10 @@ class ScriptEditor : public VBoxContainer { void _add_callback(Object *p_obj, const String& p_function, const StringArray& p_args); void _res_saved_callback(const Ref<Resource>& p_res); + bool trim_trailing_whitespace_on_save; + + void _trim_trailing_whitespace(TextEdit *tx); + void _goto_script_line2(int p_line); void _goto_script_line(REF p_script,int p_line); void _breaked(bool p_breaked,bool p_can_debug); |
