diff options
author | Paulb23 <p_batty@hotmail.co.uk> | 2016-04-23 19:21:34 +0100 |
---|---|---|
committer | Paulb23 <p_batty@hotmail.co.uk> | 2016-04-23 19:21:34 +0100 |
commit | f3e6569e00b6fcebe5f932d91cf0df24411ce062 (patch) | |
tree | 89ef5b058194a2c7d1ca413d9aa690197bae2181 /tools/editor/plugins | |
parent | 7d89a8b7481c1e172958010366b3735b6d000591 (diff) |
Trim trailing white space on save, issue 4383
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 6ed44901f1..6515b9b69e 100644 --- a/tools/editor/plugins/script_editor_plugin.cpp +++ b/tools/editor/plugins/script_editor_plugin.cpp @@ -620,6 +620,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(); @@ -778,7 +806,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(); } @@ -1029,11 +1059,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; @@ -1246,7 +1282,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); @@ -1299,6 +1335,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()); @@ -1953,6 +1992,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; @@ -2049,6 +2092,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); @@ -2394,6 +2438,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"); @@ -2584,7 +2629,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 5664b26580..68aef4d39c 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); |