Merge pull request #8424 from Paulb23/convert_indent

Support for space indentation
This commit is contained in:
Rémi Verschelde 2017-04-18 14:16:58 +02:00 committed by GitHub
commit 5237bc952d
13 changed files with 313 additions and 34 deletions

View file

@ -530,6 +530,15 @@ void ScriptEditor::_resave_scripts(const String &p_str) {
if (trim_trailing_whitespace_on_save) {
se->trim_trailing_whitespace();
}
if (convert_indent_on_save) {
if (use_space_indentation) {
se->convert_indent_to_spaces();
} else {
se->convert_indent_to_tabs();
}
}
editor->save_resource(script);
se->tag_saved_version();
}
@ -795,12 +804,28 @@ void ScriptEditor::_menu_option(int p_option) {
if (trim_trailing_whitespace_on_save)
current->trim_trailing_whitespace();
if (convert_indent_on_save) {
if (use_space_indentation) {
current->convert_indent_to_spaces();
} else {
current->convert_indent_to_tabs();
}
}
editor->save_resource(current->get_edited_script());
} break;
case FILE_SAVE_AS: {
current->trim_trailing_whitespace();
if (convert_indent_on_save) {
if (use_space_indentation) {
current->convert_indent_to_spaces();
} else {
current->convert_indent_to_tabs();
}
}
editor->push_item(current->get_edited_script()->cast_to<Object>());
editor->save_resource_as(current->get_edited_script());
@ -1484,6 +1509,14 @@ void ScriptEditor::save_all_scripts() {
se->trim_trailing_whitespace();
}
if (convert_indent_on_save) {
if (use_space_indentation) {
se->convert_indent_to_spaces();
} else {
se->convert_indent_to_tabs();
}
}
Ref<Script> script = se->get_edited_script();
if (script.is_valid())
se->apply_code();
@ -1583,6 +1616,9 @@ void ScriptEditor::_save_layout() {
void ScriptEditor::_editor_settings_changed() {
trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/files/trim_trailing_whitespace_on_save");
convert_indent_on_save = EditorSettings::get_singleton()->get("text_editor/indent/convert_indent_on_save");
use_space_indentation = EditorSettings::get_singleton()->get("text_editor/indent/type") == "Tabs" ? 0 : 1;
float autosave_time = EditorSettings::get_singleton()->get("text_editor/files/autosave_interval_secs");
if (autosave_time > 0) {
autosave_timer->set_wait_time(autosave_time);
@ -2161,6 +2197,8 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) {
edit_pass = 0;
trim_trailing_whitespace_on_save = false;
convert_indent_on_save = false;
use_space_indentation = false;
ScriptServer::edit_request_func = _open_script_request;
}

View file

@ -91,6 +91,8 @@ 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 convert_indent_to_spaces() = 0;
virtual void convert_indent_to_tabs() = 0;
virtual void ensure_focus() = 0;
virtual void tag_saved_version() = 0;
virtual void reload(bool p_soft) = 0;
@ -252,6 +254,8 @@ class ScriptEditor : public VBoxContainer {
void _res_saved_callback(const Ref<Resource> &p_res);
bool trim_trailing_whitespace_on_save;
bool use_space_indentation;
bool convert_indent_on_save;
void _trim_trailing_whitespace(TextEdit *tx);

View file

@ -290,6 +290,96 @@ void ScriptTextEditor::trim_trailing_whitespace() {
}
}
void ScriptTextEditor::convert_indent_to_spaces() {
TextEdit *tx = code_editor->get_text_edit();
Ref<Script> scr = get_edited_script();
if (scr.is_null()) {
return;
}
int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
String indent = "";
for (int i = 0; i < indent_size; i++) {
indent += " ";
}
bool changed_indentation = false;
for (int i = 0; i < tx->get_line_count(); i++) {
String line = tx->get_line(i);
if (line.length() <= 0) {
continue;
}
int j = 0;
while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
if (line[j] == '\t') {
if (!changed_indentation) {
tx->begin_complex_operation();
changed_indentation = true;
}
line = line.left(j) + indent + line.right(j + 1);
}
j++;
}
tx->set_line(i, line);
}
if (changed_indentation) {
tx->end_complex_operation();
tx->update();
}
}
void ScriptTextEditor::convert_indent_to_tabs() {
TextEdit *tx = code_editor->get_text_edit();
Ref<Script> scr = get_edited_script();
if (scr.is_null()) {
return;
}
int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
indent_size -= 1;
bool changed_indentation = false;
for (int i = 0; i < tx->get_line_count(); i++) {
String line = tx->get_line(i);
if (line.length() <= 0) {
continue;
}
int j = 0;
int space_count = -1;
while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
if (line[j] != '\t') {
space_count++;
if (space_count == indent_size) {
if (!changed_indentation) {
tx->begin_complex_operation();
changed_indentation = true;
}
line = line.left(j - indent_size) + "\t" + line.right(j + 1);
j = 0;
space_count = -1;
}
} else {
space_count = -1;
}
j++;
}
tx->set_line(i, line);
}
if (changed_indentation) {
tx->end_complex_operation();
tx->update();
}
}
void ScriptTextEditor::tag_saved_version() {
code_editor->get_text_edit()->tag_saved_version();
@ -827,6 +917,12 @@ void ScriptTextEditor::_edit_option(int p_op) {
case EDIT_TRIM_TRAILING_WHITESAPCE: {
trim_trailing_whitespace();
} break;
case EDIT_CONVERT_INDENT_TO_SPACES: {
convert_indent_to_spaces();
} break;
case EDIT_CONVERT_INDENT_TO_TABS: {
convert_indent_to_tabs();
} break;
case EDIT_PICK_COLOR: {
color_panel->popup();
} break;
@ -1237,6 +1333,8 @@ ScriptTextEditor::ScriptTextEditor() {
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
#endif
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
edit_menu->get_popup()->connect("id_pressed", this, "_edit_option");
edit_menu->get_popup()->add_separator();
@ -1305,6 +1403,8 @@ void ScriptTextEditor::register_editor() {
ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD | KEY_SPACE);
#endif
ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CTRL | KEY_MASK_ALT | KEY_T);
ED_SHORTCUT("script_text_editor/convert_indent_to_spaces", TTR("Convert Indent To Spaces"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_Y);
ED_SHORTCUT("script_text_editor/convert_indent_to_tabs", TTR("Convert Indent To Tabs"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_X);
ED_SHORTCUT("script_text_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD | KEY_I);
ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);

View file

@ -67,6 +67,8 @@ class ScriptTextEditor : public ScriptEditorBase {
EDIT_COMPLETE,
EDIT_AUTO_INDENT,
EDIT_TRIM_TRAILING_WHITESAPCE,
EDIT_CONVERT_INDENT_TO_SPACES,
EDIT_CONVERT_INDENT_TO_TABS,
EDIT_TOGGLE_COMMENT,
EDIT_MOVE_LINE_UP,
EDIT_MOVE_LINE_DOWN,
@ -125,6 +127,8 @@ public:
virtual void set_edit_state(const Variant &p_state);
virtual void ensure_focus();
virtual void trim_trailing_whitespace();
virtual void convert_indent_to_spaces();
virtual void convert_indent_to_tabs();
virtual void tag_saved_version();
virtual void goto_line(int p_line, bool p_with_error = false);

View file

@ -370,7 +370,8 @@ void ShaderEditor::_editor_settings_changed() {
shader_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
shader_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
shader_editor->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/indent/tab_size"));
shader_editor->get_text_edit()->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
shader_editor->get_text_edit()->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type") == "Tabs" ? 0 : 1);
shader_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
shader_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_numbers"));
shader_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));