Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script editor: Convert indentation on paste #28561

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,15 @@ void CodeTextEditor::insert_final_newline() {
}
}

void CodeTextEditor::convert_indent_to_spaces() {
void CodeTextEditor::convert_indent_to_spaces(int p_from, int p_to) {

int line_count = text_editor->get_line_count();
ERR_FAIL_INDEX(p_from, line_count);
if (p_to < 0) {
p_to = line_count - 1;
}
ERR_FAIL_INDEX(p_to, line_count);

int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
String indent = "";

Expand All @@ -940,7 +948,7 @@ void CodeTextEditor::convert_indent_to_spaces() {
int cursor_column = text_editor->cursor_get_column();

bool changed_indentation = false;
for (int i = 0; i < text_editor->get_line_count(); i++) {
for (int i = p_from; i < p_to + 1; i++) {
String line = text_editor->get_line(i);

if (line.length() <= 0) {
Expand Down Expand Up @@ -972,15 +980,23 @@ void CodeTextEditor::convert_indent_to_spaces() {
}
}

void CodeTextEditor::convert_indent_to_tabs() {
void CodeTextEditor::convert_indent_to_tabs(int p_from, int p_to) {

int line_count = text_editor->get_line_count();
ERR_FAIL_INDEX(p_from, line_count);
if (p_to < 0) {
p_to = line_count - 1;
}
ERR_FAIL_INDEX(p_to, line_count);

int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
indent_size -= 1;

int cursor_line = text_editor->cursor_get_line();
int cursor_column = text_editor->cursor_get_column();

bool changed_indentation = false;
for (int i = 0; i < text_editor->get_line_count(); i++) {
for (int i = p_from; i < p_to + 1; i++) {
String line = text_editor->get_line(i);

if (line.length() <= 0) {
Expand Down
4 changes: 2 additions & 2 deletions editor/code_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ class CodeTextEditor : public VBoxContainer {
void trim_trailing_whitespace();
void insert_final_newline();

void convert_indent_to_spaces();
void convert_indent_to_tabs();
void convert_indent_to_spaces(int p_from = 0, int p_to = -1);
void convert_indent_to_tabs(int p_from = 0, int p_to = -1);

enum CaseStyle {
UPPER,
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3445,7 +3445,7 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) {
edit_pass = 0;
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");
use_space_indentation = EditorSettings::get_singleton()->get("text_editor/indent/type"); // Tabs 0, spaces 1

ScriptServer::edit_request_func = _open_script_request;

Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/script_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ class ScriptEditorBase : public VBoxContainer {
virtual void clear_executing_line() = 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 convert_indent_to_spaces(int p_from = 0, int p_to = -1) = 0;
virtual void convert_indent_to_tabs(int p_from = 0, int p_to = -1) = 0;
virtual void ensure_focus() = 0;
virtual void tag_saved_version() = 0;
virtual void reload(bool p_soft) {}
Expand Down
25 changes: 21 additions & 4 deletions editor/plugins/script_text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,14 @@ void ScriptTextEditor::insert_final_newline() {
code_editor->insert_final_newline();
}

void ScriptTextEditor::convert_indent_to_spaces() {
void ScriptTextEditor::convert_indent_to_spaces(int p_from, int p_to) {

code_editor->convert_indent_to_spaces();
code_editor->convert_indent_to_spaces(p_from, p_to);
}

void ScriptTextEditor::convert_indent_to_tabs() {
void ScriptTextEditor::convert_indent_to_tabs(int p_from, int p_to) {

code_editor->convert_indent_to_tabs();
code_editor->convert_indent_to_tabs(p_from, p_to);
}

void ScriptTextEditor::tag_saved_version() {
Expand Down Expand Up @@ -1028,6 +1028,19 @@ void ScriptTextEditor::_edit_option(int p_op) {
} break;
case EDIT_PASTE: {

int from = tx->cursor_get_line(); // Save original position.
tx->paste();
int to = tx->cursor_get_line();
// Convert clipboard indentation to match configured indent style
if (use_space_indentation) {
convert_indent_to_spaces(from, to);
} else {
convert_indent_to_tabs(from, to);
}
tx->call_deferred("grab_focus");
} break;
case EDIT_PASTE_ORIGINAL_INDENT: {

tx->paste();
tx->call_deferred("grab_focus");
} break;
Expand Down Expand Up @@ -1627,6 +1640,7 @@ void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color, bool p
}

context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste_original_indent"), EDIT_PASTE_ORIGINAL_INDENT);
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
Expand Down Expand Up @@ -1661,6 +1675,7 @@ void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color, bool p
ScriptTextEditor::ScriptTextEditor() {

theme_loaded = false;
use_space_indentation = EditorSettings::get_singleton()->get("text_editor/indent/type"); // Tabs 0, spaces 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be set under update_settings else it won't update if the setting is changed.


VSplitContainer *editor_box = memnew(VSplitContainer);
add_child(editor_box);
Expand Down Expand Up @@ -1725,6 +1740,7 @@ ScriptTextEditor::ScriptTextEditor() {
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste_original_indent"), EDIT_PASTE_ORIGINAL_INDENT);
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
edit_menu->get_popup()->add_separator();
Expand Down Expand Up @@ -1843,6 +1859,7 @@ void ScriptTextEditor::register_editor() {
ED_SHORTCUT("script_text_editor/cut", TTR("Cut"), KEY_MASK_CMD | KEY_X);
ED_SHORTCUT("script_text_editor/copy", TTR("Copy"), KEY_MASK_CMD | KEY_C);
ED_SHORTCUT("script_text_editor/paste", TTR("Paste"), KEY_MASK_CMD | KEY_V);
ED_SHORTCUT("script_text_editor/paste_original_indent", TTR("Paste with Original Indent"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_V);
ED_SHORTCUT("script_text_editor/select_all", TTR("Select All"), KEY_MASK_CMD | KEY_A);
ED_SHORTCUT("script_text_editor/move_up", TTR("Move Up"), KEY_MASK_ALT | KEY_UP);
ED_SHORTCUT("script_text_editor/move_down", TTR("Move Down"), KEY_MASK_ALT | KEY_DOWN);
Expand Down
6 changes: 4 additions & 2 deletions editor/plugins/script_text_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ class ScriptTextEditor : public ScriptEditorBase {
} colors_cache;

bool theme_loaded;
bool use_space_indentation;

enum {
EDIT_UNDO,
EDIT_REDO,
EDIT_CUT,
EDIT_COPY,
EDIT_PASTE,
EDIT_PASTE_ORIGINAL_INDENT,
EDIT_SELECT_ALL,
EDIT_COMPLETE,
EDIT_AUTO_INDENT,
Expand Down Expand Up @@ -202,8 +204,8 @@ class ScriptTextEditor : public ScriptEditorBase {
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 convert_indent_to_spaces(int p_from = 0, int p_to = -1);
virtual void convert_indent_to_tabs(int p_from = 0, int p_to = -1);
virtual void tag_saved_version();

virtual void goto_line(int p_line, bool p_with_error = false);
Expand Down
8 changes: 4 additions & 4 deletions editor/plugins/text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ void TextEditor::insert_final_newline() {
code_editor->insert_final_newline();
}

void TextEditor::convert_indent_to_spaces() {
void TextEditor::convert_indent_to_spaces(int p_from, int p_to) {

code_editor->convert_indent_to_spaces();
code_editor->convert_indent_to_spaces(p_from, p_to);
}

void TextEditor::convert_indent_to_tabs() {
void TextEditor::convert_indent_to_tabs(int p_from, int p_to) {

code_editor->convert_indent_to_tabs();
code_editor->convert_indent_to_tabs(p_from, p_to);
}

void TextEditor::tag_saved_version() {
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/text_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ class TextEditor : public ScriptEditorBase {
virtual void clear_executing_line();
virtual void trim_trailing_whitespace();
virtual void insert_final_newline();
virtual void convert_indent_to_spaces();
virtual void convert_indent_to_tabs();
virtual void convert_indent_to_spaces(int p_from = 0, int p_to = -1);
virtual void convert_indent_to_tabs(int p_from = 0, int p_to = -1);
virtual void ensure_focus();
virtual void tag_saved_version();
virtual void update_settings();
Expand Down
4 changes: 2 additions & 2 deletions modules/visual_script/visual_script_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2116,10 +2116,10 @@ void VisualScriptEditor::trim_trailing_whitespace() {
void VisualScriptEditor::insert_final_newline() {
}

void VisualScriptEditor::convert_indent_to_spaces() {
void VisualScriptEditor::convert_indent_to_spaces(int p_from, int p_to) {
}

void VisualScriptEditor::convert_indent_to_tabs() {
void VisualScriptEditor::convert_indent_to_tabs(int p_from, int p_to) {
}

void VisualScriptEditor::ensure_focus() {
Expand Down
4 changes: 2 additions & 2 deletions modules/visual_script/visual_script_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ class VisualScriptEditor : public ScriptEditorBase {
virtual void clear_executing_line();
virtual void trim_trailing_whitespace();
virtual void insert_final_newline();
virtual void convert_indent_to_spaces();
virtual void convert_indent_to_tabs();
virtual void convert_indent_to_spaces(int p_from = 0, int p_to = -1);
virtual void convert_indent_to_tabs(int p_from = 0, int p_to = -1);
virtual void ensure_focus();
virtual void tag_saved_version();
virtual void reload(bool p_soft);
Expand Down