Ability to Convert Case, issue 8079

This commit is contained in:
Paulb23 2017-04-24 19:59:55 +01:00
parent b6e0eaf3ca
commit 4b8152d599
2 changed files with 74 additions and 1 deletions

View file

@ -240,6 +240,48 @@ Variant ScriptTextEditor::get_edit_state() {
return state;
}
void ScriptTextEditor::_convert_case(CaseStyle p_case) {
TextEdit *te = code_editor->get_text_edit();
Ref<Script> scr = get_edited_script();
if (scr.is_null()) {
return;
}
if (te->is_selection_active()) {
te->begin_complex_operation();
int begin = te->get_selection_from_line();
int end = te->get_selection_to_line();
int begin_col = te->get_selection_from_column();
int end_col = te->get_selection_to_column();
for (int i = begin; i <= end; i++) {
String new_line = te->get_line(i);
switch (p_case) {
case UPPER: {
new_line = new_line.to_upper();
} break;
case LOWER: {
new_line = new_line.to_lower();
} break;
case CAPITALIZE: {
new_line = new_line.capitalize();
} break;
}
if (i == begin) {
new_line = te->get_line(i).left(begin_col) + new_line.right(begin_col);
}
if (i == end) {
new_line = new_line.left(end_col) + te->get_line(i).right(end_col);
}
te->set_line(i, new_line);
}
te->end_complex_operation();
}
}
void ScriptTextEditor::trim_trailing_whitespace() {
TextEdit *tx = code_editor->get_text_edit();
@ -919,7 +961,15 @@ void ScriptTextEditor::_edit_option(int p_op) {
case EDIT_PICK_COLOR: {
color_panel->popup();
} break;
case EDIT_TO_UPPERCASE: {
_convert_case(UPPER);
} break;
case EDIT_TO_LOWERCASE: {
_convert_case(LOWER);
} break;
case EDIT_CAPITALIZE: {
_convert_case(CAPITALIZE);
} break;
case SEARCH_FIND: {
code_editor->get_find_replace_bar()->popup_search();
@ -1335,6 +1385,15 @@ ScriptTextEditor::ScriptTextEditor() {
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
edit_menu->get_popup()->add_separator();
PopupMenu *convert_case = memnew(PopupMenu);
convert_case->set_name("convert_case");
edit_menu->get_popup()->add_child(convert_case);
edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case");
convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase")), EDIT_TO_UPPERCASE);
convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase")), EDIT_TO_LOWERCASE);
convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize")), EDIT_CAPITALIZE);
convert_case->connect("id_pressed", this, "_edit_option");
search_menu = memnew(MenuButton);
edit_hb->add_child(search_menu);
@ -1405,6 +1464,10 @@ void ScriptTextEditor::register_editor() {
ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTR("Goto Next Breakpoint"), KEY_MASK_CTRL | KEY_PERIOD);
ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTR("Goto Previous Breakpoint"), KEY_MASK_CTRL | KEY_COMMA);
ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Convert To Uppercase"), KEY_MASK_SHIFT | KEY_F4);
ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Convert To Lowercase"), KEY_MASK_SHIFT | KEY_F3);
ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize"), KEY_MASK_SHIFT | KEY_F2);
ED_SHORTCUT("script_text_editor/find", TTR("Find.."), KEY_MASK_CMD | KEY_F);
ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), KEY_F3);
ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT | KEY_F3);

View file

@ -76,6 +76,9 @@ class ScriptTextEditor : public ScriptEditorBase {
EDIT_INDENT_LEFT,
EDIT_CLONE_DOWN,
EDIT_PICK_COLOR,
EDIT_TO_UPPERCASE,
EDIT_TO_LOWERCASE,
EDIT_CAPITALIZE,
SEARCH_FIND,
SEARCH_FIND_NEXT,
SEARCH_FIND_PREV,
@ -109,6 +112,13 @@ protected:
void _goto_line(int p_line) { goto_line(p_line); }
void _lookup_symbol(const String &p_symbol, int p_row, int p_column);
enum CaseStyle {
UPPER,
LOWER,
CAPITALIZE,
};
void _convert_case(CaseStyle p_case);
Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);