Merge pull request #933 from adolson/alt-arrows-editor-fix

Alt+arrows editor undo/redo fix
This commit is contained in:
Juan Linietsky 2014-12-07 11:25:00 -02:00
commit da91ca7164
3 changed files with 103 additions and 30 deletions

View file

@ -3150,12 +3150,15 @@ void TextEdit::set_line(int line, String new_text)
{ {
if (line < 0 || line > text.size()) if (line < 0 || line > text.size())
return; return;
text.set(line, new_text); _remove_text(line, 0, line, text[line].length());
_insert_text(line, 0, new_text);
} }
void TextEdit::insert_at(const String &p_text, int at) void TextEdit::insert_at(const String &p_text, int at)
{ {
text.insert(at, p_text); cursor_set_column(0);
cursor_set_line(at);
_insert_text(at, 0, p_text+"\n");
} }
void TextEdit::set_show_line_numbers(bool p_show) { void TextEdit::set_show_line_numbers(bool p_show) {

View file

@ -608,6 +608,16 @@ bool ScriptEditor::_test_script_times_on_disk() {
return all_ok; return all_ok;
} }
void ScriptEditor::swap_lines(TextEdit *tx, int line1, int line2)
{
String tmp = tx->get_line(line1);
String tmp2 = tx->get_line(line2);
tx->set_line(line2, tmp);
tx->set_line(line1, tmp2);
tx->cursor_set_line(line2);
}
void ScriptEditor::_menu_option(int p_option) { void ScriptEditor::_menu_option(int p_option) {
@ -690,18 +700,38 @@ void ScriptEditor::_menu_option(int p_option) {
if (scr.is_null()) if (scr.is_null())
return; return;
if (tx->is_selection_active())
{
int from_line = tx->get_selection_from_line();
int from_col = tx->get_selection_from_column();
int to_line = tx->get_selection_to_line();
int to_column = tx->get_selection_to_column();
for (int i = from_line; i <= to_line; i++)
{
int line_id = i;
int next_id = i - 1;
if (line_id == 0 || next_id < 0)
return;
swap_lines(tx, line_id, next_id);
}
int from_line_up = from_line > 0 ? from_line-1 : from_line;
int to_line_up = to_line > 0 ? to_line-1 : to_line;
tx->select(from_line_up, from_col, to_line_up, to_column);
}
else
{
int line_id = tx->cursor_get_line(); int line_id = tx->cursor_get_line();
int next_id = line_id - 1; int next_id = line_id - 1;
if (line_id == 0 || next_id < 0) if (line_id == 0 || next_id < 0)
return; return;
String tmp = tx->get_line(line_id); swap_lines(tx, line_id, next_id);
String tmp2 = tx->get_line(next_id); }
tx->set_line(next_id, tmp);
tx->set_line(line_id, tmp2);
tx->update(); tx->update();
tx->cursor_set_line(next_id);
} break; } break;
case EDIT_MOVE_LINE_DOWN: { case EDIT_MOVE_LINE_DOWN: {
@ -711,18 +741,38 @@ void ScriptEditor::_menu_option(int p_option) {
if (scr.is_null()) if (scr.is_null())
return; return;
if (tx->is_selection_active())
{
int from_line = tx->get_selection_from_line();
int from_col = tx->get_selection_from_column();
int to_line = tx->get_selection_to_line();
int to_column = tx->get_selection_to_column();
for (int i = to_line; i >= from_line; i--)
{
int line_id = i;
int next_id = i + 1;
if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
return;
swap_lines(tx, line_id, next_id);
}
int from_line_down = from_line < tx->get_line_count() ? from_line+1 : from_line;
int to_line_down = to_line < tx->get_line_count() ? to_line+1 : to_line;
tx->select(from_line_down, from_col, to_line_down, to_column);
}
else
{
int line_id = tx->cursor_get_line(); int line_id = tx->cursor_get_line();
int next_id = line_id + 1; int next_id = line_id + 1;
if (line_id == tx->get_line_count() || next_id > tx->get_line_count()) if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
return; return;
String tmp = tx->get_line(line_id); swap_lines(tx, line_id, next_id);
String tmp2 = tx->get_line(next_id); }
tx->set_line(next_id, tmp);
tx->set_line(line_id, tmp2);
tx->update(); tx->update();
tx->cursor_set_line(next_id);
} break; } break;
case EDIT_INDENT_LEFT: { case EDIT_INDENT_LEFT: {
@ -740,19 +790,39 @@ void ScriptEditor::_menu_option(int p_option) {
for (int i = begin; i <= end; i++) for (int i = begin; i <= end; i++)
{ {
String line_text = tx->get_line(i); String line_text = tx->get_line(i);
// begins with tab
if (line_text.begins_with("\t"))
{
line_text = line_text.substr(1, line_text.length()); line_text = line_text.substr(1, line_text.length());
tx->set_line(i, line_text); tx->set_line(i, line_text);
} }
// begins with 4 spaces
else if (line_text.begins_with(" "))
{
line_text = line_text.substr(4, line_text.length());
tx->set_line(i, line_text);
}
}
} }
else else
{ {
begin = tx->cursor_get_line(); begin = tx->cursor_get_line();
String line_text = tx->get_line(begin); String line_text = tx->get_line(begin);
// begins with tab
if (line_text.begins_with("\t"))
{
line_text = line_text.substr(1, line_text.length()); line_text = line_text.substr(1, line_text.length());
tx->set_line(begin, line_text); tx->set_line(begin, line_text);
} }
// begins with 4 spaces
else if (line_text.begins_with(" "))
{
line_text = line_text.substr(4, line_text.length());
tx->set_line(begin, line_text);
}
}
tx->update(); tx->update();
tx->deselect(); //tx->deselect();
} break; } break;
case EDIT_INDENT_RIGHT: { case EDIT_INDENT_RIGHT: {
@ -782,7 +852,7 @@ void ScriptEditor::_menu_option(int p_option) {
tx->set_line(begin, line_text); tx->set_line(begin, line_text);
} }
tx->update(); tx->update();
tx->deselect(); //tx->deselect();
} break; } break;
case EDIT_CLONE_DOWN: { case EDIT_CLONE_DOWN: {
@ -837,7 +907,7 @@ void ScriptEditor::_menu_option(int p_option) {
tx->set_line(begin, line_text); tx->set_line(begin, line_text);
} }
tx->update(); tx->update();
tx->deselect(); //tx->deselect();
} break; } break;
case EDIT_COMPLETE: { case EDIT_COMPLETE: {
@ -1498,8 +1568,8 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) {
edit_menu->get_popup()->add_separator(); edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_item("Select All",EDIT_SELECT_ALL,KEY_MASK_CMD|KEY_A); edit_menu->get_popup()->add_item("Select All",EDIT_SELECT_ALL,KEY_MASK_CMD|KEY_A);
edit_menu->get_popup()->add_separator(); edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_item("Move Line Up",EDIT_MOVE_LINE_UP,KEY_MASK_ALT|KEY_UP); edit_menu->get_popup()->add_item("Move Up",EDIT_MOVE_LINE_UP,KEY_MASK_ALT|KEY_UP);
edit_menu->get_popup()->add_item("Move Line Down",EDIT_MOVE_LINE_DOWN,KEY_MASK_ALT|KEY_DOWN); edit_menu->get_popup()->add_item("Move Down",EDIT_MOVE_LINE_DOWN,KEY_MASK_ALT|KEY_DOWN);
edit_menu->get_popup()->add_item("Indent Left",EDIT_INDENT_LEFT,KEY_MASK_ALT|KEY_LEFT); edit_menu->get_popup()->add_item("Indent Left",EDIT_INDENT_LEFT,KEY_MASK_ALT|KEY_LEFT);
edit_menu->get_popup()->add_item("Indent Right",EDIT_INDENT_RIGHT,KEY_MASK_ALT|KEY_RIGHT); edit_menu->get_popup()->add_item("Indent Right",EDIT_INDENT_RIGHT,KEY_MASK_ALT|KEY_RIGHT);
edit_menu->get_popup()->add_item("Toggle Comment",EDIT_TOGGLE_COMMENT,KEY_MASK_CMD|KEY_SLASH); edit_menu->get_popup()->add_item("Toggle Comment",EDIT_TOGGLE_COMMENT,KEY_MASK_CMD|KEY_SLASH);

View file

@ -218,7 +218,7 @@ public:
void get_breakpoints(List<String> *p_breakpoints); void get_breakpoints(List<String> *p_breakpoints);
void swap_lines(TextEdit *tx, int line1, int line2);
void save_external_data(); void save_external_data();